swigFunctions.js 2.7 KB

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