collapsible.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'jquery',
  7. 'jquery/ui',
  8. 'jquery/jquery-storageapi',
  9. 'mage/mage'
  10. ], function ($) {
  11. 'use strict';
  12. var hideProps = {},
  13. showProps = {};
  14. hideProps.height = 'hide';
  15. showProps.height = 'show';
  16. $.widget('mage.collapsible', {
  17. options: {
  18. active: false,
  19. disabled: false,
  20. collapsible: true,
  21. header: '[data-role=title]',
  22. content: '[data-role=content]',
  23. trigger: '[data-role=trigger]',
  24. closedState: null,
  25. openedState: null,
  26. disabledState: null,
  27. ajaxUrlElement: '[data-ajax=true]',
  28. ajaxContent: false,
  29. loadingClass: null,
  30. saveState: false,
  31. animate: false,
  32. icons: {
  33. activeHeader: null,
  34. header: null
  35. },
  36. collateral: {
  37. element: null,
  38. openedState: null
  39. }
  40. },
  41. /**
  42. * @private
  43. */
  44. _create: function () {
  45. this.storage = $.localStorage;
  46. this.icons = false;
  47. if (typeof this.options.icons === 'string') {
  48. this.options.icons = $.parseJSON(this.options.icons);
  49. }
  50. this._processPanels();
  51. this._processState();
  52. this._refresh();
  53. if (this.options.icons.header && this.options.icons.activeHeader) {
  54. this._createIcons();
  55. this.icons = true;
  56. }
  57. this.element.on('dimensionsChanged', function (e) {
  58. if (e.target && e.target.classList.contains('active')) {
  59. this._scrollToTopIfVisible(e.target);
  60. }
  61. }.bind(this));
  62. this._bind('click');
  63. this._trigger('created');
  64. },
  65. /**
  66. * @private
  67. */
  68. _refresh: function () {
  69. this.trigger.attr('tabIndex', 0);
  70. if (this.options.active && !this.options.disabled) {
  71. if (this.options.openedState) {
  72. this.element.addClass(this.options.openedState);
  73. }
  74. if (this.options.collateral.element && this.options.collateral.openedState) {
  75. $(this.options.collateral.element).addClass(this.options.collateral.openedState);
  76. }
  77. if (this.options.ajaxContent) {
  78. this._loadContent();
  79. }
  80. // ARIA (updates aria attributes)
  81. this.header.attr({
  82. 'aria-selected': false
  83. });
  84. } else if (this.options.disabled) {
  85. this.disable();
  86. } else {
  87. this.content.hide();
  88. if (this.options.closedState) {
  89. this.element.addClass(this.options.closedState);
  90. }
  91. }
  92. },
  93. /**
  94. * Processing the state:
  95. * If deep linking is used and the anchor is the id of the content or the content contains this id,
  96. * and the collapsible element is a nested one having collapsible parents, in order to see the content,
  97. * all the parents must be expanded.
  98. * @private
  99. */
  100. _processState: function () {
  101. var anchor = window.location.hash,
  102. isValid = $.mage.isValidSelector(anchor),
  103. urlPath = window.location.pathname.replace(/\./g, ''),
  104. state;
  105. this.stateKey = encodeURIComponent(urlPath + this.element.attr('id'));
  106. if (isValid &&
  107. ($(this.content.find(anchor)).length > 0 || this.content.attr('id') === anchor.replace('#', ''))
  108. ) {
  109. this.element.parents('[data-collapsible=true]').collapsible('forceActivate');
  110. if (!this.options.disabled) {
  111. this.options.active = true;
  112. if (this.options.saveState) { //eslint-disable-line max-depth
  113. this.storage.set(this.stateKey, true);
  114. }
  115. }
  116. } else if (this.options.saveState && !this.options.disabled) {
  117. state = this.storage.get(this.stateKey);
  118. if (typeof state === 'undefined' || state === null) {
  119. this.storage.set(this.stateKey, this.options.active);
  120. } else if (state === true) {
  121. this.options.active = true;
  122. } else if (state === false) {
  123. this.options.active = false;
  124. }
  125. }
  126. },
  127. /**
  128. * @private
  129. */
  130. _createIcons: function () {
  131. var icons = this.options.icons;
  132. if (icons) {
  133. $('<span>')
  134. .addClass(icons.header)
  135. .attr('data-role', 'icons')
  136. .prependTo(this.header);
  137. if (this.options.active && !this.options.disabled) {
  138. this.header.children('[data-role=icons]')
  139. .removeClass(icons.header)
  140. .addClass(icons.activeHeader);
  141. }
  142. }
  143. },
  144. /**
  145. * @private
  146. */
  147. _destroyIcons: function () {
  148. this.header
  149. .children('[data-role=icons]')
  150. .remove();
  151. },
  152. /**
  153. * @private
  154. */
  155. _destroy: function () {
  156. var options = this.options;
  157. this.element.removeAttr('data-collapsible');
  158. this.trigger.removeAttr('tabIndex');
  159. if (options.openedState) {
  160. this.element.removeClass(options.openedState);
  161. }
  162. if (this.options.collateral.element && this.options.collateral.openedState) {
  163. $(this.options.collateral.element).removeClass(this.options.collateral.openedState);
  164. }
  165. if (options.closedState) {
  166. this.element.removeClass(options.closedState);
  167. }
  168. if (options.disabledState) {
  169. this.element.removeClass(options.disabledState);
  170. }
  171. if (this.icons) {
  172. this._destroyIcons();
  173. }
  174. },
  175. /**
  176. * @private
  177. */
  178. _processPanels: function () {
  179. var headers, triggers;
  180. this.element.attr('data-collapsible', 'true');
  181. if (typeof this.options.header === 'object') {
  182. this.header = this.options.header;
  183. } else {
  184. headers = this.element.find(this.options.header);
  185. if (headers.length > 0) {
  186. this.header = headers.eq(0);
  187. } else {
  188. this.header = this.element;
  189. }
  190. }
  191. if (typeof this.options.content === 'object') {
  192. this.content = this.options.content;
  193. } else {
  194. this.content = this.header.next(this.options.content).eq(0);
  195. }
  196. // ARIA (init aria attributes)
  197. if (this.header.attr('id')) {
  198. this.content.attr('aria-labelledby', this.header.attr('id'));
  199. }
  200. if (this.content.attr('id')) {
  201. this.header.attr('aria-controls', this.content.attr('id'));
  202. }
  203. this.header
  204. .attr({
  205. 'role': 'tab',
  206. 'aria-selected': this.options.active,
  207. 'aria-expanded': this.options.active
  208. });
  209. // For collapsible widget only (not tabs or accordion)
  210. if (this.header.parent().attr('role') !== 'presentation') {
  211. this.header
  212. .parent()
  213. .attr('role', 'tablist');
  214. }
  215. this.content.attr({
  216. 'role': 'tabpanel',
  217. 'aria-hidden': !this.options.active
  218. });
  219. if (typeof this.options.trigger === 'object') {
  220. this.trigger = this.options.trigger;
  221. } else {
  222. triggers = this.header.find(this.options.trigger);
  223. if (triggers.length > 0) {
  224. this.trigger = triggers.eq(0);
  225. } else {
  226. this.trigger = this.header;
  227. }
  228. }
  229. },
  230. /**
  231. * @param {jQuery.Event} event
  232. * @private
  233. */
  234. _keydown: function (event) {
  235. var keyCode;
  236. if (event.altKey || event.ctrlKey) {
  237. return;
  238. }
  239. keyCode = $.ui.keyCode;
  240. switch (event.keyCode) {
  241. case keyCode.SPACE:
  242. case keyCode.ENTER:
  243. this._eventHandler(event);
  244. break;
  245. }
  246. },
  247. /**
  248. * @param {jQuery.Event} event
  249. * @private
  250. */
  251. _bind: function (event) {
  252. var self = this;
  253. this.events = {
  254. keydown: '_keydown'
  255. };
  256. if (event) {
  257. $.each(event.split(' '), function (index, eventName) {
  258. self.events[ eventName ] = '_eventHandler';
  259. });
  260. }
  261. this._off(this.trigger);
  262. if (!this.options.disabled) {
  263. this._on(this.trigger, this.events);
  264. }
  265. },
  266. /**
  267. * Disable.
  268. */
  269. disable: function () {
  270. this.options.disabled = true;
  271. this._off(this.trigger);
  272. this.forceDeactivate();
  273. if (this.options.disabledState) {
  274. this.element.addClass(this.options.disabledState);
  275. }
  276. this.trigger.attr('tabIndex', -1);
  277. },
  278. /**
  279. * Enable.
  280. */
  281. enable: function () {
  282. this.options.disabled = false;
  283. this._on(this.trigger, this.events);
  284. this.forceActivate();
  285. if (this.options.disabledState) {
  286. this.element.removeClass(this.options.disabledState);
  287. }
  288. this.trigger.attr('tabIndex', 0);
  289. },
  290. /**
  291. * @param {jQuery.Event} event
  292. * @private
  293. */
  294. _eventHandler: function (event) {
  295. if (this.options.active && this.options.collapsible) {
  296. this.deactivate();
  297. } else {
  298. this.activate();
  299. }
  300. event.preventDefault();
  301. },
  302. /**
  303. * @param {*} prop
  304. * @private
  305. */
  306. _animate: function (prop) {
  307. var duration,
  308. easing,
  309. animate = this.options.animate;
  310. if (typeof animate === 'number') {
  311. duration = animate;
  312. }
  313. if (typeof animate === 'string') {
  314. animate = $.parseJSON(animate);
  315. }
  316. duration = duration || animate.duration;
  317. easing = animate.easing;
  318. this.content.animate(prop, duration, easing);
  319. },
  320. /**
  321. * Deactivate.
  322. */
  323. deactivate: function () {
  324. if (this.options.animate) {
  325. this._animate(hideProps);
  326. } else {
  327. this.content.hide();
  328. }
  329. this._close();
  330. },
  331. /**
  332. * Force deactivate.
  333. */
  334. forceDeactivate: function () {
  335. this.content.hide();
  336. this._close();
  337. },
  338. /**
  339. * @private
  340. */
  341. _close: function () {
  342. this.options.active = false;
  343. if (this.options.saveState) {
  344. this.storage.set(this.stateKey, false);
  345. }
  346. if (this.options.openedState) {
  347. this.element.removeClass(this.options.openedState);
  348. }
  349. if (this.options.collateral.element && this.options.collateral.openedState) {
  350. $(this.options.collateral.element).removeClass(this.options.collateral.openedState);
  351. }
  352. if (this.options.closedState) {
  353. this.element.addClass(this.options.closedState);
  354. }
  355. if (this.icons) {
  356. this.header.children('[data-role=icons]')
  357. .removeClass(this.options.icons.activeHeader)
  358. .addClass(this.options.icons.header);
  359. }
  360. // ARIA (updates aria attributes)
  361. this.header.attr({
  362. 'aria-selected': 'false',
  363. 'aria-expanded': 'false'
  364. });
  365. this.content.attr({
  366. 'aria-hidden': 'true'
  367. });
  368. this.element.trigger('dimensionsChanged', {
  369. opened: false
  370. });
  371. },
  372. /**
  373. * Activate.
  374. *
  375. * @return void;
  376. */
  377. activate: function () {
  378. if (this.options.disabled) {
  379. return;
  380. }
  381. if (this.options.animate) {
  382. this._animate(showProps);
  383. } else {
  384. this.content.show();
  385. }
  386. this._open();
  387. },
  388. /**
  389. * Force activate.
  390. */
  391. forceActivate: function () {
  392. if (!this.options.disabled) {
  393. this.content.show();
  394. this._open();
  395. }
  396. },
  397. /**
  398. * @private
  399. */
  400. _open: function () {
  401. this.element.trigger('beforeOpen');
  402. this.options.active = true;
  403. if (this.options.ajaxContent) {
  404. this._loadContent();
  405. }
  406. if (this.options.saveState) {
  407. this.storage.set(this.stateKey, true);
  408. }
  409. if (this.options.openedState) {
  410. this.element.addClass(this.options.openedState);
  411. }
  412. if (this.options.collateral.element && this.options.collateral.openedState) {
  413. $(this.options.collateral.element).addClass(this.options.collateral.openedState);
  414. }
  415. if (this.options.closedState) {
  416. this.element.removeClass(this.options.closedState);
  417. }
  418. if (this.icons) {
  419. this.header.children('[data-role=icons]')
  420. .removeClass(this.options.icons.header)
  421. .addClass(this.options.icons.activeHeader);
  422. }
  423. // ARIA (updates aria attributes)
  424. this.header.attr({
  425. 'aria-selected': 'true',
  426. 'aria-expanded': 'true'
  427. });
  428. this.content.attr({
  429. 'aria-hidden': 'false'
  430. });
  431. this.element.trigger('dimensionsChanged', {
  432. opened: true
  433. });
  434. },
  435. /**
  436. * @private
  437. */
  438. _loadContent: function () {
  439. var url = this.element.find(this.options.ajaxUrlElement).attr('href'),
  440. that = this;
  441. if (url) {
  442. that.xhr = $.get({
  443. url: url,
  444. dataType: 'html'
  445. }, function () {
  446. });
  447. }
  448. if (that.xhr && that.xhr.statusText !== 'canceled') {
  449. if (that.options.loadingClass) {
  450. that.element.addClass(that.options.loadingClass);
  451. }
  452. that.content.attr('aria-busy', 'true');
  453. that.xhr.done(function (response) {
  454. setTimeout(function () {
  455. that.content.html(response);
  456. }, 1);
  457. });
  458. that.xhr.always(function (jqXHR, status) {
  459. setTimeout(function () {
  460. if (status === 'abort') {
  461. that.content.stop(false, true);
  462. }
  463. if (that.options.loadingClass) {
  464. that.element.removeClass(that.options.loadingClass);
  465. }
  466. that.content.removeAttr('aria-busy');
  467. if (jqXHR === that.xhr) {
  468. delete that.xhr;
  469. }
  470. }, 1);
  471. });
  472. }
  473. },
  474. /**
  475. * @param {HTMLElement} elem
  476. * @private
  477. */
  478. _scrollToTopIfVisible: function (elem) {
  479. if (this._isElementOutOfViewport(elem)) {
  480. elem.scrollIntoView();
  481. }
  482. },
  483. /**
  484. * @param {HTMLElement} elem
  485. * @private
  486. * @return {Boolean}
  487. */
  488. _isElementOutOfViewport: function (elem) {
  489. var rect = elem.getBoundingClientRect();
  490. return rect.bottom < 0 || rect.right < 0 || rect.left > window.innerWidth || rect.top > window.innerHeight;
  491. }
  492. });
  493. return $.mage.collapsible;
  494. });