define(['taskbar'], function(taskbar) {
var composer = {
initialized: false,
active: undefined,
taskbar: taskbar,
posts: {},
postContainer: undefined,
};
function createImagePlaceholder(img) {
var text = $('.post-window textarea').val(),
textarea = $('.post-window textarea'),
imgText = "",
uuid = $('.post-window .imagedrop').parents('[data-uuid]').attr('data-uuid');
text += imgText;
textarea.val(text + " ");
if(!composer.posts[uuid].uploadsInProgress) {
composer.posts[uuid].uploadsInProgress = [];
}
composer.posts[uuid].uploadsInProgress.push(1);
socket.emit("api:posts.uploadImage", img, function(err, data) {
if(err) {
return app.alertError(err.message);
}
var currentText = textarea.val();
imgText = "";
if(!err)
textarea.val(currentText.replace(imgText, ""));
else
textarea.val(currentText.replace(imgText, ""));
composer.posts[uuid].uploadsInProgress.pop();
});
}
function loadFile(file) {
var reader = new FileReader(),
dropDiv = $('.post-window .imagedrop');
$(reader).on('loadend', function(e) {
var bin = this.result;
bin = bin.split(',')[1];
var img = {
name: file.name,
data: bin
};
createImagePlaceholder(img);
dropDiv.hide();
});
reader.readAsDataURL(file);
}
function initializeFileReader() {
jQuery.event.props.push( "dataTransfer" );
var draggingDocument = false;
if(window.FileReader) {
var drop = $('.post-window .imagedrop'),
textarea = $('.post-window textarea');
$(document).on('dragstart', function(e) {
draggingDocument = true;
}).on('dragend', function(e) {
draggingDocument = false;
});
textarea.on('dragenter', function(e) {
if(draggingDocument) {
return;
}
drop.css('top', textarea.position().top + 'px');
drop.show();
drop.on('dragleave', function(ev) {
drop.hide();
drop.off('dragleave');
});
});
function cancel(e) {
e.preventDefault();
return false;
}
drop.on('dragover', cancel);
drop.on('dragenter', cancel);
drop.on('drop', function(e) {
e.preventDefault();
var uuid = drop.parents('[data-uuid]').attr('data-uuid'),
dt = e.dataTransfer,
files = dt.files;
for (var i=0; i' +
// '
' +
// '' +
// 'Drag and Drop Images Here
'+
// '' +
// '';
// document.body.insertBefore(composer.postContainer, taskbar);
}
}
composer.push = function(tid, cid, pid, text) {
if (!composer.initialized) {
var args = arguments;
setTimeout(function() {
composer.push.apply(composer, args);
}, 500);
} else {
socket.emit('api:composer.push', {
tid: tid, // Replying
cid: cid, // Posting
pid: pid, // Editing
body: text // Predefined text
});
}
}
composer.load = function(post_uuid) {
var post_data = composer.posts[post_uuid],
titleEl = composer.postContainer.querySelector('input'),
bodyEl = composer.postContainer.querySelector('textarea');
composer.reposition(post_uuid);
composer.active = post_uuid;
composer.postContainer.setAttribute('data-uuid', post_uuid);
if (parseInt(post_data.tid) > 0) {
titleEl.value = 'Replying to: ' + post_data.title;
titleEl.readOnly = true;
} else if (parseInt(post_data.pid) > 0) {
titleEl.value = post_data.title;
titleEl.readOnly = true;
socket.emit('api:composer.editCheck', post_data.pid);
} else {
titleEl.value = post_data.title;
titleEl.readOnly = false;
}
bodyEl.value = post_data.body;
// Direct user focus to the correct element
if ((parseInt(post_data.tid) || parseInt(post_data.pid)) > 0) {
bodyEl.focus();
bodyEl.selectionStart = bodyEl.value.length;
bodyEl.selectionEnd = bodyEl.value.length;
} else if (parseInt(post_data.cid) > 0) {
titleEl.focus();
}
}
composer.reposition = function(post_uuid) {
// Resize the composer to the saved size
var percentage = localStorage.getItem('composer:resizePercentage'),
bodyRect = document.body.getBoundingClientRect();
if (bodyRect.width >= 768) {
composer.postContainer.style.width = Math.floor(bodyRect.width * percentage) + 'px';
}
composer.postContainer.style.visibility = 'visible';
}
composer.post = function(post_uuid) {
// Check title and post length
var postData = composer.posts[post_uuid],
titleEl = composer.postContainer.querySelector('input'),
bodyEl = composer.postContainer.querySelector('textarea');
titleEl.value = titleEl.value.trim();
bodyEl.value = bodyEl.value.trim();
if(postData.uploadsInProgress && postData.uploadsInProgress.length) {
return app.alert({
type: 'warning',
timeout: 2000,
title: 'Still uploading',
message: "Please wait for uploads to complete.",
alert_id: 'post_error'
});
}
if (titleEl.value.length < config.minimumTitleLength) {
return app.alert({
type: 'danger',
timeout: 2000,
title: 'Title too short',
message: "Please enter a longer title. At least " + config.minimumTitleLength+ " characters.",
alert_id: 'post_error'
});
}
if (bodyEl.value.length < config.minimumPostLength) {
return app.alert({
type: 'danger',
timeout: 2000,
title: 'Content too short',
message: "Please enter a longer post. At least " + config.minimumPostLength + " characters.",
alert_id: 'post_error'
});
}
// Still here? Let's post.
if (parseInt(postData.cid) > 0) {
socket.emit('api:topics.post', {
'title' : titleEl.value,
'content' : bodyEl.value,
'category_id' : postData.cid
});
} else if (parseInt(postData.tid) > 0) {
socket.emit('api:posts.reply', {
'topic_id' : postData.tid,
'content' : bodyEl.value
});
} else if (parseInt(postData.pid) > 0) {
socket.emit('api:posts.edit', {
pid: postData.pid,
content: bodyEl.value,
title: titleEl.value
});
}
composer.discard(post_uuid);
}
composer.discard = function(post_uuid) {
if (composer.posts[post_uuid]) {
$(composer.postContainer).find('.imagedrop').hide();
delete composer.posts[post_uuid];
composer.minimize();
taskbar.discard('composer', post_uuid);
}
}
composer.minimize = function(uuid) {
composer.postContainer.style.visibility = 'hidden';
composer.active = undefined;
taskbar.minimize('composer', uuid);
}
composer.init();
return {
push: composer.push,
load: composer.load,
minimize: composer.minimize
};
});