diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 4c6b83991a..7b8cf215fb 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -76,6 +76,9 @@ var ajaxify = {}; templates.flush(); templates.load_template(function () { exec_body_scripts(content); + require(['forum/' + tpl_url], function(script) { + if (script && script.init) script.init(); + }); if (callback) { callback(); diff --git a/public/src/forum/account.js b/public/src/forum/account.js index 45d5813ae6..aea4cf764e 100644 --- a/public/src/forum/account.js +++ b/public/src/forum/account.js @@ -1,85 +1,92 @@ -(function() { - var yourid = templates.get('yourid'), - theirid = templates.get('theirid'), - isFollowing = templates.get('isFollowing'); +define(['forum/accountheader'], function(header) { + var Account = {}; - $(document).ready(function() { - var username = $('.account-username a').html(); - app.enter_room('user/' + theirid); + Account.init = function() { + header.init(); - app.addCommasToNumbers(); + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'), + isFollowing = templates.get('isFollowing'); - var followBtn = $('#follow-btn'); - var unfollowBtn = $('#unfollow-btn'); + $(document).ready(function() { + var username = $('.account-username a').html(); + app.enter_room('user/' + theirid); - if (yourid !== theirid) { - if (isFollowing) { - followBtn.hide(); - unfollowBtn.show(); - } else { - followBtn.show(); - unfollowBtn.hide(); - } - } else { - followBtn.hide(); - unfollowBtn.hide(); - } + app.addCommasToNumbers(); - followBtn.on('click', function() { - socket.emit('api:user.follow', { - uid: theirid - }, function(success) { - if (success) { + var followBtn = $('#follow-btn'); + var unfollowBtn = $('#unfollow-btn'); + + if (yourid !== theirid) { + if (isFollowing) { followBtn.hide(); unfollowBtn.show(); - app.alertSuccess('You are now following ' + username + '!'); } else { - app.alertError('There was an error following' + username + '!'); - } - }); - return false; - }); - - unfollowBtn.on('click', function() { - socket.emit('api:user.unfollow', { - uid: theirid - }, function(success) { - if (success) { followBtn.show(); unfollowBtn.hide(); - app.alertSuccess('You are no longer following ' + username + '!'); - } else { - app.alertError('There was an error unfollowing ' + username + '!'); } + } else { + followBtn.hide(); + unfollowBtn.hide(); + } + + followBtn.on('click', function() { + socket.emit('api:user.follow', { + uid: theirid + }, function(success) { + if (success) { + followBtn.hide(); + unfollowBtn.show(); + app.alertSuccess('You are now following ' + username + '!'); + } else { + app.alertError('There was an error following' + username + '!'); + } + }); + return false; }); - return false; - }); - $('.user-recent-posts .topic-row').on('click', function() { - ajaxify.go($(this).attr('topic-url')); - }); + unfollowBtn.on('click', function() { + socket.emit('api:user.unfollow', { + uid: theirid + }, function(success) { + if (success) { + followBtn.show(); + unfollowBtn.hide(); + app.alertSuccess('You are no longer following ' + username + '!'); + } else { + app.alertError('There was an error unfollowing ' + username + '!'); + } + }); + return false; + }); - var onlineStatus = $('.account-online-status'); + $('.user-recent-posts .topic-row').on('click', function() { + ajaxify.go($(this).attr('topic-url')); + }); - function handleUserOnline(data) { - if (data.online) { - onlineStatus.find('span span').text('online'); - onlineStatus.find('i').attr('class', 'icon-circle'); - } else { - onlineStatus.find('span span').text('offline'); - onlineStatus.find('i').attr('class', 'icon-circle-blank'); - } - } + socket.on('api:user.isOnline', Account.handleUserOnline); - socket.on('api:user.isOnline', handleUserOnline); + socket.emit('api:user.isOnline', theirid, Account.handleUserOnline); - socket.emit('api:user.isOnline', theirid, handleUserOnline); + socket.on('event:new_post', function(data) { + var html = templates.prepare(templates['account'].blocks['posts']).parse(data); + $('.user-recent-posts').prepend(html); + }); - socket.on('event:new_post', function(data) { - var html = templates.prepare(templates['account'].blocks['posts']).parse(data); - $('.user-recent-posts').prepend(html); }); + }; - }); + Account.handleUserOnline = function(data) { + var onlineStatus = $('.account-online-status'); + + if (data.online) { + onlineStatus.find('span span').text('online'); + onlineStatus.find('i').attr('class', 'icon-circle'); + } else { + onlineStatus.find('span span').text('offline'); + onlineStatus.find('i').attr('class', 'icon-circle-blank'); + } + }; -}()); \ No newline at end of file + return Account; +}); \ No newline at end of file diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.js index 25c6018e91..7771562454 100644 --- a/public/src/forum/accountedit.js +++ b/public/src/forum/accountedit.js @@ -1,88 +1,258 @@ -var gravatarPicture = templates.get('gravatarpicture'); -var uploadedPicture = templates.get('uploadedpicture'); +define(['forum/accountheader'], function(header) { + var AccountEdit = {}; -$(document).ready(function() { + AccountEdit.init = function() { + header.init(); + var gravatarPicture = templates.get('gravatarpicture'); + var uploadedPicture = templates.get('uploadedpicture'); + $('#uploadForm').submit(function() { + AccountEdit.status('uploading the file ...'); - $('#uploadForm').submit(function() { - status('uploading the file ...'); + $('#upload-progress-bar').css('width', '0%'); + $('#upload-progress-box').show(); + $('#upload-progress-box').removeClass('hide'); - $('#upload-progress-bar').css('width', '0%'); - $('#upload-progress-box').show(); - $('#upload-progress-box').removeClass('hide'); + if (!$('#userPhotoInput').val()) { + AccountEdit.error('select an image to upload!'); + return false; + } + + $(this).find('#imageUploadCsrf').val($('#csrf_token').val()); - if (!$('#userPhotoInput').val()) { - error('select an image to upload!'); - return false; - } - $(this).find('#imageUploadCsrf').val($('#csrf_token').val()); + $(this).ajaxSubmit({ + error: function(xhr) { + AccountEdit.error('Error: ' + xhr.status); + }, - $(this).ajaxSubmit({ + uploadProgress: function(event, position, total, percent) { + console.log(percent); + $('#upload-progress-bar').css('width', percent + '%'); + }, - error: function(xhr) { - error('Error: ' + xhr.status); - }, - uploadProgress: function(event, position, total, percent) { - console.log(percent); - $('#upload-progress-bar').css('width', percent + '%'); - }, + success: function(response) { + if (response.error) { + AccountEdit.error(response.error); + return; + } + var imageUrlOnServer = response.path; - success: function(response) { - if (response.error) { - error(response.error); - return; + $('#user-current-picture').attr('src', imageUrlOnServer); + $('#user-uploaded-picture').attr('src', imageUrlOnServer); + + uploadedPicture = imageUrlOnServer; + + setTimeout(function() { + AccountEdit.hideAlerts(); + $('#upload-picture-modal').modal('hide'); + }, 750); + + socket.emit('api:updateHeader', { + fields: ['username', 'picture', 'userslug'] + }); + AccountEdit.success('File uploaded successfully!'); } + }); - var imageUrlOnServer = response.path; + return false; + }); - $('#user-current-picture').attr('src', imageUrlOnServer); - $('#user-uploaded-picture').attr('src', imageUrlOnServer); + var selectedImageType = ''; + + $('#submitBtn').on('click', function() { + + var userData = { + uid: $('#inputUID').val(), + email: $('#inputEmail').val(), + fullname: $('#inputFullname').val(), + website: $('#inputWebsite').val(), + birthday: $('#inputBirthday').val(), + location: $('#inputLocation').val(), + signature: $('#inputSignature').val() + }; + + socket.emit('api:user.updateProfile', userData, function(err, data) { + if (data.success) { + app.alertSuccess('Your profile has been updated successfully!'); + if (data.picture) { + $('#user-current-picture').attr('src', data.picture); + $('#user_label img').attr('src', data.picture); + } + if (data.gravatarpicture) { + $('#user-gravatar-picture').attr('src', data.gravatarpicture); + gravatarPicture = data.gravatarpicture; + } + } else { + app.alertError('There was an error updating your profile! ' + err.error); + } + }); + return false; + }); + + $('#changePictureBtn').on('click', function() { + selectedImageType = ''; + AccountEdit.updateImages(); - uploadedPicture = imageUrlOnServer; + $('#change-picture-modal').modal('show'); + $('#change-picture-modal').removeClass('hide'); - setTimeout(function() { - hideAlerts(); - $('#upload-picture-modal').modal('hide'); - }, 750); + return false; + }); + + $('#gravatar-box').on('click', function() { + $('#gravatar-box .icon-ok').show(); + $('#uploaded-box .icon-ok').hide(); + selectedImageType = 'gravatar'; + }); - socket.emit('api:updateHeader', { - fields: ['username', 'picture', 'userslug'] - }); - success('File uploaded successfully!'); + $('#uploaded-box').on('click', function() { + $('#gravatar-box .icon-ok').hide(); + $('#uploaded-box .icon-ok').show(); + selectedImageType = 'uploaded'; + }); + + $('#savePictureChangesBtn').on('click', function() { + $('#change-picture-modal').modal('hide'); + + if (selectedImageType) { + AccountEdit.changeUserPicture(selectedImageType); + + if (selectedImageType == 'gravatar') + $('#user-current-picture').attr('src', gravatarPicture); + else if (selectedImageType == 'uploaded') + $('#user-current-picture').attr('src', uploadedPicture); } + + }); + + $('#upload-picture-modal').on('hide', function() { + $('#userPhotoInput').val(''); + }); + + $('#uploadPictureBtn').on('click', function() { + + $('#change-picture-modal').modal('hide'); + $('#upload-picture-modal').modal('show'); + $('#upload-picture-modal').removeClass('hide'); + + AccountEdit.hideAlerts(); + + return false; + }); + + $('#pictureUploadSubmitBtn').on('click', function() { + $('#uploadForm').submit(); }); - return false; - }); + (function handlePasswordChange() { + var currentPassword = $('#inputCurrentPassword'); + var password_notify = $('#password-notify'); + var password_confirm_notify = $('#password-confirm-notify'); + var password = $('#inputNewPassword'); + var password_confirm = $('#inputNewPasswordAgain'); + var passwordvalid = false; + var passwordsmatch = false; + + + function onPasswordChanged() { + passwordvalid = utils.isPasswordValid(password.val()); + if (password.val().length < config.minimumPasswordLength) { + password_notify.html('Password too short'); + password_notify.attr('class', 'alert alert-danger'); + password_notify.removeClass('hide'); + } else if (!passwordvalid) { + password_notify.html('Invalid password'); + password_notify.attr('class', 'alert alert-danger'); + password_notify.removeClass('hide'); + } else { + password_notify.html('OK!'); + password_notify.attr('class', 'alert alert-success'); + password_notify.removeClass('hide'); + } + + onPasswordConfirmChanged(); + } + + function onPasswordConfirmChanged() { + if (password_notify.hasClass('alert-danger') || !password_confirm.val()) { + password_confirm_notify.addClass('hide'); + return; + } + if (password.val() !== password_confirm.val()) { + password_confirm_notify.html('Passwords must match!'); + password_confirm_notify.attr('class', 'alert alert-danger'); + password_confirm_notify.removeClass('hide'); + passwordsmatch = false; + } else { + password_confirm_notify.html('OK!'); + password_confirm_notify.attr('class', 'alert alert-success'); + password_confirm_notify.removeClass('hide'); + passwordsmatch = true; + } + } + + password.on('blur', onPasswordChanged); + password_confirm.on('blur', onPasswordConfirmChanged); - function hideAlerts() { + $('#changePasswordBtn').on('click', function() { + + if (passwordvalid && passwordsmatch && currentPassword.val()) { + socket.emit('api:user.changePassword', { + 'currentPassword': currentPassword.val(), + 'newPassword': password.val() + }, function(err) { + + currentPassword.val(''); + password.val(''); + password_confirm.val(''); + password_notify.addClass('hide'); + password_confirm_notify.addClass('hide'); + passwordsmatch = false; + passwordvalid = false; + + if (err) { + app.alertError(err.error); + return; + } + + app.alertSuccess('Your password is updated!'); + + }); + } + return false; + }); + + }()); + }; + + AccountEdit.hideAlerts = function() { $('#alert-status').addClass('hide'); $('#alert-success').addClass('hide'); $('#alert-error').addClass('hide'); $('#upload-progress-box').addClass('hide'); } - function status(message) { - hideAlerts(); + AccountEdit.status = function(message) { + AccountEdit.hideAlerts(); $('#alert-status').text(message).removeClass('hide'); } - function success(message) { - hideAlerts(); + AccountEdit.success = function(message) { + AccountEdit.hideAlerts(); $('#alert-success').text(message).removeClass('hide'); } - function error(message) { - hideAlerts(); + AccountEdit.error = function(message) { + AccountEdit.hideAlerts(); $('#alert-error').text(message).removeClass('hide'); } - function changeUserPicture(type) { + AccountEdit.changeUserPicture = function(type) { var userData = { type: type }; @@ -94,40 +264,10 @@ $(document).ready(function() { }); } - var selectedImageType = ''; - - $('#submitBtn').on('click', function() { - - var userData = { - uid: $('#inputUID').val(), - email: $('#inputEmail').val(), - fullname: $('#inputFullname').val(), - website: $('#inputWebsite').val(), - birthday: $('#inputBirthday').val(), - location: $('#inputLocation').val(), - signature: $('#inputSignature').val() - }; - - socket.emit('api:user.updateProfile', userData, function(err, data) { - if (data.success) { - app.alertSuccess('Your profile has been updated successfully!'); - if (data.picture) { - $('#user-current-picture').attr('src', data.picture); - $('#user_label img').attr('src', data.picture); - } - if (data.gravatarpicture) { - $('#user-gravatar-picture').attr('src', data.gravatarpicture); - gravatarPicture = data.gravatarpicture; - } - } else { - app.alertError('There was an error updating your profile! ' + err.error); - } - }); - return false; - }); - - function updateImages() { + AccountEdit.updateImages = function() { var currentPicture = $('#user-current-picture').attr('src'); + var gravatarPicture = templates.get('gravatarpicture'); + var uploadedPicture = templates.get('uploadedpicture'); if (gravatarPicture) { $('#user-gravatar-picture').attr('src', gravatarPicture); @@ -153,139 +293,5 @@ $(document).ready(function() { $('#uploaded-box .icon-ok').hide(); } - - $('#changePictureBtn').on('click', function() { - selectedImageType = ''; - updateImages(); - - $('#change-picture-modal').modal('show'); - $('#change-picture-modal').removeClass('hide'); - - return false; - }); - - $('#gravatar-box').on('click', function() { - $('#gravatar-box .icon-ok').show(); - $('#uploaded-box .icon-ok').hide(); - selectedImageType = 'gravatar'; - }); - - $('#uploaded-box').on('click', function() { - $('#gravatar-box .icon-ok').hide(); - $('#uploaded-box .icon-ok').show(); - selectedImageType = 'uploaded'; - }); - - $('#savePictureChangesBtn').on('click', function() { - $('#change-picture-modal').modal('hide'); - - if (selectedImageType) { - changeUserPicture(selectedImageType); - - if (selectedImageType == 'gravatar') - $('#user-current-picture').attr('src', gravatarPicture); - else if (selectedImageType == 'uploaded') - $('#user-current-picture').attr('src', uploadedPicture); - } - - }); - - $('#upload-picture-modal').on('hide', function() { - $('#userPhotoInput').val(''); - }); - - $('#uploadPictureBtn').on('click', function() { - - $('#change-picture-modal').modal('hide'); - $('#upload-picture-modal').modal('show'); - $('#upload-picture-modal').removeClass('hide'); - - hideAlerts(); - - return false; - }); - - $('#pictureUploadSubmitBtn').on('click', function() { - $('#uploadForm').submit(); - }); - - (function handlePasswordChange() { - var currentPassword = $('#inputCurrentPassword'); - var password_notify = $('#password-notify'); - var password_confirm_notify = $('#password-confirm-notify'); - var password = $('#inputNewPassword'); - var password_confirm = $('#inputNewPasswordAgain'); - var passwordvalid = false; - var passwordsmatch = false; - - - function onPasswordChanged() { - passwordvalid = utils.isPasswordValid(password.val()); - if (password.val().length < config.minimumPasswordLength) { - password_notify.html('Password too short'); - password_notify.attr('class', 'alert alert-danger'); - password_notify.removeClass('hide'); - } else if (!passwordvalid) { - password_notify.html('Invalid password'); - password_notify.attr('class', 'alert alert-danger'); - password_notify.removeClass('hide'); - } else { - password_notify.html('OK!'); - password_notify.attr('class', 'alert alert-success'); - password_notify.removeClass('hide'); - } - - onPasswordConfirmChanged(); - } - - function onPasswordConfirmChanged() { - if (password_notify.hasClass('alert-danger') || !password_confirm.val()) { - password_confirm_notify.addClass('hide'); - return; - } - if (password.val() !== password_confirm.val()) { - password_confirm_notify.html('Passwords must match!'); - password_confirm_notify.attr('class', 'alert alert-danger'); - password_confirm_notify.removeClass('hide'); - passwordsmatch = false; - } else { - password_confirm_notify.html('OK!'); - password_confirm_notify.attr('class', 'alert alert-success'); - password_confirm_notify.removeClass('hide'); - passwordsmatch = true; - } - } - - password.on('blur', onPasswordChanged); - password_confirm.on('blur', onPasswordConfirmChanged); - - $('#changePasswordBtn').on('click', function() { - - if (passwordvalid && passwordsmatch && currentPassword.val()) { - socket.emit('api:user.changePassword', { - 'currentPassword': currentPassword.val(), - 'newPassword': password.val() - }, function(err) { - - currentPassword.val(''); - password.val(''); - password_confirm.val(''); - password_notify.addClass('hide'); - password_confirm_notify.addClass('hide'); - passwordsmatch = false; - passwordvalid = false; - - if (err) { - app.alertError(err.error); - return; - } - - app.alertSuccess('Your password is updated!'); - - }); - } - return false; - }); - - }()); + return AccountEdit; }); \ No newline at end of file diff --git a/public/src/forum/accountheader.js b/public/src/forum/accountheader.js index 15bb2db614..d865a83b1b 100644 --- a/public/src/forum/accountheader.js +++ b/public/src/forum/accountheader.js @@ -1,24 +1,11 @@ -(function() { - var yourid = templates.get('yourid'), - theirid = templates.get('theirid'); +define(function() { + var AccountHeader = {}; + AccountHeader.init = function() { + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'); - function createMenu() { - var userslug = $('.account-username-box').attr('data-userslug'); - var links = $('
'); - - $('.account-username-box').append(links); - } - - $(document).ready(function() { - - createMenu(); + AccountHeader.createMenu(); var editLink = $('#editLink'); var settingsLink = $('#settingsLink'); @@ -37,6 +24,20 @@ return false; } }); - }); + } + + AccountHeader.createMenu = function() { + var userslug = $('.account-username-box').attr('data-userslug'); + var links = $(''); + + $('.account-username-box').append(links); + } -}()); \ No newline at end of file + return AccountHeader; +}); \ No newline at end of file diff --git a/public/src/forum/accountsettings.js b/public/src/forum/accountsettings.js index d121ecbe85..6f6d6edcbb 100644 --- a/public/src/forum/accountsettings.js +++ b/public/src/forum/accountsettings.js @@ -1,19 +1,25 @@ -$(document).ready(function() { +define(['forum/accountheader'], function(header) { + var AccountSettings = {}; - $('#submitBtn').on('click', function() { + AccountSettings.init = function() { + header.init(); - var settings = { - showemail: $('#showemailCheckBox').is(':checked') ? 1 : 0 - }; + $('#submitBtn').on('click', function() { - socket.emit('api:user.saveSettings', settings, function(success) { - if (success) { - app.alertSuccess('Settings saved!'); - } else { - app.alertError('There was an error saving settings!'); - } + var settings = { + showemail: $('#showemailCheckBox').is(':checked') ? 1 : 0 + }; + + socket.emit('api:user.saveSettings', settings, function(success) { + if (success) { + app.alertSuccess('Settings saved!'); + } else { + app.alertError('There was an error saving settings!'); + } + }); + return false; }); - return false; - }); + }; -}); \ No newline at end of file + return AccountSettings; +}); diff --git a/public/src/forum/admin/categories.js b/public/src/forum/admin/categories.js index cb7ddbd39e..4568e7ceae 100644 --- a/public/src/forum/admin/categories.js +++ b/public/src/forum/admin/categories.js @@ -1,139 +1,144 @@ -var modified_categories = {}; +define(function() { + var Categories = {}; -function modified(el) { - var cid = $(el).parents('li').attr('data-cid'); + Categories.init = function() { + var modified_categories = {}; - modified_categories[cid] = modified_categories[cid] || {}; - modified_categories[cid][$(el).attr('data-name')] = $(el).val(); -} + function modified(el) { + var cid = $(el).parents('li').attr('data-cid'); -function save() { - socket.emit('api:admin.categories.update', modified_categories); - modified_categories = {}; -} - -function select_icon(el) { - var selected = el.attr('class').replace(' icon-2x', ''); - jQuery('#icons .selected').removeClass('selected'); - if (selected) - jQuery('#icons .' + selected).parent().addClass('selected'); - - - bootbox.confirm('There seems to be a group with this name already.
'; - break; - case 'name-too-short': - errorText = 'Please specify a grou nameA group name is required for administrative purposes.
'; - break; - default: - errorText = 'Uh-OhThere was a problem creating your group. Please try again later!
'; - break; +define(function() { + var Groups = {}; + + Groups.init = function() { + var createEl = document.getElementById('create'), + createModal = $('#create-modal'), + createSubmitBtn = document.getElementById('create-modal-go'), + createNameEl = $('#create-group-name'), + detailsModal = $('#group-details-modal'), + detailsSearch = detailsModal.find('#group-details-search'), + searchResults = detailsModal.find('#group-details-search-results'), + groupMembersEl = detailsModal.find('ul.current_members'), + detailsModalSave = detailsModal.find('.btn-primary'), + searchDelay = undefined, + listEl = $('#groups-list'); + + createEl.addEventListener('click', function() { + createModal.modal('show'); + setTimeout(function() { + createNameEl.focus(); + }, 250); + }, false); + + createSubmitBtn.addEventListener('click', function() { + var submitObj = { + name: createNameEl.val(), + description: $('#create-group-desc').val() + }, + errorEl = $('#create-modal-error'), + errorText; + + socket.emit('api:groups.create', submitObj, function(err, data) { + if (err) { + switch (err) { + case 'group-exists': + errorText = 'Please choose another nameThere seems to be a group with this name already.
'; + break; + case 'name-too-short': + errorText = 'Please specify a grou nameA group name is required for administrative purposes.
'; + break; + default: + errorText = 'Uh-OhThere was a problem creating your group. Please try again later!
'; + break; + } + + errorEl.html(errorText).removeClass('hide'); + } else { + createModal.modal('hide'); + errorEl.addClass('hide'); + createNameEl.val(''); + ajaxify.go('admin/groups'); } + }); + }); - errorEl.html(errorText).removeClass('hide'); - } else { - createModal.modal('hide'); - errorEl.addClass('hide'); - createNameEl.val(''); - ajaxify.go('admin/groups'); + listEl.on('click', 'button[data-action]', function() { + var action = this.getAttribute('data-action'), + gid = $(this).parents('li[data-gid]').attr('data-gid'); + + switch (action) { + case 'delete': + bootbox.confirm('Are you sure you wish to delete this group?', function(confirm) { + if (confirm) { + socket.emit('api:groups.delete', gid, function(err, data) { + if (data === 'OK') ajaxify.go('admin/groups'); + }); + } + }); + break; + case 'members': + socket.emit('api:groups.get', gid, function(err, groupObj) { + var formEl = detailsModal.find('form'), + nameEl = formEl.find('#change-group-name'), + descEl = formEl.find('#change-group-desc'), + memberIcon = document.createElement('li'), + numMembers = groupObj.members.length, + membersFrag = document.createDocumentFragment(), + memberIconImg, x; + + + nameEl.val(groupObj.name); + descEl.val(groupObj.description); + + // Member list + memberIcon.innerHTML = '' + + themes[x].description + + (themes[x].url ? ' (Homepage)' : '') + + '
' + + '' + - themes[x].description + - (themes[x].url ? ' (Homepage)' : '') + - '
' + - '' + + posts[i].content + + '
' + + ' ' + + ''; + + frag.appendChild(li.cloneNode(true)); + recent_replies.appendChild(frag); + } + $('#category_recent_replies span.timeago').timeago(); }); - } - ajaxify.register_events([ - 'event:new_topic' - ]); + $(window).off('scroll').on('scroll', function (ev) { + var bottom = ($(document).height() - $(window).height()) * 0.9; - function onNewTopic(data) { + if ($(window).scrollTop() > bottom && !loadingMoreTopics) { + Category.loadMoreTopics(cid); + } + }); + }; + + Category.onNewTopic = function(data) { var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: [data] }), @@ -64,40 +109,7 @@ $('#topics-container span.timeago').timeago(); } - socket.on('event:new_topic', onNewTopic); - - socket.emit('api:categories.getRecentReplies', cid); - socket.on('api:categories.getRecentReplies', function (posts) { - if (!posts || posts.length === 0) { - return; - } - - var recent_replies = document.getElementById('category_recent_replies'); - - recent_replies.innerHTML = ''; - - var frag = document.createDocumentFragment(), - li = document.createElement('li'); - for (var i = 0, numPosts = posts.length; i < numPosts; i++) { - - li.setAttribute('data-pid', posts[i].pid); - - - li.innerHTML = '' + - posts[i].content + - '
' + - ' ' + - ''; - - frag.appendChild(li.cloneNode(true)); - recent_replies.appendChild(frag); - } - $('#category_recent_replies span.timeago').timeago(); - }); - - function onTopicsLoaded(topics) { + Category.onTopicsLoaded = function(topics) { var html = templates.prepare(templates['category'].blocks['topics']).parse({ topics: topics @@ -113,26 +125,18 @@ } - function loadMoreTopics(cid) { + Category.loadMoreTopics = function(cid) { loadingMoreTopics = true; socket.emit('api:category.loadMore', { cid: cid, after: $('#topics-container').children().length }, function (data) { if (data.topics.length) { - onTopicsLoaded(data.topics); + Category.onTopicsLoaded(data.topics); } loadingMoreTopics = false; }); } - $(window).off('scroll').on('scroll', function (ev) { - var bottom = ($(document).height() - $(window).height()) * 0.9; - - if ($(window).scrollTop() > bottom && !loadingMoreTopics) { - loadMoreTopics(cid); - } - }); - - -})(); \ No newline at end of file + return Category; +}); \ No newline at end of file diff --git a/public/src/forum/favourites.js b/public/src/forum/favourites.js index cff01ab271..20b77b4d41 100644 --- a/public/src/forum/favourites.js +++ b/public/src/forum/favourites.js @@ -1,7 +1,13 @@ -(function() { - $(document).ready(function() { +define(['forum/accountheader'], function(header) { + var AccountHeader = {}; + + AccountHeader.init = function() { + header.init(); + $('.user-favourite-posts .topic-row').on('click', function() { ajaxify.go($(this).attr('topic-url')); }); - }); -}()); \ No newline at end of file + }; + + return AccountHeader; +}); \ No newline at end of file diff --git a/public/src/forum/followers.js b/public/src/forum/followers.js index 0fcb464f0b..ee955ff615 100644 --- a/public/src/forum/followers.js +++ b/public/src/forum/followers.js @@ -1,18 +1,20 @@ -(function() { +define(['forum/accountheader'], function(header) { + var Followers = {}; - var yourid = templates.get('yourid'), - theirid = templates.get('theirid'), - followersCount = templates.get('followersCount'); + Followers.init = function() { + header.init(); - $(document).ready(function() { + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'), + followersCount = templates.get('followersCount'); - if (parseInt(followersCount, 10) === 0) { - $('#no-followers-notice').removeClass('hide'); - } - app.addCommasToNumbers(); - - }); + if (parseInt(followersCount, 10) === 0) { + $('#no-followers-notice').removeClass('hide'); + } + app.addCommasToNumbers(); + }; -}()); \ No newline at end of file + return Followers; +}); \ No newline at end of file diff --git a/public/src/forum/following.js b/public/src/forum/following.js index cd31d25466..7e1c2cde4c 100644 --- a/public/src/forum/following.js +++ b/public/src/forum/following.js @@ -1,10 +1,12 @@ -(function() { +define(['forum/accountheader'], function(header) { + var Following = {}; - var yourid = templates.get('yourid'), - theirid = templates.get('theirid'), - followingCount = templates.get('followingCount'); + Following.init = function() { + header.init(); - $(document).ready(function() { + var yourid = templates.get('yourid'), + theirid = templates.get('theirid'), + followingCount = templates.get('followingCount'); if (parseInt(followingCount, 10) === 0) { $('#no-following-notice').removeClass('hide'); @@ -34,7 +36,7 @@ } app.addCommasToNumbers(); - }); + }; - -}()); \ No newline at end of file + return Following; +}); \ No newline at end of file diff --git a/public/src/forum/login.js b/public/src/forum/login.js index b99465668a..ed4e0753f6 100644 --- a/public/src/forum/login.js +++ b/public/src/forum/login.js @@ -1,60 +1,66 @@ -(function() { - // Alternate Logins - var altLoginEl = document.querySelector('.alt-logins'); - altLoginEl.addEventListener('click', function(e) { - var target; - switch (e.target.nodeName) { - case 'LI': - target = e.target; - break; - case 'I': - target = e.target.parentNode; - break; - } - if (target) { - document.location.href = target.getAttribute('data-url'); - } - }); - - $('#login').on('click', function() { - var loginData = { - 'username': $('#username').val(), - 'password': $('#password').val(), - '_csrf': $('#csrf-token').val() - }; - - $.ajax({ - type: "POST", - url: RELATIVE_PATH + '/login', - data: loginData, - success: function(data, textStatus, jqXHR) { - if (!data.success) { +define(function() { + var Login = {}; + + Login.init = function() { + // Alternate Logins + var altLoginEl = document.querySelector('.alt-logins'); + altLoginEl.addEventListener('click', function(e) { + var target; + switch (e.target.nodeName) { + case 'LI': + target = e.target; + break; + case 'I': + target = e.target.parentNode; + break; + } + if (target) { + document.location.href = target.getAttribute('data-url'); + } + }); + + $('#login').on('click', function() { + var loginData = { + 'username': $('#username').val(), + 'password': $('#password').val(), + '_csrf': $('#csrf-token').val() + }; + + $.ajax({ + type: "POST", + url: RELATIVE_PATH + '/login', + data: loginData, + success: function(data, textStatus, jqXHR) { + if (!data.success) { + $('#login-error-notify').show(); + } else { + $('#login-error-notify').hide(); + if(app.previousUrl.indexOf('/reset/') != -1) + window.location.replace(RELATIVE_PATH + "/?loggedin"); + else + window.location.replace(app.previousUrl + "?loggedin"); + + app.loadConfig(); + } + }, + error: function(data, textStatus, jqXHR) { $('#login-error-notify').show(); - } else { - $('#login-error-notify').hide(); - if(app.previousUrl.indexOf('/reset/') != -1) - window.location.replace(RELATIVE_PATH + "/?loggedin"); - else - window.location.replace(app.previousUrl + "?loggedin"); - - app.loadConfig(); - } - }, - error: function(data, textStatus, jqXHR) { - $('#login-error-notify').show(); - }, - dataType: 'json', - async: true, - timeout: 2000 + }, + dataType: 'json', + async: true, + timeout: 2000 + }); + + return false; }); - return false; - }); + $('#login-error-notify button').on('click', function() { + $('#login-error-notify').hide(); + return false; + }); - $('#login-error-notify button').on('click', function() { - $('#login-error-notify').hide(); - return false; - }); + document.querySelector('#content input').focus(); + }; - document.querySelector('#content input').focus(); -}()); \ No newline at end of file + return Login; +}); \ No newline at end of file diff --git a/public/src/forum/recent.js b/public/src/forum/recent.js index f9b1fe228c..5fed07107e 100644 --- a/public/src/forum/recent.js +++ b/public/src/forum/recent.js @@ -1,40 +1,56 @@ -(function() { - var loadingMoreTopics = false; +define(function() { + var Recent = {}; - app.enter_room('recent_posts'); + Recent.newTopicCount = 0; + Recent.newPostCount = 0; + Recent.loadingMoreTopics = false; - ajaxify.register_events([ - 'event:new_topic', - 'event:new_post' - ]); + Recent.init = function() { + app.enter_room('recent_posts'); - var newTopicCount = 0, - newPostCount = 0; + ajaxify.register_events([ + 'event:new_topic', + 'event:new_post' + ]); - $('#new-topics-alert').on('click', function() { - $(this).hide(); - }); + $('#new-topics-alert').on('click', function() { + $(this).hide(); + }); + + socket.on('event:new_topic', function(data) { + + ++Recent.newTopicCount; + Recent.updateAlertText(); - socket.on('event:new_topic', function(data) { + }); + + socket.on('event:new_post', function(data) { + ++Recent.newPostCount; + Recent.updateAlertText(); + }); - ++newTopicCount; - updateAlertText(); + $(window).off('scroll').on('scroll', function() { + var bottom = ($(document).height() - $(window).height()) * 0.9; - }); + if ($(window).scrollTop() > bottom && !Recent.loadingMoreTopics) { + Recent.loadMoreTopics(); + } + }); + }; - function updateAlertText() { + Recent.updateAlertText = function() { var text = ''; - if (newTopicCount > 1) - text = 'There are ' + newTopicCount + ' new topics'; - else if (newTopicCount === 1) + if (Recent.newTopicCount > 1) + text = 'There are ' + Recent.newTopicCount + ' new topics'; + else if (Recent.newTopicCount === 1) text = 'There is 1 new topic'; else text = 'There are no new topics'; - if (newPostCount > 1) - text += ' and ' + newPostCount + ' new posts.'; - else if (newPostCount === 1) + if (Recent.newPostCount > 1) + text += ' and ' + Recent.newPostCount + ' new posts.'; + else if (Recent.newPostCount === 1) text += ' and 1 new post.'; else text += ' and no new posts.'; @@ -44,12 +60,7 @@ $('#new-topics-alert').html(text).fadeIn('slow'); } - socket.on('event:new_post', function(data) { - ++newPostCount; - updateAlertText(); - }); - - function onTopicsLoaded(topics) { + Recent.onTopicsLoaded = function(topics) { var html = templates.prepare(templates['recent'].blocks['topics']).parse({ topics: topics @@ -61,25 +72,17 @@ container.append(html); } - function loadMoreTopics() { - loadingMoreTopics = true; + Recent.loadMoreTopics = function() { + Recent.loadingMoreTopics = true; socket.emit('api:topics.loadMoreRecentTopics', { after: $('#topics-container').children().length }, function(data) { if (data.topics && data.topics.length) { - onTopicsLoaded(data.topics); + Recent.onTopicsLoaded(data.topics); } - loadingMoreTopics = false; + Recent.loadingMoreTopics = false; }); } - $(window).off('scroll').on('scroll', function() { - var bottom = ($(document).height() - $(window).height()) * 0.9; - - if ($(window).scrollTop() > bottom && !loadingMoreTopics) { - loadMoreTopics(); - } - }); - - -})(); \ No newline at end of file + return Recent; +}); diff --git a/public/src/forum/register.js b/public/src/forum/register.js index e485f4960f..fc7cf26b2a 100644 --- a/public/src/forum/register.js +++ b/public/src/forum/register.js @@ -1,154 +1,159 @@ -(function() { - var username = $('#username'), - password = $('#password'), - password_confirm = $('#password-confirm'), - register = $('#register'), - emailEl = $('#email'), - username_notify = $('#username-notify'), - email_notify = $('#email-notify'), - password_notify = $('#password-notify'), - password_confirm_notify = $('#password-confirm-notify'), - validationError = false, - successIcon = ''; - - $('#referrer').val(app.previousUrl); - - function showError(element, msg) { - element.html(msg); - element.parent() - .removeClass('alert-success') - .addClass('alert-danger'); - element.show(); - validationError = true; - } - - function showSuccess(element, msg) { - element.html(msg); - element.parent() - .removeClass('alert-danger') - .addClass('alert-success'); - element.show(); - } - - function validateEmail() { - if (!emailEl.val()) { +define(function() { + var Register = {}; + + Register.init = function() { + var username = $('#username'), + password = $('#password'), + password_confirm = $('#password-confirm'), + register = $('#register'), + emailEl = $('#email'), + username_notify = $('#username-notify'), + email_notify = $('#email-notify'), + password_notify = $('#password-notify'), + password_confirm_notify = $('#password-confirm-notify'), + validationError = false, + successIcon = ''; + + $('#referrer').val(app.previousUrl); + + function showError(element, msg) { + element.html(msg); + element.parent() + .removeClass('alert-success') + .addClass('alert-danger'); + element.show(); validationError = true; - return; } - if (!utils.isEmailValid(emailEl.val())) { - showError(email_notify, 'Invalid email address.'); - } else - socket.emit('user.email.exists', { - email: emailEl.val() - }); - } - - emailEl.on('blur', function() { - validateEmail(); - }); - - function validateUsername() { - if (!username.val()) { - validationError = true; - return; - } - - if (username.val().length < config.minimumUsernameLength) { - showError(username_notify, 'Username too short!'); - } else if (username.val().length > config.maximumUsernameLength) { - showError(username_notify, 'Username too long!'); - } else if (!utils.isUserNameValid(username.val())) { - showError(username_notify, 'Invalid username!'); - } else { - socket.emit('user.exists', { - username: username.val() - }); - } - } - - username.on('keyup', function() { - jQuery('#yourUsername').html(this.value.length > 0 ? this.value : 'username'); - }); - username.on('blur', function() { - validateUsername(); - }); - - function validatePassword() { - if (!password.val()) { - validationError = true; - return; - } - - if (password.val().length < config.minimumPasswordLength) { - showError(password_notify, 'Password too short!'); - } else if (!utils.isPasswordValid(password.val())) { - showError(password_notify, 'Invalid password!'); - } else { - showSuccess(password_notify, successIcon); + function showSuccess(element, msg) { + element.html(msg); + element.parent() + .removeClass('alert-danger') + .addClass('alert-success'); + element.show(); } - if (password.val() !== password_confirm.val() && password_confirm.val() !== '') { - showError(password_confirm_notify, 'Passwords must match!'); + function validateEmail() { + if (!emailEl.val()) { + validationError = true; + return; + } + + if (!utils.isEmailValid(emailEl.val())) { + showError(email_notify, 'Invalid email address.'); + } else + socket.emit('user.email.exists', { + email: emailEl.val() + }); } - } - - $(password).on('blur', function() { - validatePassword(); - }); - function validatePasswordConfirm() { - if (!password.val() || password_notify.hasClass('alert-error')) { - return; + emailEl.on('blur', function() { + validateEmail(); + }); + + function validateUsername() { + if (!username.val()) { + validationError = true; + return; + } + + if (username.val().length < config.minimumUsernameLength) { + showError(username_notify, 'Username too short!'); + } else if (username.val().length > config.maximumUsernameLength) { + showError(username_notify, 'Username too long!'); + } else if (!utils.isUserNameValid(username.val())) { + showError(username_notify, 'Invalid username!'); + } else { + socket.emit('user.exists', { + username: username.val() + }); + } } - if (password.val() !== password_confirm.val()) { - showError(password_confirm_notify, 'Passwords must match!'); - } else { - showSuccess(password_confirm_notify, successIcon); + username.on('keyup', function() { + jQuery('#yourUsername').html(this.value.length > 0 ? this.value : 'username'); + }); + username.on('blur', function() { + validateUsername(); + }); + + function validatePassword() { + if (!password.val()) { + validationError = true; + return; + } + + if (password.val().length < config.minimumPasswordLength) { + showError(password_notify, 'Password too short!'); + } else if (!utils.isPasswordValid(password.val())) { + showError(password_notify, 'Invalid password!'); + } else { + showSuccess(password_notify, successIcon); + } + + if (password.val() !== password_confirm.val() && password_confirm.val() !== '') { + showError(password_confirm_notify, 'Passwords must match!'); + } } - } - $(password_confirm).on('blur', function() { - validatePasswordConfirm(); - }); + $(password).on('blur', function() { + validatePassword(); + }); - ajaxify.register_events(['user.exists', 'user.email.exists']); + function validatePasswordConfirm() { + if (!password.val() || password_notify.hasClass('alert-error')) { + return; + } - socket.on('user.exists', function(data) { - if (data.exists === true) { - showError(username_notify, 'Username already taken!'); - } else { - showSuccess(username_notify, successIcon); + if (password.val() !== password_confirm.val()) { + showError(password_confirm_notify, 'Passwords must match!'); + } else { + showSuccess(password_confirm_notify, successIcon); + } } - }); - socket.on('user.email.exists', function(data) { - if (data.exists === true) { - showError(email_notify, 'Email address already taken!'); - } else { - showSuccess(email_notify, successIcon); + $(password_confirm).on('blur', function() { + validatePasswordConfirm(); + }); + + ajaxify.register_events(['user.exists', 'user.email.exists']); + + socket.on('user.exists', function(data) { + if (data.exists === true) { + showError(username_notify, 'Username already taken!'); + } else { + showSuccess(username_notify, successIcon); + } + }); + + socket.on('user.email.exists', function(data) { + if (data.exists === true) { + showError(email_notify, 'Email address already taken!'); + } else { + showSuccess(email_notify, successIcon); + } + }); + + // Alternate Logins + $('.alt-logins li').on('click', function(e) { + document.location.href = $(this).attr('data-url'); + }); + + function validateForm() { + validationError = false; + + validateEmail(); + validateUsername(); + validatePassword(); + validatePasswordConfirm(); + + return validationError; } - }); - - // Alternate Logins - $('.alt-logins li').on('click', function(e) { - document.location.href = $(this).attr('data-url'); - }); - - function validateForm() { - validationError = false; - - validateEmail(); - validateUsername(); - validatePassword(); - validatePasswordConfirm(); - - return validationError; - } - register.on('click', function(e) { - if (validateForm()) e.preventDefault(); - }); + register.on('click', function(e) { + if (validateForm()) e.preventDefault(); + }); + }; -}()); \ No newline at end of file + return Register; +}); \ No newline at end of file diff --git a/public/src/forum/reset.js b/public/src/forum/reset.js index 366d382356..150b282fe0 100644 --- a/public/src/forum/reset.js +++ b/public/src/forum/reset.js @@ -1,41 +1,47 @@ -(function() { - var inputEl = document.getElementById('email'), - errorEl = document.getElementById('error'), - errorTextEl = errorEl.querySelector('p'); +define(function() { + var ResetPassword = {}; - document.getElementById('reset').onclick = function() { - if (inputEl.value.length > 0 && inputEl.value.indexOf('@') !== -1) { - socket.emit('user:reset.send', { - email: inputEl.value - }); - } else { - jQuery('#success').hide(); - jQuery(errorEl).show(); - errorTextEl.innerHTML = 'Please enter a valid email'; - } - }; + ResetPassword.init = function() { + var inputEl = document.getElementById('email'), + errorEl = document.getElementById('error'), + errorTextEl = errorEl.querySelector('p'); + + document.getElementById('reset').onclick = function() { + if (inputEl.value.length > 0 && inputEl.value.indexOf('@') !== -1) { + socket.emit('user:reset.send', { + email: inputEl.value + }); + } else { + jQuery('#success').hide(); + jQuery(errorEl).show(); + errorTextEl.innerHTML = 'Please enter a valid email'; + } + }; - ajaxify.register_events(['user.send_reset']); + ajaxify.register_events(['user.send_reset']); - socket.on('user.send_reset', function(data) { - var submitEl = document.getElementById('reset'); + socket.on('user.send_reset', function(data) { + var submitEl = document.getElementById('reset'); - if (data.status === 'ok') { - jQuery('#error').hide(); - jQuery('#success').show(); - jQuery('#success p').html('An email has been dispatched to "' + data.email + '" with instructions on setting a new password.'); - inputEl.value = ''; - } else { - jQuery('#success').hide(); - jQuery(errorEl).show(); - switch (data.message) { - case 'invalid-email': - errorTextEl.innerHTML = 'The email you put in (' + data.email + ') is not registered with us. Please try again.'; - break; - case 'send-failed': - errorTextEl.innerHTML = 'There was a problem sending the reset code. Please try again later.'; - break; + if (data.status === 'ok') { + jQuery('#error').hide(); + jQuery('#success').show(); + jQuery('#success p').html('An email has been dispatched to "' + data.email + '" with instructions on setting a new password.'); + inputEl.value = ''; + } else { + jQuery('#success').hide(); + jQuery(errorEl).show(); + switch (data.message) { + case 'invalid-email': + errorTextEl.innerHTML = 'The email you put in (' + data.email + ') is not registered with us. Please try again.'; + break; + case 'send-failed': + errorTextEl.innerHTML = 'There was a problem sending the reset code. Please try again later.'; + break; + } } - } - }); -}()); \ No newline at end of file + }); + }; + + return ResetPassword; +}); \ No newline at end of file diff --git a/public/src/forum/reset_code.js b/public/src/forum/reset_code.js index 527b0c7bf6..f36ff00836 100644 --- a/public/src/forum/reset_code.js +++ b/public/src/forum/reset_code.js @@ -1,52 +1,58 @@ -(function() { - var reset_code = templates.get('reset_code'); - - var resetEl = document.getElementById('reset'), - password = document.getElementById('password'), - repeat = document.getElementById('repeat'), - noticeEl = document.getElementById('notice'); - - resetEl.addEventListener('click', function() { - if (password.value.length < 6) { - $('#error').hide(); - noticeEl.querySelector('strong').innerHTML = 'Invalid Password'; - noticeEl.querySelector('p').innerHTML = 'The password entered is too short, please pick a different password.'; - noticeEl.style.display = 'block'; - } else if (password.value !== repeat.value) { - $('#error').hide(); - noticeEl.querySelector('strong').innerHTML = 'Invalid Password'; - noticeEl.querySelector('p').innerHTML = 'The two passwords you\'ve entered do not match.'; - noticeEl.style.display = 'block'; - } else { - socket.emit('user:reset.commit', { - code: reset_code, - password: password.value - }); - } - }, false); - - // Enable the form if the code is valid - socket.emit('user:reset.valid', { - code: reset_code - }); - - - ajaxify.register_events(['user:reset.valid', 'user:reset.commit']); - socket.on('user:reset.valid', function(data) { - if ( !! data.valid) resetEl.disabled = false; - else { - var formEl = document.getElementById('reset-form'); - // Show error message - $('#error').show(); - formEl.parentNode.removeChild(formEl); - } - }) - - socket.on('user:reset.commit', function(data) { - if (data.status === 'ok') { - $('#error').hide(); - $('#notice').hide(); - $('#success').show(); - } - }); -}()); \ No newline at end of file +define(function() { + var ResetCode = {}; + + ResetCode.init = function() { + var reset_code = templates.get('reset_code'); + + var resetEl = document.getElementById('reset'), + password = document.getElementById('password'), + repeat = document.getElementById('repeat'), + noticeEl = document.getElementById('notice'); + + resetEl.addEventListener('click', function() { + if (password.value.length < 6) { + $('#error').hide(); + noticeEl.querySelector('strong').innerHTML = 'Invalid Password'; + noticeEl.querySelector('p').innerHTML = 'The password entered is too short, please pick a different password.'; + noticeEl.style.display = 'block'; + } else if (password.value !== repeat.value) { + $('#error').hide(); + noticeEl.querySelector('strong').innerHTML = 'Invalid Password'; + noticeEl.querySelector('p').innerHTML = 'The two passwords you\'ve entered do not match.'; + noticeEl.style.display = 'block'; + } else { + socket.emit('user:reset.commit', { + code: reset_code, + password: password.value + }); + } + }, false); + + // Enable the form if the code is valid + socket.emit('user:reset.valid', { + code: reset_code + }); + + + ajaxify.register_events(['user:reset.valid', 'user:reset.commit']); + socket.on('user:reset.valid', function(data) { + if ( !! data.valid) resetEl.disabled = false; + else { + var formEl = document.getElementById('reset-form'); + // Show error message + $('#error').show(); + formEl.parentNode.removeChild(formEl); + } + }) + + socket.on('user:reset.commit', function(data) { + if (data.status === 'ok') { + $('#error').hide(); + $('#notice').hide(); + $('#success').show(); + } + }); + }; + + return ResetCode; +}); \ No newline at end of file diff --git a/public/src/forum/search.js b/public/src/forum/search.js index f7238ef8c3..619f2676e5 100644 --- a/public/src/forum/search.js +++ b/public/src/forum/search.js @@ -1,6 +1,7 @@ -(function() { +define(function() { + var Search = {}; - $(document).ready(function() { + Search.init = function() { var searchQuery = $('#topics-container').attr('data-search-query'); $('.search-result-text').each(function() { @@ -21,4 +22,5 @@ }); }); -})(); \ No newline at end of file + return Search; +}); \ No newline at end of file diff --git a/public/src/forum/topic.js b/public/src/forum/topic.js index 159fb970bc..7e10e1c762 100644 --- a/public/src/forum/topic.js +++ b/public/src/forum/topic.js @@ -1,716 +1,722 @@ -(function() { - var expose_tools = templates.get('expose_tools'), - tid = templates.get('topic_id'), - postListEl = document.getElementById('post-container'), - editBtns = document.querySelectorAll('#post-container .post-buttons .edit, #post-container .post-buttons .edit i'), - thread_state = { - locked: templates.get('locked'), - deleted: templates.get('deleted'), - pinned: templates.get('pinned') - }, - topic_name = templates.get('topic_name'); +define(function() { + var Topic = {}; + Topic.init = function() { + var expose_tools = templates.get('expose_tools'), + tid = templates.get('topic_id'), + postListEl = document.getElementById('post-container'), + editBtns = document.querySelectorAll('#post-container .post-buttons .edit, #post-container .post-buttons .edit i'), + thread_state = { + locked: templates.get('locked'), + deleted: templates.get('deleted'), + pinned: templates.get('pinned') + }, + topic_name = templates.get('topic_name'); - jQuery('document').ready(function() { - app.addCommasToNumbers(); + jQuery('document').ready(function() { - var room = 'topic_' + tid, - adminTools = document.getElementById('thread-tools'); + app.addCommasToNumbers(); - app.enter_room(room); + var room = 'topic_' + tid, + adminTools = document.getElementById('thread-tools'); - // Resetting thread state - if (thread_state.locked === '1') set_locked_state(true); - if (thread_state.deleted === '1') set_delete_state(true); - if (thread_state.pinned === '1') set_pinned_state(true); + app.enter_room(room); - if (expose_tools === '1') { - var moveThreadModal = $('#move_thread_modal'); + // Resetting thread state + if (thread_state.locked === '1') set_locked_state(true); + if (thread_state.deleted === '1') set_delete_state(true); + if (thread_state.pinned === '1') set_pinned_state(true); - adminTools.style.visibility = 'inherit'; + if (expose_tools === '1') { + var moveThreadModal = $('#move_thread_modal'); - // Add events to the thread tools - $('#delete_thread').on('click', function(e) { - if (thread_state.deleted !== '1') { - bootbox.confirm('Are you sure you want to delete this thread?', function(confirm) { - if (confirm) socket.emit('api:topic.delete', { + adminTools.style.visibility = 'inherit'; + + // Add events to the thread tools + $('#delete_thread').on('click', function(e) { + if (thread_state.deleted !== '1') { + bootbox.confirm('Are you sure you want to delete this thread?', function(confirm) { + if (confirm) socket.emit('api:topic.delete', { + tid: tid + }); + }); + } else { + bootbox.confirm('Are you sure you want to restore this thread?', function(confirm) { + if (confirm) socket.emit('api:topic.restore', { + tid: tid + }); + }); + } + return false; + }); + + $('#lock_thread').on('click', function(e) { + if (thread_state.locked !== '1') { + socket.emit('api:topic.lock', { tid: tid }); - }); - } else { - bootbox.confirm('Are you sure you want to restore this thread?', function(confirm) { - if (confirm) socket.emit('api:topic.restore', { + } else { + socket.emit('api:topic.unlock', { tid: tid }); - }); - } - return false; - }); - - $('#lock_thread').on('click', function(e) { - if (thread_state.locked !== '1') { - socket.emit('api:topic.lock', { - tid: tid - }); - } else { - socket.emit('api:topic.unlock', { - tid: tid - }); - } - return false; - }); + } + return false; + }); - $('#pin_thread').on('click', function(e) { - if (thread_state.pinned !== '1') { - socket.emit('api:topic.pin', { - tid: tid - }); - } else { - socket.emit('api:topic.unpin', { - tid: tid - }); - } - return false; - }); + $('#pin_thread').on('click', function(e) { + if (thread_state.pinned !== '1') { + socket.emit('api:topic.pin', { + tid: tid + }); + } else { + socket.emit('api:topic.unpin', { + tid: tid + }); + } + return false; + }); - $('#move_thread').on('click', function(e) { - moveThreadModal.modal('show'); - return false; - }); + $('#move_thread').on('click', function(e) { + moveThreadModal.modal('show'); + return false; + }); - moveThreadModal.on('shown.bs.modal', function() { - - var loadingEl = document.getElementById('categories-loading'); - if (loadingEl) { - socket.once('api:categories.get', function(data) { - // Render categories - var categoriesFrag = document.createDocumentFragment(), - categoryEl = document.createElement('li'), - numCategories = data.categories.length, - modalBody = moveThreadModal.find('.modal-body'), - categoriesEl = modalBody[0].getElementsByTagName('ul')[0], - confirmDiv = document.getElementById('move-confirm'), - confirmCat = confirmDiv.getElementsByTagName('span')[0], - commitEl = document.getElementById('move_thread_commit'), - cancelEl = document.getElementById('move_thread_cancel'), - x, info, targetCid, targetCatLabel; - - categoriesEl.className = 'category-list'; - for (x = 0; x < numCategories; x++) { - info = data.categories[x]; - categoryEl.className = info.blockclass; - categoryEl.innerHTML = ' ' + info.name; - categoryEl.setAttribute('data-cid', info.cid); - categoriesFrag.appendChild(categoryEl.cloneNode(true)); - } - categoriesEl.appendChild(categoriesFrag); - modalBody[0].removeChild(loadingEl); - - categoriesEl.addEventListener('click', function(e) { - if (e.target.nodeName === 'LI') { - confirmCat.innerHTML = e.target.innerHTML; - confirmDiv.style.display = 'block'; - targetCid = e.target.getAttribute('data-cid'); - targetCatLabel = e.target.innerHTML; - commitEl.disabled = false; - } - }, false); - - commitEl.addEventListener('click', function() { - if (!commitEl.disabled && targetCid) { - commitEl.disabled = true; - $(cancelEl).fadeOut(250); - $(moveThreadModal).find('.modal-header button').fadeOut(250); - commitEl.innerHTML = 'Moving '; - - socket.once('api:topic.move', function(data) { - moveThreadModal.modal('hide'); - if (data.status === 'ok') { - app.alert({ - 'alert_id': 'thread_move', - type: 'success', - title: 'Topic Successfully Moved', - message: 'This topic has been successfully moved to ' + targetCatLabel, - timeout: 5000 - }); - } else { - app.alert({ - 'alert_id': 'thread_move', - type: 'danger', - title: 'Unable to Move Topic', - message: 'This topic could not be moved to ' + targetCatLabel + '.