2014-10-08 15:36:47 -04:00
|
|
|
"use strict";
|
|
|
|
|
/*global define, socket, app, bootbox, templates, ajaxify, RELATIVE_PATH*/
|
|
|
|
|
|
2015-01-01 15:35:22 -05:00
|
|
|
define('admin/manage/categories', function() {
|
2014-10-08 15:36:47 -04:00
|
|
|
var Categories = {};
|
|
|
|
|
|
|
|
|
|
Categories.init = function() {
|
2014-12-30 12:25:39 -05:00
|
|
|
var bothEl = $('#active-categories, #disabled-categories');
|
|
|
|
|
|
2014-12-29 21:20:43 -05:00
|
|
|
function updateCategoryOrders(evt, ui) {
|
|
|
|
|
var categories = $(evt.target).children(),
|
|
|
|
|
modified = {},
|
|
|
|
|
cid;
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2014-12-29 21:20:43 -05:00
|
|
|
for(var i=0;i<categories.length;i++) {
|
|
|
|
|
cid = $(categories[i]).attr('data-cid');
|
|
|
|
|
modified[cid] = {
|
|
|
|
|
order: i+1
|
|
|
|
|
};
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
2014-12-29 21:20:43 -05:00
|
|
|
|
|
|
|
|
socket.emit('admin.categories.update', modified);
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-30 12:25:39 -05:00
|
|
|
bothEl.sortable({
|
2014-12-29 21:20:43 -05:00
|
|
|
stop: updateCategoryOrders,
|
2014-12-30 12:25:39 -05:00
|
|
|
distance: 15
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Category enable/disable
|
|
|
|
|
bothEl.on('click', '[data-action="toggle"]', function(ev) {
|
|
|
|
|
var btnEl = $(this),
|
|
|
|
|
cid = btnEl.parents('tr').attr('data-cid'),
|
|
|
|
|
disabled = btnEl.attr('data-disabled') === 'false' ? '1' : '0',
|
|
|
|
|
payload = {};
|
|
|
|
|
|
|
|
|
|
payload[cid] = {
|
|
|
|
|
disabled: disabled
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socket.emit('admin.categories.update', payload, function(err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return app.alertError(err.message);
|
|
|
|
|
} else {
|
|
|
|
|
ajaxify.refresh();
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function showCreateCategoryModal() {
|
|
|
|
|
$('#new-category-modal').modal();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createNewCategory() {
|
|
|
|
|
var category = {
|
|
|
|
|
name: $('#inputName').val(),
|
|
|
|
|
description: $('#inputDescription').val(),
|
|
|
|
|
icon: $('#new-category-modal i').attr('value'),
|
|
|
|
|
bgColor: '#0059b2',
|
|
|
|
|
color: '#fff',
|
2014-12-30 12:25:39 -05:00
|
|
|
order: $('#disabled-categories').children().length + 1
|
2014-10-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
socket.emit('admin.categories.create', category, 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
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#new-category-modal').modal('hide');
|
|
|
|
|
ajaxify.refresh();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-01 15:35:22 -05:00
|
|
|
$('#addNew').on('click', showCreateCategoryModal);
|
|
|
|
|
$('#create-category-btn').on('click', createNewCategory);
|
2014-10-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Categories;
|
|
|
|
|
});
|