Crowi.js 4.2 KB

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