diff --git a/public/src/forum/category.js b/public/src/forum/category.js index 6b25a85701..0472569989 100644 --- a/public/src/forum/category.js +++ b/public/src/forum/category.js @@ -1,4 +1,4 @@ -define(function () { +define(['composer'], function(composer) { var Category = {}, loadingMoreTopics = false; @@ -27,9 +27,7 @@ define(function () { }); $('#new_post').on('click', function () { - require(['composer'], function (cmp) { - cmp.push(0, cid); - }); + composer.newTopic(cid); }); ajaxify.register_events([ @@ -84,7 +82,7 @@ define(function () { var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: [data] }); - + translator.translate(html, function(translatedHTML) { var topic = $(translatedHTML), container = $('#topics-container'), @@ -137,7 +135,7 @@ define(function () { var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: topics }); - + translator.translate(html, function(translatedHTML) { var container = $('#topics-container'); diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 1022b098e1..73fa5903d6 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -274,7 +274,7 @@ define(['composer'], function(composer) { } if (thread_state.locked !== '1') { - composer.push(tid, null, null, selectionText.length > 0 ? selectionText + '\n\n' + username : '' + username); + composer.newReply(tid, topic_name, selectionText.length > 0 ? selectionText + '\n\n' + username : '' + username); } }); @@ -286,7 +286,7 @@ define(['composer'], function(composer) { quoted = '> ' + data.post.replace(/\n/g, '\n> ') + '\n\n'; - composer.push(tid, null, null, quoted); + composer.newReply(tid, topic_name, quoted); }); } }); @@ -337,8 +337,7 @@ define(['composer'], function(composer) { $('#post-container').delegate('.edit', 'click', function(e) { var pid = $(this).parents('li').attr('data-pid'); - - composer.push(null, null, pid); + composer.editPost(pid); }); $('#post-container').delegate('.delete', 'click', function(e) { diff --git a/public/src/modules/composer.js b/public/src/modules/composer.js index c9c08da3a6..15949c550e 100644 --- a/public/src/modules/composer.js +++ b/public/src/modules/composer.js @@ -4,47 +4,72 @@ define(['taskbar'], function(taskbar) { posts: {} }; - composer.push = function(tid, cid, pid, text) { - - socket.emit('api:composer.push', { - tid: tid, // Replying - cid: cid, // Posting - pid: pid, // Editing - body: text // Predefined text - }, function(threadData) { - - if(threadData.error) { - 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; - } - - var uuid = utils.generateUUID(); - - taskbar.push('composer', uuid, { - title: (!threadData.cid ? (threadData.title || '') : 'New Topic'), - icon: threadData.picture + 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; + } - composer.posts[uuid] = { - tid: threadData.tid, - cid: threadData.cid, - pid: threadData.pid, - title: threadData.title || '', - body: threadData.body || '', + composer.newTopic = function(cid) { + if(allowed()) { + push({ + cid: cid, + title: '', + body: '', modified: false - }; + }); + } + } - composer.load(uuid); + composer.newReply = function(tid, title, text) { + if(allowed()) { + push({ + tid: tid, + title: title, + body: text, + modified: false + }); + } + } + + 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 + }); + }); + } + } + + function push(post) { + var uuid = utils.generateUUID(); + + taskbar.push('composer', uuid, { + title: post.title ? post.title : 'New Topic', + icon: post.picture }); + + composer.posts[uuid] = post; + + composer.load(uuid); } composer.load = function(post_uuid) { @@ -470,7 +495,9 @@ define(['taskbar'], function(taskbar) { } return { - push: composer.push, + newTopic: composer.newTopic, + newReply: composer.newReply, + editPost: composer.editPost, load: composer.load, minimize: composer.minimize }; diff --git a/public/src/templates.js b/public/src/templates.js index 8390f54075..5829ac5195 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -123,6 +123,11 @@ } templates.preload_template = function(tpl_name, callback) { + + if(templates[tpl_name]) { + return callback(); + } + // TODO: This should be "load_template", and the current load_template // should be named something else // TODO: The "Date.now()" in the line below is only there for development purposes. @@ -355,7 +360,7 @@ template = template.replace(regex, ''); namespace = ''; } - + // clean up all undefined conditionals template = template.replace(//gi, ''); diff --git a/src/routes/api.js b/src/routes/api.js index 6f05f46bec..fe6589140e 100644 --- a/src/routes/api.js +++ b/src/routes/api.js @@ -36,6 +36,7 @@ var path = require('path'), config.minimumPasswordLength = meta.config.minimumPasswordLength; config.maximumSignatureLength = meta.config.maximumSignatureLength; config.useOutgoingLinksPage = meta.config.useOutgoingLinksPage; + config.allowGuestPosting = meta.config.allowGuestPosting; config.emailSetup = !!meta.config['email:from']; res.json(200, config); diff --git a/src/websockets.js b/src/websockets.js index bcbc1cc370..307abf80ec 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -803,32 +803,9 @@ websockets.init = function(io) { }); socket.on('api:composer.push', function(data, callback) { - if (parseInt(uid, 10) > 0 || parseInt(meta.config.allowGuestPosting, 10) === 1) { - if (parseInt(data.tid) > 0) { - topics.getTopicData(data.tid, function(err, topicData) { - if (data.body) { - topicData.body = data.body; - } - callback({ - tid: data.tid, - title: topicData.title, - body: topicData.body - }); - }); - } else if (parseInt(data.cid) > 0) { - user.getUserFields(uid, ['username', 'picture'], function(err, userData) { - if (!err && userData) { - callback({ - tid: 0, - cid: data.cid, - username: userData.username, - picture: userData.picture, - title: undefined - }); - } - }); - } else if (parseInt(data.pid) > 0) { + if (parseInt(uid, 10) > 0 || parseInt(meta.config.allowGuestPosting, 10) === 1) { + if (parseInt(data.pid) > 0) { async.parallel([ function(next) {