swigFunctions.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. module.exports = function(crowi, req, locals) {
  2. const debug = require('debug')('growi:lib:swigFunctions');
  3. const stringWidth = require('string-width');
  4. const { pathUtils } = require('@growi/core');
  5. const Page = crowi.model('Page');
  6. const User = crowi.model('User');
  7. const {
  8. configManager,
  9. cdnResourcesService,
  10. passportService,
  11. appService,
  12. aclService,
  13. customizeService,
  14. pageService,
  15. } = crowi;
  16. debug('initializing swigFunctions');
  17. locals.nodeVersion = function() {
  18. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  19. };
  20. locals.npmVersion = function() {
  21. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  22. };
  23. locals.yarnVersion = function() {
  24. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  25. };
  26. locals.getAppTitleFontSize = function(appTitle) {
  27. const appTitleWidth = stringWidth(appTitle);
  28. let fontSize = 22;
  29. if (appTitleWidth < 13) { /* do nothing */ }
  30. else if (appTitleWidth < 21) {
  31. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  32. }
  33. else {
  34. fontSize = 11;
  35. }
  36. return fontSize;
  37. };
  38. /**
  39. * @see ConfigManager#getConfig
  40. */
  41. locals.getConfig = configManager.getConfig.bind(configManager);
  42. /**
  43. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  44. */
  45. locals.getConfigFromDB = configManager.getConfigFromDB.bind(configManager);
  46. /**
  47. * **Do not use this unless absolutely necessary. Use getConfig instead.**
  48. */
  49. locals.getConfigFromEnvVars = configManager.getConfigFromEnvVars.bind(configManager);
  50. /**
  51. * pass service/utils instances to swig
  52. */
  53. locals.appService = appService;
  54. locals.aclService = aclService;
  55. locals.customizeService = customizeService;
  56. locals.passportService = passportService;
  57. locals.pageService = pageService;
  58. locals.pathUtils = pathUtils;
  59. locals.noCdn = function() {
  60. return cdnResourcesService.noCdn;
  61. };
  62. locals.cdnScriptTag = function(name) {
  63. return cdnResourcesService.getScriptTagByName(name);
  64. };
  65. locals.cdnScriptTagsByGroup = function(group) {
  66. const tags = cdnResourcesService.getScriptTagsByGroup(group);
  67. return tags.join('\n');
  68. };
  69. locals.cdnStyleTag = function(name) {
  70. return cdnResourcesService.getStyleTagByName(name);
  71. };
  72. locals.cdnStyleTagsByGroup = function(group) {
  73. const tags = cdnResourcesService.getStyleTagsByGroup(group);
  74. return tags.join('\n');
  75. };
  76. locals.cdnHighlightJsStyleTag = function(styleName) {
  77. return cdnResourcesService.getHighlightJsStyleTag(styleName);
  78. };
  79. /**
  80. * return true if enabled but strategy has some problem
  81. */
  82. locals.isLdapSetupFailed = function() {
  83. return (
  84. configManager.getConfig('crowi', 'security:passport-ldap:isEnabled')
  85. && !passportService.isLdapStrategySetup
  86. );
  87. };
  88. locals.getSamlMissingMandatoryConfigKeys = function() {
  89. return crowi.passportService.getSamlMissingMandatoryConfigKeys();
  90. };
  91. locals.isHackmdSetup = function() {
  92. return process.env.HACKMD_URI != null;
  93. };
  94. locals.parentPath = function(path) {
  95. if (path === '/') {
  96. return path;
  97. }
  98. if (path.match(/.+\/$/)) {
  99. return path;
  100. }
  101. return `${path}/`;
  102. };
  103. locals.isUserPageList = function(path) {
  104. if (path.match(/^\/user\/[^/]+\/$/)) {
  105. return true;
  106. }
  107. return false;
  108. };
  109. locals.isTopPage = function() {
  110. const path = req.path || '';
  111. if (path === '/') {
  112. return true;
  113. }
  114. return false;
  115. };
  116. locals.isTrashPage = function(path = '') {
  117. if (path.match(/^\/trash(\/.*)?$/)) {
  118. return true;
  119. }
  120. return false;
  121. };
  122. locals.userPageRoot = function(user) {
  123. if (!user || !user.username) {
  124. return '';
  125. }
  126. return `/user/${user.username}`;
  127. };
  128. locals.pagesDataForTimeline = function(pages) {
  129. return pages.map((page) => {
  130. return {
  131. id: page.id,
  132. path: page.path,
  133. revision: page.revision,
  134. };
  135. });
  136. };
  137. locals.attachTitleHeader = function(path) {
  138. return pathUtils.attachTitleHeader(path);
  139. };
  140. locals.css = {
  141. grant(pageData) {
  142. if (!pageData) {
  143. return '';
  144. }
  145. switch (pageData.grant) {
  146. case Page.GRANT_PUBLIC:
  147. return 'grant-public';
  148. case Page.GRANT_RESTRICTED:
  149. return 'grant-restricted';
  150. // case Page.GRANT_SPECIFIED:
  151. // return 'grant-specified';
  152. // break;
  153. case Page.GRANT_OWNER:
  154. return 'grant-owner';
  155. default:
  156. break;
  157. }
  158. return '';
  159. },
  160. userStatus(user) {
  161. switch (user.status) {
  162. case User.STATUS_REGISTERED:
  163. return 'label-info';
  164. case User.STATUS_ACTIVE:
  165. return 'label-success';
  166. case User.STATUS_SUSPENDED:
  167. return 'label-warning';
  168. case User.STATUS_DELETED:
  169. return 'label-danger';
  170. case User.STATUS_INVITED:
  171. return 'label-info';
  172. default:
  173. break;
  174. }
  175. return '';
  176. },
  177. };
  178. };