Skip to content

Commit

Permalink
Merge pull request pencilblue#104 from cbdr/refactor_mt_base_admin
Browse files Browse the repository at this point in the history
Refactor mt base admin
  • Loading branch information
andparker committed Jun 26, 2015
2 parents a312ccf + 4841e31 commit 0b85a1b
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 60 deletions.
29 changes: 4 additions & 25 deletions controllers/admin/base_admin_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ module.exports = function BaseAdminControllerModule(pb) {
* This class serves as a base class for all the controllers used in the admin control panel
* @constructor
*/
function BaseAdminController() {
}
function BaseAdminController() {}
util.inherits(BaseAdminController, BaseController);

/**
Expand All @@ -43,21 +42,9 @@ module.exports = function BaseAdminControllerModule(pb) {
};

BaseAdminController.prototype.extendedInit = function(cb) {
var self = this;
var siteService = new pb.SiteService();
siteService.getByUid(self.site, function (err, siteInfo) {
if (err || !siteInfo) {
self.reqHandler.serve404();
} else {
self.sectionService = new pb.SectionService(self.site, true);
self.siteQueryService = new pb.SiteQueryService(self.site, true);
self.settings = pb.SettingServiceFactory.getServiceBySite(self.site, true);
self.siteObj = siteInfo;
self.isGlobalSite = pb.SiteService.isGlobal(siteInfo.uid);
self.siteName = self.isGlobalSite ? siteInfo.uid : siteInfo.displayName;
cb();
}
});
this.siteQueryService = new pb.SiteQueryService(this.site, true);
this.settings = pb.SettingServiceFactory.getServiceBySite(this.site, true);
cb();
};

/**
Expand All @@ -70,14 +57,6 @@ module.exports = function BaseAdminControllerModule(pb) {
return util.merge(BaseAdminController.super_.prototype.getServiceContext.apply(this), { onlyThisSite: true});
};

/**
* @method getTemplateService
* @return {Object} TemplateService
*/
BaseAdminController.prototype.getTemplateService = function(tsOpts) {
tsOpts.site = this.site;
return new pb.TemplateService(tsOpts);
};

/**
* Centralized place to obtain the pills to be displayed on top of the admin controller
Expand Down
49 changes: 29 additions & 20 deletions controllers/base_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var Sanitizer = require('sanitize-html');
var util = require('../include/util.js');

module.exports = function BaseControllerModule(pb) {


// pb dependancies
var SiteService = pb.SiteService;

/**
* The base controller provides functions for the majority of
* the heavy lifing for a controller. It accepts and provides access to
Expand Down Expand Up @@ -137,38 +140,39 @@ module.exports = function BaseControllerModule(pb) {
site: this.site
};

this.templateService = this.getTemplateService(tsOpts);
this.templateService.registerLocal('locale', this.ls.language);
this.templateService.registerLocal('error_success', function(flag, cb) {

this.ts = this.getTemplateService(tsOpts);
this.ts.registerLocal('locale', this.ls.language);
this.ts.registerLocal('error_success', function(flag, cb) {
self.displayErrorOrSuccessCallback(flag, cb);
});
this.templateService.registerLocal('page_name', function(flag, cb) {
this.ts.registerLocal('page_name', function(flag, cb) {
cb(null, self.getPageName());
});
this.templateService.registerLocal('localization_script', function(flag, cb) {
this.ts.registerLocal('localization_script', function(flag, cb) {
self.requiresClientLocalizationCallback(flag, cb);
});
this.templateService.registerLocal('analytics', function(flag, cb) {
this.ts.registerLocal('analytics', function(flag, cb) {
pb.AnalyticsManager.onPageRender(self.req, self.session, self.ls, cb);
});
this.templateService.registerLocal('wysiwyg', function(flag, cb) {
this.ts.registerLocal('wysiwyg', function(flag, cb) {
var wysiwygId = util.uniqueId();

self.templateService.registerLocal('wys_id', wysiwygId);
self.templateService.load('admin/elements/wysiwyg', function(err, data) {
self.ts.registerLocal('wys_id', wysiwygId);
self.ts.load('admin/elements/wysiwyg', function(err, data) {
cb(err, new pb.TemplateValue(data, false));
});
});
this.ts = this.templateService;


/**
*
* @property activeTheme
* @type {String}
*/
this.activeTheme = props.activeTheme;
//build out a base service context that can be cloned and passed to any

//build out a base service context that can be cloned and passed to any
//service objects
this.context = {
req: this.req,
Expand All @@ -180,17 +184,22 @@ module.exports = function BaseControllerModule(pb) {
onlyThisSite: true
};

this.siteService = new pb.SiteService();
this.siteService.getByUid(this.site, function (err, siteInfo) {
var siteService = new SiteService();
siteService.getByUid(this.site, function (err, siteInfo) {
if(pb.util.isError(err)) {
self.reqHandler.serve404();
return;
}
self.siteObj = siteInfo;
self.siteName = SiteService.isGlobal(siteInfo.uid) ? siteInfo.uid : siteInfo.displayName;
self.context.siteObj = self.siteObj;

self.templateService.registerLocal('site_root', function(flag, cb) {
cb(null, pb.SiteService.getHostWithProtocol(self.siteObj.hostname) || self.templateService.siteRoot);
self.ts.registerLocal('site_root', function(flag, cb) {
cb(null, SiteService.getHostWithProtocol(self.siteObj.hostname) || self.ts.siteRoot);
});
self.templateService.registerLocal('site_name', function(flag, cb) {
cb(null, self.siteObj.displayName || self.templateService.siteName);
self.ts.registerLocal('site_name', function(flag, cb) {
cb(null, self.siteName);
});

cb();
});
};
Expand Down
11 changes: 6 additions & 5 deletions include/service/entities/content/content_view_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ module.exports = function(pb) {
var ClientJs = pb.ClientJs;

function ContentViewLoader(context) {

this.ts = context.ts;
this.ls = context.ls;
this.req = context.req;
this.contentSettings = context.contentSettings;
this.session = context.session;
this.service = context.service;
this.site = context.site;
this.onlyThisSite = context.site;
this.siteObj = context.siteObj;
this.onlyThisSite = context.onlyThisSite;
this.activeTheme = context.activeTheme;
};

Expand Down Expand Up @@ -269,7 +269,7 @@ module.exports = function(pb) {
ContentViewLoader.prototype.onPageName = function(contentArray, options, cb) {
var content = contentArray[0];
if (!util.isObject(content)) {
return cb(null, options.metaTitle || pb.config.siteName);
return cb(null, options.metaTitle || this.siteObj.displayName);
}

var name = '';
Expand All @@ -286,7 +286,7 @@ module.exports = function(pb) {
name = options.metaTitle || '';
}

cb(null, name ? name + ' | ' + pb.config.siteName : pb.config.siteName);
cb(null, name ? name + ' | ' + this.siteObj.displayName : this.siteObj.displayName);
};

/**
Expand Down Expand Up @@ -521,7 +521,8 @@ module.exports = function(pb) {
*/
ContentViewLoader.prototype.createContentPermalink = function(content) {
var prefix = '/' + this.service.getType();
return pb.UrlService.createSystemUrl(pb.UrlService.urlJoin(prefix, content.url));
var hostname = pb.SiteService.getHostWithProtocol(this.siteObj.hostname);
return pb.UrlService.createSystemUrl(pb.UrlService.urlJoin(prefix, content.url), hostname);
};

/**
Expand Down
4 changes: 2 additions & 2 deletions include/service/entities/url_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ module.exports = function UrlServiceModule(pb) {
return util.isString(urlStr) && urlStr.indexOf('http') === 0;
};

UrlService.createSystemUrl = function(path) {
return UrlService.urlJoin(pb.config.siteRoot, path);
UrlService.createSystemUrl = function(path, hostname) {
return UrlService.urlJoin(hostname, path);
};

//exports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ module.exports = function(pb) {
};

DeleteNavItem.prototype.updateNavMap = function(removeID, cb) {
this.sectionService.removeFromSectionMap(removeID, cb);
var sectionService = new pb.SectionService(this.site, true);
sectionService.removeFromSectionMap(removeID, cb);
};

//exports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ module.exports = function(pb) {

//pb dependencies
var util = pb.util;

var BaseAdminController = pb.BaseAdminController;
/**
* Edits a nav item
*/
function EditNavItem(){}
util.inherits(EditNavItem, pb.BaseAdminController);
util.inherits(EditNavItem, BaseAdminController);

EditNavItem.prototype.init = function (props, cb) {
var self = this;
BaseAdminController.prototype.init.call(self, props, function () {
self.sectionService = new pb.SectionService(self.site, true);
cb();
});
};

EditNavItem.prototype.render = function(cb){
var self = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ module.exports = function(pb) {

//pb dependencies
var util = pb.util;
var BaseAdminController = pb.BaseAdminController;

/**
* Creates a nav item
*/
function NewNavItem(){}
util.inherits(NewNavItem, pb.BaseAdminController);
util.inherits(NewNavItem, BaseAdminController);

NewNavItem.prototype.init = function (props, cb) {
var self = this;
BaseAdminController.prototype.init.call(self, props, function () {
self.sectionService = new pb.SectionService(self.site, true);
cb();
});
};

NewNavItem.prototype.render = function(cb){
var self = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module.exports = function(pb) {
//pb dependencies
var util = pb.util;
var SectionService = pb.SectionService;

/**
* Interface for creating and editing navigation items
* @class NavItemFormController
Expand Down Expand Up @@ -83,7 +82,8 @@ module.exports = function(pb) {

//get parents
parents: function(callback) {
self.sectionService.getParentSelectList(self.pathVars.id, function(err, parents) {
var sectionService = new SectionService(self.site, true);
sectionService.getParentSelectList(self.pathVars.id, function(err, parents) {
if(util.isError(err)) {
callback(err, parents);
return;
Expand Down
2 changes: 1 addition & 1 deletion plugins/pencilblue/controllers/admin/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function LoginViewControllerModule(pb) {
}

this.setPageName(' ' + this.ls.get('LOGIN'));
this.templateService.load('admin/login', function(err, data) {
this.ts.load('admin/login', function(err, data) {
cb({content: data});
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module.exports = function(pb) {
navigation: pb.AdminNavigation.get(self.session, ['settings', 'site_settings'], self.ls),
pills: self.getAdminPills(SUB_NAV_KEY, self.ls, 'configuration', {site: self.site}),
config: config,
isGlobalSite: self.isGlobalSite
isGlobalSite: pb.SiteService.isGlobal(self.site)
});

self.setPageName(self.ls.get('CONFIGURATION'));
Expand Down

0 comments on commit 0b85a1b

Please sign in to comment.