swigFunctions.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.isEnabledLinebreaks = function() {
  22. var config = crowi.getConfig()
  23. return Config.isEnabledLinebreaks(config);
  24. }
  25. locals.isEnabledLinebreaksInComments = function() {
  26. var config = crowi.getConfig()
  27. return Config.isEnabledLinebreaksInComments(config);
  28. }
  29. locals.slackConfigured = function() {
  30. var config = crowi.getConfig()
  31. if (Config.hasSlackToken(config)) {
  32. return true;
  33. }
  34. return false;
  35. };
  36. locals.isUploadable = function() {
  37. var config = crowi.getConfig()
  38. return Config.isUploadable(config);
  39. };
  40. locals.parentPath = function(path) {
  41. if (path == '/') {
  42. return path;
  43. }
  44. if (path.match(/.+\/$/)) {
  45. return path;
  46. }
  47. return path + '/';
  48. };
  49. locals.isUserPageList = function(path) {
  50. if (path.match(/^\/user\/[^\/]+\/$/)) {
  51. return true;
  52. }
  53. return false;
  54. };
  55. locals.isTopPage = function() {
  56. var path = req.path || '';
  57. if (path === '/') {
  58. return true;
  59. }
  60. return false;
  61. };
  62. locals.isTrashPage = function() {
  63. var path = req.path || '';
  64. if (path.match(/^\/trash\/.*/)) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. locals.isDeletablePage = function() {
  70. var Page = crowi.model('Page');
  71. var path = req.path || '';
  72. return Page.isDeletableName(path);
  73. };
  74. locals.userPageRoot = function(user) {
  75. if (!user || !user.username) {
  76. return '';
  77. }
  78. return '/user/' + user.username;
  79. };
  80. locals.css = {
  81. grant: function (pageData) {
  82. if (!pageData) {
  83. return '';
  84. }
  85. switch (pageData.grant) {
  86. case Page.GRANT_PUBLIC:
  87. return 'grant-public';
  88. case Page.GRANT_RESTRICTED:
  89. return 'grant-restricted';
  90. //case Page.GRANT_SPECIFIED:
  91. // return 'grant-specified';
  92. // break;
  93. case Page.GRANT_OWNER:
  94. return 'grant-owner';
  95. default:
  96. break;
  97. }
  98. return '';
  99. },
  100. userStatus: function (user) {
  101. //debug('userStatus', user._id, user.usename, user.status);
  102. switch (user.status) {
  103. case User.STATUS_REGISTERED:
  104. return 'label-info';
  105. case User.STATUS_ACTIVE:
  106. return 'label-success';
  107. case User.STATUS_SUSPENDED:
  108. return 'label-warning';
  109. case User.STATUS_DELETED:
  110. return 'label-danger';
  111. case User.STATUS_INVITED:
  112. return 'label-info';
  113. default:
  114. break;
  115. }
  116. return '';
  117. },
  118. };
  119. };