You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.8 KiB
JavaScript

8 years ago
'use strict';
9 years ago
var async = require('async');
6 years ago
const _ = require('lodash');
9 years ago
var plugins = require('../plugins');
var db = require('../database');
var translator = require('../translator');
9 years ago
var pubsub = require('../pubsub');
8 years ago
var admin = module.exports;
6 years ago
let cache = null;
9 years ago
pubsub.on('admin:navigation:save', function () {
6 years ago
cache = null;
9 years ago
});
admin.save = function (data, callback) {
9 years ago
var order = Object.keys(data);
var items = data.map(function (item, index) {
9 years ago
for (var i in item) {
7 years ago
if (item.hasOwnProperty(i) && typeof item[i] === 'string' && (i === 'title' || i === 'text')) {
item[i] = translator.escape(item[i]);
}
9 years ago
}
item.order = order[index];
7 years ago
return JSON.stringify(item);
9 years ago
});
6 years ago
cache = null;
9 years ago
pubsub.publish('admin:navigation:save');
async.waterfall([
function (next) {
db.delete('navigation:enabled', next);
},
function (next) {
db.sortedSetAdd('navigation:enabled', order, items, next);
},
], callback);
};
admin.getAdmin = function (callback) {
async.parallel({
enabled: admin.get,
available: getAvailable,
}, callback);
};
admin.get = function (callback) {
6 years ago
if (cache) {
return setImmediate(callback, null, _.cloneDeep(cache));
}
async.waterfall([
function (next) {
db.getSortedSetRange('navigation:enabled', 0, -1, next);
},
function (data, next) {
7 years ago
data = data.map(function (item) {
6 years ago
item = JSON.parse(item);
item.groups = item.groups || [];
if (item.groups && !Array.isArray(item.groups)) {
item.groups = [item.groups];
}
return item;
});
6 years ago
cache = data;
next(null, _.cloneDeep(cache));
},
], callback);
};
function getAvailable(callback) {
var core = require('../../install/data/navigation.json').map(function (item) {
item.core = true;
return item;
});
9 years ago
plugins.fireHook('filter:navigation.available', core, callback);
}
require('../promisify')(admin);