swigFunctions.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-commons');
  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. fileUploadService,
  14. customizeService,
  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.fileUploadService = fileUploadService;
  63. locals.customizeService = customizeService;
  64. locals.passportService = passportService;
  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.isSearchServiceConfigured = function() {
  99. const { searchService } = crowi;
  100. return searchService.isConfigured;
  101. };
  102. locals.isHackmdSetup = function() {
  103. return process.env.HACKMD_URI != null;
  104. };
  105. locals.parentPath = function(path) {
  106. if (path === '/') {
  107. return path;
  108. }
  109. if (path.match(/.+\/$/)) {
  110. return path;
  111. }
  112. return `${path}/`;
  113. };
  114. locals.isUserPageList = function(path) {
  115. if (path.match(/^\/user\/[^/]+\/$/)) {
  116. return true;
  117. }
  118. return false;
  119. };
  120. locals.isTopPage = function() {
  121. const path = req.path || '';
  122. if (path === '/') {
  123. return true;
  124. }
  125. return false;
  126. };
  127. locals.isTrashPage = function() {
  128. const path = req.path || '';
  129. if (path.match(/^\/trash(\/.*)?$/)) {
  130. return true;
  131. }
  132. return false;
  133. };
  134. locals.isDeletablePage = function() {
  135. const Page = crowi.model('Page');
  136. const path = req.path || '';
  137. return Page.isDeletableName(path);
  138. };
  139. locals.userPageRoot = function(user) {
  140. if (!user || !user.username) {
  141. return '';
  142. }
  143. return `/user/${user.username}`;
  144. };
  145. locals.pagesDataForTimeline = function(pages) {
  146. return pages.map((page) => {
  147. return {
  148. id: page.id,
  149. path: page.path,
  150. revision: page.revision,
  151. };
  152. });
  153. };
  154. locals.css = {
  155. grant(pageData) {
  156. if (!pageData) {
  157. return '';
  158. }
  159. switch (pageData.grant) {
  160. case Page.GRANT_PUBLIC:
  161. return 'grant-public';
  162. case Page.GRANT_RESTRICTED:
  163. return 'grant-restricted';
  164. // case Page.GRANT_SPECIFIED:
  165. // return 'grant-specified';
  166. // break;
  167. case Page.GRANT_OWNER:
  168. return 'grant-owner';
  169. default:
  170. break;
  171. }
  172. return '';
  173. },
  174. userStatus(user) {
  175. switch (user.status) {
  176. case User.STATUS_REGISTERED:
  177. return 'label-info';
  178. case User.STATUS_ACTIVE:
  179. return 'label-success';
  180. case User.STATUS_SUSPENDED:
  181. return 'label-warning';
  182. case User.STATUS_DELETED:
  183. return 'label-danger';
  184. case User.STATUS_INVITED:
  185. return 'label-info';
  186. default:
  187. break;
  188. }
  189. return '';
  190. },
  191. };
  192. };