Skip to content

Commit

Permalink
Added wildcard routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
petersirka committed Aug 20, 2020
1 parent 8f1d1c2 commit f59ed20
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 66 deletions.
26 changes: 13 additions & 13 deletions cms.bundle

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions definitions/database.js
Expand Up @@ -22,7 +22,7 @@ FUNC.write = function(type, id, content, callback, exists) {

var db = TABLE(type + 'data');
if (exists) {
db.modify({ body: content }, true).where('id', id).insert(function(doc) {
db.modify({ body: content }, true).id(id).insert(function(doc) {
doc.id = id;
doc.dtcreated = NOW;
}).callback(callback);
Expand All @@ -31,7 +31,7 @@ FUNC.write = function(type, id, content, callback, exists) {
};

FUNC.read = function(type, id, callback) {
TABLE(type + 'data').read().where('id', id).fields('body').callback(function(err, doc) {
TABLE(type + 'data').read().id(id).fields('body').callback(function(err, doc) {
callback(null, doc ? doc.body : '');
});
};
Expand Down
2 changes: 1 addition & 1 deletion plugins/contactforms/schemas/contacts.js
Expand Up @@ -27,7 +27,7 @@ NEWSCHEMA('ContactForms', function(schema) {
});

schema.setGet(function($) {
NOSQL('contactforms').read().where('id', $.id).callback($.callback, 'error-contacforms-404');
NOSQL('contactforms').read().id($.id).callback($.callback, 'error-contacforms-404');
});

schema.setRemove(function($) {
Expand Down
8 changes: 4 additions & 4 deletions plugins/newsletters/schemas/newsletters.js
Expand Up @@ -53,7 +53,7 @@ NEWSCHEMA('Newsletters', function(schema) {
schema.setRemove(function($) {
var id = $.id;
FUNC.remove('newsletters', id);
NOSQL('newsletters').remove().where('id', id).callback(() => $.success());
NOSQL('newsletters').remove().id(id).callback(() => $.success());
NOSQL('parts').remove().where('ownerid', id).where('type', 'newsletter');
});

Expand Down Expand Up @@ -87,7 +87,7 @@ NEWSCHEMA('Newsletters', function(schema) {

model.body = undefined;

var db = isUpdate ? nosql.modify(model).where('id', model.id).backup($.user.meta(model)) : nosql.insert(model);
var db = isUpdate ? nosql.modify(model).id(model.id).backup($.user.meta(model)) : nosql.insert(model);

db.callback(function() {
$SAVE('Events', { type: 'newsletters/save', user: user, id: model.id, body: model.name, admin: true }, NOOP, $);
Expand Down Expand Up @@ -209,15 +209,15 @@ NEWSCHEMA('Newsletters', function(schema) {
setTimeout(() => Mail.send2(messages, next), 60000);

// Updates DB
NOSQL('newsletters').modify({ issending: true, count: sum, dtsent: NOW }).first().where('id', MAIN.newsletter.id);
NOSQL('newsletters').modify({ issending: true, count: sum, dtsent: NOW }).first().id(MAIN.newsletter.id);

} else
Mail.send2(messages, () => setTimeout(next, 2000));

}, function() {
PREF.set('newsletters', null);
$SAVE('Event', { type: 'newsletters/sent', body: repository.page.name, admin: true }, NOOP, $);
NOSQL('newsletters').modify({ issending: false, issent: true, count: sum, dtsent: NOW }).first().where('id', MAIN.newsletter.id);
NOSQL('newsletters').modify({ issending: false, issent: true, count: sum, dtsent: NOW }).first().id(MAIN.newsletter.id);
MAIN.newsletter.sending = false;
MAIN.newsletter.percentage = 0;
MAIN.newsletter.id = null;
Expand Down
2 changes: 1 addition & 1 deletion plugins/notices/index.js
Expand Up @@ -7,7 +7,7 @@ exports.install = function() {
ROUTE('GET /admin/api/notices/ *Notices --> @query');
ROUTE('GET /admin/api/notices/{id}/ *Notices --> @read');
ROUTE('POST /admin/api/notices/ *Notices --> @save');
ROUTE('DELETE /admin/api/notices/ *Notices --> @remove');
ROUTE('DELETE /admin/api/notices/{id}/ *Notices --> @remove');
ROUTE('GET /admin/api/notices/toggle/ *Notices --> @toggle');
ROUTE('POST /admin/api/notices/preview/', preview, ['json']);
};
Expand Down
4 changes: 2 additions & 2 deletions plugins/notices/public/form.html
Expand Up @@ -16,7 +16,7 @@
<div class="padding bg-smoke" style="padding-bottom:10px">
<div class="row">
<div class="col-md-4 m">
<div data---="input__?.category__dirsource:common.dependencies.notices;icon:folder-o;required:1;dircustom:1;dirplaceholder:@(Search or enter a new);placeholder:@(Choose a category)">@(Category)</div>
<div data---="input__?.category__dirsource:common.dependencies.notices;icon:folder-o;required:1;dircustom:1;dirvalue:name;dirplaceholder:@(Search or enter a new);placeholder:@(Choose a category)">@(Category)</div>
</div>
<div class="col-md-4 m">
<div data---="input__?.author__maxlength:30;icon:user;placeholder:@(Who is the autor?)">@(Author)</div>
Expand All @@ -27,6 +27,7 @@
</div>
</div>
<div class="padding" style="padding-bottom:10px">
<div data---="input__?.summary__maxlength:500" class="m">@(Summarization)</div>
<div class="row">
<div class="col-md-8 m">
<div data---="input__?.url__placeholder:@(Type a URL address)">@(URL address)</div>
Expand All @@ -36,7 +37,6 @@
<div data---="faiconsbutton__?.icon"></div>
</div>
</div>
<div data---="input__?.summary__maxlength:500" class="m">@(Summarization)</div>
</div>
<div class="tabmenu-container">
<ul data---="tabmenu__?tab__'editor'" class="tabmenu">
Expand Down
12 changes: 6 additions & 6 deletions plugins/notices/schemas/notices.js
Expand Up @@ -2,9 +2,9 @@ NEWSCHEMA('Notices', function(schema) {

schema.define('id', UID);
schema.define('category', 'String(50)', true);
schema.define('name', 'String(200)', true);
schema.define('name', String, true);
schema.define('author', 'String(30)', true);
schema.define('summary', 'String(500)');
schema.define('summary', String);
schema.define('body', String, true);
schema.define('date', Date);
schema.define('event', Date);
Expand Down Expand Up @@ -50,15 +50,15 @@ NEWSCHEMA('Notices', function(schema) {
var options = $.options;
var filter = NOSQL('notices').one();
var id = options.id || $.id;
filter.where('id', id);
filter.id(id);
filter.callback($.callback, 'error-notices-404');
FUNC.alert($.user, 'notices/edit', id);
});

// Removes a specific post
schema.setRemove(function($) {
var id = $.body.id;
NOSQL('notices').remove().where('id', id).callback(function() {
var id = $.id;
NOSQL('notices').remove().id(id).callback(function() {
F.cache.removeAll('cachecms');
$.success();
});
Expand Down Expand Up @@ -88,7 +88,7 @@ NEWSCHEMA('Notices', function(schema) {
model.search = ((model.name || '') + ' ' + (model.body || '')).keywords(true, true).join(' ').max(1000);
model.linker_category = model.category.slug();

var db = update ? nosql.modify(model).where('id', model.id) : nosql.insert(model);
var db = update ? nosql.modify(model).id(model.id) : nosql.insert(model);

db.callback(function() {
$SAVE('Events', { type: 'notices/save', id: model.id, user: user, body: model.name, admin: true }, NOOP, $);
Expand Down
8 changes: 4 additions & 4 deletions plugins/pages/schemas/navigations.js
Expand Up @@ -18,7 +18,7 @@ NEWSCHEMA('Navigations', function(schema) {

schema.setGet(function($) {
FUNC.alert($.user, 'navigations/edit', $.id);
NOSQL('navigations').read().where('id', $.id).callback(function(err, response) {
NOSQL('navigations').read().id($.id).callback(function(err, response) {
if (response) {
$.callback(response);
} else {
Expand Down Expand Up @@ -83,7 +83,7 @@ NEWSCHEMA('Navigations', function(schema) {
children.push(obj);

count++;
NOSQL('navigations').modify({ children: children }).where('id', navid).callback(next);
NOSQL('navigations').modify({ children: children }).id(navid).callback(next);

}, function() {

Expand Down Expand Up @@ -111,7 +111,7 @@ NEWSCHEMA('Navigations', function(schema) {
return;
}

db.update(model, model).where('id', model.id).callback(function() {
db.update(model, model).id(model.id).callback(function() {
$SAVE('Events', { type: 'navigations/save', user: user, id: model.id, body: model.name, admin: true }, NOOP, $);
EMIT('navigations.save', model);
refresh();
Expand Down Expand Up @@ -143,7 +143,7 @@ NEWSCHEMA('Navigations', function(schema) {
}

item.dtupdated = NOW;
db.update(nav).where('id', nav.id).callback(done);
db.update(nav).id(nav.id).callback(done);
}
}
$.success();
Expand Down
55 changes: 39 additions & 16 deletions plugins/pages/schemas/pages.js
Expand Up @@ -20,10 +20,10 @@ NEWSCHEMA('Pages', function(schema) {
schema.define('ispartial', Boolean); // Is only partial page (the page will be shown in another page)
schema.define('keywords', 'String(200)'); // Meta keywords
schema.define('description', 'String(200)'); // Meta description
schema.define('name', 'String(50)', true); // Name in admin
schema.define('name', String, true); // Name in admin
schema.define('parent', UID); // Parent page for breadcrumb
schema.define('partial', '[UID]'); // A partial content
schema.define('summary', 'String(500)'); // Short page description generated according to the "CMS_summary" class in CMS editor
schema.define('summary', 'String(1000)'); // Short page description generated according to the "CMS_summary" class in CMS editor
schema.define('pictures', '[String]'); // URL addresses for first 5 pictures
schema.define('search', 'String(1000)'); // Search pharses
schema.define('template', UID); // Render template views/*.html
Expand Down Expand Up @@ -62,8 +62,8 @@ NEWSCHEMA('Pages', function(schema) {
var opt = $.options;
var filter = NOSQL('pages').one();
opt.url && filter.where('url', opt.url);
opt.id && filter.where('id', opt.id);
$.id && filter.where('id', $.id);
opt.id && filter.id(opt.id);
$.id && filter.id($.id);

filter.callback(function(err, response) {

Expand Down Expand Up @@ -102,7 +102,7 @@ NEWSCHEMA('Pages', function(schema) {
var id = $.body.id;
var db = NOSQL('pages');

db.remove().where('id', id).callback(function(err, count) {
db.remove().id(id).callback(function(err, count) {
$.success();
if (count) {
FUNC.remove('pages', id);
Expand Down Expand Up @@ -189,7 +189,7 @@ NEWSCHEMA('Pages', function(schema) {
}

model.body = undefined;
var db = update ? nosql.modify(model).where('id', model.id).backup($.user.meta(model)) : nosql.insert(model);
var db = update ? nosql.modify(model).id(model.id).backup($.user.meta(model)) : nosql.insert(model);

// Update a URL in all navigations where this page is used
if (!model.ispartial)
Expand Down Expand Up @@ -398,6 +398,7 @@ function refresh() {
var helper = {};
var partial = [];
var pages = [];
var wildcard = [];

for (var i = 0, length = response.length; i < length; i++) {
var doc = response[i];
Expand All @@ -408,13 +409,20 @@ function refresh() {
continue;
}

var wild = doc.url.indexOf('*/') !== -1;
if (wild)
doc.url = doc.url.replace('*/', '');

var key = doc.url;
var lng = doc.language;
var obj = { id: doc.id, url: doc.url, name: doc.name, title: doc.title, parent: doc.parent, icon: doc.icon, links: [], language: doc.language, dtcreated: doc.dtcreated, dtupdated: doc.dtupdated };
var obj = { id: doc.id, url: doc.url, name: doc.name, title: doc.title, parent: doc.parent, icon: doc.icon, links: [], language: doc.language, dtcreated: doc.dtcreated, dtupdated: doc.dtupdated, wildcard: wild };

helper[doc.id] = key;
sitemap[key] = obj;

if (wild)
wildcard.push(obj);

if (lng) {
key = lng + ' ' + key;
sitemap[key] = CLONE(obj);
Expand All @@ -439,6 +447,7 @@ function refresh() {
MAIN.sitemap = sitemap;
MAIN.partial = partial;
MAIN.pages = pages;
MAIN.wildcard = wildcard;

$GET('Pages/Globals', function(err, response) {
response.body && parseGlobals(response.body);
Expand Down Expand Up @@ -726,6 +735,7 @@ function loadpartial(page, callback, controller) {
Controller.prototype.CMSpage = function(callback, cache) {

var self = this;
var is = false;
var page;

if (self.language) {
Expand All @@ -734,19 +744,32 @@ Controller.prototype.CMSpage = function(callback, cache) {
} else
page = MAIN.sitemap[self.url];


if (!page) {

if (MAIN.redirects && MAIN.redirects[self.url]) {
self.redirect(MAIN.redirects[self.url], RELEASE);
COUNTER('pages').hit('redirect');
} else {

for (var i = 0; i < MAIN.wildcard.length; i++) {
page = MAIN.wildcard[i];
if (self.url.substring(0, page.url.length) === page.url) {
is = true;
break;
}
}

// tries to redirect to admin
if (self.url === '/')
self.redirect('/admin/');
else
self.throw404();
if (!is) {
if (self.url === '/')
self.redirect('/admin/');
else
self.throw404();
}
}
return self;

if (!is)
return self;
}

if (typeof(callback) === 'boolean') {
Expand All @@ -764,7 +787,7 @@ Controller.prototype.CMSpage = function(callback, cache) {

self.memorize('cachecms' + (self.language || '') + '_' + self.url, cache || '1 minute', cache === false, function() {

NOSQL('pages').one().where('id', page.id).callback(function(err, response) {
NOSQL('pages').one().id(page.id).callback(function(err, response) {

if (!response.template) {
self.invalid('error-pages-template');
Expand Down Expand Up @@ -923,7 +946,7 @@ Controller.prototype.CMSrender = function(url, callback) {
return self;
}

NOSQL('pages').one().where('id', page.id).callback(function(err, response) {
NOSQL('pages').one().id(page.id).callback(function(err, response) {

var repo = self.repository;
self.title(response.title);
Expand Down Expand Up @@ -981,7 +1004,7 @@ Controller.prototype.CMSpartial = function(url, callback) {
}

COUNTER('pages').hit(page.id);
NOSQL('pages').one().where('id', page.id).callback(function(err, response) {
NOSQL('pages').one().id(page.id).callback(function(err, response) {

if (response.css) {
response.css = U.minifyStyle('/*auto*/\n' + response.css);
Expand Down
4 changes: 2 additions & 2 deletions plugins/pages/schemas/parts.js
Expand Up @@ -36,14 +36,14 @@ NEWSCHEMA('Parts', function(schema) {
item.type = model.type;
FUNC.write('parts', item.id, U.minifyHTML(item.body), true);
item.body = undefined;
NOSQL('parts').update(item, true).where('id', item.id);
NOSQL('parts').update(item, true).id(item.id);
}
$.success();
});
});

schema.addWorkflow('render', function($) {
NOSQL('parts').find().where('id', $.options.id).first().callback(function(err, response) {
NOSQL('parts').find().id($.options.id).first().callback(function(err, response) {
if (response) {
FUNC.read('parts', response.id, function(err, body) {
response.body = body;
Expand Down
6 changes: 3 additions & 3 deletions plugins/pages/schemas/templates.js
Expand Up @@ -8,7 +8,7 @@ NEWSCHEMA('Templates', function(schema) {
schema.define('body', String, true);

schema.setGet(function($) {
NOSQL('templates').read().where('id', $.id).callback($.callback, 'error-templates-404');
NOSQL('templates').read().id($.id).callback($.callback, 'error-templates-404');
});

schema.setSave(function($, model) {
Expand All @@ -23,7 +23,7 @@ NEWSCHEMA('Templates', function(schema) {
if (model.id) {
model.id = undefined;
model.dtupdated = NOW;
db.modify(model).where('id', id).callback(done);
db.modify(model).id(id).callback(done);
} else {
model.id = id;
model.dtcreated = NOW;
Expand All @@ -33,7 +33,7 @@ NEWSCHEMA('Templates', function(schema) {
});

schema.setRemove(function($) {
NOSQL('templates').remove().where('id', $.id).callback(function() {
NOSQL('templates').remove().id($.id).callback(function() {

NOSQL('pages').modify({ template: '' }).where('template', $.id);
NOSQL('posts').modify({ template: '' }).where('template', $.id);
Expand Down
2 changes: 1 addition & 1 deletion plugins/posts/index.js
Expand Up @@ -7,7 +7,7 @@ exports.install = function() {
ROUTE('GET /admin/api/posts/ *Posts --> @query');
ROUTE('GET /admin/api/posts/{id}/ *Posts --> @read');
ROUTE('POST /admin/api/posts/ *Posts --> @save');
ROUTE('DELETE /admin/api/posts/ *Posts --> @remove');
ROUTE('DELETE /admin/api/posts/{id}/ *Posts --> @remove');
ROUTE('GET /admin/api/posts/toggle/ *Posts --> @toggle');
ROUTE('GET /admin/api/posts/stats/ *Posts --> @stats');
ROUTE('GET /admin/api/posts/{id}/stats/ *Posts --> @stats');
Expand Down

0 comments on commit f59ed20

Please sign in to comment.