Crowi.js 3.8 KB

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