Crowi.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Crowi context class for client
  3. */
  4. import axios from 'axios'
  5. export default class Crowi {
  6. constructor(context, window) {
  7. this.context = context;
  8. this.location = window.location || {};
  9. this.document = window.document || {};
  10. this.localStorage = window.localStorage || {};
  11. this.fetchUsers = this.fetchUsers.bind(this);
  12. this.apiGet = this.apiGet.bind(this);
  13. this.apiPost = this.apiPost.bind(this);
  14. this.apiRequest = this.apiRequest.bind(this);
  15. // FIXME
  16. this.me = context.me;
  17. this.users = [];
  18. this.userByName = {};
  19. this.draft = {};
  20. this.recoverData();
  21. }
  22. getContext() {
  23. return context;
  24. }
  25. recoverData() {
  26. const keys = [
  27. 'userByName',
  28. 'users',
  29. 'draft',
  30. ];
  31. keys.forEach(key => {
  32. if (this.localStorage[key]) {
  33. try {
  34. this[key] = JSON.parse(this.localStorage[key]);
  35. } catch (e) {
  36. this.localStorage.removeItem(key);
  37. }
  38. }
  39. });
  40. }
  41. fetchUsers () {
  42. const interval = 1000*60*10; // 5min
  43. const currentTime = new Date();
  44. if (!this.localStorage.lastFetched && interval > currentTime - new Date(this.localStorage.lastFetched)) {
  45. return ;
  46. }
  47. this.apiGet('/users.list', {})
  48. .then(data => {
  49. this.users = data.users;
  50. this.localStorage.users = JSON.stringify(data.users);
  51. let userByName = {};
  52. for (let i = 0; i < data.users.length; i++) {
  53. const user = data.users[i];
  54. userByName[user.username] = user;
  55. }
  56. this.userByName = userByName;
  57. this.localStorage.userByName = JSON.stringify(userByName);
  58. this.localStorage.lastFetched = new Date();
  59. }).catch(err => {
  60. this.localStorage.removeItem('lastFetched');
  61. // ignore errors
  62. });
  63. }
  64. clearDraft(path) {
  65. delete this.draft[path];
  66. this.localStorage.draft = JSON.stringify(this.draft);
  67. }
  68. saveDraft(path, body) {
  69. this.draft[path] = body;
  70. this.localStorage.draft = JSON.stringify(this.draft);
  71. }
  72. findDraft(path) {
  73. if (this.draft && this.draft[path]) {
  74. return this.draft[path];
  75. }
  76. return null;
  77. }
  78. findUser(username) {
  79. if (this.userByName && this.userByName[username]) {
  80. return this.userByName[username];
  81. }
  82. return null;
  83. }
  84. apiGet(path, params) {
  85. return this.apiRequest('get', path, params);
  86. }
  87. apiPost(path, params) {
  88. return this.apiRequest('post', path, params);
  89. }
  90. apiRequest(method, path, params) {
  91. return new Promise((resolve, reject) => {
  92. axios[method](`/_api${path}`, {params})
  93. .then(res => {
  94. if (res.data.ok) {
  95. resolve(res.data);
  96. } else {
  97. // FIXME?
  98. reject(new Error(res.data));
  99. }
  100. }).catch(res => {
  101. // FIXME?
  102. reject(new Error('Error'));
  103. });
  104. });
  105. }
  106. static escape (html, encode) {
  107. return html
  108. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  109. .replace(/</g, '&lt;')
  110. .replace(/>/g, '&gt;')
  111. .replace(/"/g, '&quot;')
  112. .replace(/'/g, '&#39;');
  113. }
  114. static unescape(html) {
  115. return html.replace(/&([#\w]+);/g, function(_, n) {
  116. n = n.toLowerCase();
  117. if (n === 'colon') return ':';
  118. if (n.charAt(0) === '#') {
  119. return n.charAt(1) === 'x'
  120. ? String.fromCharCode(parseInt(n.substring(2), 16))
  121. : String.fromCharCode(+n.substring(1));
  122. }
  123. return '';
  124. });
  125. }
  126. }