Advertisement
Guest User

Ibrahim

a guest
Feb 23rd, 2010
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * jQuery selectbox plugin
  3.  *
  4.  * Copyright (c) 2007 Sadri Sahraoui (brainfault.com)
  5.  * Licensed under the GPL license and MIT:
  6.  *   http://www.opensource.org/licenses/GPL-license.php
  7.  *   http://www.opensource.org/licenses/mit-license.php
  8.  *
  9.  * The code is inspired from Autocomplete plugin (http://www.dyve.net/jquery/?autocomplete)
  10.  *
  11.  * Revision: $Id$
  12.  * Version: 1.2
  13.  *
  14.  * Changelog :
  15.  *  Version 1.2 By Guillaume Vergnolle (web-opensource.com)
  16.  *  - Add optgroup support
  17.  *  - possibility to choose between span or input as replacement of the select box
  18.  *  - support for jquery change event
  19.  *  - add a max height option for drop down list
  20.  *  Version 1.1
  21.  *  - Fix IE bug
  22.  *  Version 1.0
  23.  *  - Support jQuery noConflict option
  24.  *  - Add callback for onChange event, thanks to Jason
  25.  *  - Fix IE8 support
  26.  *  - Fix auto width support
  27.  *  - Fix focus on firefox dont show the carret
  28.  *  Version 0.6
  29.  *  - Fix IE scrolling problem
  30.  *  Version 0.5
  31.  *  - separate css style for current selected element and hover element which solve the highlight issue
  32.  *  Version 0.4
  33.  *  - Fix width when the select is in a hidden div   @Pawel Maziarz
  34.  *  - Add a unique id for generated li to avoid conflict with other selects and empty values @Pawel Maziarz
  35.  */
  36. jQuery.fn.extend({
  37.     selectbox: function(options) {
  38.         return this.each(function() {
  39.             new jQuery.SelectBox(this, options);
  40.         });
  41.     }
  42. });
  43.  
  44.  
  45. /* pawel maziarz: work around for ie logging */
  46. if (!window.console) {
  47.     var console = {
  48.         log: function(msg) {
  49.         }
  50.     }
  51. }
  52. /* */
  53.  
  54. jQuery.SelectBox = function(selectobj, options) {
  55.    
  56.     var opt = options || {};
  57.     opt.inputType = opt.inputType || "input";
  58.     opt.inputClass = opt.inputClass || "selectbox";
  59.     opt.containerClass = opt.containerClass || "selectbox-wrapper";
  60.     opt.hoverClass = opt.hoverClass || "current";
  61.     opt.currentClass = opt.currentClass || "selected";
  62.     opt.groupClass = opt.groupClass || "groupname"; //css class for group
  63.     opt.maxHeight = opt.maxHeight || 200; // max height of dropdown list
  64.     opt.loopnoStep = opt.loopnoStep || false; // to remove the step in list moves loop
  65.     opt.onChangeCallback = opt.onChangeCallback || false;
  66.     opt.onChangeParams = opt.onChangeParams || false;
  67.     opt.debug = opt.debug || false;
  68.    
  69.     var elm_id = selectobj.id;
  70.     var active = 0;
  71.     var inFocus = false;
  72.     var hasfocus = 0;
  73.     //jquery object for select element
  74.     var $select = jQuery(selectobj);
  75.     // jquery container object
  76.     var $container = setupContainer(opt);
  77.     //jquery input object
  78.     var $input = setupInput(opt);
  79.     // hide select and append newly created elements
  80.     $select.hide().before($input).before($container);
  81.    
  82.    
  83.     init();
  84.    
  85.     $input
  86.     .click(function(){
  87.         if (!inFocus) {
  88.             $container.toggle();
  89.         }
  90.     })
  91.     .focus(function(){
  92.         if ($container.not(':visible')) {
  93.             inFocus = true;
  94.             $container.show();
  95.         }
  96.     })
  97.     .keydown(function(event) {     
  98.         switch(event.keyCode) {
  99.             case 38: // up
  100.                 event.preventDefault();
  101.                 moveSelect(-1);
  102.                 break;
  103.             case 40: // down
  104.                 event.preventDefault();
  105.                 moveSelect(1);
  106.                 break;
  107.             //case 9:  // tab
  108.             case 13: // return
  109.                 event.preventDefault(); // seems not working in mac !
  110.                 $('li.'+opt.hoverClass).trigger('click');
  111.                 break;
  112.             case 27: //escape
  113.               hideMe();
  114.               break;
  115.         }
  116.     })
  117.     .blur(function() {
  118.         if ($container.is(':visible') && hasfocus > 0 ) {
  119.             if(opt.debug) console.log('container visible and has focus')
  120.         } else {
  121.             // Workaround for ie scroll - thanks to Bernd Matzner
  122.             if((jQuery.browser.msie && jQuery.browser.version.substr(0,1) < 8) || jQuery.browser.safari){ // check for safari too - workaround for webkit
  123.                 if(document.activeElement.getAttribute('id').indexOf('_container')==-1){
  124.                     hideMe();
  125.                 } else {
  126.                     $input.focus();
  127.                 }
  128.             } else {
  129.                 hideMe();
  130.             }
  131.         }
  132.     });
  133.  
  134.     function hideMe() {
  135.         hasfocus = 0;
  136.         $container.hide();
  137.     }
  138.    
  139.     function init() {
  140.         $container.append(getSelectOptions($input.attr('id'))).hide();
  141.         var width = $input.css('width');
  142.         if($container.height() > opt.maxHeight){
  143.             $container.width(parseInt(width)+parseInt($input.css('paddingRight'))+parseInt($input.css('paddingLeft')));
  144.             $container.height(opt.maxHeight);
  145.         } else $container.width(width);
  146.     }
  147.    
  148.     function setupContainer(options) {
  149.         var container = document.createElement("div");
  150.         $container = jQuery(container);
  151.         $container.attr('id', elm_id+'_container');
  152.         $container.addClass(options.containerClass);
  153.             $container.css('display', 'none');
  154.        
  155.         return $container;
  156.     }
  157.    
  158.     function setupInput(options) {
  159.         if(opt.inputType == "span"){
  160.             var input = document.createElement("span");
  161.             var $input = jQuery(input);
  162.             $input.attr("id", elm_id+"_input");
  163.             $input.addClass(options.inputClass);
  164.             $input.attr("tabIndex", $select.attr("tabindex"));
  165.         } else {
  166.             var input = document.createElement("input");
  167.             var $input = jQuery(input);
  168.             $input.attr("id", elm_id+"_input");
  169.             $input.attr("type", "text");
  170.             $input.addClass(options.inputClass);
  171.             $input.attr("autocomplete", "off");
  172.             $input.attr("readonly", "readonly");
  173.             $input.attr("tabIndex", $select.attr("tabindex")); // "I" capital is important for ie
  174.             $input.css("width", $select.css("width"));
  175.             }
  176.         return $input; 
  177.     }
  178.    
  179.     function moveSelect(step) {
  180.         var lis = jQuery("li", $container);
  181.         if (!lis || lis.length == 0) return false;
  182.         // find the first non-group (first option)
  183.         firstchoice = 0;
  184.         while($(lis[firstchoice]).hasClass(opt.groupClass)) firstchoice++;
  185.         active += step;
  186.             // if we are on a group step one more time
  187.             if($(lis[active]).hasClass(opt.groupClass)) active += step;
  188.         //loop through list from the first possible option
  189.         if (active < firstchoice) {
  190.             (opt.loopnoStep ? active = lis.size()-1 : active = lis.size() );
  191.         } else if (opt.loopnoStep && active > lis.size()-1) {
  192.             active = firstchoice;
  193.         } else if (active > lis.size()) {
  194.             active = firstchoice;
  195.         }
  196.             scroll(lis, active);
  197.         lis.removeClass(opt.hoverClass);
  198.  
  199.         jQuery(lis[active]).addClass(opt.hoverClass);
  200.     }
  201.    
  202.     function scroll(list, active) {
  203.             var el = jQuery(list[active]).get(0);
  204.             var list = $container.get(0);
  205.      
  206.         if (el.offsetTop + el.offsetHeight > list.scrollTop + list.clientHeight) {
  207.             list.scrollTop = el.offsetTop + el.offsetHeight - list.clientHeight;      
  208.         } else if(el.offsetTop < list.scrollTop) {
  209.             list.scrollTop = el.offsetTop;
  210.         }
  211.     }
  212.    
  213.     function setCurrent() {
  214.         var li = jQuery("li."+opt.currentClass, $container).get(0);
  215.         var ar = (''+li.id).split('_');
  216.         var el = ar[ar.length-1];
  217.         if (opt.onChangeCallback){
  218.                 $select.get(0).selectedIndex = $('li', $container).index(li);
  219.                 opt.onChangeParams = { selectedVal : $select.val() };
  220.             opt.onChangeCallback(opt.onChangeParams);
  221.         } else {
  222.             $select.val(el);
  223.             $select.change();
  224.         }
  225.         if(opt.inputType == 'span') $input.html($(li).html());
  226.         else $input.val($(li).html());
  227.         return true;
  228.     }
  229.    
  230.     // select value
  231.     function getCurrentSelected() {
  232.         return $select.val();
  233.     }
  234.    
  235.     // input value
  236.     function getCurrentValue() {
  237.         return $input.val();
  238.     }
  239.    
  240.     function getSelectOptions(parentid) {
  241.         var select_options = new Array();
  242.         var ul = document.createElement('ul');
  243.         select_options = $select.children('option');
  244.         if(select_options.length == 0) {
  245.             var select_optgroups = new Array();
  246.             select_optgroups = $select.children('optgroup');
  247.             for(x=0;x<select_optgroups.length;x++){
  248.                 select_options = $("#"+select_optgroups[x].id).children('option');
  249.                 var li = document.createElement('li');
  250.                 li.setAttribute('id', parentid + '_' + $(this).val());
  251.                 li.innerHTML = $("#"+select_optgroups[x].id).attr('label');
  252.                 li.className = opt.groupClass;
  253.                 ul.appendChild(li);
  254.                 select_options.each(function() {
  255.                     var li = document.createElement('li');
  256.                     li.setAttribute('id', parentid + '_' + $(this).val());
  257.                     li.innerHTML = $(this).html();
  258.                     if ($(this).is(':selected')) {
  259.                         $input.html($(this).html());
  260.                         $(li).addClass(opt.currentClass);
  261.                     }
  262.                     ul.appendChild(li);
  263.                     $(li)
  264.                     .mouseover(function(event) {
  265.                         hasfocus = 1;
  266.                         if (opt.debug) console.log('over on : '+this.id);
  267.                         jQuery(event.target, $container).addClass(opt.hoverClass);
  268.                     })
  269.                     .mouseout(function(event) {
  270.                         hasfocus = -1;
  271.                         if (opt.debug) console.log('out on : '+this.id);
  272.                         jQuery(event.target, $container).removeClass(opt.hoverClass);
  273.                     })
  274.                     .click(function(event) {
  275.                         var fl = $('li.'+opt.hoverClass, $container).get(0);
  276.                         if (opt.debug) console.log('click on :'+this.id);
  277.                         $('li.'+opt.currentClass, $container).removeClass(opt.currentClass);
  278.                         $(this).addClass(opt.currentClass);
  279.                         setCurrent();
  280.                         $select.get(0).blur();
  281.                         hideMe();
  282.                     });
  283.                 });
  284.             }
  285.         } else select_options.each(function() {
  286.             var li = document.createElement('li');
  287.             li.setAttribute('id', parentid + '_' + $(this).val());
  288.             li.innerHTML = $(this).html();
  289.             if ($(this).is(':selected')) {
  290.                 $input.val($(this).html());
  291.                 $(li).addClass(opt.currentClass);
  292.             }
  293.             ul.appendChild(li);
  294.             $(li)
  295.             .mouseover(function(event) {
  296.                 hasfocus = 1;
  297.                 if (opt.debug) console.log('over on : '+this.id);
  298.                 jQuery(event.target, $container).addClass(opt.hoverClass);
  299.             })
  300.             .mouseout(function(event) {
  301.                 hasfocus = -1;
  302.                 if (opt.debug) console.log('out on : '+this.id);
  303.                 jQuery(event.target, $container).removeClass(opt.hoverClass);
  304.             })
  305.             .click(function(event) {
  306.                 var fl = $('li.'+opt.hoverClass, $container).get(0);
  307.                 if (opt.debug) console.log('click on :'+this.id);
  308.                 $('li.'+opt.currentClass, $container).removeClass(opt.currentClass);
  309.                 $(this).addClass(opt.currentClass);
  310.                 setCurrent();
  311.                 $select.get(0).blur();
  312.                 hideMe();
  313.             });
  314.         });
  315.         return ul;
  316.     }
  317.    
  318.    
  319.    
  320. };
  321.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement