2
0

Crowi.js 3.8 KB

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