Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

635 righe
16 KiB

  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define('globalNavigationScroll', [
  6. 'jquery'
  7. ], function ($) {
  8. 'use strict';
  9. var win = $(window),
  10. subMenuClass = '.submenu',
  11. fixedClassName = '_fixed',
  12. menu = $('.menu-wrapper'),
  13. content = $('.page-wrapper'),
  14. menuItems = $('#nav').children('li'),
  15. winHeight,
  16. menuHeight = menu.height(),
  17. menuScrollMax = 0,
  18. submenuHeight = 0,
  19. contentHeight,
  20. winTop = 0,
  21. winTopLast = 0,
  22. scrollStep = 0,
  23. nextTop = 0;
  24. /**
  25. * Check if menu is fixed
  26. * @returns {Boolean}
  27. */
  28. function isMenuFixed() {
  29. return menuHeight < contentHeight && contentHeight > winHeight;
  30. }
  31. /**
  32. * Check if class exist than add or do nothing
  33. * @param {jQuery} el
  34. * @param {String} $class
  35. */
  36. function checkAddClass(el, $class) {
  37. if (!el.hasClass($class)) {
  38. el.addClass($class);
  39. }
  40. }
  41. /**
  42. * Check if class exist than remove or do nothing
  43. * @param {jQuery} el
  44. * @param {String} $class
  45. */
  46. function checkRemoveClass(el, $class) {
  47. if (el.hasClass($class)) {
  48. el.removeClass($class);
  49. }
  50. }
  51. /**
  52. * Calculate and apply menu position
  53. */
  54. function positionMenu() {
  55. // Spotting positions and heights
  56. winHeight = win.height();
  57. contentHeight = content.height();
  58. winTop = win.scrollTop();
  59. scrollStep = winTop - winTopLast;
  60. if (isMenuFixed()) { // fixed menu cases
  61. checkAddClass(menu, fixedClassName);
  62. if (menuHeight > winHeight) { // smart scroll cases
  63. if (winTop > winTopLast) { //eslint-disable-line max-depth
  64. menuScrollMax = menuHeight - winHeight;
  65. nextTop < menuScrollMax - scrollStep ?
  66. nextTop += scrollStep : nextTop = menuScrollMax;
  67. menu.css('top', -nextTop);
  68. } else if (winTop <= winTopLast) { // scroll up
  69. nextTop > -scrollStep ?
  70. nextTop += scrollStep : nextTop = 0;
  71. menu.css('top', -nextTop);
  72. }
  73. }
  74. } else { // static menu cases
  75. checkRemoveClass(menu, fixedClassName);
  76. menu.css('top', 'auto');
  77. }
  78. // Save previous window scrollTop
  79. winTopLast = winTop;
  80. }
  81. positionMenu(); // page start calculation
  82. // Change position on scroll
  83. win.on('scroll', function () {
  84. positionMenu();
  85. });
  86. win.on('resize', function () {
  87. winHeight = win.height();
  88. // Reset position if fixed and out of smart scroll
  89. if (menuHeight < contentHeight && menuHeight <= winHeight) {
  90. menu.removeAttr('style');
  91. menuItems.off();
  92. }
  93. });
  94. // Add event to menuItems to check submenu overlap
  95. menuItems.on('click', function () {
  96. var submenu = $(this).children(subMenuClass),
  97. delta,
  98. logo = $('.logo')[0].offsetHeight;
  99. submenuHeight = submenu.height();
  100. if (submenuHeight > menuHeight && menuHeight + logo > winHeight) {
  101. menu.height(submenuHeight - logo);
  102. delta = -menu.position().top;
  103. window.scrollTo(0, 0);
  104. positionMenu();
  105. window.scrollTo(0, delta);
  106. positionMenu();
  107. menuHeight = submenuHeight;
  108. }
  109. });
  110. });
  111. define('globalNavigation', [
  112. 'jquery',
  113. 'jquery/ui',
  114. 'globalNavigationScroll'
  115. ], function ($) {
  116. 'use strict';
  117. $.widget('mage.globalNavigation', {
  118. options: {
  119. selectors: {
  120. menu: '#nav',
  121. currentItem: '._current',
  122. topLevelItem: '.level-0',
  123. topLevelHref: '> a',
  124. subMenu: '> .submenu',
  125. closeSubmenuBtn: '[data-role="close-submenu"]'
  126. },
  127. overlayTmpl: '<div class="admin__menu-overlay"></div>'
  128. },
  129. /** @inheritdoc */
  130. _create: function () {
  131. var selectors = this.options.selectors;
  132. this.menu = this.element;
  133. this.menuLinks = $(selectors.topLevelHref, selectors.topLevelItem);
  134. this.closeActions = $(selectors.closeSubmenuBtn);
  135. this._initOverlay()
  136. ._bind();
  137. },
  138. /**
  139. * @return {Object}
  140. * @private
  141. */
  142. _initOverlay: function () {
  143. this.overlay = $(this.options.overlayTmpl).appendTo('body').hide(0);
  144. return this;
  145. },
  146. /**
  147. * @private
  148. */
  149. _bind: function () {
  150. var focus = this._focus.bind(this),
  151. open = this._open.bind(this),
  152. blur = this._blur.bind(this),
  153. keyboard = this._keyboard.bind(this);
  154. this.menuLinks
  155. .on('focus', focus)
  156. .on('click', open);
  157. this.menuLinks.last().on('blur', blur);
  158. this.closeActions.on('keydown', keyboard);
  159. },
  160. /**
  161. * Remove active class from current menu item
  162. * Turn back active class to current page menu item
  163. */
  164. _blur: function (e) {
  165. var selectors = this.options.selectors,
  166. menuItem = $(e.target).closest(selectors.topLevelItem),
  167. currentItem = $(selectors.menu).find(selectors.currentItem);
  168. menuItem.removeClass('_active');
  169. currentItem.addClass('_active');
  170. },
  171. /**
  172. * Add focus to active menu item
  173. */
  174. _keyboard: function (e) {
  175. var selectors = this.options.selectors,
  176. menuItem = $(e.target).closest(selectors.topLevelItem);
  177. if (e.which === 13) {
  178. this._close(e);
  179. $(selectors.topLevelHref, menuItem).trigger('focus');
  180. }
  181. },
  182. /**
  183. * Toggle active state on focus
  184. */
  185. _focus: function (e) {
  186. var selectors = this.options.selectors,
  187. menuItem = $(e.target).closest(selectors.topLevelItem);
  188. menuItem.addClass('_active')
  189. .siblings(selectors.topLevelItem)
  190. .removeClass('_active');
  191. },
  192. /**
  193. * @param {jQuery.Event} e
  194. * @private
  195. */
  196. _closeSubmenu: function (e) {
  197. var selectors = this.options.selectors,
  198. currentItem = $(selectors.menu).find(selectors.currentItem);
  199. this._close(e);
  200. currentItem.addClass('_active');
  201. },
  202. /**
  203. * @param {jQuery.Event} e
  204. * @private
  205. */
  206. _open: function (e) {
  207. var selectors = this.options.selectors,
  208. menuItemSelector = selectors.topLevelItem,
  209. menuItem = $(e.target).closest(menuItemSelector),
  210. subMenu = $(selectors.subMenu, menuItem),
  211. close = this._closeSubmenu.bind(this),
  212. closeBtn = subMenu.find(selectors.closeSubmenuBtn);
  213. if (subMenu.length) {
  214. e.preventDefault();
  215. }
  216. closeBtn.on('click', close);
  217. if ($(menuItem).hasClass('_show')) {
  218. closeBtn.trigger('click');
  219. } else {
  220. menuItem.addClass('_show')
  221. .siblings(menuItemSelector)
  222. .removeClass('_show');
  223. subMenu.attr('aria-expanded', 'true');
  224. this.overlay.show(0).on('click', close);
  225. this.menuLinks.last().off('blur');
  226. }
  227. },
  228. /**
  229. * @param {jQuery.Event} e
  230. * @private
  231. */
  232. _close: function (e) {
  233. var selectors = this.options.selectors,
  234. menuItem = this.menu.find(selectors.topLevelItem + '._show'),
  235. subMenu = $(selectors.subMenu, menuItem),
  236. closeBtn = subMenu.find(selectors.closeSubmenuBtn),
  237. blur = this._blur.bind(this);
  238. e.preventDefault();
  239. this.overlay.hide(0).off('click');
  240. this.menuLinks.last().on('blur', blur);
  241. closeBtn.off('click');
  242. subMenu.attr('aria-expanded', 'false');
  243. menuItem.removeClass('_show _active');
  244. }
  245. });
  246. return $.mage.globalNavigation;
  247. });
  248. define('globalSearch', [
  249. 'jquery',
  250. 'Magento_Ui/js/lib/key-codes',
  251. 'jquery-ui-modules/widget'
  252. ], function ($, keyCodes) {
  253. 'use strict';
  254. $.widget('mage.globalSearch', {
  255. options: {
  256. field: '.search-global-field',
  257. fieldActiveClass: '_active',
  258. input: '#search-global'
  259. },
  260. /** @inheritdoc */
  261. _create: function () {
  262. this.field = $(this.options.field);
  263. this.input = $(this.options.input);
  264. this._events();
  265. },
  266. /**
  267. * @private
  268. */
  269. _events: function () {
  270. var self = this;
  271. this.input.on('blur.resetGlobalSearchForm', function () {
  272. if (!self.input.val()) {
  273. self.field.removeClass(self.options.fieldActiveClass);
  274. }
  275. });
  276. this.input.on('focus.activateGlobalSearchForm', function () {
  277. self.field.addClass(self.options.fieldActiveClass);
  278. });
  279. $(document).on('keydown.activateGlobalSearchForm', function (event) {
  280. var inputs = [
  281. 'input',
  282. 'select',
  283. 'textarea'
  284. ];
  285. if (keyCodes[event.which] !== 'forwardSlashKey' ||
  286. inputs.indexOf(event.target.tagName.toLowerCase()) !== -1 ||
  287. event.target.isContentEditable
  288. ) {
  289. return;
  290. }
  291. event.preventDefault();
  292. self.input.focus();
  293. });
  294. }
  295. });
  296. return $.mage.globalSearch;
  297. });
  298. define('modalPopup', [
  299. 'jquery',
  300. 'jquery/ui'
  301. ], function ($) {
  302. 'use strict';
  303. $.widget('mage.modalPopup', {
  304. options: {
  305. popup: '.popup',
  306. btnDismiss: '[data-dismiss="popup"]',
  307. btnHide: '[data-hide="popup"]'
  308. },
  309. /** @inheritdoc */
  310. _create: function () {
  311. this.fade = this.element;
  312. this.popup = $(this.options.popup, this.fade);
  313. this.btnDismiss = $(this.options.btnDismiss, this.popup);
  314. this.btnHide = $(this.options.btnHide, this.popup);
  315. this._events();
  316. },
  317. /**
  318. * @private
  319. */
  320. _events: function () {
  321. var self = this;
  322. this.btnDismiss
  323. .on('click.dismissModalPopup', function () {
  324. self.fade.remove();
  325. });
  326. this.btnHide
  327. .on('click.hideModalPopup', function () {
  328. self.fade.hide();
  329. });
  330. }
  331. });
  332. return $.mage.modalPopup;
  333. });
  334. define('useDefault', [
  335. 'jquery',
  336. 'jquery/ui'
  337. ], function ($) {
  338. 'use strict';
  339. $.widget('mage.useDefault', {
  340. options: {
  341. field: '.field',
  342. useDefault: '.use-default',
  343. checkbox: '.use-default-control',
  344. label: '.use-default-label'
  345. },
  346. /** @inheritdoc */
  347. _create: function () {
  348. this.el = this.element;
  349. this.field = $(this.el).closest(this.options.field);
  350. this.useDefault = $(this.options.useDefault, this.field);
  351. this.checkbox = $(this.options.checkbox, this.useDefault);
  352. this.label = $(this.options.label, this.useDefault);
  353. this.origValue = this.el.attr('data-store-label');
  354. this._events();
  355. },
  356. /**
  357. * @private
  358. */
  359. _events: function () {
  360. var self = this;
  361. this.el.on(
  362. 'change.toggleUseDefaultVisibility keyup.toggleUseDefaultVisibility',
  363. $.proxy(this._toggleUseDefaultVisibility, this)
  364. ).trigger('change.toggleUseDefaultVisibility');
  365. this.checkboxon('change.setOrigValue', function () {
  366. if ($(this).prop('checked')) {
  367. self.el
  368. .val(self.origValue)
  369. .trigger('change.toggleUseDefaultVisibility');
  370. $(this).prop('checked', false);
  371. }
  372. });
  373. },
  374. /**
  375. * @private
  376. */
  377. _toggleUseDefaultVisibility: function () {
  378. var curValue = this.el.val(),
  379. origValue = this.origValue;
  380. this[curValue != origValue ? '_show' : '_hide'](); //eslint-disable-line eqeqeq
  381. },
  382. /**
  383. * @private
  384. */
  385. _show: function () {
  386. this.useDefault.show();
  387. },
  388. /**
  389. * @private
  390. */
  391. _hide: function () {
  392. this.useDefault.hide();
  393. }
  394. });
  395. return $.mage.useDefault;
  396. });
  397. define('loadingPopup', [
  398. 'jquery',
  399. 'jquery/ui'
  400. ], function ($) {
  401. 'use strict';
  402. $.widget('mage.loadingPopup', {
  403. options: {
  404. message: 'Please wait...',
  405. timeout: 5000,
  406. timeoutId: null,
  407. callback: null,
  408. template: null
  409. },
  410. /** @inheritdoc */
  411. _create: function () {
  412. this.template =
  413. '<div class="popup popup-loading">' +
  414. '<div class="popup-inner">' + this.options.message + '</div>' +
  415. '</div>';
  416. this.popup = $(this.template);
  417. this._show();
  418. this._events();
  419. },
  420. /**
  421. * @private
  422. */
  423. _events: function () {
  424. var self = this;
  425. this.element
  426. .on('showLoadingPopup', function () {
  427. self._show();
  428. })
  429. .on('hideLoadingPopup', function () {
  430. self._hide();
  431. });
  432. },
  433. /**
  434. * @private
  435. */
  436. _show: function () {
  437. var options = this.options,
  438. timeout = options.timeout;
  439. $('body').trigger('processStart');
  440. if (timeout) {
  441. options.timeoutId = setTimeout(this._delayedHide.bind(this), timeout);
  442. }
  443. },
  444. /**
  445. * @private
  446. */
  447. _hide: function () {
  448. $('body').trigger('processStop');
  449. },
  450. /**
  451. * @private
  452. */
  453. _delayedHide: function () {
  454. this._hide();
  455. this.options.callback && this.options.callback();
  456. this.options.timeoutId && clearTimeout(this.options.timeoutId);
  457. }
  458. });
  459. return $.mage.loadingPopup;
  460. });
  461. define('collapsable', [
  462. 'jquery',
  463. 'jquery/ui',
  464. 'jquery/jquery.tabs'
  465. ], function ($) {
  466. 'use strict';
  467. $.widget('mage.collapsable', {
  468. options: {
  469. parent: null,
  470. openedClass: 'opened',
  471. wrapper: '.fieldset-wrapper'
  472. },
  473. /** @inheritdoc */
  474. _create: function () {
  475. this._events();
  476. },
  477. /** @inheritdoc */
  478. _events: function () {
  479. var self = this;
  480. this.element
  481. .on('show.bs.collapse', function (e) {
  482. var fieldsetWrapper = $(this).closest(self.options.wrapper);
  483. fieldsetWrapper.addClass(self.options.openedClass);
  484. e.stopPropagation();
  485. })
  486. .on('hide.bs.collapse', function (e) {
  487. var fieldsetWrapper = $(this).closest(self.options.wrapper);
  488. fieldsetWrapper.removeClass(self.options.openedClass);
  489. e.stopPropagation();
  490. });
  491. }
  492. });
  493. return $.mage.collapsable;
  494. });
  495. define('js/theme', [
  496. 'jquery',
  497. 'mage/smart-keyboard-handler',
  498. 'mage/ie-class-fixer',
  499. 'collapsable',
  500. 'domReady!'
  501. ], function ($, keyboardHandler) {
  502. 'use strict';
  503. /* @TODO refactor collapsible as widget and avoid logic binding with such a general selectors */
  504. $('.collapse').collapsable();
  505. $.each($('.entry-edit'), function (i, entry) {
  506. $('.collapse:first', entry).filter(function () {
  507. return $(this).data('collapsed') !== true;
  508. }).collapse('show');
  509. });
  510. keyboardHandler.apply();
  511. });