swigFunctions.js 5.2 KB

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