swigFunctions.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. module.exports = function(crowi, app, req, locals) {
  2. var debug = require('debug')('crowi:lib:swigFunctions')
  3. , Page = crowi.model('Page')
  4. , Config = crowi.model('Config')
  5. , User = crowi.model('User')
  6. ;
  7. // token getter
  8. locals.csrf = function() {
  9. return req.csrfToken;
  10. };
  11. locals.googleLoginEnabled = function() {
  12. var config = crowi.getConfig()
  13. return config.crowi['google:clientId'] && config.crowi['google:clientSecret'];
  14. };
  15. locals.searchConfigured = function() {
  16. if (crowi.getSearcher()) {
  17. return true;
  18. }
  19. return false;
  20. };
  21. locals.isEnabledPlugins = function() {
  22. var config = crowi.getConfig()
  23. return Config.isEnabledPlugins(config);
  24. }
  25. locals.isEnabledLinebreaks = function() {
  26. var config = crowi.getConfig()
  27. return Config.isEnabledLinebreaks(config);
  28. }
  29. locals.isEnabledLinebreaksInComments = function() {
  30. var config = crowi.getConfig()
  31. return Config.isEnabledLinebreaksInComments(config);
  32. }
  33. locals.customCss = function() {
  34. return Config.customCss();
  35. }
  36. locals.slackConfigured = function() {
  37. var config = crowi.getConfig()
  38. if (Config.hasSlackToken(config)) {
  39. return true;
  40. }
  41. return false;
  42. };
  43. locals.isUploadable = function() {
  44. var config = crowi.getConfig()
  45. return Config.isUploadable(config);
  46. };
  47. locals.parentPath = function(path) {
  48. if (path == '/') {
  49. return path;
  50. }
  51. if (path.match(/.+\/$/)) {
  52. return path;
  53. }
  54. return path + '/';
  55. };
  56. locals.isUserPageList = function(path) {
  57. if (path.match(/^\/user\/[^\/]+\/$/)) {
  58. return true;
  59. }
  60. return false;
  61. };
  62. locals.isTopPage = function() {
  63. var path = req.path || '';
  64. if (path === '/') {
  65. return true;
  66. }
  67. return false;
  68. };
  69. locals.isTrashPage = function() {
  70. var path = req.path || '';
  71. if (path.match(/^\/trash\/.*/)) {
  72. return true;
  73. }
  74. return false;
  75. };
  76. locals.isDeletablePage = function() {
  77. var Page = crowi.model('Page');
  78. var path = req.path || '';
  79. return Page.isDeletableName(path);
  80. };
  81. locals.userPageRoot = function(user) {
  82. if (!user || !user.username) {
  83. return '';
  84. }
  85. return '/user/' + user.username;
  86. };
  87. locals.css = {
  88. grant: function (pageData) {
  89. if (!pageData) {
  90. return '';
  91. }
  92. switch (pageData.grant) {
  93. case Page.GRANT_PUBLIC:
  94. return 'grant-public';
  95. case Page.GRANT_RESTRICTED:
  96. return 'grant-restricted';
  97. //case Page.GRANT_SPECIFIED:
  98. // return 'grant-specified';
  99. // break;
  100. case Page.GRANT_OWNER:
  101. return 'grant-owner';
  102. default:
  103. break;
  104. }
  105. return '';
  106. },
  107. userStatus: function (user) {
  108. //debug('userStatus', user._id, user.usename, user.status);
  109. switch (user.status) {
  110. case User.STATUS_REGISTERED:
  111. return 'label-info';
  112. case User.STATUS_ACTIVE:
  113. return 'label-success';
  114. case User.STATUS_SUSPENDED:
  115. return 'label-warning';
  116. case User.STATUS_DELETED:
  117. return 'label-danger';
  118. case User.STATUS_INVITED:
  119. return 'label-info';
  120. default:
  121. break;
  122. }
  123. return '';
  124. },
  125. };
  126. };