section-config.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, sections, clientSideSections, canonize;
  8. /**
  9. * @param {String} url
  10. * @return {String}
  11. */
  12. canonize = function (url) {
  13. var route = url,
  14. key;
  15. for (key in baseUrls) { //eslint-disable-line guard-for-in
  16. route = url.replace(baseUrls[key], '');
  17. if (route != url) { //eslint-disable-line eqeqeq
  18. break;
  19. }
  20. }
  21. return route.replace(/^\/?index.php\/?/, '').toLowerCase();
  22. };
  23. return {
  24. /**
  25. * @param {String} url
  26. * @return {Array}
  27. */
  28. getAffectedSections: function (url) {
  29. var route = canonize(url),
  30. actions = _.find(sections, function (val, section) {
  31. var matched;
  32. if (section.indexOf('*') >= 0) {
  33. section = section.replace(/\*/g, '[^/]+') + '$';
  34. matched = route.match(section);
  35. return matched && matched[0] == route; //eslint-disable-line eqeqeq
  36. }
  37. return route.indexOf(section) === 0;
  38. });
  39. return _.union(_.toArray(actions), _.toArray(sections['*']));
  40. },
  41. /**
  42. * @param {*} allSections
  43. * @return {*}
  44. */
  45. filterClientSideSections: function (allSections) {
  46. if (Array.isArray(allSections)) {
  47. return _.difference(allSections, clientSideSections);
  48. }
  49. return allSections;
  50. },
  51. /**
  52. * @param {String} sectionName
  53. * @return {Boolean}
  54. */
  55. isClientSideSection: function (sectionName) {
  56. return _.contains(clientSideSections, sectionName);
  57. },
  58. /**
  59. * @param {Object} options
  60. * @constructor
  61. */
  62. 'Magento_Customer/js/section-config': function (options) {
  63. baseUrls = options.baseUrls;
  64. sections = options.sections;
  65. clientSideSections = options.clientSideSections;
  66. }
  67. };
  68. });