1/* 2 * Plugin name: jQuery feastSelect plugin 3 * Plugin Version: 1.1 4 * Pluign URI: http://eadhassan.com/feastselect 5 * Author: Ead Hassan 6 * Author Email: [email protected] 7 * Author URI: http://eadhassan.com 8 */ 9(function($, window, document, undefined) { 10 "use strict"; 11 $.fn.FeastSelect = function(options){ 12 13 // Default options 14 var defaults = { 15 btnClass : 'btn-info', 16 btnText : 'Select your option', 17 showEffect : 'slideDown', // fade, hide, slideDown 18 hideEffect : 'slideUp', // fade, hide, slideDown 19 iconPlace : 'left', // left or right 20 hideOriginal : true 21 }; 22 23 24 var options = $.extend(defaults, options); 25 26 var errors = false; 27 28 return this.each(function() { 29 30 var $this = $(this), 31 $selectOptions = $('option', this), 32 dropdownId = 'feastSelect_'+ $this.attr('name'), 33 listContainer = $('<ul></ul>').addClass('dropdown-menu calendar-dropdown').attr('aria-labelledby', dropdownId), 34 listItems = {}; 35 36 $selectOptions.each(function(index, ele) { 37 if($(this).attr('selected') == 'selected') { 38 options.btnText = $(this).text(); 39 } 40 }); 41 42 var buttonDom = $('<button></button>').addClass('btn dropdown-toggle ' + options.btnClass).html(options.btnText + '<span class="caret"></span>'); 43 44 if(options.hideOriginal == "false" || options.hideOriginal == 'false') { 45 options.hideOriginal = false;
46 } 47 48 $this.addClass('feastSelect__initialized'); 49 $this.parent().css('position', 'relative'); 50 buttonDom.attr('id', dropdownId) 51 .addClass(dropdownId) 52 .data('toggle', 'dropdown') 53 .attr('aria-haspopup', 'true') 54 .attr('aria-expand', 'true') 55 .click(function(e) { 56 e.stopPropagation(); 57 e.preventDefault(); 58 hideAnimate($('.dropdown-menu').not('.'+dropdownId)); 59 if( $(this).next('.dropdown-menu').is(":hidden") ) { 60 showAnimate($(this).next('.dropdown-menu')) 61 } else { 62 hideAnimate($(this).next('.dropdown-menu')); 63 } 64 }); 65 66 document.addEventListener('click', function(e) { 67 if(hasClass(e.target, dropdownId)) { 68 return; 69 } else { 70 hideAnimate($('.dropdown-menu.' + dropdownId)); 71 } 72 }); 73 74 $selectOptions.each(function(index, ele) { 75 if($(this).attr('value') === "" || $(this).attr('value') === undefined) { 76 return; 77 } else { 78 var value = $(this).attr('value'), 79 text = $(this).text(), 80 selected = $(this).attr('selected') == 'selected' ? true : false, 81 icon = $(this).data('icon') || false; 82 listItems[index] = {id: value, text: text, icon: icon, selected: selected}; 83 } 84 }); 85 86 if( options.hideOriginal === true ) { 87 $this.hide(); 88 } 89 $.each(listItems, function(index, value) { 90 var itemId = value.id; 91 var itemText = value.text; 92 var itemIcon = value.icon; 93 var itemDom = $('<li>').attr('id', $this.attr('id') +itemId).text(itemText); 94 if (value.selected) 95 itemDom.addClass('current'); 96 97 // Check if there is data-icon in item and append it to li 98 if( itemIcon ) { 99 if(options.iconPlace === 'left') { 100 itemDom.prepend($('<i>').attr('class', itemIcon).css('marginRight', '5px')); 101 } else { 102 itemDom.append($('<i>').attr('class', itemIcon).css('marginLeft', '5px')); 103 } 104 } 105 itemDom.click(function(i) { 106 i.preventDefault(); 107 i.stopPropagation(); 108 109 var val = $(this).attr('id').replace( $this.attr('id'), '' ); 110 111 $this.val( val ); 112 hideAnimate($(this).parent('.dropdown-menu')); 113 buttonDom.text( $(this).text() ); 114 //console.log('Selected option is: ' + $this.val()); 115 $this.trigger('change.daterangepicker'); 116 }) 117 listContainer.addClass( dropdownId ); 118 listContainer.append( itemDom ); 119 }); 120 121 buttonDom.appendTo($this.parent()); 122 listContainer.appendTo($this.parent()); 123 124 $(document).on('change', 'select[name="'+$this.attr('name')+'"]', function() { 125 var value = $this.val(); 126 var text = $this.find('option[value="'+ value +'"]').text(); 127 buttonDom.text( text ); 128 }); 129 }); 130 131 132 133 function hasClass(elem, className) { 134 return elem.className.split(' ').indexOf(className) > -1; 135 } 136 137 function hideAnimate(elem) { 138 switch(options.hideEffect) { 139 case 'fadeOut': 140 elem.fadeOut('fast'); 141 break; 142 case 'hide': 143 elem.hide(); 144 break; 145 default: 146 elem.slideUp('fast'); 147 break; 148 } 149 } 150 151 function showAnimate(elem) { 152 switch(options.showEffect) { 153 case 'fadeIn': 154 elem.fadeIn('fast'); 155 break; 156 case 'show': 157 elem.show(); 158 break; 159 default: 160 elem.slideDown('fast'); 161 break; 162 } 163 } 164 165 166 } 167 168 $(document).ready(function() { 169 if( $("[data-feast-select]").length ) { 170 $("[data-feast-select]").not('.feastSelect__initialized').each(function() { 171 $(this).FeastSelect({ 172 btnClass : $(this).data('btn-class') || options.btnClass, 173 btnText : $(this).data('btn-text') || options.btnText, 174 hideEffect : $(this).data('hide-effect') || options.hideEffect, 175 showEffect : $(this).data('show-effect') || options.showEffect, 176 iconPlace : $(this).data('icon-place') || options.iconPlace, 177 hideOriginal: $(this).data('hide-original') || options.hideOriginal 178 }); 179 }); 180 } 181 }); 182 183 184}(jQuery, window, document));
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.