cache.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import $ from 'jquery';
  2. import { config } from 'grav-config';
  3. import request from '../utils/request';
  4. const getUrl = (type = '') => {
  5. if (type) {
  6. type = `cleartype:${type}/`;
  7. }
  8. return `${config.base_url_relative}/cache.json/task${config.param_sep}clearCache/${type}admin-nonce${config.param_sep}${config.admin_nonce}`;
  9. };
  10. export default class Cache {
  11. constructor() {
  12. this.element = $('[data-clear-cache]');
  13. $('body').on('click', '[data-clear-cache]', (event) => this.clear(event, event.target));
  14. }
  15. clear(event, element) {
  16. let type = '';
  17. if (event && event.preventDefault) { event.preventDefault(); }
  18. if (typeof event === 'string') { type = event; }
  19. element = element ? $(element) : $(`[data-clear-cache-type="${type}"]`);
  20. type = type || $(element).data('clear-cache-type') || '';
  21. let url = element.data('clearCache') || getUrl(type);
  22. this.disable();
  23. request(url, () => this.enable());
  24. }
  25. enable() {
  26. this.element
  27. .removeAttr('disabled')
  28. .find('> .fa').removeClass('fa-refresh fa-spin').addClass('fa-trash');
  29. }
  30. disable() {
  31. this.element
  32. .attr('disabled', 'disabled')
  33. .find('> .fa').removeClass('fa-trash').addClass('fa-refresh fa-spin');
  34. }
  35. }
  36. let Instance = new Cache();
  37. export { Instance };