Crowi.js 4.5 KB

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