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