apostrophe-redirects
Advanced tools
Comparing version 0.5.7 to 0.6.0
244
index.js
@@ -1,142 +0,124 @@ | ||
var async = require('async'); | ||
var _ = require('lodash'); | ||
var extend = require('extend'); | ||
var async = require('async'); | ||
module.exports = { | ||
extend: 'apostrophe-pieces', | ||
name: 'apostrophe-redirect', | ||
alias: 'redirects', | ||
label: 'Redirect', | ||
pluralLabel: 'Redirects', | ||
searchable: false, | ||
adminOnly: true, | ||
module.exports = redirects; | ||
function redirects(options, callback) { | ||
return new redirects.Construct(options, callback); | ||
} | ||
// Everything in this constructor that should really be in an apostrophe-modules | ||
// metamodule is marked REFACTOR. -Tom | ||
redirects.Construct = function(options, callback) { | ||
var self = this; | ||
// "Protected" properties. We want related modules and subclasses to be able | ||
// to access these, thus no variables defined in the closure | ||
self._apos = options.apos; | ||
self._app = options.app; | ||
self._options = options; | ||
self.redirects = self._apos.redirects; | ||
self._apos.mixinModuleAssets(self, 'redirects', __dirname, options); | ||
// REFACTOR | ||
self._action = '/apos-redirects'; | ||
self.permissions = function(req, res, next) { | ||
if (!req.user.permissions.admin) { | ||
res.statusCode = 404; | ||
return res.send('notfound'); | ||
addFields: [ | ||
{ | ||
name: 'redirectSlug', | ||
type: 'slug', | ||
label: 'Old URL', | ||
required: true, | ||
page: true | ||
}, | ||
{ | ||
// contextual flag set so you don't see it in the editor; this gets updated | ||
// by beforeSave | ||
type: 'slug', | ||
name: 'slug', | ||
label: 'Slug', | ||
required: true, | ||
contextual: true | ||
}, | ||
{ | ||
name: 'title', | ||
label: 'Description', | ||
type: 'string', | ||
required: true | ||
}, | ||
{ | ||
// contextual: true to hide this property | ||
type: 'boolean', | ||
name: 'published', | ||
label: 'Published', | ||
required: true, | ||
def: true, | ||
contextual: true | ||
}, | ||
{ | ||
name: 'urlType', | ||
label: 'Link To', | ||
type: 'select', | ||
choices: [ | ||
{ label: 'Internal Page', value: 'internal', showFields: [ '_newPage' ] }, | ||
{ label: 'External URL', value: 'external', showFields: [ 'externalUrl' ] } | ||
] | ||
}, | ||
{ | ||
name: '_newPage', | ||
type: 'joinByOne', | ||
withType: 'apostrophe-page', | ||
label: 'Page Title', | ||
idField: 'pageId', | ||
filters: { | ||
projection: { slug: 1, title: 1 }, | ||
// Admins set up redirects, so it's OK for non-admins to follow them anywhere | ||
// (they won't actually get access without logging in) | ||
permission: false | ||
} | ||
}, | ||
{ | ||
name: 'externalUrl', | ||
label: 'URL', | ||
type: 'url' | ||
} | ||
return next(); | ||
}; | ||
self.deliver = function(res, err, result) { | ||
if (err) { | ||
console.log(err); | ||
return res.send({ | ||
status: 'failed' | ||
}); | ||
], | ||
removeFields: ['tags'], | ||
arrangeFields: [ | ||
{ | ||
name: 'info', | ||
label: 'Info', | ||
fields: [ | ||
'redirectSlug', | ||
'title', | ||
'urlType', | ||
'_newPage', | ||
'externalUrl', | ||
] | ||
} | ||
var response = { 'status': 'ok' }; | ||
if (result !== undefined) { | ||
response.result = result; | ||
} | ||
return res.send(response); | ||
}; | ||
], | ||
function thenDeliver(res) { | ||
return function(err, result) { | ||
return self.deliver(res, err, result); | ||
}; | ||
} | ||
construct: function(self, options) { | ||
var pages = self.apos.pages; | ||
// Middleware to check for hard redirects before actual pages | ||
self.pageMiddleware = function(req, res, next) { | ||
return self._apos.redirects.findOne({ from: req.params[0], hard: true }, function(err, redirect) { | ||
if (redirect) { | ||
return res.redirect(redirect.to); | ||
self.beforeSave = function(req, doc, options, callback) { | ||
//prefix the actual slug so it's not treated as a page | ||
doc.slug = 'redirect-' + doc.redirectSlug; | ||
if (!doc.title) { | ||
doc.title = doc.redirectSlug; | ||
} | ||
return next(); | ||
}); | ||
}; | ||
self.validate = function(body) { | ||
var redirect = { | ||
_id: body._id, | ||
from: self._apos.sanitizeString(body.from), | ||
to: self._apos.sanitizeString(body.to), | ||
reason: self._apos.sanitizeString(body.reason), | ||
hard: true | ||
return callback(null); | ||
}; | ||
if ((!redirect.from) || (!redirect.to)) { | ||
return null; | ||
} | ||
return redirect; | ||
}; | ||
// Fetch the whole list to initialize the editor | ||
self._app.get(options.loadUrl || self._action + '/load', self.permissions, function(req, res) { | ||
self.redirects.find({ hard: true }).sort({from: 1}).toArray(thenDeliver(res)); | ||
}); | ||
self._app.post(options.updateUrl || self._action + '/update', self.permissions, function(req, res) { | ||
if (typeof(req.body) !== 'object') { | ||
return self.deliver(res, 'invalid'); | ||
} | ||
item = self.validate(req.body); | ||
if (!item) { | ||
return self.deliver(res, 'invalid'); | ||
} | ||
var newItem; | ||
return async.series({ | ||
remove: function(callback) { | ||
return self.redirects.remove({ $or: [ { _id: item._id }, { from: item.from } ] }, callback); | ||
}, | ||
insert: function(callback) { | ||
if (!item._id) { | ||
item._id = self._apos.generateId(); | ||
// ALL pages should check to see if a redirect exists | ||
var superPagesServe = pages.serve; | ||
pages.serve = function(req, res) { | ||
var slug = req.params[0]; | ||
return self.find(req, { slug: 'redirect-' + slug }, { | ||
title: 1, | ||
slug: 1, | ||
urlType: 1, | ||
pageId: 1, | ||
type: 1, | ||
externalUrl: 1, | ||
redirectSlug: 1, | ||
_newPage: 1 | ||
}).toObject(function(err, result) { | ||
if(result) { | ||
if(result.urlType == 'internal' && result._newPage) { | ||
return req.res.redirect(result._newPage._url); | ||
} else if(result.urlType == 'external' && result.externalUrl.length) { | ||
return req.res.redirect(result.externalUrl); | ||
} | ||
} | ||
return self.redirects.insert(item, { upsert: true }, function(err, item) { | ||
newItem = item; | ||
return callback(err); | ||
}); | ||
} | ||
}, function(err) { | ||
return self.deliver(res, err, newItem); | ||
}); | ||
}); | ||
self._app.post(options.removeUrl || self._action + '/remove', self.permissions, function(req, res) { | ||
return self.redirects.remove({ _id: req.body._id }, function(err) { | ||
if (err) { | ||
return self.deliver(res, 'invalid'); | ||
} | ||
return self.deliver(res); | ||
}); | ||
}); | ||
self._apos.pushGlobalCallWhen('user', 'window.aposRedirects = new AposRedirects()'); | ||
self.pushAsset('script', 'editor', { when: 'user' }); | ||
self.pushAsset('stylesheet', 'editor', { when: 'user' }); | ||
self.pushAsset('template', 'editor', { when: 'user' }); | ||
self._apos.addLocal('aposRedirectsMenu', function(args) { | ||
var result = self.render('menu', args); | ||
return result; | ||
}); | ||
if (callback) { | ||
// Invoke callback on next tick so that the constructor's return | ||
// value can be assigned to a variable in the same closure where | ||
// the callback resides | ||
process.nextTick(function() { return callback(null); }); | ||
return superPagesServe(req, res); | ||
}); | ||
}; | ||
} | ||
}; | ||
} |
{ | ||
"name": "apostrophe-redirects", | ||
"version": "0.5.7", | ||
"version": "0.6.0", | ||
"description": "Allows admins to create redirects within an Apostrophe site", | ||
@@ -24,6 +24,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "2.4.x", | ||
"async": "0.8.x", | ||
"extend": "~1.2.1" | ||
} | ||
} |
@@ -1,9 +0,3 @@ | ||
## Currently for 0.5.x projects only | ||
This module allows admins to add redirects from one URL to another within an [Apostrophe site](http://apostrophecms.org/). | ||
This module has not yet been ported to A2 2.x. | ||
# apostrophe-redirects | ||
Allows admins to add redirects from one URL to another within an [Apostrophe site](http://github.com/punkave/apostrophe-sandbox). | ||
**Table of Contents** | ||
@@ -19,3 +13,3 @@ | ||
First make sure you have an [Apostrophe project](http://github.com/punkave/apostrophe-sandbox)! | ||
First make sure you have an [Apostrophe project](http://apostrophecms.org/)! | ||
@@ -33,6 +27,2 @@ Then: | ||
Also, edit `outerLayout.html` so your admins can find the UI: | ||
{{ aposRedirectsMenu({ edit: permissions.admin }) }} | ||
That's it! | ||
@@ -39,0 +29,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
4970
4
118
35
1
- Removedasync@0.8.x
- Removedextend@~1.2.1
- Removedlodash@2.4.x
- Removedasync@0.8.0(transitive)
- Removedextend@1.2.1(transitive)
- Removedlodash@2.4.2(transitive)