Crowi.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Crowi context class for client
  3. */
  4. import axios from 'axios'
  5. import InterceptorManager from '../../../lib/util/interceptor-manager';
  6. //// disable Detach/Restore interceptors
  7. //// because markdown-it handles emoji and Linker in code blocks well -- 2018.02.01 Yuki Takei
  8. // import {
  9. // DetachCodeBlockInterceptor,
  10. // RestoreCodeBlockInterceptor,
  11. // } from './interceptor/detach-code-blocks';
  12. export default class Crowi {
  13. constructor(context, window) {
  14. this.context = context;
  15. this.config = {};
  16. this.csrfToken = context.csrfToken;
  17. this.window = window;
  18. this.location = window.location || {};
  19. this.document = window.document || {};
  20. this.localStorage = window.localStorage || {};
  21. this.pageEditor = undefined;
  22. this.fetchUsers = this.fetchUsers.bind(this);
  23. this.apiGet = this.apiGet.bind(this);
  24. this.apiPost = this.apiPost.bind(this);
  25. this.apiRequest = this.apiRequest.bind(this);
  26. this.interceptorManager = new InterceptorManager();
  27. this.interceptorManager.addInterceptors([
  28. //// disable Detach/Restore interceptors
  29. //// because markdown-it handles emoji and Linker in code blocks well -- 2018.02.01 Yuki Takei
  30. // new DetachCodeBlockInterceptor(this),
  31. // new RestoreCodeBlockInterceptor(this),
  32. ]);
  33. // FIXME
  34. this.me = context.me;
  35. this.users = [];
  36. this.userByName = {};
  37. this.userById = {};
  38. this.draft = {};
  39. this.editorOptions = {};
  40. this.recoverData();
  41. }
  42. /**
  43. * @return {Object} window.Crowi (/resource/js/crowi.js)
  44. */
  45. getCrowiForJquery() {
  46. return window.Crowi;
  47. }
  48. getContext() {
  49. return context;
  50. }
  51. setConfig(config) {
  52. this.config = config;
  53. }
  54. getConfig() {
  55. return this.config;
  56. }
  57. setPageEditor(pageEditor) {
  58. this.pageEditor = pageEditor;
  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. } 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. } else {
  174. reject(new Error(res.data.error));
  175. }
  176. })
  177. .catch(res => {
  178. reject(res);
  179. });
  180. });
  181. }
  182. }