Crowi.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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.pageEditor = 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.editorOptions = {};
  36. this.userRelatedGroups = {};
  37. this.recoverData();
  38. }
  39. /**
  40. * @return {Object} window.Crowi (/resource/js/crowi.js)
  41. */
  42. getCrowiForJquery() {
  43. return window.Crowi;
  44. }
  45. getContext() {
  46. return context;
  47. }
  48. setConfig(config) {
  49. this.config = config;
  50. }
  51. getConfig() {
  52. return this.config;
  53. }
  54. setPageEditor(pageEditor) {
  55. this.pageEditor = pageEditor;
  56. }
  57. recoverData() {
  58. const keys = [
  59. 'userByName',
  60. 'userById',
  61. 'users',
  62. 'draft',
  63. 'editorOptions',
  64. 'previewOptions',
  65. ];
  66. keys.forEach(key => {
  67. if (this.localStorage[key]) {
  68. try {
  69. this[key] = JSON.parse(this.localStorage[key]);
  70. } catch (e) {
  71. this.localStorage.removeItem(key);
  72. }
  73. }
  74. });
  75. }
  76. fetchUsers () {
  77. const interval = 1000*60*15; // 15min
  78. const currentTime = new Date();
  79. if (this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  80. return ;
  81. }
  82. this.apiGet('/users.list', {})
  83. .then(data => {
  84. this.users = data.users;
  85. this.localStorage.users = JSON.stringify(data.users);
  86. let userByName = {};
  87. let userById = {};
  88. for (let i = 0; i < data.users.length; i++) {
  89. const user = data.users[i];
  90. userByName[user.username] = user;
  91. userById[user._id] = user;
  92. }
  93. this.userByName = userByName;
  94. this.localStorage.userByName = JSON.stringify(userByName);
  95. this.userById = userById;
  96. this.localStorage.userById = JSON.stringify(userById);
  97. this.localStorage.lastFetched = new Date();
  98. }).catch(err => {
  99. this.localStorage.removeItem('lastFetched');
  100. // ignore errors
  101. });
  102. }
  103. setCaretLine(line) {
  104. if (this.pageEditor != null) {
  105. this.pageEditor.setCaretLine(line);
  106. }
  107. }
  108. focusToEditor() {
  109. if (this.pageEditor != null) {
  110. this.pageEditor.focusToEditor();
  111. }
  112. }
  113. clearDraft(path) {
  114. delete this.draft[path];
  115. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  116. }
  117. saveDraft(path, body) {
  118. this.draft[path] = body;
  119. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  120. }
  121. findDraft(path) {
  122. if (this.draft && this.draft[path]) {
  123. return this.draft[path];
  124. }
  125. return null;
  126. }
  127. saveEditorOptions(options) {
  128. this.localStorage.setItem('editorOptions', JSON.stringify(options));
  129. }
  130. savePreviewOptions(options) {
  131. this.localStorage.setItem('previewOptions', JSON.stringify(options));
  132. }
  133. findUserById(userId) {
  134. if (this.userById && this.userById[userId]) {
  135. return this.userById[userId];
  136. }
  137. return null;
  138. }
  139. findUserByIds(userIds) {
  140. let users = [];
  141. for (let userId of userIds) {
  142. let user = this.findUserById(userId);
  143. if (user) {
  144. users.push(user);
  145. }
  146. }
  147. return users;
  148. }
  149. findUser(username) {
  150. if (this.userByName && this.userByName[username]) {
  151. return this.userByName[username];
  152. }
  153. return null;
  154. }
  155. apiGet(path, params) {
  156. return this.apiRequest('get', path, {params: params});
  157. }
  158. apiPost(path, params) {
  159. if (!params._csrf) {
  160. params._csrf = this.csrfToken;
  161. }
  162. return this.apiRequest('post', path, params);
  163. }
  164. apiRequest(method, path, params) {
  165. return new Promise((resolve, reject) => {
  166. axios[method](`/_api${path}`, params)
  167. .then(res => {
  168. if (res.data.ok) {
  169. resolve(res.data);
  170. } else {
  171. reject(new Error(res.data.error));
  172. }
  173. })
  174. .catch(res => {
  175. reject(res);
  176. });
  177. });
  178. }
  179. }