Crowi.js 4.8 KB

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