Crowi.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Crowi context class for client
  3. */
  4. import axios from 'axios'
  5. import InterceptorManager from '../../../lib/util/interceptor-manager';
  6. import {
  7. DetachCodeBlockInterceptor,
  8. RestoreCodeBlockInterceptor,
  9. } from './interceptor/detach-code-blocks';
  10. export default class Crowi {
  11. constructor(context, window) {
  12. this.context = context;
  13. this.config = {};
  14. this.csrfToken = context.csrfToken;
  15. this.window = window;
  16. this.location = window.location || {};
  17. this.document = window.document || {};
  18. this.localStorage = window.localStorage || {};
  19. this.componentInstances = undefined;
  20. this.fetchUsers = this.fetchUsers.bind(this);
  21. this.apiGet = this.apiGet.bind(this);
  22. this.apiPost = this.apiPost.bind(this);
  23. this.apiRequest = this.apiRequest.bind(this);
  24. this.interceptorManager = new InterceptorManager();
  25. this.interceptorManager.addInterceptors([
  26. new DetachCodeBlockInterceptor(this),
  27. new RestoreCodeBlockInterceptor(this),
  28. ]);
  29. // FIXME
  30. this.me = context.me;
  31. this.users = [];
  32. this.userByName = {};
  33. this.userById = {};
  34. this.draft = {};
  35. this.recoverData();
  36. }
  37. /**
  38. * @return {Object} window.Crowi (/resource/js/crowi.js)
  39. */
  40. getCrowiForJquery() {
  41. return window.Crowi;
  42. }
  43. getContext() {
  44. return context;
  45. }
  46. setConfig(config) {
  47. this.config = config;
  48. }
  49. getConfig() {
  50. return this.config;
  51. }
  52. recoverData() {
  53. const keys = [
  54. 'userByName',
  55. 'userById',
  56. 'users',
  57. 'draft',
  58. ];
  59. keys.forEach(key => {
  60. if (this.localStorage[key]) {
  61. try {
  62. this[key] = JSON.parse(this.localStorage[key]);
  63. } catch (e) {
  64. this.localStorage.removeItem(key);
  65. }
  66. }
  67. });
  68. }
  69. fetchUsers () {
  70. const interval = 1000*60*15; // 15min
  71. const currentTime = new Date();
  72. if (this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  73. return ;
  74. }
  75. this.apiGet('/users.list', {})
  76. .then(data => {
  77. this.users = data.users;
  78. this.localStorage.users = JSON.stringify(data.users);
  79. let userByName = {};
  80. let userById = {};
  81. for (let i = 0; i < data.users.length; i++) {
  82. const user = data.users[i];
  83. userByName[user.username] = user;
  84. userById[user._id] = user;
  85. }
  86. this.userByName = userByName;
  87. this.localStorage.userByName = JSON.stringify(userByName);
  88. this.userById = userById;
  89. this.localStorage.userById = JSON.stringify(userById);
  90. this.localStorage.lastFetched = new Date();
  91. }).catch(err => {
  92. this.localStorage.removeItem('lastFetched');
  93. // ignore errors
  94. });
  95. }
  96. clearDraft(path) {
  97. delete this.draft[path];
  98. this.localStorage.draft = JSON.stringify(this.draft);
  99. }
  100. saveDraft(path, body) {
  101. this.draft[path] = body;
  102. this.localStorage.draft = JSON.stringify(this.draft);
  103. }
  104. findDraft(path) {
  105. if (this.draft && this.draft[path]) {
  106. return this.draft[path];
  107. }
  108. return null;
  109. }
  110. findUserById(userId) {
  111. if (this.userById && this.userById[userId]) {
  112. return this.userById[userId];
  113. }
  114. return null;
  115. }
  116. findUserByIds(userIds) {
  117. let users = [];
  118. for (let userId of userIds) {
  119. let user = this.findUserById(userId);
  120. if (user) {
  121. users.push(user);
  122. }
  123. }
  124. return users;
  125. }
  126. findUser(username) {
  127. if (this.userByName && this.userByName[username]) {
  128. return this.userByName[username];
  129. }
  130. return null;
  131. }
  132. apiGet(path, params) {
  133. return this.apiRequest('get', path, {params: params});
  134. }
  135. apiPost(path, params) {
  136. if (!params._csrf) {
  137. params._csrf = this.csrfToken;
  138. }
  139. return this.apiRequest('post', path, params);
  140. }
  141. apiRequest(method, path, params) {
  142. return new Promise((resolve, reject) => {
  143. axios[method](`/_api${path}`, params)
  144. .then(res => {
  145. if (res.data.ok) {
  146. resolve(res.data);
  147. } else {
  148. reject(new Error(res.data.error));
  149. }
  150. })
  151. .catch(res => {
  152. reject(res);
  153. });
  154. });
  155. }
  156. }