Crowi.js 5.1 KB

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