1/** 2 * Copyright © Magento, Inc. All rights reserved. 3 * See COPYING.txt for license details. 4 */ 5 6define([ 7 'jquery' 8], function ($) { 9 'use strict'; 10 11 /** 12 * @param {Object} options 13 */ 14 $.fn.dropdown = function (options) { 15 var defaults = { 16 parent: null, 17 autoclose: true, 18 btnArrow: '.arrow', 19 menu: '[data-target="dropdown"]', 20 activeClass: 'active' 21 }, 22 actionElem = $(this), 23 self = this; 24 25 options = $.extend(defaults, options); 26 actionElem = $(this); 27 self = this; 28 29 /** 30 * @param {HTMLElement} elem 31 */ 32 this.openDropdown = function (elem) { 33 elem 34 .addClass(options.activeClass) 35 .attr('aria-expanded', true) 36 .parent() 37 .addClass(options.activeClass); 38 39 elem.parent() 40 .find(options.menu) 41 .attr('aria-hidden', false); 42 43 $(options.btnArrow, elem).text('-'); 44 }; 45 46 /** 47 * @param {HTMLElement} elem 48 */ 49 this.closeDropdown = function (elem) { 50 elem.removeClass(options.activeClass) 51 .attr('aria-expanded', false) 52 .parent() 53 .removeClass(options.activeClass); 54 55 elem.parent() 56 .find(options.menu) 57 .attr('aria-hidden', true); 58 59 $(options.btnArrow, elem).text('+'); 60 }; 61 62 /** 63 * Reset all dropdowns. 64 * 65 * @param {Object} param 66 */ 67 this.reset = function (param) { 68 var params = param || {}, 69 dropdowns = params.elems || actionElem; 70 71 dropdowns.each(function (index, elem) { 72 self.closeDropdown($(elem)); 73 }); 74 }; 75 76 /* document Event bindings */ 77 if (options.autoclose === true) { 78 $(document).on('click.hideDropdown', this.reset); 79 $(document).on('keyup.hideDropdown', function (e) { 80 var ESC_CODE = '27'; 81 82 if (e.keyCode == ESC_CODE) { //eslint-disable-line eqeqeq 83 self.reset(); 84 } 85 }); 86 } 87 88 if (options.events) { 89 $.each(options.events, function (index, event) { 90 $(document).on(event.name, event.selector, event.action); 91 }); 92 } 93 94 return this.each(function () { 95 var elem = $(this), 96 parent = $(options.parent).length > 0 ? $(options.parent) : elem.parent(), 97 menu = $(options.menu, parent) || $('.dropdown-menu', parent); 98 99 // ARIA (adding aria attributes) 100 if (menu.length) { 101 elem.attr('aria-haspopup', true); 102 } 103 104 if (!elem.hasClass(options.activeClass)) { 105 elem.attr('aria-expanded', false); 106 menu.attr('aria-hidden', true); 107 } else { 108 elem.attr('aria-expanded', true); 109 menu.attr('aria-hidden', false); 110 } 111 112 if (!elem.is('a, button')) { 113 elem.attr('role', 'button'); 114 elem.attr('tabindex', 0); 115 } 116 117 if (elem.attr('data-trigger-keypress-button')) { 118 elem.on('keypress', function (e) { 119 var keyCode = e.keyCode || e.which, 120 ENTER_CODE = 13; 121 122 if (keyCode === ENTER_CODE) { 123 e.preventDefault(); 124 elem.trigger('click.toggleDropdown'); 125 } 126 }); 127 } 128 129 elem.on('click.toggleDropdown', function () { 130 var el = actionElem; 131 132 if (options.autoclose === true) { 133 actionElem = $(); 134 $(document).trigger('click.hideDropdown'); 135 actionElem = el; 136 } 137 138 self[el.hasClass(options.activeClass) ? 'closeDropdown' : 'openDropdown'](elem); 139 140 return false;
141 }); 142 }); 143 }; 144 145 return function (data, el) { 146 $(el).dropdown(data); 147 }; 148});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.