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. 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) {
  43. this.localStorage.lastFetched = new Date();
  44. }
  45. if (interval > currentTime - new Date(this.localStorage.lastFetched)) {
  46. return ;
  47. }
  48. this.apiGet('/users.list', {})
  49. .then(data => {
  50. this.users = data.users;
  51. this.localStorage.users = JSON.stringify(data.users);
  52. let userByName = {};
  53. for (let i = 0; i < data.users.length; i++) {
  54. const user = data.users[i];
  55. userByName[user.username] = user;
  56. }
  57. this.userByName = userByName;
  58. this.localStorage.userByName = JSON.stringify(userByName);
  59. this.localStorage.lastFetched = new Date();
  60. //console.log('userByName', this.localStorage.userByName);
  61. }).catch(err => {
  62. // ignore errors
  63. });
  64. }
  65. clearDraft(path) {
  66. this.draft[path] = null;
  67. this.localStorage.draft = JSON.stringify(this.draft);
  68. }
  69. saveDraft(path, body) {
  70. this.draft[path] = body;
  71. this.localStorage.draft = JSON.stringify(this.draft);
  72. }
  73. findDraft(path) {
  74. if (this.draft && this.draft[path]) {
  75. return this.draft[path];
  76. }
  77. return null;
  78. }
  79. findUser(username) {
  80. if (this.userByName && this.userByName[username]) {
  81. return this.userByName[username];
  82. }
  83. return null;
  84. }
  85. apiGet(path, params) {
  86. return this.apiRequest('get', path, params);
  87. }
  88. apiPost(path, params) {
  89. return this.apiRequest('post', path, params);
  90. }
  91. apiRequest(method, path, params) {
  92. return new Promise((resolve, reject) => {
  93. axios[method](`/_api${path}`, {params})
  94. .then(res => {
  95. if (res.data.ok) {
  96. resolve(res.data);
  97. } else {
  98. // FIXME?
  99. throw new Error(res.data);
  100. }
  101. }).catch(res => {
  102. // FIXME?
  103. throw new Error(res);
  104. });
  105. });
  106. }
  107. static escape (html, encode) {
  108. return html
  109. .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
  110. .replace(/</g, '&lt;')
  111. .replace(/>/g, '&gt;')
  112. .replace(/"/g, '&quot;')
  113. .replace(/'/g, '&#39;');
  114. }
  115. static unescape(html) {
  116. return html.replace(/&([#\w]+);/g, function(_, n) {
  117. n = n.toLowerCase();
  118. if (n === 'colon') return ':';
  119. if (n.charAt(0) === '#') {
  120. return n.charAt(1) === 'x'
  121. ? String.fromCharCode(parseInt(n.substring(2), 16))
  122. : String.fromCharCode(+n.substring(1));
  123. }
  124. return '';
  125. });
  126. }
  127. }