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

179 lines
4.6 KiB
JavaScript
Raw Normal View History

"use strict";
/*global define, socket, app, bootbox, templates, ajaxify, RELATIVE_PATH*/
2015-01-01 15:35:22 -05:00
define('admin/manage/categories', function() {
2015-06-02 22:20:09 +03:00
var Categories = {}, newCategoryId = -1, sortables, itemTemplate;
Categories.init = function() {
2015-06-02 22:20:09 +03:00
$.get(RELATIVE_PATH + '/templates/admin/partials/categories/category-item.tpl', function(data){
itemTemplate = data;
socket.emit('admin.categories.getAll', function(error, payload){
if(error){
return app.alertError(error.message);
}
2015-05-30 18:44:31 +03:00
2015-06-02 22:20:09 +03:00
Categories.render(payload);
});
2015-05-30 18:44:31 +03:00
});
2014-12-30 12:25:39 -05:00
2015-03-25 18:08:11 -04:00
$('button[data-action="create"]').on('click', Categories.create);
};
2015-03-25 18:08:11 -04:00
Categories.create = function() {
bootbox.prompt('Category Name', function(name) {
if (!name) {
return;
}
2015-02-25 18:29:15 -05:00
2015-03-25 18:08:11 -04:00
socket.emit('admin.categories.create', {
name: name,
description: '',
icon: 'fa-comments'
}, function(err, data) {
if(err) {
return app.alertError(err.message);
}
app.alert({
alert_id: 'category_created',
title: 'Created',
message: 'Category successfully created!',
type: 'success',
timeout: 2000
});
2015-03-25 18:08:11 -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');
if(!categories || categories.length == 0){
$('<div></div>')
.addClass('alert alert-info text-center')
.text('You have no active categories.')
.appendTo(container);
}else{
2015-05-31 10:49:03 +03:00
sortables = {};
renderList(categories, 0, container, 0);
2015-05-30 18:44:31 +03:00
}
};
2015-05-31 10:49:03 +03:00
function itemDidAdd(e){
newCategoryId = e.to.dataset.cid;
}
function itemDragDidEnd(e){
var isCategoryUpdate = (newCategoryId != -1);
//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)
}
}
if(isCategoryUpdate){
modified[e.item.dataset.cid]['parentCid'] = newCategoryId;
}
newCategoryId = -1
socket.emit('admin.categories.update', modified);
}
}
/**
* 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, level, container, parentId){
var i = 0, len = categories.length, category, marginLeft = 48, listItem;
var list = $('<ul />', {'data-cid': parentId});
if(level > 0){
list.css('margin-left', marginLeft);
}
2015-05-30 18:44:31 +03:00
for(i; i < len; ++i){
category = categories[i];
2015-05-31 10:49:03 +03:00
listItem = $('<li></li>', {'data-cid': category.cid})
2015-05-30 18:44:31 +03:00
.append(renderListItem(category))
.appendTo(list);
if(category.disabled){
listItem.addClass('disabled');
}
if(category.children.length > 0){
2015-05-31 10:49:03 +03:00
renderList(category.children, level + 1, listItem, category.cid);
2015-05-30 18:44:31 +03:00
}
}
2015-05-31 10:49:03 +03:00
list.appendTo(container);
sortables[parentId] = Sortable.create(list[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
}
function renderListItem(categoryEntity){
var listItem = $(templates.parse(
2015-06-02 22:20:09 +03:00
itemTemplate,
2015-05-30 18:44:31 +03:00
categoryEntity
));
var icon = listItem.find('.icon'),
button = listItem.find('[data-action="toggle"]');
if(categoryEntity.backgroundImage){
icon.css('background-image', 'url(' + categoryEntity.backgroundImage + ')');
}
icon
.css('color', categoryEntity.color)
.css('background-color', categoryEntity.bgColor);
if(categoryEntity.disabled){
button.text('Enable').addClass('btn-success');
}else{
button.text('Disable').addClass('btn-danger');
}
// Category enable/disable
button.on('click', function(e) {
var payload = {};
payload[categoryEntity.cid] = {
disabled: !categoryEntity.disabled | 0
};
socket.emit('admin.categories.update', payload, function(err, result) {
if (err) {
return app.alertError(err.message);
} else {
ajaxify.refresh();
}
});
});
return listItem;
}
return Categories;
});