Crowi.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.location = window.location || {};
  11. this.document = window.document || {};
  12. this.localStorage = window.localStorage || {};
  13. this.fetchUsers = this.fetchUsers.bind(this);
  14. this.apiGet = this.apiGet.bind(this);
  15. this.apiPost = this.apiPost.bind(this);
  16. this.apiRequest = this.apiRequest.bind(this);
  17. // FIXME
  18. this.me = context.me;
  19. this.users = [];
  20. this.userByName = {};
  21. this.userById = {};
  22. this.draft = {};
  23. this.recoverData();
  24. }
  25. getContext() {
  26. return context;
  27. }
  28. setConfig(config) {
  29. this.config = config;
  30. }
  31. getConfig() {
  32. return this.config;
  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. findUserByIds(userIds) {
  99. let users = [];
  100. for (let userId of userIds) {
  101. let user = this.findUserById(userId);
  102. if (user) {
  103. users.push(user);
  104. }
  105. }
  106. return users;
  107. }
  108. findUser(username) {
  109. if (this.userByName && this.userByName[username]) {
  110. return this.userByName[username];
  111. }
  112. return null;
  113. }
  114. apiGet(path, params) {
  115. return this.apiRequest('get', path, {params: params});
  116. }
  117. apiPost(path, params) {
  118. if (!params._csrf) {
  119. params._csrf = this.csrfToken;
  120. }
  121. return this.apiRequest('post', path, params);
  122. }
  123. apiRequest(method, path, params) {
  124. return new Promise((resolve, reject) => {
  125. axios[method](`/_api${path}`, params)
  126. .then(res => {
  127. if (res.data.ok) {
  128. resolve(res.data);
  129. } else {
  130. // FIXME?
  131. reject(new Error(res.error));
  132. }
  133. }).catch(res => {
  134. // FIXME?
  135. reject(new Error('Error'));
  136. });
  137. });
  138. }
  139. static escape (html, encode) {
  140. return html
  141. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  142. .replace(/</g, '&lt;')
  143. .replace(/>/g, '&gt;')
  144. .replace(/"/g, '&quot;')
  145. .replace(/'/g, '&#39;');
  146. }
  147. static unescape(html) {
  148. return html.replace(/&([#\w]+);/g, function(_, n) {
  149. n = n.toLowerCase();
  150. if (n === 'colon') return ':';
  151. if (n.charAt(0) === '#') {
  152. return n.charAt(1) === 'x'
  153. ? String.fromCharCode(parseInt(n.substring(2), 16))
  154. : String.fromCharCode(+n.substring(1));
  155. }
  156. return '';
  157. });
  158. }
  159. }