swigFunctions.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('growi:lib:swigFunctions')
  3. , stringWidth = require('string-width')
  4. , Page = crowi.model('Page')
  5. , Config = crowi.model('Config')
  6. , User = crowi.model('User')
  7. , passportService = crowi.passportService
  8. ;
  9. debug('initializing swigFunctions');
  10. locals.nodeVersion = function() {
  11. return crowi.runtimeVersions.versions.node ? crowi.runtimeVersions.versions.node.version : '-';
  12. };
  13. locals.npmVersion = function() {
  14. return crowi.runtimeVersions.versions.npm ? crowi.runtimeVersions.versions.npm.version : '-';
  15. };
  16. locals.yarnVersion = function() {
  17. return crowi.runtimeVersions.versions.yarn ? crowi.runtimeVersions.versions.yarn.version : '-';
  18. };
  19. locals.growiVersion = function() {
  20. return crowi.version;
  21. };
  22. // token getter
  23. locals.csrf = function() {
  24. return req.csrfToken;
  25. };
  26. locals.getAppTitleFontSize = function(appTitle) {
  27. let appTitleWidth = stringWidth(appTitle);
  28. let fontSize = 22;
  29. if (appTitleWidth < 13) { /* do nothing */ }
  30. else if (appTitleWidth < 21) {
  31. fontSize -= 3 * (Math.floor((appTitleWidth - 13) / 3) + 1);
  32. }
  33. else {
  34. fontSize = 11;
  35. }
  36. return fontSize;
  37. };
  38. /**
  39. * return app title
  40. */
  41. locals.appTitle = function() {
  42. var config = crowi.getConfig();
  43. return Config.appTitle(config);
  44. };
  45. /**
  46. * return true if enabled
  47. */
  48. locals.isEnabledPassport = function() {
  49. var config = crowi.getConfig();
  50. return Config.isEnabledPassport(config);
  51. };
  52. /**
  53. * return true if local strategy has been setup successfully
  54. * used whether restarting the server needed
  55. */
  56. locals.isPassportLocalStrategySetup = function() {
  57. return passportService != null && passportService.isLocalStrategySetup;
  58. };
  59. /**
  60. * return true if enabled and strategy has been setup successfully
  61. */
  62. locals.isLdapSetup = function() {
  63. var config = crowi.getConfig();
  64. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && passportService.isLdapStrategySetup;
  65. };
  66. /**
  67. * return true if enabled but strategy has some problem
  68. */
  69. locals.isLdapSetupFailed = function() {
  70. var config = crowi.getConfig();
  71. return Config.isEnabledPassport(config) && Config.isEnabledPassportLdap(config) && !passportService.isLdapStrategySetup;
  72. };
  73. locals.googleLoginEnabled = function() {
  74. // return false if Passport is enabled
  75. // because official crowi mechanism is not used.
  76. if (locals.isEnabledPassport()) {
  77. return false;
  78. }
  79. var config = crowi.getConfig();
  80. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  81. };
  82. locals.searchConfigured = function() {
  83. if (crowi.getSearcher()) {
  84. return true;
  85. }
  86. return false;
  87. };
  88. locals.isEnabledPlugins = function() {
  89. var config = crowi.getConfig();
  90. return Config.isEnabledPlugins(config);
  91. };
  92. locals.isEnabledLinebreaks = function() {
  93. var config = crowi.getConfig();
  94. return Config.isEnabledLinebreaks(config);
  95. };
  96. locals.isEnabledLinebreaksInComments = function() {
  97. var config = crowi.getConfig();
  98. return Config.isEnabledLinebreaksInComments(config);
  99. };
  100. locals.customCss = function() {
  101. return Config.customCss();
  102. };
  103. locals.customScript = function() {
  104. return Config.customScript();
  105. };
  106. locals.customHeader = function() {
  107. var config = crowi.getConfig();
  108. return Config.customHeader(config);
  109. };
  110. locals.theme = function() {
  111. var config = crowi.getConfig();
  112. return Config.theme(config);
  113. };
  114. locals.customTitle = function(page) {
  115. const config = crowi.getConfig();
  116. return Config.customTitle(config, page);
  117. };
  118. locals.behaviorType = function() {
  119. var config = crowi.getConfig();
  120. return Config.behaviorType(config);
  121. };
  122. locals.layoutType = function() {
  123. var config = crowi.getConfig();
  124. return Config.layoutType(config);
  125. };
  126. locals.highlightJsStyle = function() {
  127. var config = crowi.getConfig();
  128. return Config.highlightJsStyle(config);
  129. };
  130. locals.highlightJsStyleBorder = function() {
  131. var config = crowi.getConfig();
  132. return Config.highlightJsStyleBorder(config);
  133. };
  134. locals.isEnabledTimeline = function() {
  135. var config = crowi.getConfig();
  136. return Config.isEnabledTimeline(config);
  137. };
  138. locals.slackConfigured = function() {
  139. var config = crowi.getConfig();
  140. if (Config.hasSlackToken(config) || Config.hasSlackIwhUrl(config)) {
  141. return true;
  142. }
  143. return false;
  144. };
  145. locals.isUploadable = function() {
  146. var config = crowi.getConfig();
  147. return Config.isUploadable(config);
  148. };
  149. locals.isEnabledAttachTitleHeader = function() {
  150. var config = crowi.getConfig();
  151. return Config.isEnabledAttachTitleHeader(config);
  152. };
  153. locals.parentPath = function(path) {
  154. if (path == '/') {
  155. return path;
  156. }
  157. if (path.match(/.+\/$/)) {
  158. return path;
  159. }
  160. return path + '/';
  161. };
  162. locals.isUserPageList = function(path) {
  163. if (path.match(/^\/user\/[^/]+\/$/)) {
  164. return true;
  165. }
  166. return false;
  167. };
  168. locals.isTopPage = function() {
  169. var path = req.path || '';
  170. if (path === '/') {
  171. return true;
  172. }
  173. return false;
  174. };
  175. locals.isTrashPage = function() {
  176. var path = req.path || '';
  177. if (path.match(/^\/trash\/.*/)) {
  178. return true;
  179. }
  180. return false;
  181. };
  182. locals.isDeletablePage = function() {
  183. var Page = crowi.model('Page');
  184. var path = req.path || '';
  185. return Page.isDeletableName(path);
  186. };
  187. locals.userPageRoot = function(user) {
  188. if (!user || !user.username) {
  189. return '';
  190. }
  191. return '/user/' + user.username;
  192. };
  193. locals.css = {
  194. grant: function(pageData) {
  195. if (!pageData) {
  196. return '';
  197. }
  198. switch (pageData.grant) {
  199. case Page.GRANT_PUBLIC:
  200. return 'grant-public';
  201. case Page.GRANT_RESTRICTED:
  202. return 'grant-restricted';
  203. //case Page.GRANT_SPECIFIED:
  204. // return 'grant-specified';
  205. // break;
  206. case Page.GRANT_OWNER:
  207. return 'grant-owner';
  208. default:
  209. break;
  210. }
  211. return '';
  212. },
  213. userStatus: function(user) {
  214. //debug('userStatus', user._id, user.usename, user.status);
  215. switch (user.status) {
  216. case User.STATUS_REGISTERED:
  217. return 'label-info';
  218. case User.STATUS_ACTIVE:
  219. return 'label-success';
  220. case User.STATUS_SUSPENDED:
  221. return 'label-warning';
  222. case User.STATUS_DELETED:
  223. return 'label-danger';
  224. case User.STATUS_INVITED:
  225. return 'label-info';
  226. default:
  227. break;
  228. }
  229. return '';
  230. },
  231. };
  232. };