| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- module.exports = function(crowi, app, req, locals) {
- var debug = require('debug')('growi:lib:swigFunctions')
- , stringWidth = require('string-width')
- , Page = crowi.model('Page')
- , Config = crowi.model('Config')
- , User = crowi.model('User')
- , passportService = crowi.passportService
- ;
- debug('initializing swigFunctions');
- locals.nodeVersion = function() {
- return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
- };
- locals.npmVersion = function() {
- return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
- };
- locals.yarnVersion = function() {
- return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
- };
- locals.growiVersion = function() {
- return crowi.version;
- };
- // token getter
- locals.csrf = function() {
- return req.csrfToken;
- };
- locals.getAppTitleFontSize = function(appTitle) {
- let appTitleWidth = stringWidth(appTitle);
- let fontSize = 22;
- if (appTitleWidth < 13) { /* do nothing */ }
- else if (appTitleWidth < 21) {
- fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
- }
- else {
- fontSize = 11;
- }
- return fontSize;
- };
- /**
- * return app title
- */
- locals.appTitle = function() {
- var config = crowi.getConfig();
- return Config.appTitle(config);
- };
- /**
- * return true if enabled
- */
- locals.isEnabledPassport = function() {
- var config = crowi.getConfig();
- return Config.isEnabledPassport(config);
- };
- /**
- * return true if local strategy has been setup successfully
- * used whether restarting the server needed
- */
- locals.isPassportLocalStrategySetup = function() {
- return passportService != null && passportService.isLocalStrategySetup;
- };
- /**
- * return true if enabled and strategy has been setup successfully
- */
- locals.isLdapSetup = function() {
- var config = crowi.getConfig();
- return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
- };
- /**
- * return true if enabled but strategy has some problem
- */
- locals.isLdapSetupFailed = function() {
- var config = crowi.getConfig();
- return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
- };
- locals.googleLoginEnabled = function() {
- // return false if Passport is enabled
- // because official crowi mechanism is not used.
- if (locals.isEnabledPassport()) {
- return false;
- }
- var config = crowi.getConfig();
- return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
- };
- locals.passportGoogleLoginEnabled = function() {
- var config = crowi.getConfig();
- return locals.isEnabledPassport() && config.crowi['security:passport-google:isEnabled'];
- };
- locals.passportGitHubLoginEnabled = function() {
- var config = crowi.getConfig();
- return locals.isEnabledPassport() && config.crowi['security:passport-github:isEnabled'];
- };
- locals.searchConfigured = function() {
- if (crowi.getSearcher()) {
- return true;
- }
- return false;
- };
- locals.isEnabledPlugins = function() {
- var config = crowi.getConfig();
- return Config.isEnabledPlugins(config);
- };
- locals.isEnabledLinebreaks = function() {
- var config = crowi.getConfig();
- return Config.isEnabledLinebreaks(config);
- };
- locals.isEnabledLinebreaksInComments = function() {
- var config = crowi.getConfig();
- return Config.isEnabledLinebreaksInComments(config);
- };
- locals.customCss = function() {
- return Config.customCss();
- };
- locals.customScript = function() {
- return Config.customScript();
- };
- locals.customHeader = function() {
- var config = crowi.getConfig();
- return Config.customHeader(config);
- };
- locals.theme = function() {
- var config = crowi.getConfig();
- return Config.theme(config);
- };
- locals.customTitle = function(page) {
- const config = crowi.getConfig();
- return Config.customTitle(config, page);
- };
- locals.behaviorType = function() {
- var config = crowi.getConfig();
- return Config.behaviorType(config);
- };
- locals.layoutType = function() {
- var config = crowi.getConfig();
- return Config.layoutType(config);
- };
- locals.highlightJsStyle = function() {
- var config = crowi.getConfig();
- return Config.highlightJsStyle(config);
- };
- locals.highlightJsStyleBorder = function() {
- var config = crowi.getConfig();
- return Config.highlightJsStyleBorder(config);
- };
- locals.isEnabledTimeline = function() {
- var config = crowi.getConfig();
- return Config.isEnabledTimeline(config);
- };
- locals.slackConfigured = function() {
- var config = crowi.getConfig();
- if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
- return true;
- }
- return false;
- };
- locals.isUploadable = function() {
- var config = crowi.getConfig();
- return Config.isUploadable(config);
- };
- locals.isEnabledAttachTitleHeader = function() {
- var config = crowi.getConfig();
- return Config.isEnabledAttachTitleHeader(config);
- };
- locals.parentPath = function(path) {
- if (path == '/') {
- return path;
- }
- if (path.match(/.+\/$/)) {
- return path;
- }
- return path + '/';
- };
- locals.isUserPageList = function(path) {
- if (path.match(/^\/user\/[^/]+\/$/)) {
- return true;
- }
- return false;
- };
- locals.isTopPage = function() {
- var path = req.path || '';
- if (path === '/') {
- return true;
- }
- return false;
- };
- locals.isTrashPage = function() {
- var path = req.path || '';
- if (path.match(/^\/trash\/.*/)) {
- return true;
- }
- return false;
- };
- locals.isDeletablePage = function() {
- var Page = crowi.model('Page');
- var path = req.path || '';
- return Page.isDeletableName(path);
- };
- locals.userPageRoot = function(user) {
- if (!user || !user.username) {
- return '';
- }
- return '/user/' + user.username;
- };
- locals.css = {
- grant: function(pageData) {
- if (!pageData) {
- return '';
- }
- switch (pageData.grant) {
- case Page.GRANT_PUBLIC:
- return 'grant-public';
- case Page.GRANT_RESTRICTED:
- return 'grant-restricted';
- //case Page.GRANT_SPECIFIED:
- // return 'grant-specified';
- // break;
- case Page.GRANT_OWNER:
- return 'grant-owner';
- default:
- break;
- }
- return '';
- },
- userStatus: function(user) {
- //debug('userStatus', user._id, user.usename, user.status);
- switch (user.status) {
- case User.STATUS_REGISTERED:
- return 'label-info';
- case User.STATUS_ACTIVE:
- return 'label-success';
- case User.STATUS_SUSPENDED:
- return 'label-warning';
- case User.STATUS_DELETED:
- return 'label-danger';
- case User.STATUS_INVITED:
- return 'label-info';
- default:
- break;
- }
- return '';
- },
- };
- };
|