swigFunctions.js 5.2 KB

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