Crowi.js 3.7 KB

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