Crowi.js 4.5 KB

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