swigFunctions.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. module.exports = function(crowi, app, 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.searchConfigured = function() {
  99. if (crowi.getSearcher()) {
  100. return true;
  101. }
  102. return false;
  103. };
  104. locals.isHackmdSetup = function() {
  105. return process.env.HACKMD_URI != null;
  106. };
  107. locals.parentPath = function(path) {
  108. if (path === '/') {
  109. return path;
  110. }
  111. if (path.match(/.+\/$/)) {
  112. return path;
  113. }
  114. return `${path}/`;
  115. };
  116. locals.isUserPageList = function(path) {
  117. if (path.match(/^\/user\/[^/]+\/$/)) {
  118. return true;
  119. }
  120. return false;
  121. };
  122. locals.isTopPage = function() {
  123. const path = req.path || '';
  124. if (path === '/') {
  125. return true;
  126. }
  127. return false;
  128. };
  129. locals.isTrashPage = function() {
  130. const path = req.path || '';
  131. if (path.match(/^\/trash\/.*/)) {
  132. return true;
  133. }
  134. return false;
  135. };
  136. locals.isDeletablePage = function() {
  137. const Page = crowi.model('Page');
  138. const path = req.path || '';
  139. return Page.isDeletableName(path);
  140. };
  141. locals.userPageRoot = function(user) {
  142. if (!user || !user.username) {
  143. return '';
  144. }
  145. return `/user/${user.username}`;
  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. };