2013-06-14 15:10:22 -04:00
|
|
|
define(['taskbar'], function(taskbar) {
|
2013-06-03 16:06:11 -04:00
|
|
|
var composer = {
|
2013-09-17 13:37:03 -04:00
|
|
|
active: undefined,
|
2013-12-20 18:41:56 -05:00
|
|
|
posts: {}
|
2013-09-17 13:37:03 -04:00
|
|
|
};
|
|
|
|
|
|
2013-12-22 15:15:59 -05:00
|
|
|
function allowed() {
|
|
|
|
|
if(!(parseInt(app.uid, 10) > 0 || parseInt(config.allowGuestPosting, 10) === 1)) {
|
|
|
|
|
app.alert({
|
|
|
|
|
type: 'danger',
|
|
|
|
|
timeout: 5000,
|
|
|
|
|
alert_id: 'post_error',
|
|
|
|
|
title: 'Please Log In to Post',
|
|
|
|
|
message: 'Posting is currently restricted to registered members only, click here to log in',
|
|
|
|
|
clickfn: function() {
|
|
|
|
|
ajaxify.go('login');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-22 15:15:59 -05:00
|
|
|
composer.newTopic = function(cid) {
|
|
|
|
|
if(allowed()) {
|
|
|
|
|
push({
|
|
|
|
|
cid: cid,
|
|
|
|
|
title: '',
|
|
|
|
|
body: '',
|
|
|
|
|
modified: false
|
2013-12-20 18:41:56 -05:00
|
|
|
});
|
2013-12-22 15:15:59 -05:00
|
|
|
}
|
|
|
|
|
}
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2013-12-22 15:15:59 -05:00
|
|
|
composer.newReply = function(tid, title, text) {
|
|
|
|
|
if(allowed()) {
|
|
|
|
|
push({
|
|
|
|
|
tid: tid,
|
|
|
|
|
title: title,
|
|
|
|
|
body: text,
|
2013-12-20 18:41:56 -05:00
|
|
|
modified: false
|
2013-12-22 15:15:59 -05:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
composer.editPost = function(pid) {
|
|
|
|
|
if(allowed()) {
|
|
|
|
|
socket.emit('api:composer.push', {
|
|
|
|
|
pid: pid
|
|
|
|
|
}, function(threadData) {
|
|
|
|
|
console.log(threadData);
|
|
|
|
|
push({
|
|
|
|
|
pid: pid,
|
|
|
|
|
title: threadData.title,
|
|
|
|
|
body: threadData.body,
|
|
|
|
|
modified: false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2013-12-22 15:15:59 -05:00
|
|
|
function push(post) {
|
|
|
|
|
var uuid = utils.generateUUID();
|
|
|
|
|
|
|
|
|
|
taskbar.push('composer', uuid, {
|
|
|
|
|
title: post.title ? post.title : 'New Topic',
|
|
|
|
|
icon: post.picture
|
2013-07-21 12:29:57 -04:00
|
|
|
});
|
2013-12-22 15:15:59 -05:00
|
|
|
|
|
|
|
|
composer.posts[uuid] = post;
|
|
|
|
|
|
|
|
|
|
composer.load(uuid);
|
2013-12-20 18:41:56 -05:00
|
|
|
}
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.load = function(post_uuid) {
|
|
|
|
|
if($('#cmp-uuid-' + post_uuid).length) {
|
|
|
|
|
composer.activateReposition(post_uuid);
|
|
|
|
|
} else {
|
|
|
|
|
composer.createNewComposer(post_uuid);
|
|
|
|
|
}
|
2013-07-21 12:29:57 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.createNewComposer = function(post_uuid) {
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
templates.preload_template('composer', function() {
|
2013-08-15 17:03:43 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
var composerTemplate = templates['composer'].parse({});
|
|
|
|
|
composerTemplate = $(composerTemplate);
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composerTemplate.attr('id', 'cmp-uuid-' + post_uuid);
|
2013-08-15 14:04:12 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
$(document.body).append(composerTemplate);
|
2013-08-15 16:50:00 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.activateReposition(post_uuid);
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
var postContainer = $(composerTemplate[0]);
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if(config.imgurClientIDSet) {
|
|
|
|
|
initializeFileReader(post_uuid);
|
|
|
|
|
}
|
2013-08-15 17:03:43 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
var postData = composer.posts[post_uuid],
|
|
|
|
|
titleEl = postContainer.find('.title'),
|
|
|
|
|
bodyEl = postContainer.find('textarea');
|
|
|
|
|
|
|
|
|
|
if (parseInt(postData.tid) > 0) {
|
|
|
|
|
titleEl.val('Replying to: ' + postData.title);
|
|
|
|
|
titleEl.prop('readOnly', true);
|
|
|
|
|
} else if (parseInt(postData.pid) > 0) {
|
|
|
|
|
titleEl.val(postData.title);
|
|
|
|
|
titleEl.prop('readOnly', true);
|
|
|
|
|
socket.emit('api:composer.editCheck', postData.pid, function(editCheck) {
|
|
|
|
|
if (editCheck.titleEditable) {
|
|
|
|
|
postContainer.find('input').prop('readonly', false);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
titleEl.val(postData.title);
|
|
|
|
|
titleEl.prop('readOnly', false);
|
|
|
|
|
}
|
2013-08-15 16:50:00 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
bodyEl.val(postData.body);
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
postContainer.on('change', 'input, textarea', function() {
|
|
|
|
|
composer.posts[post_uuid].modified = true;
|
2013-07-21 12:29:57 -04:00
|
|
|
});
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
postContainer.on('click', '.action-bar button', function() {
|
|
|
|
|
var action = $(this).attr('data-action');
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
switch(action) {
|
|
|
|
|
case 'post':
|
|
|
|
|
composer.post(post_uuid);
|
|
|
|
|
break;
|
|
|
|
|
case 'discard':
|
|
|
|
|
if (composer.posts[post_uuid].modified) {
|
|
|
|
|
bootbox.confirm('Are you sure you wish to discard this post?', function(discard) {
|
|
|
|
|
if (discard) {
|
|
|
|
|
composer.discard(post_uuid);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
composer.discard(post_uuid);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2013-07-16 11:03:51 -04:00
|
|
|
}
|
2013-12-20 18:41:56 -05:00
|
|
|
});
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
postContainer.on('click', '.formatting-bar span', function() {
|
|
|
|
|
var postContentEl = postContainer.find('textarea'),
|
|
|
|
|
iconClass = $(this).find('i').attr('class'),
|
|
|
|
|
cursorEnd = postContentEl.val().length,
|
|
|
|
|
selectionStart = postContentEl[0].selectionStart,
|
|
|
|
|
selectionEnd = postContentEl[0].selectionEnd,
|
|
|
|
|
selectionLength = selectionEnd - selectionStart;
|
2013-08-15 16:50:00 -04:00
|
|
|
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
function insertIntoInput(element, value) {
|
|
|
|
|
var start = postContentEl[0].selectionStart;
|
|
|
|
|
element.val(element.val().slice(0, start) + value + element.val().slice(start, element.val().length));
|
|
|
|
|
postContentEl[0].selectionStart = postContentEl[0].selectionEnd = start + value.length;
|
|
|
|
|
}
|
2013-12-17 23:42:02 -05:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
switch(iconClass) {
|
|
|
|
|
case 'fa fa-bold':
|
|
|
|
|
if (selectionStart === selectionEnd) {
|
2013-07-16 11:03:51 -04:00
|
|
|
// Nothing selected
|
2013-12-20 18:41:56 -05:00
|
|
|
insertIntoInput(postContentEl, "**bolded text**");
|
|
|
|
|
} else {
|
|
|
|
|
// Text selected
|
|
|
|
|
postContentEl.val(postContentEl.val().slice(0, selectionStart) + '**' + postContentEl.val().slice(selectionStart, selectionEnd) + '**' + postContentEl.val().slice(selectionEnd));
|
|
|
|
|
postContentEl[0].selectionStart = selectionStart + 2;
|
|
|
|
|
postContentEl[0].selectionEnd = selectionEnd + 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'fa fa-italic':
|
|
|
|
|
if (selectionStart === selectionEnd) {
|
|
|
|
|
// Nothing selected
|
|
|
|
|
insertIntoInput(postContentEl, "*italicised text*");
|
|
|
|
|
} else {
|
|
|
|
|
// Text selected
|
|
|
|
|
postContentEl.val(postContentEl.val().slice(0, selectionStart) + '*' + postContentEl.val().slice(selectionStart, selectionEnd) + '*' + postContentEl.val().slice(selectionEnd));
|
|
|
|
|
postContentEl[0].selectionStart = selectionStart + 1;
|
|
|
|
|
postContentEl[0].selectionEnd = selectionEnd + 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'fa fa-list':
|
|
|
|
|
// Nothing selected
|
|
|
|
|
insertIntoInput(postContentEl, "\n\n* list item");
|
|
|
|
|
break;
|
|
|
|
|
case 'fa fa-link':
|
|
|
|
|
if (selectionStart === selectionEnd) {
|
|
|
|
|
// Nothing selected
|
|
|
|
|
insertIntoInput(postContentEl, "[link text](link url)");
|
|
|
|
|
} else {
|
|
|
|
|
// Text selected
|
|
|
|
|
postContentEl.val(postContentEl.val().slice(0, selectionStart) + '[' + postContentEl.val().slice(selectionStart, selectionEnd) + '](link url)' + postContentEl.val().slice(selectionEnd));
|
|
|
|
|
postContentEl[0].selectionStart = selectionStart + selectionLength + 3;
|
|
|
|
|
postContentEl[0].selectionEnd = selectionEnd + 11;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-11-21 16:47:32 -05:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
var resizeActive = false,
|
|
|
|
|
resizeCenterX = 0,
|
|
|
|
|
resizeOffset = 0,
|
|
|
|
|
resizeAction = function(e) {
|
|
|
|
|
if (resizeActive) {
|
|
|
|
|
position = (e.clientX + resizeOffset);
|
|
|
|
|
if (Math.abs(position - resizeSnaps.half) <= 15) {
|
|
|
|
|
// Half snap
|
|
|
|
|
postContainer.css('width', resizeSnaps.half);
|
|
|
|
|
resizeSavePosition(resizeSnaps.half);
|
|
|
|
|
} else if (Math.abs(position - resizeSnaps.none) <= 30) {
|
|
|
|
|
// Minimize snap
|
|
|
|
|
postContainer.css('width', bodyRect.width - resizeSnaps.none + 15);
|
|
|
|
|
resizeSavePosition(resizeSnaps.none);
|
|
|
|
|
} else if (position <= 30) {
|
|
|
|
|
// Full snap
|
|
|
|
|
postContainer.css('width', bodyRect.width - 15);
|
|
|
|
|
resizeSavePosition(bodyRect.width - 15);
|
|
|
|
|
} else {
|
|
|
|
|
// OH SNAP, NO SNAPS!
|
|
|
|
|
postContainer.css('width', bodyRect.width - position);
|
|
|
|
|
resizeSavePosition(bodyRect.width - position);
|
2013-12-18 18:48:42 -05:00
|
|
|
}
|
2013-12-20 18:41:56 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
resizeSavePosition = function(px) {
|
|
|
|
|
var percentage = px/bodyRect.width;
|
|
|
|
|
localStorage.setItem('composer:resizePercentage', percentage);
|
|
|
|
|
},
|
|
|
|
|
resizeSnaps = {
|
|
|
|
|
none: 0,
|
|
|
|
|
half: 0,
|
|
|
|
|
full: 0
|
|
|
|
|
},
|
|
|
|
|
resizeRect, bodyRect;
|
|
|
|
|
|
|
|
|
|
var resizeEl = postContainer.find('.resizer');
|
|
|
|
|
|
|
|
|
|
resizeEl
|
|
|
|
|
.on('mousedown', function(e) {
|
|
|
|
|
bodyRect = document.body.getBoundingClientRect();
|
|
|
|
|
resizeRect = resizeEl[0].getBoundingClientRect();
|
|
|
|
|
resizeCenterX = resizeRect.left + (resizeRect.width/2);
|
|
|
|
|
resizeOffset = resizeCenterX - e.clientX;
|
|
|
|
|
resizeSnaps.half = bodyRect.width / 2;
|
|
|
|
|
resizeSnaps.none = bodyRect.width;
|
|
|
|
|
resizeActive = true;
|
|
|
|
|
|
|
|
|
|
$(document.body).on('mousemove', resizeAction);
|
|
|
|
|
})
|
|
|
|
|
.on('mouseup', function() {
|
|
|
|
|
resizeActive = false;
|
|
|
|
|
$(document.body).off('mousemove', resizeAction);
|
2013-12-17 23:42:02 -05:00
|
|
|
});
|
2013-06-03 16:06:11 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
window.addEventListener('resize', function() {
|
|
|
|
|
if (composer.active !== undefined) {
|
|
|
|
|
composer.activateReposition(composer.active);
|
|
|
|
|
}
|
2013-12-17 23:42:02 -05:00
|
|
|
});
|
2013-12-20 18:41:56 -05:00
|
|
|
});
|
2013-06-03 16:06:11 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.activateReposition = function(post_uuid) {
|
|
|
|
|
|
|
|
|
|
if(composer.active && composer.active !== post_uuid) {
|
|
|
|
|
composer.minimize(composer.active);
|
2013-12-18 10:03:49 -05:00
|
|
|
}
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
var percentage = localStorage.getItem('composer:resizePercentage'),
|
|
|
|
|
bodyRect = document.body.getBoundingClientRect(),
|
|
|
|
|
postContainer = $('#cmp-uuid-' + post_uuid);
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2013-07-25 13:18:55 -04:00
|
|
|
composer.active = post_uuid;
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if (percentage) {
|
|
|
|
|
if (bodyRect.width >= 768) {
|
|
|
|
|
postContainer.css('width', Math.floor(bodyRect.width * percentage) + 'px');
|
|
|
|
|
} else {
|
|
|
|
|
postContainer.css('width', '100%');
|
|
|
|
|
}
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
2013-08-15 16:50:00 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
postContainer.css('visibility', 'visible');
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.focusElements(post_uuid);
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.focusElements = function(post_uuid) {
|
|
|
|
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
|
|
|
|
postData = composer.posts[post_uuid],
|
|
|
|
|
titleEl = postContainer.find('.title'),
|
|
|
|
|
bodyEl = postContainer.find('textarea');
|
2013-12-18 23:56:59 -05:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if ((parseInt(postData.tid) || parseInt(postData.pid)) > 0) {
|
|
|
|
|
bodyEl.focus();
|
|
|
|
|
bodyEl.selectionStart = bodyEl.val().length;
|
|
|
|
|
bodyEl.selectionEnd = bodyEl.val().length;
|
|
|
|
|
} else if (parseInt(postData.cid) > 0) {
|
|
|
|
|
titleEl.focus();
|
2013-12-18 23:56:59 -05:00
|
|
|
}
|
2013-07-25 13:18:55 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-04 16:20:27 -04:00
|
|
|
composer.post = function(post_uuid) {
|
2013-08-15 16:50:00 -04:00
|
|
|
var postData = composer.posts[post_uuid],
|
2013-12-20 18:41:56 -05:00
|
|
|
postContainer = $('#cmp-uuid-' + post_uuid),
|
|
|
|
|
titleEl = postContainer.find('.title'),
|
|
|
|
|
bodyEl = postContainer.find('textarea');
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
titleEl.val(titleEl.val().trim());
|
|
|
|
|
bodyEl.val(bodyEl.val().trim());
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-09 13:56:09 -05:00
|
|
|
if(postData.uploadsInProgress && postData.uploadsInProgress.length) {
|
2013-09-17 13:37:03 -04:00
|
|
|
return app.alert({
|
|
|
|
|
type: 'warning',
|
|
|
|
|
timeout: 2000,
|
|
|
|
|
title: 'Still uploading',
|
|
|
|
|
message: "Please wait for uploads to complete.",
|
|
|
|
|
alert_id: 'post_error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if (titleEl.val().length < config.minimumTitleLength) {
|
2013-06-04 16:20:27 -04:00
|
|
|
return app.alert({
|
2013-08-24 03:36:44 +08:00
|
|
|
type: 'danger',
|
2013-07-18 14:47:41 -04:00
|
|
|
timeout: 2000,
|
2013-06-04 16:20:27 -04:00
|
|
|
title: 'Title too short',
|
2013-08-12 19:00:31 -04:00
|
|
|
message: "Please enter a longer title. At least " + config.minimumTitleLength+ " characters.",
|
2013-06-04 16:20:27 -04:00
|
|
|
alert_id: 'post_error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if (bodyEl.val().length < config.minimumPostLength) {
|
2013-07-17 10:45:16 -04:00
|
|
|
return app.alert({
|
2013-08-24 03:36:44 +08:00
|
|
|
type: 'danger',
|
2013-07-18 14:47:41 -04:00
|
|
|
timeout: 2000,
|
2013-07-17 10:45:16 -04:00
|
|
|
title: 'Content too short',
|
2013-08-12 19:00:31 -04:00
|
|
|
message: "Please enter a longer post. At least " + config.minimumPostLength + " characters.",
|
2013-07-17 10:45:16 -04:00
|
|
|
alert_id: 'post_error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-04 16:20:27 -04:00
|
|
|
// Still here? Let's post.
|
2013-06-05 10:18:29 -04:00
|
|
|
if (parseInt(postData.cid) > 0) {
|
2013-06-04 16:20:27 -04:00
|
|
|
socket.emit('api:topics.post', {
|
2013-12-20 18:41:56 -05:00
|
|
|
'title' : titleEl.val(),
|
|
|
|
|
'content' : bodyEl.val(),
|
2013-09-17 13:37:03 -04:00
|
|
|
'category_id' : postData.cid
|
2013-06-04 16:20:27 -04:00
|
|
|
});
|
|
|
|
|
} else if (parseInt(postData.tid) > 0) {
|
|
|
|
|
socket.emit('api:posts.reply', {
|
|
|
|
|
'topic_id' : postData.tid,
|
2013-12-20 18:41:56 -05:00
|
|
|
'content' : bodyEl.val()
|
2013-06-04 16:20:27 -04:00
|
|
|
});
|
2013-06-05 10:18:29 -04:00
|
|
|
} else if (parseInt(postData.pid) > 0) {
|
|
|
|
|
socket.emit('api:posts.edit', {
|
|
|
|
|
pid: postData.pid,
|
2013-12-20 18:41:56 -05:00
|
|
|
content: bodyEl.val(),
|
|
|
|
|
title: titleEl.val()
|
2013-06-05 10:18:29 -04:00
|
|
|
});
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
composer.discard(post_uuid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
composer.discard = function(post_uuid) {
|
|
|
|
|
if (composer.posts[post_uuid]) {
|
2013-12-20 18:41:56 -05:00
|
|
|
$('#cmp-uuid-' + post_uuid).remove();
|
2013-06-04 16:20:27 -04:00
|
|
|
delete composer.posts[post_uuid];
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.active = undefined;
|
2013-06-14 15:10:22 -04:00
|
|
|
taskbar.discard('composer', post_uuid);
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
composer.minimize = function(post_uuid) {
|
|
|
|
|
var postContainer = $('#cmp-uuid-' + post_uuid);
|
|
|
|
|
postContainer.css('visibility', 'hidden');
|
2013-07-25 13:18:55 -04:00
|
|
|
composer.active = undefined;
|
2013-12-20 18:41:56 -05:00
|
|
|
taskbar.minimize('composer', post_uuid);
|
2013-06-03 16:06:11 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
function initializeFileReader(post_uuid) {
|
|
|
|
|
if(jQuery.event.props.indexOf('dataTransfer') === -1) {
|
|
|
|
|
jQuery.event.props.push('dataTransfer');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var draggingDocument = false;
|
|
|
|
|
|
|
|
|
|
if(window.FileReader) {
|
|
|
|
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
|
|
|
|
drop = postContainer.find('.imagedrop'),
|
|
|
|
|
textarea = postContainer.find('textarea');
|
|
|
|
|
|
|
|
|
|
$(document).off('dragstart').on('dragstart', function(e) {
|
|
|
|
|
draggingDocument = true;
|
|
|
|
|
}).off('dragend').on('dragend', function(e) {
|
|
|
|
|
draggingDocument = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
textarea.on('dragenter', function(e) {
|
|
|
|
|
if(draggingDocument) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
drop.css('top', textarea.position().top + 'px');
|
|
|
|
|
drop.css('height', textarea.height());
|
|
|
|
|
drop.css('line-height', textarea.height() + '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 dt = e.dataTransfer,
|
|
|
|
|
files = dt.files;
|
|
|
|
|
|
|
|
|
|
for (var i=0; i<files.length; i++) {
|
|
|
|
|
loadFile(post_uuid, files[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!files.length) {
|
|
|
|
|
drop.hide();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function loadFile(post_uuid, file) {
|
|
|
|
|
var reader = new FileReader(),
|
|
|
|
|
dropDiv = $('#cmp-uuid-' + post_uuid).find('.imagedrop');
|
|
|
|
|
|
|
|
|
|
$(reader).on('loadend', function(e) {
|
|
|
|
|
var bin = this.result;
|
|
|
|
|
bin = bin.split(',')[1];
|
|
|
|
|
|
|
|
|
|
var img = {
|
|
|
|
|
name: file.name,
|
|
|
|
|
data: bin
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createImagePlaceholder(post_uuid, img);
|
|
|
|
|
|
|
|
|
|
dropDiv.hide();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function createImagePlaceholder(post_uuid, img) {
|
|
|
|
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
|
|
|
|
textarea = postContainer.find('textarea'),
|
|
|
|
|
text = textarea.val(),
|
|
|
|
|
imgText = "";
|
|
|
|
|
|
|
|
|
|
text += imgText;
|
|
|
|
|
textarea.val(text + " ");
|
|
|
|
|
|
|
|
|
|
if(!composer.posts[post_uuid].uploadsInProgress) {
|
|
|
|
|
composer.posts[post_uuid].uploadsInProgress = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
composer.posts[post_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[post_uuid].uploadsInProgress.pop();
|
|
|
|
|
});
|
|
|
|
|
}
|
2013-06-03 16:06:11 -04:00
|
|
|
|
|
|
|
|
return {
|
2013-12-22 15:15:59 -05:00
|
|
|
newTopic: composer.newTopic,
|
|
|
|
|
newReply: composer.newReply,
|
|
|
|
|
editPost: composer.editPost,
|
2013-06-14 15:32:49 -04:00
|
|
|
load: composer.load,
|
|
|
|
|
minimize: composer.minimize
|
2013-06-03 16:06:11 -04:00
|
|
|
};
|
|
|
|
|
});
|