swigFunctions.js 2.9 KB

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