| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- module.exports = function(crowi, req, locals) {
- const debug = require('debug')('growi:lib:swigFunctions');
- const stringWidth = require('string-width');
- const { pathUtils } = require('growi-commons');
- const Page = crowi.model('Page');
- const User = crowi.model('User');
- const {
- configManager,
- cdnResourcesService,
- passportService,
- appService,
- aclService,
- fileUploadService,
- customizeService,
- } = crowi;
- 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) {
- const 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;
- };
- /**
- * @see ConfigManager#getConfig
- */
- locals.getConfig = configManager.getConfig.bind(configManager);
- /**
- * **Do not use this unless absolutely necessary. Use getConfig instead.**
- */
- locals.getConfigFromDB = configManager.getConfigFromDB.bind(configManager);
- /**
- * **Do not use this unless absolutely necessary. Use getConfig instead.**
- */
- locals.getConfigFromEnvVars = configManager.getConfigFromEnvVars.bind(configManager);
- /**
- * pass service/utils instances to swig
- */
- locals.appService = appService;
- locals.aclService = aclService;
- locals.fileUploadService = fileUploadService;
- locals.customizeService = customizeService;
- locals.passportService = passportService;
- locals.pathUtils = pathUtils;
- locals.noCdn = function() {
- return cdnResourcesService.noCdn;
- };
- locals.cdnScriptTag = function(name) {
- return cdnResourcesService.getScriptTagByName(name);
- };
- locals.cdnScriptTagsByGroup = function(group) {
- const tags = cdnResourcesService.getScriptTagsByGroup(group);
- return tags.join('\n');
- };
- locals.cdnStyleTag = function(name) {
- return cdnResourcesService.getStyleTagByName(name);
- };
- locals.cdnStyleTagsByGroup = function(group) {
- const tags = cdnResourcesService.getStyleTagsByGroup(group);
- return tags.join('\n');
- };
- locals.cdnHighlightJsStyleTag = function(styleName) {
- return cdnResourcesService.getHighlightJsStyleTag(styleName);
- };
- /**
- * return true if enabled but strategy has some problem
- */
- locals.isLdapSetupFailed = function() {
- return (
- configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
- && !passportService.isLdapStrategySetup
- );
- };
- locals.getSamlMissingMandatoryConfigKeys = function() {
- return crowi.passportService.getSamlMissingMandatoryConfigKeys();
- };
- locals.isSearchServiceConfigured = function() {
- const { searchService } = crowi;
- return searchService.isConfigured;
- };
- locals.isHackmdSetup = function() {
- return process.env.HACKMD_URI != null;
- };
- 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() {
- const path = req.path || '';
- if (path === '/') {
- return true;
- }
- return false;
- };
- locals.isTrashPage = function() {
- const path = req.path || '';
- if (path.match(/^\/trash(\/.*)?$/)) {
- return true;
- }
- return false;
- };
- locals.isDeletablePage = function() {
- const Page = crowi.model('Page');
- const path = req.path || '';
- return Page.isDeletableName(path);
- };
- locals.userPageRoot = function(user) {
- if (!user || !user.username) {
- return '';
- }
- return `/user/${user.username}`;
- };
- locals.pagesDataForTimeline = function(pages) {
- return pages.map((page) => {
- return {
- id: page.id,
- path: page.path,
- revision: page.revision,
- };
- });
- };
- locals.css = {
- grant(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(user) {
- 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 '';
- },
- };
- };
|