; (function ($, window) { "use strict"; var pluginName = "DropDown", defaults = { title: "", multiple: false, css_class: '', asRadio: false }; function Plugin(element, options) { this.element = element; this.settings = $.extend({}, defaults, options); this._defaults = defaults; this._name = pluginName; this.wmp_dropdown_list = null; this.init(); } // Avoid Plugin.prototype conflicts $.extend(Plugin.prototype, { init: function () { this.render(); this.onChange(); this.onScroll(); }, render: function () { var plugin = this; if (!plugin.settings.asRadio) { this.wmp_dropdown = $('
').addClass(plugin.settings.css_class); $(this.element).parent().append(this.wmp_dropdown); this.wmp_dropdown.text(this.getValue().text); plugin.wmp_dropdown.click(function () { plugin.openDropDown(); }); } else { $(this.element).parent().append(this.wmp_dropdown); plugin.onItemClick(); } }, onItemClick: function () { var plugin = this; var $items = null; var val = []; if (!plugin.settings.asRadio) { $items = plugin.wmp_dropdown_list.find('.wmp-drop-down-list-items').children(); } else { $items = $(plugin.element).parent().find('ul li'); } $items.click(function () { if (!plugin.settings.asRadio) { if (plugin.settings.multiple) { $(this).toggleClass('selected'); $items.filter('.selected').each(function () { val.push($(this).attr('data-item-value')); }); if ($(plugin.element).val() != val) { $(plugin.element).val(val).trigger('change'); } } else { val = $(this).attr('data-item-value'); if ($(plugin.element).val() != val) { $(plugin.element).val(val).trigger('change'); } plugin.closeDropDown(); } } else { val = $(this).attr('data-item-value'); if ($(plugin.element).val() != val) { $(plugin.element).val(val).trigger('change'); $(this).parent().find('li.active').removeClass('active'); $(this).addClass('active'); } } }); }, onChange: function () { var plugin = this; $(plugin.element).change(function () { if (!plugin.settings.asRadio) { plugin.wmp_dropdown.text(plugin.getValue().text); } else { var options = plugin.getAllOptions(); $(plugin.element).parent().find('ul > li').hide().removeClass('active'); $(options).each(function () { var option = this; var $item = $('.wmp-dropdown > ul > li[data-item-value=' + option.val + ']'); if (option.selected) { $item.addClass('active'); } $item.show(); }); } }); }, onScroll: function () { var plugin = this; $(window).scroll(function () { plugin.updateListPosition(); }); }, getValue: function () { var element = $(this.element); var result = {val: '', 'text': 'Не выбрано'}; if (element.val() != null) { var text = []; element.find('option:selected').each(function () { text.push($(this).text()); }); text = text.join(', '); result = {val: element.val(), text: text}; } return result; }, getAllOptions: function () { var element = $(this.element); var options = []; element.find('option').each(function () { var option = $(this); options.push({val: option.val(), label: option.attr('data-label'), text: option.text(), image: option.attr('data-image'), selected: option.is(':selected')}); }); return options; }, openDropDown: function () { var plugin = this; var options = plugin.getAllOptions(); var $wrapper = $('
'); $wrapper.appendTo('body'); $wrapper.click(function () { plugin.closeDropDown(); }); plugin.wmp_dropdown_list = $('
').addClass(plugin.settings.css_class).hide(); plugin.wmp_dropdown_list.css('min-width', plugin.wmp_dropdown.outerWidth(true)); if (plugin.settings.title) { plugin.wmp_dropdown_list.append('
' + plugin.settings.title + '
'); } var $close = $('
'); $close.click(function () { plugin.closeDropDown(); }); plugin.wmp_dropdown_list.append($close); var $items = $('
'); $(options).each(function () { var option = this; var $item = $('
'); $item.attr('data-item-value', option.val); $item.text(option.text); if (option.selected) { $item.addClass('selected'); } $items.append($item); }); plugin.wmp_dropdown_list.append($items); plugin.wmp_dropdown_list.appendTo('body'); plugin.updateListPosition(); plugin.onItemClick(); plugin.wmp_dropdown_list.fadeIn(); }, closeDropDown: function () { var plugin = this; if (plugin.wmp_dropdown_list) { $('.wmp-drop-down-wrapper').remove(); plugin.wmp_dropdown_list.fadeOut(function () { $(this).remove(); plugin.wmp_dropdown_list = null; }); } }, updateListPosition: function () { var plugin = this; if (plugin.wmp_dropdown_list) { var dropDownPosition = plugin.wmp_dropdown.offset(); var topDelta = 0; if (dropDownPosition.top - $(window).scrollTop() + plugin.wmp_dropdown_list.outerHeight() > $(window).height()) { topDelta = plugin.wmp_dropdown_list.outerHeight() - plugin.wmp_dropdown.outerHeight(); } plugin.wmp_dropdown_list.css({ left: dropDownPosition.left, top: dropDownPosition.top - topDelta }); } } }); $.fn[pluginName] = function (options) { return this.each(function () { if (!$.data(this, "plugin_" + pluginName)) { $.data(this, "plugin_" + pluginName, new Plugin(this, options)); } }); }; })(jQuery, window, document);