2
0

Crowi.js 4.1 KB

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