2011-06-23 18:18:36 +02:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Chosen for Protoype.js
|
|
|
|
by Patrick Filler for Harvest
|
|
|
|
|
|
|
|
Copyright (c) 2011 Harvest
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
|
|
|
*/
|
2011-06-23 23:42:25 +02:00
|
|
|
var Chosen;Chosen=Class.create();Chosen.prototype={active_field:false,mouse_on_container:false,results_showing:false,result_highlighted:null,result_single_selected:null,choices:0,single_temp:new Template('<a href="#" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>'),multi_temp:new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>'),choice_temp:new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="#" class="search-choice-close" rel="#{position}"></a></li>'),no_results_temp:new Template('<li class="no-results">No results match "<span>#{terms}</span>"</li>'),initialize:function(el){if(!Prototype.Browser.IE7&&!Prototype.Browser.IE6){this.form_field=el;this.is_multiple=this.form_field.multiple;this.form_field.observe("liszt:updated",this.results_update_field.bindAsEventListener(this));this.default_text_default=(this.form_field.multiple)?"Select Some Options":"Select an Option";this.click_test_action=this.test_active_click.bindAsEventListener(this);this.set_up_html();this.register_observers();}},set_up_html:function(){this.container_id=this.form_field.id+"_chzn";this.f_width=(this.form_field.getStyle("width"))?parseInt(this.form_field.getStyle("width"),10):this.form_field.getWidth();var container_props={id:this.container_id,"class":"chzn-container",style:"width:"+this.f_width+"px"};this.default_text=this.form_field.readAttribute("title")?this.form_field.readAttribute("title"):this.default_text_default;var base_template=(this.is_multiple)?new Element("div",container_props).update(this.multi_temp.evaluate({"default":this.default_text})):new Element("div",container_props).update(this.single_temp.evaluate({"default":this.default_text}));this.form_field.hide().insert({after:base_template});this.container=$(this.container_id);this.container.addClassName("chzn-container-"+((this.is_multiple)?"multi":"single"));this.dropdown=this.container.down("div.chzn-drop");var dd_top=this.container.getHeight();var dd_width=(this.f_width-get_side_border_padding(this.dropdown));this.dropdown.setStyle({width:dd_width+"px",top:dd_top+"px"});this.search_field=this.container.down("input");this.search_results=this.container.down("ul.chzn-results");this.search_field_scale();this.search_no_results=this.container.down("li.no-results");if(!this.is_multiple){this.search_container=this.container.down("div.chzn-search");this.selected_item=this.container.down(".chzn-single");var sf_width=(dd_width-get_side_border_padding(this.search_container)-get_side_border_padding(this.search_field));this.search_field.setStyle({width:sf_width+"px"});}else{this.search_choices=this.container.down("ul.chzn-choices");this.search_container=this.container.down("li.search-field");this.search_choices.observe("click",this.choices_click.bindAsEventListener(this));}this.results_build();},register_observers:function(){this.container.observe("click",this.container_click.bindAsEventListener(this));this.container.observe("mouseenter",this.mouse_enter.bindAsEventListener(this));this.container.observe("mouseleave",this.mouse_leave.bindAsEventListener(this));this.search_field.observe("blur",this.input_blur.bindAsEventListener(this));this.search_field.observe("keyup",this.keyup_checker.bindAsEventListener(this));this.search_field.observe("keydown",this.keydown_checker.bindAsEventListener(this));this.set_tab_index();this.search_results.observe("click",this.search_results_click.bindAsEventListener(this));this.search_results.observe("mouseover",this.search_results_mouseover.bindAsEventListener(this));this.search_results.observe("mouseout",this.search_results_mouseout.bindAsEventListener(this));if(!this.is_multiple){this.selected_item.observe("focus",this.activate_field.bindAsEventListener(this));}this.search_field.observe("focus",this.input_focu
|