routing.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. exports.setLocation = function setLocation(
  2. isReplace,
  3. activeSort,
  4. isFlat,
  5. activeFilters,
  6. fileFilter,
  7. expandedLines
  8. ) {
  9. const params = [
  10. activeSort.sortKey,
  11. activeSort.order,
  12. isFlat,
  13. activeFilters.low,
  14. activeFilters.medium,
  15. activeFilters.high,
  16. encodeURIComponent(fileFilter),
  17. expandedLines.map(encodeURIComponent).join(',')
  18. ];
  19. const newUrl = `#${params.join('/')}`;
  20. if (newUrl === location.hash) {
  21. return;
  22. }
  23. window.history[isReplace ? 'replaceState' : 'pushState'](null, '', newUrl);
  24. };
  25. exports.decodeLocation = function decodeLocation() {
  26. const items = location.hash.substr(1).split('/');
  27. if (items.length !== 8) {
  28. return null;
  29. }
  30. try {
  31. return {
  32. activeSort: {
  33. sortKey: items[0],
  34. order: items[1]
  35. },
  36. isFlat: JSON.parse(items[2]),
  37. activeFilters: {
  38. low: JSON.parse(items[3]),
  39. medium: JSON.parse(items[4]),
  40. high: JSON.parse(items[5])
  41. },
  42. fileFilter: decodeURIComponent(items[6]),
  43. expandedLines: items[7].split(',').map(decodeURIComponent)
  44. };
  45. } catch (e) {
  46. return null;
  47. }
  48. };