Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

80 строки
1.9 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'underscore'
  7. ], function (_) {
  8. 'use strict';
  9. /**
  10. * Checks if provided string is a valid DOM selector.
  11. *
  12. * @param {String} selector - Selector to be checked.
  13. * @returns {Boolean}
  14. */
  15. function isSelector(selector) {
  16. try {
  17. document.querySelector(selector);
  18. return true;
  19. } catch (e) {
  20. return false;
  21. }
  22. }
  23. /**
  24. * Unescapes characters used in underscore templates.
  25. *
  26. * @param {String} str - String to be processed.
  27. * @returns {String}
  28. */
  29. function unescape(str) {
  30. return str.replace(/&lt;%|%3C%/g, '<%').replace(/%&gt;|%%3E/g, '%>');
  31. }
  32. /**
  33. * If 'tmpl' is a valid selector, returns target node's innerHTML if found.
  34. * Else, returns empty string and emits console warning.
  35. * If 'tmpl' is not a selector, returns 'tmpl' as is.
  36. *
  37. * @param {String} tmpl
  38. * @returns {String}
  39. */
  40. function getTmplString(tmpl) {
  41. if (isSelector(tmpl)) {
  42. tmpl = document.querySelector(tmpl);
  43. if (tmpl) {
  44. tmpl = tmpl.innerHTML.trim();
  45. } else {
  46. console.warn('No template was found by selector: ' + tmpl);
  47. tmpl = '';
  48. }
  49. }
  50. return unescape(tmpl);
  51. }
  52. /**
  53. * Compiles or renders template provided either
  54. * by selector or by the template string.
  55. *
  56. * @param {String} tmpl - Template string or selector.
  57. * @param {(Object|Array|Function)} [data] - Data object with which to render template.
  58. * @returns {String|Function}
  59. */
  60. return function (tmpl, data) {
  61. var render;
  62. tmpl = getTmplString(tmpl);
  63. render = _.template(tmpl);
  64. return !_.isUndefined(data) ?
  65. render(data) :
  66. render;
  67. };
  68. });