You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

94 rivejä
2.7 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define(['underscore'], function (_) {
  6. 'use strict';
  7. var baseUrls = [],
  8. sections = [],
  9. clientSideSections = [],
  10. sectionNames = [],
  11. canonize;
  12. /**
  13. * @param {String} url
  14. * @return {String}
  15. */
  16. canonize = function (url) {
  17. var route = url;
  18. _.some(baseUrls, function (baseUrl) {
  19. route = url.replace(baseUrl, '');
  20. return route !== url;
  21. });
  22. return route.replace(/^\/?index.php\/?/, '').toLowerCase();
  23. };
  24. return {
  25. /**
  26. * Returns a list of sections which should be invalidated for given URL.
  27. * @param {String} url - URL which was requested.
  28. * @return {Object} - List of sections to invalidate.
  29. */
  30. getAffectedSections: function (url) {
  31. var route = canonize(url),
  32. actions = _.find(sections, function (val, section) {
  33. var matched;
  34. // Covers the case where "*" works as a glob pattern.
  35. if (section.indexOf('*') >= 0) {
  36. section = section.replace(/\*/g, '[^/]+') + '$';
  37. matched = route.match(section);
  38. return matched && matched[0] === route;
  39. }
  40. return route.indexOf(section) === 0;
  41. });
  42. return _.union(_.toArray(actions), sections['*']);
  43. },
  44. /**
  45. * Filters the list of given sections to the ones defined as client side.
  46. * @param {Object} allSections - List of sections to check.
  47. * @return {Object} - List of filtered sections.
  48. */
  49. filterClientSideSections: function (allSections) {
  50. return _.difference(allSections, clientSideSections);
  51. },
  52. /**
  53. * Tells if section is defined as client side.
  54. * @param {String} sectionName - Name of the section to check.
  55. * @return {Boolean}
  56. */
  57. isClientSideSection: function (sectionName) {
  58. return _.contains(clientSideSections, sectionName);
  59. },
  60. /**
  61. * Returns array of section names.
  62. * @returns {Array}
  63. */
  64. getSectionNames: function () {
  65. return sectionNames;
  66. },
  67. /**
  68. * @param {Object} options
  69. * @constructor
  70. */
  71. 'Magento_Customer/js/section-config': function (options) {
  72. baseUrls = options.baseUrls;
  73. sections = options.sections;
  74. clientSideSections = options.clientSideSections;
  75. sectionNames = options.sectionNames;
  76. }
  77. };
  78. });