swigFunctions.js 5.4 KB

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