Files
NodeBB/public/src/modules/composer.js

504 lines
13 KiB
JavaScript
Raw Normal View History

define(['taskbar'], function(taskbar) {
var composer = {
2013-09-17 13:37:03 -04:00
active: undefined,
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-12-22 15:15:59 -05:00
composer.newTopic = function(cid) {
if(allowed()) {
push({
cid: cid,
title: '',
body: '',
modified: false
});
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,
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);
}
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
}
composer.createNewComposer = function(post_uuid) {
templates.preload_template('composer', function() {
2013-08-15 17:03:43 -04:00
var composerTemplate = templates['composer'].parse({});
composerTemplate = $(composerTemplate);
2013-07-21 12:29:57 -04:00
composerTemplate.attr('id', 'cmp-uuid-' + post_uuid);
2013-08-15 14:04:12 -04:00
$(document.body).append(composerTemplate);
2013-08-15 16:50:00 -04:00
composer.activateReposition(post_uuid);
var postContainer = $(composerTemplate[0]);
if(config.imgurClientIDSet) {
initializeFileReader(post_uuid);
}
2013-08-15 17:03:43 -04: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
bodyEl.val(postData.body);
postContainer.on('change', 'input, textarea', function() {
composer.posts[post_uuid].modified = true;
2013-07-21 12:29:57 -04:00
});
postContainer.on('click', '.action-bar button', function() {
var action = $(this).attr('data-action');
2013-07-21 12:29:57 -04: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;
}
});
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
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
switch(iconClass) {
case 'fa fa-bold':
if (selectionStart === selectionEnd) {
// Nothing selected
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
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
}
}
},
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
});
window.addEventListener('resize', function() {
if (composer.active !== undefined) {
composer.activateReposition(composer.active);
}
2013-12-17 23:42:02 -05:00
});
});
}
composer.activateReposition = function(post_uuid) {
if(composer.active && composer.active !== post_uuid) {
composer.minimize(composer.active);
}
var percentage = localStorage.getItem('composer:resizePercentage'),
bodyRect = document.body.getBoundingClientRect(),
postContainer = $('#cmp-uuid-' + post_uuid);
2013-07-25 13:18:55 -04:00
composer.active = post_uuid;
if (percentage) {
if (bodyRect.width >= 768) {
postContainer.css('width', Math.floor(bodyRect.width * percentage) + 'px');
} else {
postContainer.css('width', '100%');
}
}
2013-08-15 16:50:00 -04:00
postContainer.css('visibility', 'visible');
composer.focusElements(post_uuid);
}
composer.focusElements = function(post_uuid) {
var postContainer = $('#cmp-uuid-' + post_uuid),
postData = composer.posts[post_uuid],
titleEl = postContainer.find('.title'),
bodyEl = postContainer.find('textarea');
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-07-25 13:18:55 -04:00
}
composer.post = function(post_uuid) {
2013-08-15 16:50:00 -04:00
var postData = composer.posts[post_uuid],
postContainer = $('#cmp-uuid-' + post_uuid),
titleEl = postContainer.find('.title'),
bodyEl = postContainer.find('textarea');
titleEl.val(titleEl.val().trim());
bodyEl.val(bodyEl.val().trim());
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'
});
}
if (titleEl.val().length < config.minimumTitleLength) {
return app.alert({
type: 'danger',
2013-07-18 14:47:41 -04:00
timeout: 2000,
title: 'Title too short',
2013-08-12 19:00:31 -04:00
message: "Please enter a longer title. At least " + config.minimumTitleLength+ " characters.",
alert_id: 'post_error'
});
}
if (bodyEl.val().length < config.minimumPostLength) {
2013-07-17 10:45:16 -04:00
return app.alert({
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'
});
}
// Still here? Let's post.
2013-06-05 10:18:29 -04:00
if (parseInt(postData.cid) > 0) {
socket.emit('api:topics.post', {
'title' : titleEl.val(),
'content' : bodyEl.val(),
2013-09-17 13:37:03 -04:00
'category_id' : postData.cid
});
} else if (parseInt(postData.tid) > 0) {
socket.emit('api:posts.reply', {
'topic_id' : postData.tid,
'content' : bodyEl.val()
});
2013-06-05 10:18:29 -04:00
} else if (parseInt(postData.pid) > 0) {
socket.emit('api:posts.edit', {
pid: postData.pid,
content: bodyEl.val(),
title: titleEl.val()
2013-06-05 10:18:29 -04:00
});
}
composer.discard(post_uuid);
}
composer.discard = function(post_uuid) {
if (composer.posts[post_uuid]) {
$('#cmp-uuid-' + post_uuid).remove();
delete composer.posts[post_uuid];
composer.active = undefined;
taskbar.discard('composer', post_uuid);
}
}
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;
taskbar.minimize('composer', post_uuid);
}
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 = "![" + img.name + "](uploading...)";
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 = "![" + data.name + "](uploading...)";
if(!err) {
textarea.val(currentText.replace(imgText, "![" + data.name + "](" + data.url + ")"));
} else {
textarea.val(currentText.replace(imgText, "![" + data.name + "](upload error)"));
}
composer.posts[post_uuid].uploadsInProgress.pop();
});
}
return {
2013-12-22 15:15:59 -05:00
newTopic: composer.newTopic,
newReply: composer.newReply,
editPost: composer.editPost,
load: composer.load,
minimize: composer.minimize
};
});