Crowi.js 3.0 KB

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