Crowi.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.isAdmin = context.isAdmin;
  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 this.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. getEmojiStrategy() {
  58. return emojiStrategy;
  59. }
  60. recoverData() {
  61. const keys = [
  62. 'userByName',
  63. 'userById',
  64. 'users',
  65. 'draft',
  66. 'editorOptions',
  67. 'previewOptions',
  68. ];
  69. keys.forEach(key => {
  70. if (this.localStorage[key]) {
  71. try {
  72. this[key] = JSON.parse(this.localStorage[key]);
  73. }
  74. catch (e) {
  75. this.localStorage.removeItem(key);
  76. }
  77. }
  78. });
  79. }
  80. fetchUsers() {
  81. const interval = 1000*60*15; // 15min
  82. const currentTime = new Date();
  83. if (this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  84. return ;
  85. }
  86. this.apiGet('/users.list', {})
  87. .then(data => {
  88. this.users = data.users;
  89. this.localStorage.users = JSON.stringify(data.users);
  90. let userByName = {};
  91. let userById = {};
  92. for (let i = 0; i < data.users.length; i++) {
  93. const user = data.users[i];
  94. userByName[user.username] = user;
  95. userById[user._id] = user;
  96. }
  97. this.userByName = userByName;
  98. this.localStorage.userByName = JSON.stringify(userByName);
  99. this.userById = userById;
  100. this.localStorage.userById = JSON.stringify(userById);
  101. this.localStorage.lastFetched = new Date();
  102. }).catch(err => {
  103. this.localStorage.removeItem('lastFetched');
  104. // ignore errors
  105. });
  106. }
  107. setCaretLine(line) {
  108. if (this.pageEditor != null) {
  109. this.pageEditor.setCaretLine(line);
  110. }
  111. }
  112. focusToEditor() {
  113. if (this.pageEditor != null) {
  114. this.pageEditor.focusToEditor();
  115. }
  116. }
  117. clearDraft(path) {
  118. delete this.draft[path];
  119. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  120. }
  121. saveDraft(path, body) {
  122. this.draft[path] = body;
  123. this.localStorage.setItem('draft', JSON.stringify(this.draft));
  124. }
  125. findDraft(path) {
  126. if (this.draft && this.draft[path]) {
  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. }
  175. else {
  176. reject(new Error(res.data.error));
  177. }
  178. })
  179. .catch(res => {
  180. reject(res);
  181. });
  182. });
  183. }
  184. }