/*! * jQuery Pretty Dropdowns Plugin v2.0.0 by T. H. Doan (http://thdoan.github.io/pretty-dropdowns/) * * jQuery Pretty Dropdowns by T. H. Doan is licensed under the MIT License. * Read a copy of the license in the LICENSE file or at * http://choosealicense.com/licenses/mit */ (function($) { $.fn.prettyDropdown = function(oOptions) { // Default options oOptions = $.extend({ customClass: 'arrow', height: 48, hoverIntent: 100 }, oOptions); var nTimer, resetDropdown = function(o) { var $dropdown = $(o.currentTarget||o); $dropdown.data('hover', false); clearTimeout(nTimer); nTimer = setTimeout(function() { if (!$dropdown.data('hover')) { if ($dropdown.hasClass('reverse')) $dropdown.prepend($('li:last-child', $dropdown)); $dropdown.removeClass('active reverse').css('height', ''); } }, (o.type==='mouseleave') ? oOptions.hoverIntent : 0); }; // Validate options if (isNaN(oOptions.height) || oOptions.height<8) oOptions.height = 8; if (isNaN(oOptions.hoverIntent) || oOptions.hoverIntent<0) oOptions.hoverIntent = 200; return this.each(function() { var $this = $(this); if ($this.data('loaded')) return true; // Continue $this.outerHeight(oOptions.height); // NOTE: $this.css('margin') returns empty string in Firefox. // See https://github.com/jquery/jquery/issues/3383 var nWidth = $this.outerWidth(), sHtml = '', renderItem = function(el, sClass) { return '
  • ' + el.text + '
  • '; }; $('option:selected', $this).each(function() { sHtml += renderItem(this, 'selected'); }); $('option:not(:selected)', $this).each(function() { sHtml += renderItem(this); }); sHtml += ''; $this.css('visibility', 'hidden').wrap('
    ').before(sHtml).data('loaded', true); var $dropdown = $('ul', $this.parent()), nWidth = $dropdown.outerWidth(true), nOuterWidth; // Calculate width if initially hidden if ($dropdown.width()<=0) { var $clone = $dropdown.parent().clone().css({ position: 'absolute', top: '-100%' }); $('body').append($clone); nWidth = $('ul', $clone).outerWidth(true); $('li', $clone).width(nWidth); nOuterWidth = $('ul', $clone).outerWidth(true); $clone.remove(); } // Set dropdown width and event handler $('li', $dropdown).width(nWidth).click(function() { var $li = $(this); // Only update if different value selected if ($dropdown.hasClass('active') && $li.data('value')!==$('li.selected', $dropdown).data('value')) { $('.selected', $dropdown).removeClass('selected'); $dropdown.prepend($li.addClass('selected')).removeClass('reverse'); // Sync