Crowi.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Crowi context class for client
  3. */
  4. import axios from 'axios'
  5. import InterceptorManager from '../../../lib/util/interceptorManager';
  6. export default class Crowi {
  7. constructor(context, window) {
  8. this.context = context;
  9. this.location = window.location || {};
  10. this.document = window.document || {};
  11. this.localStorage = window.localStorage || {};
  12. this.fetchUsers = this.fetchUsers.bind(this);
  13. this.apiGet = this.apiGet.bind(this);
  14. this.apiPost = this.apiPost.bind(this);
  15. this.apiRequest = this.apiRequest.bind(this);
  16. this.interceptorManager = new InterceptorManager();
  17. // FIXME
  18. this.me = context.me;
  19. this.users = [];
  20. this.userByName = {};
  21. this.userById = {};
  22. this.draft = {};
  23. this.recoverData();
  24. }
  25. /**
  26. * @return {Object} window.Crowi (/resource/js/crowi.js)
  27. */
  28. getCrowiForJquery() {
  29. return window.Crowi;
  30. }
  31. getContext() {
  32. return context;
  33. }
  34. recoverData() {
  35. const keys = [
  36. 'userByName',
  37. 'userById',
  38. 'users',
  39. 'draft',
  40. ];
  41. keys.forEach(key => {
  42. if (this.localStorage[key]) {
  43. try {
  44. this[key] = JSON.parse(this.localStorage[key]);
  45. } catch (e) {
  46. this.localStorage.removeItem(key);
  47. }
  48. }
  49. });
  50. }
  51. fetchUsers () {
  52. const interval = 1000*60*15; // 15min
  53. const currentTime = new Date();
  54. if (!this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  55. return ;
  56. }
  57. this.apiGet('/users.list', {})
  58. .then(data => {
  59. this.users = data.users;
  60. this.localStorage.users = JSON.stringify(data.users);
  61. let userByName = {};
  62. let userById = {};
  63. for (let i = 0; i < data.users.length; i++) {
  64. const user = data.users[i];
  65. userByName[user.username] = user;
  66. userById[user._id] = user;
  67. }
  68. this.userByName = userByName;
  69. this.localStorage.userByName = JSON.stringify(userByName);
  70. this.userById = userById;
  71. this.localStorage.userById = JSON.stringify(userById);
  72. this.localStorage.lastFetched = new Date();
  73. }).catch(err => {
  74. this.localStorage.removeItem('lastFetched');
  75. // ignore errors
  76. });
  77. }
  78. clearDraft(path) {
  79. delete this.draft[path];
  80. this.localStorage.draft = JSON.stringify(this.draft);
  81. }
  82. saveDraft(path, body) {
  83. this.draft[path] = body;
  84. this.localStorage.draft = JSON.stringify(this.draft);
  85. }
  86. findDraft(path) {
  87. if (this.draft && this.draft[path]) {
  88. return this.draft[path];
  89. }
  90. return null;
  91. }
  92. findUserById(userId) {
  93. if (this.userById && this.userById[userId]) {
  94. return this.userById[userId];
  95. }
  96. return null;
  97. }
  98. findUser(username) {
  99. if (this.userByName && this.userByName[username]) {
  100. return this.userByName[username];
  101. }
  102. return null;
  103. }
  104. apiGet(path, params) {
  105. return this.apiRequest('get', path, params);
  106. }
  107. apiPost(path, params) {
  108. return this.apiRequest('post', path, params);
  109. }
  110. apiRequest(method, path, params) {
  111. return new Promise((resolve, reject) => {
  112. axios[method](`/_api${path}`, {params})
  113. .then(res => {
  114. if (res.data.ok) {
  115. resolve(res.data);
  116. } else {
  117. reject(new Error(res.data));
  118. }
  119. })
  120. .catch(res => {
  121. reject(res);
  122. });
  123. });
  124. }
  125. static escape (html, encode) {
  126. return html
  127. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  128. .replace(/</g, '&lt;')
  129. .replace(/>/g, '&gt;')
  130. .replace(/"/g, '&quot;')
  131. .replace(/'/g, '&#39;');
  132. }
  133. static unescape(html) {
  134. return html.replace(/&([#\w]+);/g, function(_, n) {
  135. n = n.toLowerCase();
  136. if (n === 'colon') return ':';
  137. if (n.charAt(0) === '#') {
  138. return n.charAt(1) === 'x'
  139. ? String.fromCharCode(parseInt(n.substring(2), 16))
  140. : String.fromCharCode(+n.substring(1));
  141. }
  142. return '';
  143. });
  144. }
  145. }