Files
NodeBB/public/src/admin/general/navigation.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-02-25 15:01:28 -05:00
"use strict";
/* global define, app, ajaxify, socket, templates, bootbox */
2015-02-25 15:01:28 -05:00
define('admin/general/navigation', ['translator'], function(translator) {
2015-02-25 16:47:19 -05:00
var navigation = {},
available;
2015-02-25 15:01:28 -05:00
navigation.init = function() {
2015-07-06 14:33:43 -04:00
available = ajaxify.data.available;
2015-02-25 15:01:28 -05:00
2015-02-25 17:12:17 -05:00
$('#enabled').html(translator.unescape($('#enabled').html()));
translator.translate(translator.unescape($('#available').html()), function(html) {
2015-02-25 17:26:44 -05:00
$('#available').html(html)
.find('li').draggable({
connectToSortable: '#enabled',
helper: 'clone',
distance: 10,
stop: drop
});
2015-02-25 17:12:17 -05:00
});
2015-02-25 16:19:14 -05:00
$('#enabled')
2015-02-25 16:52:50 -05:00
.on('click', '.delete', remove)
.on('click', '.toggle', toggle)
2015-02-25 16:19:14 -05:00
.sortable()
.droppable({
accept: $('#available li')
2015-02-25 16:47:19 -05:00
});
2015-02-25 16:19:14 -05:00
2015-02-25 16:52:50 -05:00
$('#save').on('click', save);
2015-02-25 15:01:28 -05:00
};
2015-02-25 16:47:19 -05:00
function drop(ev, ui) {
var id = ui.helper.attr('data-id'),
el = $('#enabled [data-id="' + id + '"]'),
data = id === 'custom' ? {} : available[id];
2015-02-25 17:26:44 -05:00
data.enabled = false;
2015-02-25 16:47:19 -05:00
templates.parse('admin/general/navigation', 'enabled', {enabled: [data]}, function(li) {
2015-02-25 17:12:17 -05:00
li = $(translator.unescape(li));
2015-02-25 16:47:19 -05:00
el.after(li);
el.remove();
});
}
function save() {
2015-02-25 15:01:28 -05:00
var nav = [];
$('#enabled li').each(function() {
var form = $(this).find('form').serializeArray(),
2015-05-19 14:57:05 -04:00
data = {},
properties = {};
2015-02-25 15:01:28 -05:00
form.forEach(function(input) {
2015-05-19 14:57:05 -04:00
if (input.name.slice(0, 9) === 'property:' && input.value === 'on') {
properties[input.name.slice(9)] = true;
} else {
data[input.name] = translator.escape(input.value);
}
2015-02-25 15:01:28 -05:00
});
2015-05-19 14:57:05 -04:00
data.properties = {};
for (var prop in properties) {
if (properties.hasOwnProperty(prop)) {
data.properties[prop] = properties[prop];
}
}
2015-02-25 15:01:28 -05:00
nav.push(data);
});
2015-02-25 15:01:45 -05:00
socket.emit('admin.navigation.save', nav, function(err) {
2015-02-25 15:01:28 -05:00
if (err) {
app.alertError(err.message);
} else {
app.alertSuccess('Successfully saved navigation');
}
});
}
2015-02-25 16:47:19 -05:00
function remove() {
$(this).parents('li').remove();
return false;
2015-02-25 16:47:19 -05:00
}
function toggle() {
var btn = $(this),
disabled = btn.html() === 'Enable';
btn.toggleClass('btn-warning').toggleClass('btn-success').html(!disabled ? 'Enable' : 'Disable');
2015-02-26 12:53:48 -05:00
btn.parents('li').find('[name="enabled"]').val(!disabled ? '' : 'on');
2015-02-25 16:47:19 -05:00
return false;
2015-02-25 16:19:14 -05:00
}
2015-02-25 15:01:28 -05:00
return navigation;
});