Created
May 18, 2015 17:39
-
-
Save geoffreysmith/838beba854c95b0e8a1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('lodash'); | |
var snippets = require('apostrophe-snippets'); | |
module.exports = mainnav; | |
function mainnav(options, callback) { | |
return new mainnav.Mainnav(options, callback); | |
} | |
mainnav.Mainnav = function (options, callback) { | |
var self = this; | |
var apos = options.apos; | |
var app = options.app; | |
_.defaults(options, { | |
instance: 'mainnav', | |
name: options.name || 'mainnav', | |
label: options.name || 'Main Nav', | |
instanceLabel: options.instanceLabel || 'Main Nav', | |
icon: options.icon || 'content', | |
menuName: 'aposMainnavMenu', | |
searchable: true, | |
permalinks: true, | |
perPage: 100, | |
groupFields: [ | |
{ | |
name: 'topLevelNav', | |
label: 'Top Level Element', | |
icon: 'content', | |
fields: ['title'] | |
}, | |
{ | |
name: 'subLinks', | |
label: 'Sub Links', | |
fields: ['sublinks'] | |
} | |
], | |
orderFields: [ | |
'title', | |
'sublinks' | |
] | |
}); | |
options.removeFields = ['hideTitle', 'thumbnail', 'tags', 'body']; | |
options.addFields = [ | |
{ | |
name: 'title', | |
type: 'string', | |
label: 'Title' | |
}, | |
{ | |
name: 'sortorder', | |
type: 'integer', | |
label: 'Sort order' | |
}, | |
{ | |
name: 'sublinks', | |
type: 'array', | |
label: 'Sub Links', | |
schema: [ | |
{ | |
name: 'url', | |
type: 'string', | |
label: 'Url' | |
}, | |
{ | |
name: 'name', | |
type: 'string', | |
label: 'Name' | |
} | |
] | |
} | |
].concat(options.addFields || []); | |
options.requireFields = ['title']; | |
options.modules = (options.modules || []).concat([{dir: __dirname, name: 'mainnav'}]); | |
snippets.Snippets.call(this, options, null); | |
function appendExtraFields(data, snippet, callback) { | |
snippet.title = self._apos.sanitizeString(data.title, snippet.title); | |
return callback(null); | |
} | |
self.beforeInsert = function (req, data, snippet, callback) { | |
appendExtraFields(data, snippet, callback); | |
}; | |
self.beforeUpdate = function (req, data, snippet, callback) { | |
appendExtraFields(data, snippet, callback); | |
}; | |
var superGet = self.get; | |
self.get = function (req, userCriteria, optionsArg, callback) { | |
var options = {}; | |
var filterCriteria = {}; | |
_.merge(options, optionsArg || {}); | |
_.merge(filterCriteria, userCriteria || {}); | |
options.sort = { sortorder: 1}; | |
return superGet.call(self, req, filterCriteria, options, callback); | |
}; | |
self.loader = function (req, callback) { | |
var criteria = {}; | |
criteria.type = 'mainnav'; | |
criteria.published = true; | |
criteria.$or = [{trash: false}, {trash: {$exists: false}}] | |
apos.get(app.request, criteria, {}, function (err, items) { | |
req.extras.mainMenuItems = items; | |
}); | |
return callback(null); | |
}; | |
if (callback) { | |
process.nextTick(function () { | |
return callback(null); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment