Crowi.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. 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: params});
  106. }
  107. apiPost(path, params) {
  108. if (!params._csrf) {
  109. params._csrf = this.csrfToken;
  110. }
  111. return this.apiRequest('post', path, params);
  112. }
  113. apiRequest(method, path, params) {
  114. return new Promise((resolve, reject) => {
  115. axios[method](`/_api${path}`, params)
  116. .then(res => {
  117. if (res.data.ok) {
  118. resolve(res.data);
  119. } else {
  120. // FIXME?
  121. reject(new Error(res.error));
  122. }
  123. }).catch(res => {
  124. // FIXME?
  125. reject(new Error('Error'));
  126. });
  127. });
  128. }
  129. static escape (html, encode) {
  130. return html
  131. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  132. .replace(/</g, '&lt;')
  133. .replace(/>/g, '&gt;')
  134. .replace(/"/g, '&quot;')
  135. .replace(/'/g, '&#39;');
  136. }
  137. static unescape(html) {
  138. return html.replace(/&([#\w]+);/g, function(_, n) {
  139. n = n.toLowerCase();
  140. if (n === 'colon') return ':';
  141. if (n.charAt(0) === '#') {
  142. return n.charAt(1) === 'x'
  143. ? String.fromCharCode(parseInt(n.substring(2), 16))
  144. : String.fromCharCode(+n.substring(1));
  145. }
  146. return '';
  147. });
  148. }
  149. }