index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import $ from 'jquery';
  2. import { b64_decode_unicode, b64_encode_unicode, FlexPages } from './finder';
  3. import { isEnabled, getCookie, setCookie } from 'tiny-cookie';
  4. import getFilters from '../utils/get-filters';
  5. const container = document.querySelector('#pages-content-wrapper');
  6. export const getStore = () => {
  7. if (!isEnabled) {
  8. return '';
  9. }
  10. return JSON.parse(b64_decode_unicode(getCookie('grav-admin-flexpages') || 'e30='));
  11. };
  12. export const setStore = (store = {}, options = { expires: '1Y', samesite: 'Lax' }) => {
  13. if (!isEnabled) {
  14. return '';
  15. }
  16. return setCookie('grav-admin-flexpages', b64_encode_unicode(JSON.stringify(store)), options);
  17. };
  18. export const getInitialRoute = () => {
  19. const parsed = getStore();
  20. return parsed.route || '';
  21. };
  22. export const setInitialRoute = ({ route = '', filters = getStore().filters || {}, options = { expires: '1Y' }} = {}) => {
  23. return setStore({ route, filters }, options);
  24. };
  25. export let FlexPagesInstance = null;
  26. export const ReLoad = (fresh = false) => {
  27. const search = document.querySelector('#pages-filters [name="filters[search]"]');
  28. const loader = container.querySelector('.grav-loading');
  29. const content = container.querySelector('#pages-columns');
  30. const gravConfig = typeof global.GravConfig !== 'undefined' ? global.GravConfig : global.GravAdmin.config;
  31. if (fresh && search) {
  32. search.focus();
  33. }
  34. if (loader && content) {
  35. loader.style.display = 'block';
  36. content.innerHTML = '';
  37. const filters = fresh ? getStore().filters || {} : getFilters();
  38. const withFilters = Object.keys(filters).length ? { ...filters, initial: true } : {};
  39. const store = getStore();
  40. store.filters = filters;
  41. setStore(store);
  42. let isSearchFocused = false;
  43. if (search) {
  44. isSearchFocused = search === document.activeElement;
  45. }
  46. const contentWrapper = document.querySelector('.content-wrapper .gm-scroll-view');
  47. const scrollPosition = {
  48. top: contentWrapper ? contentWrapper.scrollTop : 0,
  49. left: contentWrapper ? contentWrapper.scrollLeft : 0
  50. };
  51. $.ajax({
  52. url: `${gravConfig.current_url}`,
  53. method: 'post',
  54. data: Object.assign({}, {
  55. route: b64_encode_unicode(getInitialRoute()),
  56. initial: true,
  57. action: 'listLevel'
  58. }, withFilters),
  59. success(response) {
  60. loader.style.display = 'none';
  61. if (response.status === 'error') {
  62. content.innerHTML = response.message;
  63. return true;
  64. }
  65. FlexPagesInstance = null;
  66. FlexPagesInstance = new FlexPages(content, response.data);
  67. if (search && isSearchFocused) {
  68. search.focus();
  69. }
  70. if (contentWrapper) {
  71. contentWrapper.scrollTo(scrollPosition);
  72. }
  73. return FlexPagesInstance;
  74. }
  75. });
  76. }
  77. };
  78. if (container) {
  79. ReLoad(true);
  80. }