Crowi.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * Crowi context class for client
  3. */
  4. import axios from 'axios';
  5. import emojiStrategy from './emojione/emoji_strategy_shrinked.json';
  6. import InterceptorManager from '../../../lib/util/interceptor-manager';
  7. import {
  8. DetachCodeBlockInterceptor,
  9. RestoreCodeBlockInterceptor,
  10. } from './interceptor/detach-code-blocks';
  11. export default class Crowi {
  12. constructor(context, window) {
  13. this.context = context;
  14. this.config = {};
  15. this.csrfToken = context.csrfToken;
  16. this.window = window;
  17. this.location = window.location || {};
  18. this.document = window.document || {};
  19. this.localStorage = window.localStorage || {};
  20. this.pageEditor = undefined;
  21. this.fetchUsers = this.fetchUsers.bind(this);
  22. this.apiGet = this.apiGet.bind(this);
  23. this.apiPost = this.apiPost.bind(this);
  24. this.apiRequest = this.apiRequest.bind(this);
  25. this.interceptorManager = new InterceptorManager();
  26. this.interceptorManager.addInterceptor(new DetachCodeBlockInterceptor(this), 10); // process as soon as possible
  27. this.interceptorManager.addInterceptor(new RestoreCodeBlockInterceptor(this), 900); // process as late as possible
  28. // FIXME
  29. this.me = context.me;
  30. this.users = [];
  31. this.userByName = {};
  32. this.userById = {};
  33. this.draft = {};
  34. this.editorOptions = {};
  35. this.userRelatedGroups = {};
  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 this.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. getEmojiStrategy() {
  57. return emojiStrategy;
  58. }
  59. recoverData() {
  60. const keys = [
  61. 'userByName',
  62. 'userById',
  63. 'users',
  64. 'draft',
  65. 'editorOptions',
  66. 'previewOptions',
  67. ];
  68. keys.forEach(key => {
  69. if (this.localStorage[key]) {
  70. try {
  71. this[key] = JSON.parse(this.localStorage[key]);
  72. }
  73. catch (e) {
  74. this.localStorage.removeItem(key);
  75. }
  76. }
  77. });
  78. }
  79. fetchUsers() {
  80. const interval = 1000*60*15; // 15min
  81. const currentTime = new Date();
  82. if (this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  83. return ;
  84. }
  85. this.apiGet('/users.list', {})
  86. .then(data => {
  87. this.users = data.users;
  88. this.localStorage.users = JSON.stringify(data.users);
  89. let userByName = {};
  90. let userById = {};
  91. for (let i = 0; i < data.users.length; i++) {
  92. const user = data.users[i];
  93. userByName[user.username] = user;
  94. userById[user._id] = user;
  95. }
  96. this.userByName = userByName;
  97. this.localStorage.userByName = JSON.stringify(userByName);
  98. this.userById = userById;
  99. this.localStorage.userById = JSON.stringify(userById);
  100. this.localStorage.lastFetched = new Date();
  101. }).catch(err => {
  102. this.localStorage.removeItem('lastFetched');
  103. // ignore errors
  104. });
  105. }
  106. setCaretLine(line) {
  107. if (this.pageEditor != null) {
  108. this.pageEditor.setCaretLine(line);
  109. }
  110. }
  111. focusToEditor() {
  112. if (this.pageEditor != null) {
  113. this.pageEditor.focusToEditor();
  114. }
  115. }
  116. clearDraft(path) {
  117. delete this.draft[path];
  118. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  119. }
  120. saveDraft(path, body) {
  121. this.draft[path] = body;
  122. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  123. }
  124. findDraft(path) {
  125. if (this.draft && this.draft[path]) {
  126. return this.draft[path];
  127. }
  128. return null;
  129. }
  130. saveEditorOptions(options) {
  131. this.localStorage.setItem('editorOptions', JSON.stringify(options));
  132. }
  133. savePreviewOptions(options) {
  134. this.localStorage.setItem('previewOptions', JSON.stringify(options));
  135. }
  136. findUserById(userId) {
  137. if (this.userById && this.userById[userId]) {
  138. return this.userById[userId];
  139. }
  140. return null;
  141. }
  142. findUserByIds(userIds) {
  143. let users = [];
  144. for (let userId of userIds) {
  145. let user = this.findUserById(userId);
  146. if (user) {
  147. users.push(user);
  148. }
  149. }
  150. return users;
  151. }
  152. findUser(username) {
  153. if (this.userByName && this.userByName[username]) {
  154. return this.userByName[username];
  155. }
  156. return null;
  157. }
  158. apiGet(path, params) {
  159. return this.apiRequest('get', path, {params: params});
  160. }
  161. apiPost(path, params) {
  162. if (!params._csrf) {
  163. params._csrf = this.csrfToken;
  164. }
  165. return this.apiRequest('post', path, params);
  166. }
  167. apiRequest(method, path, params) {
  168. return new Promise((resolve, reject) => {
  169. axios[method](`/_api${path}`, params)
  170. .then(res => {
  171. if (res.data.ok) {
  172. resolve(res.data);
  173. }
  174. else {
  175. reject(new Error(res.data.error));
  176. }
  177. })
  178. .catch(res => {
  179. reject(res);
  180. });
  181. });
  182. }
  183. }