2014-03-16 14:17:43 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/* globals define, socket, app, config, ajaxify, utils, translator, templates, bootbox */
|
|
|
|
|
|
2014-05-29 17:24:38 -04:00
|
|
|
define('composer', ['taskbar', 'composer/controls', 'composer/uploads', 'composer/formatting', 'composer/drafts', 'composer/tags'], function(taskbar, controls, uploads, formatting, drafts, tags) {
|
2013-06-03 16:06:11 -04:00
|
|
|
var composer = {
|
2013-09-17 13:37:03 -04:00
|
|
|
active: undefined,
|
2014-06-10 15:32:08 -04:00
|
|
|
posts: {},
|
|
|
|
|
bsEnvironment: undefined
|
2014-05-18 15:09:54 -04:00
|
|
|
};
|
2014-04-30 14:22:51 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
socket.on('event:composer.ping', function(post_uuid) {
|
|
|
|
|
if (composer.active === post_uuid) {
|
|
|
|
|
socket.emit('modules.composer.pingActive', post_uuid);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-03-16 14:17:43 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
$(window).off('resize', onWindowResize).on('resize', onWindowResize);
|
2014-03-01 15:46:13 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
function onWindowResize() {
|
|
|
|
|
if (composer.active !== undefined) {
|
|
|
|
|
activateReposition(composer.active);
|
2014-02-26 21:55:29 -05:00
|
|
|
}
|
2014-02-20 02:05:49 -05:00
|
|
|
}
|
|
|
|
|
|
2014-03-12 14:08:55 -04:00
|
|
|
function alreadyOpen(post) {
|
|
|
|
|
// If a composer for the same cid/tid/pid is already open, return the uuid, else return bool false
|
|
|
|
|
var type, id;
|
|
|
|
|
|
|
|
|
|
if (post.hasOwnProperty('cid')) {
|
|
|
|
|
type = 'cid';
|
|
|
|
|
} else if (post.hasOwnProperty('tid')) {
|
|
|
|
|
type = 'tid';
|
|
|
|
|
} else if (post.hasOwnProperty('pid')) {
|
|
|
|
|
type = 'pid';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id = post[type];
|
|
|
|
|
|
|
|
|
|
// Find a match
|
2014-03-16 14:17:43 -04:00
|
|
|
for(var uuid in composer.posts) {
|
2014-03-12 14:08:55 -04:00
|
|
|
if (composer.posts[uuid].hasOwnProperty(type) && id === composer.posts[uuid][type]) {
|
|
|
|
|
return uuid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No matches...
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 02:05:49 -05:00
|
|
|
function push(post) {
|
2014-03-12 14:08:55 -04:00
|
|
|
var uuid = utils.generateUUID(),
|
|
|
|
|
existingUUID = alreadyOpen(post);
|
|
|
|
|
|
|
|
|
|
if (existingUUID) {
|
|
|
|
|
taskbar.updateActive(existingUUID);
|
|
|
|
|
return composer.load(existingUUID);
|
|
|
|
|
}
|
2014-02-20 02:05:49 -05:00
|
|
|
|
|
|
|
|
translator.translate('[[topic:composer.new_topic]]', function(newTopicStr) {
|
|
|
|
|
taskbar.push('composer', uuid, {
|
2014-06-13 13:57:38 -04:00
|
|
|
title: post.title ? post.title : newTopicStr
|
2014-02-20 02:05:49 -05:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2014-03-12 14:08:55 -04:00
|
|
|
// Construct a save_id
|
|
|
|
|
if (0 !== parseInt(app.uid, 10)) {
|
|
|
|
|
if (post.hasOwnProperty('cid')) {
|
|
|
|
|
post.save_id = ['composer', app.uid, 'cid', post.cid].join(':');
|
|
|
|
|
} else if (post.hasOwnProperty('tid')) {
|
|
|
|
|
post.save_id = ['composer', app.uid, 'tid', post.tid].join(':');
|
|
|
|
|
} else if (post.hasOwnProperty('pid')) {
|
|
|
|
|
post.save_id = ['composer', app.uid, 'pid', post.pid].join(':');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 02:05:49 -05:00
|
|
|
composer.posts[uuid] = post;
|
|
|
|
|
|
|
|
|
|
composer.load(uuid);
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-09 14:12:46 -04:00
|
|
|
function composerAlert(message) {
|
2014-02-20 02:05:49 -05:00
|
|
|
$('.action-bar button').removeAttr('disabled');
|
|
|
|
|
app.alert({
|
|
|
|
|
type: 'danger',
|
2014-04-09 14:12:46 -04:00
|
|
|
timeout: 3000,
|
|
|
|
|
title: '',
|
2014-02-20 02:05:49 -05:00
|
|
|
message: message,
|
|
|
|
|
alert_id: 'post_error'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 14:14:48 -04:00
|
|
|
composer.addButton = function(iconClass, onClick) {
|
2014-05-18 15:09:54 -04:00
|
|
|
formatting.addButton(iconClass, onClick);
|
2014-04-30 14:14:48 -04:00
|
|
|
};
|
2014-04-19 15:13:57 +01:00
|
|
|
|
2013-12-22 15:15:59 -05:00
|
|
|
composer.newTopic = function(cid) {
|
2014-04-10 22:31:55 +01:00
|
|
|
push({
|
|
|
|
|
cid: cid,
|
|
|
|
|
title: '',
|
|
|
|
|
body: '',
|
|
|
|
|
modified: false,
|
|
|
|
|
isMain: true
|
|
|
|
|
});
|
2014-02-20 02:05:49 -05:00
|
|
|
};
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2014-01-27 13:04:03 -05:00
|
|
|
composer.addQuote = function(tid, pid, title, username, text){
|
2014-04-10 22:31:55 +01:00
|
|
|
var uuid = composer.active;
|
|
|
|
|
|
|
|
|
|
if(uuid === undefined){
|
2014-05-20 12:35:46 -04:00
|
|
|
translator.translate('[[modules:composer.user_said, ' + username + ']]', function(translated) {
|
|
|
|
|
composer.newReply(tid, pid, title, translated + text);
|
|
|
|
|
});
|
2014-04-10 22:31:55 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bodyEl = $('#cmp-uuid-'+uuid).find('textarea');
|
|
|
|
|
var prevText = bodyEl.val();
|
|
|
|
|
if(tid !== composer.posts[uuid].tid) {
|
2014-05-20 12:35:46 -04:00
|
|
|
var link = '[' + title + '](/topic/' + tid + '#' + pid + ')';
|
|
|
|
|
translator.translate('[[modules:composer.user_said_in, ' + username + ', ' + link + ']]', onTranslated);
|
2014-04-10 22:31:55 +01:00
|
|
|
} else {
|
2014-05-20 12:35:46 -04:00
|
|
|
translator.translate('[[modules:composer.user_said, ' + username + ']]', onTranslated);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onTranslated(translated) {
|
|
|
|
|
composer.posts[uuid].body = (prevText.length ? prevText + '\n\n' : '') + translated + text;
|
|
|
|
|
bodyEl.val(composer.posts[uuid].body);
|
2014-01-27 02:51:49 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-22 17:56:13 -05:00
|
|
|
composer.newReply = function(tid, pid, title, text) {
|
2014-04-10 22:31:55 +01:00
|
|
|
push({
|
|
|
|
|
tid: tid,
|
|
|
|
|
toPid: pid,
|
|
|
|
|
title: title,
|
|
|
|
|
body: text,
|
|
|
|
|
modified: false,
|
|
|
|
|
isMain: false
|
|
|
|
|
});
|
2014-02-20 02:05:49 -05:00
|
|
|
};
|
2013-12-22 15:15:59 -05:00
|
|
|
|
|
|
|
|
composer.editPost = function(pid) {
|
2014-04-10 22:31:55 +01:00
|
|
|
socket.emit('modules.composer.push', pid, function(err, threadData) {
|
|
|
|
|
if(err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
push({
|
|
|
|
|
pid: pid,
|
|
|
|
|
title: threadData.title,
|
|
|
|
|
body: threadData.body,
|
|
|
|
|
modified: false,
|
|
|
|
|
isMain: !threadData.index,
|
2014-05-21 16:13:46 -04:00
|
|
|
topic_thumb: threadData.topic_thumb,
|
|
|
|
|
tags: threadData.tags
|
2014-04-10 22:31:55 +01:00
|
|
|
});
|
|
|
|
|
});
|
2014-02-20 02:05:49 -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) {
|
2014-05-18 15:09:54 -04:00
|
|
|
activateReposition(post_uuid);
|
2013-12-20 18:41:56 -05:00
|
|
|
} else {
|
2014-05-18 15:09:54 -04:00
|
|
|
createNewComposer(post_uuid);
|
2013-12-20 18:41:56 -05:00
|
|
|
}
|
2014-03-01 15:46:13 -05:00
|
|
|
|
2014-03-01 17:49:39 -05:00
|
|
|
var postData = composer.posts[post_uuid];
|
|
|
|
|
if (postData.tid) {
|
2014-03-01 15:46:13 -05:00
|
|
|
socket.emit('modules.composer.register', {
|
|
|
|
|
uuid: post_uuid,
|
2014-03-01 17:34:06 -05:00
|
|
|
tid: postData.tid,
|
2014-03-01 15:46:13 -05:00
|
|
|
uid: app.uid
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-02-20 02:05:49 -05:00
|
|
|
};
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
function createNewComposer(post_uuid) {
|
2014-03-28 15:35:07 -04:00
|
|
|
var allowTopicsThumbnail = config.allowTopicsThumbnail && composer.posts[post_uuid].isMain && (config.hasImageUploadPlugin || config.allowFileUploads);
|
2014-05-21 16:13:46 -04:00
|
|
|
var isTopic = composer.posts[post_uuid] ? !!composer.posts[post_uuid].cid : false;
|
|
|
|
|
var isMain = composer.posts[post_uuid] ? !!composer.posts[post_uuid].isMain : false;
|
2014-04-09 14:12:46 -04:00
|
|
|
|
2014-06-10 15:32:08 -04:00
|
|
|
composer.bsEnvironment = utils.findBootstrapEnvironment()
|
|
|
|
|
|
|
|
|
|
var template = (composer.bsEnvironment === 'xs' || composer.bsEnvironment === 'sm') ? 'composer-mobile' : 'composer';
|
2014-06-10 14:45:48 -04:00
|
|
|
|
|
|
|
|
templates.parse(template, {allowTopicsThumbnail: allowTopicsThumbnail, showTags: isTopic || isMain}, function(composerTemplate) {
|
2014-02-10 13:30:26 -05:00
|
|
|
translator.translate(composerTemplate, function(composerTemplate) {
|
|
|
|
|
composerTemplate = $(composerTemplate);
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
composerTemplate.attr('id', 'cmp-uuid-' + post_uuid);
|
2013-08-15 14:04:12 -04:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
$(document.body).append(composerTemplate);
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
var postContainer = $(composerTemplate[0]);
|
2013-08-15 17:03:43 -04:00
|
|
|
|
2014-05-21 16:13:46 -04:00
|
|
|
tags.init(postContainer, composer.posts[post_uuid]);
|
|
|
|
|
|
|
|
|
|
activateReposition(post_uuid);
|
|
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
if(config.allowFileUploads || config.hasImageUploadPlugin) {
|
2014-05-18 15:09:54 -04:00
|
|
|
uploads.initialize(post_uuid);
|
2014-02-10 13:30:26 -05:00
|
|
|
}
|
2013-08-15 16:50:00 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
formatting.addHandler(postContainer);
|
|
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
var postData = composer.posts[post_uuid],
|
2014-02-20 02:05:49 -05:00
|
|
|
bodyEl = postContainer.find('textarea'),
|
2014-05-18 15:09:54 -04:00
|
|
|
draft = drafts.getDraft(postData.save_id);
|
2014-05-16 16:28:35 -04:00
|
|
|
|
2014-06-12 13:58:17 -04:00
|
|
|
postData.title = $('<div></div>').text(postData.title).html();
|
2014-02-10 13:30:26 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
updateTitle(postData, postContainer);
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
if (allowTopicsThumbnail) {
|
|
|
|
|
uploads.toggleThumbEls(postContainer, composer.posts[post_uuid].topic_thumb || '');
|
2014-03-12 14:08:55 -04:00
|
|
|
}
|
2014-01-08 02:39:06 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
bodyEl.val(draft ? draft : postData.body);
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
postContainer.on('change', 'input, textarea', function() {
|
|
|
|
|
composer.posts[post_uuid].modified = true;
|
|
|
|
|
});
|
2013-07-21 12:29:57 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
postContainer.on('click', '.action-bar button[data-action="post"]', function() {
|
|
|
|
|
$(this).attr('disabled', true);
|
|
|
|
|
post(post_uuid);
|
2014-02-20 02:05:49 -05:00
|
|
|
});
|
|
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
postContainer.on('click', '.action-bar button[data-action="discard"]', function() {
|
|
|
|
|
if (composer.posts[post_uuid].modified) {
|
|
|
|
|
bootbox.confirm('Are you sure you wish to discard this post?', function(confirm) {
|
|
|
|
|
if (confirm) {
|
|
|
|
|
discard(post_uuid);
|
2014-02-26 21:55:29 -05:00
|
|
|
}
|
2014-05-18 15:09:54 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
discard(post_uuid);
|
2013-12-23 20:59:55 -05:00
|
|
|
}
|
2014-02-10 13:30:26 -05:00
|
|
|
});
|
2013-12-23 20:59:55 -05:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
postContainer.find('.nav-tabs a').click(function (e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
$(this).tab('show');
|
|
|
|
|
var selector = $(this).attr('data-pane');
|
|
|
|
|
postContainer.find('.tab-content div').removeClass('active');
|
|
|
|
|
postContainer.find(selector).addClass('active');
|
|
|
|
|
if(selector === '.tab-write') {
|
|
|
|
|
bodyEl.focus();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2014-01-17 16:16:00 -05:00
|
|
|
|
2014-02-10 13:30:26 -05:00
|
|
|
bodyEl.on('blur', function() {
|
|
|
|
|
socket.emit('modules.composer.renderPreview', bodyEl.val(), function(err, preview) {
|
2014-02-13 19:41:54 -05:00
|
|
|
preview = $(preview);
|
|
|
|
|
preview.find('img').addClass('img-responsive');
|
2014-03-16 14:17:43 -04:00
|
|
|
postContainer.find('.preview').html(preview);
|
|
|
|
|
});
|
2014-02-10 13:30:26 -05:00
|
|
|
});
|
2014-01-17 16:16:00 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
drafts.init(postContainer, postData);
|
2014-01-31 21:27:11 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
handleResize(postContainer);
|
2014-02-10 17:31:51 -05:00
|
|
|
|
2014-02-27 14:05:31 -05:00
|
|
|
socket.emit('modules.composer.renderHelp', function(err, html) {
|
2014-05-10 23:43:15 -04:00
|
|
|
if (!err && html && html.length > 0) {
|
2014-02-27 14:05:31 -05:00
|
|
|
postContainer.find('.help').html(html);
|
|
|
|
|
postContainer.find('[data-pane=".tab-help"]').parent().removeClass('hidden');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2014-02-10 17:31:51 -05:00
|
|
|
$(window).trigger('action:composer.loaded', {
|
|
|
|
|
post_uuid: post_uuid
|
|
|
|
|
});
|
2014-04-30 14:14:48 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
formatting.addComposerButtons();
|
2014-05-21 16:13:46 -04:00
|
|
|
|
|
|
|
|
|
2013-12-17 23:42:02 -05:00
|
|
|
});
|
2013-12-20 18:41:56 -05:00
|
|
|
});
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateTitle(postData, postContainer) {
|
|
|
|
|
var titleEl = postContainer.find('.title');
|
|
|
|
|
|
|
|
|
|
if (parseInt(postData.tid, 10) > 0) {
|
|
|
|
|
translator.translate('[[topic:composer.replying_to, ' + postData.title + ']]', function(newTitle) {
|
|
|
|
|
titleEl.val(newTitle);
|
|
|
|
|
});
|
|
|
|
|
titleEl.prop('disabled', true);
|
|
|
|
|
} else if (parseInt(postData.pid, 10) > 0) {
|
|
|
|
|
titleEl.val(postData.title);
|
|
|
|
|
titleEl.prop('disabled', true);
|
|
|
|
|
socket.emit('modules.composer.editCheck', postData.pid, function(err, editCheck) {
|
|
|
|
|
if (!err && editCheck.titleEditable) {
|
|
|
|
|
titleEl.prop('disabled', false);
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-06-03 16:06:11 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
} else {
|
|
|
|
|
titleEl.val(postData.title);
|
|
|
|
|
titleEl.prop('disabled', false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleResize(postContainer) {
|
|
|
|
|
var resizeActive = false,
|
|
|
|
|
resizeOffset = 0,
|
|
|
|
|
resizeStart = function(e) {
|
|
|
|
|
var resizeRect = resizeEl[0].getBoundingClientRect();
|
|
|
|
|
var resizeCenterY = resizeRect.top + (resizeRect.height/2);
|
|
|
|
|
resizeOffset = resizeCenterY - e.clientY;
|
|
|
|
|
resizeActive = true;
|
|
|
|
|
|
|
|
|
|
$(window).on('mousemove', resizeAction);
|
|
|
|
|
$(window).on('mouseup', resizeStop);
|
|
|
|
|
$('body').on('touchmove', resizeTouchAction);
|
|
|
|
|
},
|
|
|
|
|
resizeStop = function() {
|
|
|
|
|
resizeActive = false;
|
|
|
|
|
postContainer.find('textarea').focus();
|
|
|
|
|
$(window).off('mousemove', resizeAction);
|
|
|
|
|
$(window).off('mouseup', resizeStop);
|
|
|
|
|
$('body').off('touchmove', resizeTouchAction);
|
|
|
|
|
},
|
|
|
|
|
resizeTouchAction = function(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
resizeAction(e.touches[0]);
|
|
|
|
|
},
|
|
|
|
|
resizeAction = function(e) {
|
|
|
|
|
if (resizeActive) {
|
|
|
|
|
var position = (e.clientY + resizeOffset);
|
|
|
|
|
var newHeight = $(window).height() - position;
|
2014-05-21 16:13:46 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
if(newHeight > $(window).height() - $('#header-menu').height() - 20) {
|
|
|
|
|
newHeight = $(window).height() - $('#header-menu').height() - 20;
|
2014-05-21 16:13:46 -04:00
|
|
|
} else if (newHeight < 100) {
|
|
|
|
|
newHeight = 100;
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
postContainer.css('height', newHeight);
|
|
|
|
|
$('body').css({'margin-bottom': newHeight});
|
2014-05-21 16:13:46 -04:00
|
|
|
resizeTabContent(postContainer);
|
2014-05-18 15:09:54 -04:00
|
|
|
resizeSavePosition(newHeight);
|
|
|
|
|
}
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
resizeSavePosition = function(px) {
|
|
|
|
|
var percentage = px / $(window).height();
|
|
|
|
|
localStorage.setItem('composer:resizePercentage', percentage);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var resizeEl = postContainer.find('.resizer');
|
|
|
|
|
|
|
|
|
|
resizeEl.on('mousedown', resizeStart);
|
|
|
|
|
|
|
|
|
|
resizeEl.on('touchstart', function(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
resizeStart(e.touches[0]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
resizeEl.on('touchend', function(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
resizeStop();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function activateReposition(post_uuid) {
|
2013-12-20 18:41:56 -05:00
|
|
|
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'),
|
|
|
|
|
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;
|
2014-06-10 15:32:08 -04:00
|
|
|
var env = composer.bsEnvironment;
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2013-12-20 18:41:56 -05:00
|
|
|
if (percentage) {
|
2014-01-17 16:16:00 -05:00
|
|
|
if ( env === 'md' || env === 'lg') {
|
|
|
|
|
postContainer.css('height', Math.floor($(window).height() * percentage) + 'px');
|
2013-12-20 18:41:56 -05:00
|
|
|
}
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
2013-08-15 16:50:00 -04:00
|
|
|
|
2014-01-17 16:16:00 -05:00
|
|
|
if(env === 'sm' || env === 'xs') {
|
|
|
|
|
postContainer.css('height', $(window).height() - $('#header-menu').height());
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-09 00:33:10 -05:00
|
|
|
if(config.hasImageUploadPlugin) {
|
2014-01-17 17:07:09 -05:00
|
|
|
if(env === 'md' || env === 'lg') {
|
|
|
|
|
postContainer.find('.upload-instructions').removeClass('hide');
|
|
|
|
|
}
|
2013-12-31 17:01:51 -05:00
|
|
|
postContainer.find('.img-upload-btn').removeClass('hide');
|
2014-02-26 21:55:29 -05:00
|
|
|
postContainer.find('#files.lt-ie9').removeClass('hide');
|
2013-12-31 17:01:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(config.allowFileUploads) {
|
|
|
|
|
postContainer.find('.file-upload-btn').removeClass('hide');
|
2014-02-26 21:55:29 -05:00
|
|
|
postContainer.find('#files.lt-ie9').removeClass('hide');
|
2013-12-23 20:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
2014-01-17 19:42:20 -05:00
|
|
|
postContainer.css('visibility', 'visible')
|
2014-01-24 20:50:55 -05:00
|
|
|
.css('z-index', 2);
|
2014-01-17 19:42:20 -05:00
|
|
|
|
2014-01-20 16:50:39 -05:00
|
|
|
$('body').css({'margin-bottom': postContainer.css('height')});
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2014-06-10 15:32:08 -04:00
|
|
|
if (env !== 'sm' && env !== 'xs') {
|
2014-06-12 13:58:17 -04:00
|
|
|
focusElements(post_uuid);
|
2014-06-10 15:32:08 -04:00
|
|
|
}
|
2014-06-12 13:58:17 -04:00
|
|
|
|
2014-05-21 16:13:46 -04:00
|
|
|
resizeTabContent(postContainer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resizeTabContent(postContainer) {
|
|
|
|
|
var h1 = postContainer.find('.title').outerHeight(true);
|
|
|
|
|
var h2 = postContainer.find('.tags-container').outerHeight(true);
|
|
|
|
|
var h3 = postContainer.find('.formatting-bar').outerHeight(true);
|
|
|
|
|
var h4 = postContainer.find('.nav-tabs').outerHeight(true);
|
|
|
|
|
var h5 = postContainer.find('.instructions').outerHeight(true);
|
|
|
|
|
var h6 = postContainer.find('.topic-thumb-container').outerHeight(true);
|
|
|
|
|
var h7 = $('.taskbar').height();
|
|
|
|
|
var total = h1 + h2 + h3 + h4 + h5 + h6 + h7;
|
|
|
|
|
postContainer.find('.tab-content').css('height', postContainer.height() - total);
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
2013-06-04 16:20:27 -04:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
function focusElements(post_uuid) {
|
2013-12-20 18:41:56 -05:00
|
|
|
var postContainer = $('#cmp-uuid-' + post_uuid),
|
|
|
|
|
postData = composer.posts[post_uuid],
|
|
|
|
|
bodyEl = postContainer.find('textarea');
|
2013-12-18 23:56:59 -05:00
|
|
|
|
2014-04-10 20:24:51 +01:00
|
|
|
if ((parseInt(postData.tid, 10) || parseInt(postData.pid, 10)) > 0) {
|
2013-12-20 18:41:56 -05:00
|
|
|
bodyEl.focus();
|
|
|
|
|
bodyEl.selectionStart = bodyEl.val().length;
|
|
|
|
|
bodyEl.selectionEnd = bodyEl.val().length;
|
2014-04-10 20:24:51 +01:00
|
|
|
} else if (parseInt(postData.cid, 10) > 0) {
|
2014-05-20 13:23:09 -04:00
|
|
|
postContainer.find('.title').focus();
|
2013-12-18 23:56:59 -05:00
|
|
|
}
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
2014-02-20 02:05:49 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
function post(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'),
|
2014-02-20 02:05:49 -05:00
|
|
|
bodyEl = postContainer.find('textarea'),
|
|
|
|
|
thumbEl = postContainer.find('input#topic-thumb-url');
|
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());
|
2014-06-02 17:31:09 -04:00
|
|
|
if (thumbEl.length) {
|
2014-02-22 02:01:08 -05:00
|
|
|
thumbEl.val(thumbEl.val().trim());
|
|
|
|
|
}
|
2013-08-24 03:36:44 +08:00
|
|
|
|
2014-01-22 17:23:20 -05:00
|
|
|
var checkTitle = parseInt(postData.cid, 10) || parseInt(postData.pid, 10);
|
|
|
|
|
|
2014-06-12 13:58:17 -04:00
|
|
|
if (uploads.inProgress[post_uuid] && uploads.inProgress[post_uuid].length) {
|
2014-04-09 14:12:46 -04:00
|
|
|
return composerAlert('[[error:still-uploading]]');
|
2014-01-22 17:23:20 -05:00
|
|
|
} else if (checkTitle && titleEl.val().length < parseInt(config.minimumTitleLength, 10)) {
|
2014-04-09 14:12:46 -04:00
|
|
|
return composerAlert('[[error:title-too-short, ' + config.minimumTitleLength + ']]');
|
2014-01-22 17:23:20 -05:00
|
|
|
} else if (checkTitle && titleEl.val().length > parseInt(config.maximumTitleLength, 10)) {
|
2014-04-09 14:12:46 -04:00
|
|
|
return composerAlert('[[error:title-too-long, ' + config.maximumTitleLength + ']]');
|
2014-06-02 17:31:09 -04:00
|
|
|
} else if (checkTitle && !utils.slugify(titleEl.val()).length) {
|
|
|
|
|
return composerAlert('[[error:invalid-title]]');
|
2013-12-31 14:25:26 -05:00
|
|
|
} else if (bodyEl.val().length < parseInt(config.minimumPostLength, 10)) {
|
2014-04-09 14:12:46 -04:00
|
|
|
return composerAlert('[[error:content-too-short, ' + config.minimumPostLength + ']]');
|
2013-07-17 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2014-01-22 17:23:20 -05:00
|
|
|
if (parseInt(postData.cid, 10) > 0) {
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('topics.post', {
|
2014-02-20 02:05:49 -05:00
|
|
|
title: titleEl.val(),
|
|
|
|
|
content: bodyEl.val(),
|
2014-02-22 02:01:08 -05:00
|
|
|
topic_thumb: thumbEl.val() || '',
|
2014-05-21 16:13:46 -04:00
|
|
|
category_id: postData.cid,
|
|
|
|
|
tags: tags.getTags(post_uuid)
|
2014-04-24 15:53:41 -04:00
|
|
|
}, function(err, topic) {
|
|
|
|
|
done(err);
|
|
|
|
|
if (!err) {
|
|
|
|
|
ajaxify.go('topic/' + topic.slug);
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-01-22 17:23:20 -05:00
|
|
|
} else if (parseInt(postData.tid, 10) > 0) {
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('posts.reply', {
|
2014-02-22 18:56:37 -05:00
|
|
|
tid: postData.tid,
|
2014-02-22 17:56:13 -05:00
|
|
|
content: bodyEl.val(),
|
|
|
|
|
toPid: postData.toPid
|
2014-01-27 20:21:14 -05:00
|
|
|
}, done);
|
2014-01-22 17:23:20 -05:00
|
|
|
} else if (parseInt(postData.pid, 10) > 0) {
|
2014-01-16 15:10:37 -05:00
|
|
|
socket.emit('posts.edit', {
|
2013-06-05 10:18:29 -04:00
|
|
|
pid: postData.pid,
|
2013-12-20 18:41:56 -05:00
|
|
|
content: bodyEl.val(),
|
2014-02-20 02:05:49 -05:00
|
|
|
title: titleEl.val(),
|
2014-05-21 16:13:46 -04:00
|
|
|
topic_thumb: thumbEl.val() || '',
|
|
|
|
|
tags: tags.getTags(post_uuid)
|
2014-01-27 20:21:14 -05:00
|
|
|
}, done);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
|
$('.action-bar button').removeAttr('disabled');
|
2014-04-09 14:12:46 -04:00
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
2014-01-27 20:21:14 -05:00
|
|
|
}
|
2014-04-09 14:12:46 -04:00
|
|
|
|
|
|
|
|
app.alertSuccess('[[success:topic-post]]');
|
|
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
discard(post_uuid);
|
|
|
|
|
drafts.removeDraft(postData.save_id);
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
2013-12-31 14:25:26 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
function discard(post_uuid) {
|
2013-06-04 16:20:27 -04:00
|
|
|
if (composer.posts[post_uuid]) {
|
2013-12-20 18:41:56 -05:00
|
|
|
$('#cmp-uuid-' + post_uuid).remove();
|
2014-05-18 15:09:54 -04:00
|
|
|
drafts.removeDraft(composer.posts[post_uuid].save_id);
|
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);
|
2014-01-20 16:50:39 -05:00
|
|
|
$('body').css({'margin-bottom': 0});
|
2014-01-27 20:21:14 -05:00
|
|
|
$('.action-bar button').removeAttr('disabled');
|
2014-03-01 16:53:41 -05:00
|
|
|
|
|
|
|
|
socket.emit('modules.composer.unregister', post_uuid);
|
2013-06-04 16:20:27 -04:00
|
|
|
}
|
2014-05-18 15:09:54 -04:00
|
|
|
}
|
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);
|
2014-03-01 17:49:39 -05:00
|
|
|
|
|
|
|
|
socket.emit('modules.composer.unregister', post_uuid);
|
2014-02-20 02:05:49 -05:00
|
|
|
};
|
2014-02-13 19:41:54 -05:00
|
|
|
|
2014-05-18 15:09:54 -04:00
|
|
|
return composer;
|
2014-04-10 20:31:57 +01:00
|
|
|
});
|