Crowi.js 3.3 KB

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