From d806cd134fbcd29e2a015300f69f28ca250cec21 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Wed, 25 Feb 2015 16:47:19 -0500 Subject: [PATCH] sortable menus, done --- public/src/admin/general/navigation.js | 72 ++++++++++++-------------- src/navigation/admin.js | 6 ++- src/navigation/index.js | 4 +- src/upgrade.js | 4 +- 4 files changed, 40 insertions(+), 46 deletions(-) diff --git a/public/src/admin/general/navigation.js b/public/src/admin/general/navigation.js index 6e95f578b9..6b0de51420 100644 --- a/public/src/admin/general/navigation.js +++ b/public/src/admin/general/navigation.js @@ -2,57 +2,44 @@ /* global define, app, ajaxify, socket, templates, bootbox */ define('admin/general/navigation', function() { - var navigation = {}; - - - var available; + var navigation = {}, + available; navigation.init = function() { available = JSON.parse(ajaxify.variables.get('available')); - $('.delete').on('click', function() { - $(this).parents('li').remove(); - }); - - $('.toggle').on('click', function() { - var btn = $(this), - disabled = btn.html() === 'Enable'; - - btn.toggleClass('btn-warning').toggleClass('btn-success').html(!disabled ? 'Enable' : 'Disable'); - btn.parents('li').find('[name="enabled"]').val(disabled); - return false; - }); - - $('#save').on('click', saveNavigation); + $('#save').on('click', save); + $('.delete').on('click', remove); + $('.toggle').on('click', toggle); $('#enabled') .sortable() .droppable({ accept: $('#available li') - }) - .disableSelection(); + }); $('#available li') .draggable({ connectToSortable: '#enabled', helper: 'clone', distance: 10, - stop: function(ev, ui) { - var id = ui.helper.attr('data-id'), - el = $('#enabled [data-id="' + id + '"]'), - data = id === 'custom' ? {} : available[id]; - - templates.parse('admin/general/navigation', 'enabled', {enabled: [data]}, function(li) { - li = $(li); - el.after(li); - el.remove(); - }); - } - }) - .disableSelection(); + stop: drop + }); }; - function saveNavigation() { + function drop(ev, ui) { + var id = ui.helper.attr('data-id'), + el = $('#enabled [data-id="' + id + '"]'), + data = id === 'custom' ? {} : available[id]; + + templates.parse('admin/general/navigation', 'enabled', {enabled: [data]}, function(li) { + li = $(li); + el.after(li); + el.remove(); + }); + } + + function save() { var nav = []; $('#enabled li').each(function() { @@ -81,12 +68,17 @@ define('admin/general/navigation', function() { }); } - function getDefaultsByRoute(route) { - available.forEach(function(item) { - if (item.route.match(route)) { - return item; - } - }); + function remove() { + $(this).parents('li').remove(); + } + + function toggle() { + var btn = $(this), + disabled = btn.html() === 'Enable'; + + btn.toggleClass('btn-warning').toggleClass('btn-success').html(!disabled ? 'Enable' : 'Disable'); + btn.parents('li').find('[name="enabled"]').val(disabled); + return false; } return navigation; diff --git a/src/navigation/admin.js b/src/navigation/admin.js index c60701e10d..15726001d1 100644 --- a/src/navigation/admin.js +++ b/src/navigation/admin.js @@ -9,8 +9,10 @@ var admin = {}, admin.save = function(data, callback) { var order = Object.keys(data), - items = data.map(function(item) { - return JSON.stringify(item); + items = data.map(function(item, idx) { + var data = {}; + data[idx] = item; + return JSON.stringify(data); }); async.waterfall([ diff --git a/src/navigation/index.js b/src/navigation/index.js index 6a7ea1f638..2c1cbd7fb6 100644 --- a/src/navigation/index.js +++ b/src/navigation/index.js @@ -8,8 +8,8 @@ var navigation = {}, navigation.get = function(callback) { db.getSortedSetRange('navigation:enabled', 0, -1, function(err, data) { - callback(err, data.map(function(item) { - return JSON.parse(item); + callback(err, data.map(function(item, idx) { + return JSON.parse(item)[idx]; })); }); }; diff --git a/src/upgrade.js b/src/upgrade.js index 6e2306f4fe..fee22659dd 100644 --- a/src/upgrade.js +++ b/src/upgrade.js @@ -21,7 +21,7 @@ var db = require('./database'), schemaDate, thisSchemaDate, // IMPORTANT: REMEMBER TO UPDATE VALUE OF latestSchema - latestSchema = Date.UTC(2015, 1, 25, 2); + latestSchema = Date.UTC(2015, 1, 25, 4); Upgrade.check = function(callback) { db.get('schemaDate', function(err, value) { @@ -943,7 +943,7 @@ Upgrade.upgrade = function(callback) { } }, function(next) { - thisSchemaDate = Date.UTC(2015, 1, 25, 2); + thisSchemaDate = Date.UTC(2015, 1, 25, 4); if (schemaDate < thisSchemaDate) { updatesMade = true; winston.info('[2015/02/25] Upgrading menu items to dynamic navigation system');