Files
NodeBB/public/src/admin/manage/categories.js

197 lines
4.9 KiB
JavaScript
Raw Normal View History

"use strict";
2015-10-08 15:29:00 -04:00
/*global define, socket, app, bootbox, templates, ajaxify, Sortable */
2015-07-17 18:19:51 -04:00
define('admin/manage/categories', ['vendor/jquery/serializeObject/jquery.ba-serializeobject.min'], function() {
var Categories = {}, newCategoryId = -1, sortables;
Categories.init = function() {
socket.emit('admin.categories.getAll', function(error, payload){
if(error){
return app.alertError(error.message);
}
2015-05-30 18:44:31 +03:00
Categories.render(payload);
2015-05-30 18:44:31 +03:00
});
2014-12-30 12:25:39 -05:00
2015-07-17 18:19:51 -04:00
$('button[data-action="create"]').on('click', Categories.throwCreateModal);
// Enable/Disable toggle events
$('.categories').on('click', 'button[data-action="toggle"]', function() {
var $this = $(this),
cid = $this.attr('data-cid'),
parentEl = $this.parents('li[data-cid="' + cid + '"]'),
disabled = parentEl.hasClass('disabled');
var children = parentEl.find('li[data-cid]').map(function() {
return $(this).attr('data-cid');
}).get();
Categories.toggle([cid].concat(children), !disabled);
2015-10-08 15:29:00 -04:00
return false;
});
2015-03-25 18:08:11 -04:00
};
2015-07-17 18:19:51 -04:00
Categories.throwCreateModal = function() {
socket.emit('admin.categories.getNames', {}, function(err, categories) {
2015-09-20 15:17:57 -04:00
if (err) {
return app.alertError(err.message);
}
2015-07-17 18:19:51 -04:00
templates.parse('admin/partials/categories/create', {
categories: categories
}, function(html) {
2015-09-20 15:17:57 -04:00
function submit() {
var formData = modal.find('form').serializeObject();
formData.description = '';
formData.icon = 'fa-comments';
Categories.create(formData);
2015-09-20 15:20:58 -04:00
modal.modal('hide');
2015-09-20 15:17:57 -04:00
return false;
}
2015-07-17 18:19:51 -04:00
var modal = bootbox.dialog({
title: 'Create a Category',
message: html,
buttons: {
save: {
label: 'Save',
className: 'btn-primary',
2015-09-20 15:20:58 -04:00
callback: submit
2015-07-17 18:19:51 -04:00
}
}
});
2015-09-20 15:17:57 -04:00
modal.find('form').on('submit', submit);
2015-07-17 18:19:51 -04:00
});
});
};
Categories.create = function(payload) {
socket.emit('admin.categories.create', payload, function(err, data) {
2015-09-20 15:17:57 -04:00
if (err) {
2015-07-17 18:19:51 -04:00
return app.alertError(err.message);
}
2015-07-17 18:19:51 -04:00
app.alert({
alert_id: 'category_created',
title: 'Created',
message: 'Category successfully created!',
type: 'success',
timeout: 2000
});
2015-07-17 18:19:51 -04:00
ajaxify.go('admin/manage/categories/' + data.cid);
2015-03-25 18:08:11 -04:00
});
};
2015-05-30 18:44:31 +03:00
Categories.render = function(categories){
var container = $('.categories');
2015-09-20 15:17:57 -04:00
if (!categories || !categories.length) {
$('<div></div>')
.addClass('alert alert-info text-center')
.text('You have no active categories.')
.appendTo(container);
} else {
sortables = {};
renderList(categories, container, 0);
}
2015-05-30 18:44:31 +03:00
};
Categories.toggle = function(cids, disabled) {
var payload = {};
cids.forEach(function(cid) {
payload[cid] = {
disabled: disabled ? 1 : 0
};
});
2015-10-08 15:29:00 -04:00
socket.emit('admin.categories.update', payload, function(err) {
if (err) {
return app.alertError(err.message);
}
2015-09-28 16:09:33 -04:00
ajaxify.refresh();
});
2015-09-20 15:17:57 -04:00
};
2015-09-20 15:17:57 -04:00
function itemDidAdd(e) {
newCategoryId = e.to.dataset.cid;
2015-05-31 10:49:03 +03:00
}
2015-09-28 16:09:33 -04:00
function itemDragDidEnd(e) {
var isCategoryUpdate = (newCategoryId != -1);
2015-09-28 16:09:33 -04:00
2015-05-31 10:49:03 +03:00
//Update needed?
if((e.newIndex != undefined && e.oldIndex != e.newIndex) || isCategoryUpdate){
var parentCategory = isCategoryUpdate ? sortables[newCategoryId] : sortables[e.from.dataset.cid],
modified = {}, i = 0, list = parentCategory.toArray(), len = list.length;
for(i; i < len; ++i) {
modified[list[i]] = {
order: (i + 1)
2015-09-20 15:17:57 -04:00
};
}
2015-09-20 15:17:57 -04:00
if (isCategoryUpdate){
modified[e.item.dataset.cid].parentCid = newCategoryId;
}
2015-09-20 15:17:57 -04:00
newCategoryId = -1;
socket.emit('admin.categories.update', modified);
}
2015-05-31 10:49:03 +03:00
}
/**
* Render categories - recursively
*
* @param categories {array} categories tree
* @param level {number} current sub-level of rendering
* @param container {object} parent jquery element for the list
* @param parentId {number} parent category identifier
*/
function renderList(categories, container, parentId){
2016-03-14 12:57:45 -04:00
// Translate category names if needed
var count = 0;
categories.forEach(function(category, idx, parent) {
translator.translate(category.name, function(translated) {
if (category.name !== translated) {
category.name = translated;
}
++count;
2016-03-14 12:57:45 -04:00
if (count === parent.length) {
continueRender();
}
2015-05-30 18:44:31 +03:00
});
});
2016-03-14 12:57:45 -04:00
function continueRender() {
templates.parse('admin/partials/categories/category-rows', {
cid: parentId,
categories: categories
}, function(html) {
container.append(html);
// Handle and children categories in this level have
for(var x=0,numCategories=categories.length;x<numCategories;x++) {
renderList(categories[x].children, $('li[data-cid="' + categories[x].cid + '"]'), categories[x].cid);
}
// Make list sortable
sortables[parentId] = Sortable.create($('ul[data-cid="' + parentId + '"]')[0], {
group: 'cross-categories',
animation: 150,
handle: '.icon',
dataIdAttr: 'data-cid',
ghostClass: "placeholder",
onAdd: itemDidAdd,
onEnd: itemDragDidEnd
});
});
}
}
2015-05-30 18:44:31 +03:00
return Categories;
});