chosen-0.9.15/ 0000755 0001750 0001750 00000000000 12153204007 012043 5 ustar ilias ilias chosen-0.9.15/package.json 0000644 0001750 0001750 00000001155 12153204007 014333 0 ustar ilias ilias {
"author": "harvest",
"name": "chosen",
"version": "0.9.15",
"description": "Chosen is a JavaScript plugin that makes long, unwieldy select boxes much more user-friendly. It is currently available in both jQuery and Prototype flavors.",
"repository": {
"type": "git",
"url": "https://github.com/harvesthq/chosen"
},
"engines": {
"node": ">=0.4.0"
},
"dependencies": {},
"devDependencies": {
"coffee-script": ">= 1.6",
"uglify-js": ">= 1.2.5 <2.0"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/harvesthq/chosen/blob/master/LICENSE.md"
}
]
} chosen-0.9.15/example.proto.html 0000644 0001750 0001750 00000237473 12153204007 015546 0 ustar ilias ilias
Chosen
Chosen is a JavaScript plugin for jQuery and Prototype that makes long, unwieldy select boxes much more user-friendly. For more information (including usage, explanation and faqs), check out the online documentation.
Standard Select
Turns This
Into This
Multiple Select
Turns This
Into This
<optgroup> Support
Single Select with Groups
Multiple Select with Groups
Selected and Disabled Support
Chosen automatically highlights selected options and removes disabled options.
Single Select
Multiple Select
Hide Search on Single Select
The disable_search_threshold option can be specified to hide the search input on single selects if there are fewer than (n) options.
New Chosen($("chzn_select_field"),{disable_search_threshold: 10});
Default Text Support
Chosen automatically sets the default field text ("Choose a country...") by reading the select element's data-placeholder value. If no data-placeholder value is present, it will default to "Select an Option" or "Select Some Options" depending on whether the select is single or multiple. You can change these elements in the plugin js file as you see fit.
<select data-placeholder="Choose a country..." style="width:350px;" multiple class="chzn-select">
Note: on single selects, the first element is assumed to be selected by the browser. To take advantage of the default text support, you will need to include a blank option as the first element of your select list.
No Results Text Support
Setting the "No results" search text is as easy as passing an option when you create Chosen:
New Chosen($("chzn_select_field"),{no_results_text: "Oops, nothing found!"});
Single Select
Multiple Select
Limit Selected Options in Multiselect
You can easily limit how many options can user select:
new Chosen($("chzn_select_field"),{max_selected_options: 5});
If you try to select another option with limit reached liszt:maxselected event is triggered:
When a single select box isn't a required field, you can set allow_single_deselect: true and Chosen will add a UI element for option deselection. This will only work if the first option has blank text.
Right to Left Support
Chosen supports right to left select boxes too. just add "chzn-rtl" in addition to "chzn-select" to your select tags and you are good to go.
<select class="chzn-select chzn-rtl">
Single right to left select
Multiple right to left select
Change / Update Events
Form Field Change
When working with form fields, you often want to perform some behavior after a value has been selected or deselected. Whenever a user selects a field in Chosen, it triggers a "change" event* on the original form field. That let's you do something like this:
$("#form_field").chosen().change( … );
Note: Prototype doesn't offer support for triggering standard browser events. Event.simulate is required to trigger the change event when using the Prototype version.
Updating Chosen Dynamically
If you need to update the options in your select field and want Chosen to pick up the changes, you'll need to trigger the "liszt:updated" event on the field. Chosen will re-build itself based on the updated content.
chosen-0.9.15/example.jquery.html 0000644 0001750 0001750 00000237056 12153204007 015717 0 ustar ilias ilias
chosen-0.9.15/contributing.md 0000644 0001750 0001750 00000016110 12153204007 015073 0 ustar ilias ilias # Contributing to this project
Please take a moment to review this document in order to make the contribution
process easy and effective for everyone involved.
Following these guidelines will help us get back to you more quickly, and will
show that you care about making Chosen better just like we do. In return, we'll
do our best to respond to your issue or pull request as soon as possible with
the same respect.
_**Please Note:** These guidelines are adapted from [@necolas](https://github.com/necolas)'s
[issue-guidelines](https://github.com/necolas/issue-guidelines) and serve as
an excellent starting point for contributing to any open source project._
## Using the issue tracker
The [issue tracker](https://github.com/harvesthq/chosen/issues) is the
preferred channel for [bug reports](#bugs), [features requests](#features)
and [submitting pull requests](#pull-requests), but please respect the
following restrictions:
* Please **do not** use the issue tracker for personal support requests (use
[Stack Overflow](http://stackoverflow.com)).
* Please **do not** derail or troll issues. Keep the discussion on topic and
respect the opinions of others.
## Bug reports
A bug is a _demonstrable problem_ that is caused by the code in the repository.
Good bug reports are extremely helpful — thank you!
Guidelines for bug reports:
1. **Use the [GitHub issue search](https://github.com/harvesthq/chosen/search?type=Issues)** — check if the issue has already been
reported.
2. **Check if the bug has already been fixed** — try to reproduce it using the
repository's latest `master` changes.
3. **Isolate the problem** — ideally create a [reduced test
case](http://css-tricks.com/6263-reduced-test-cases/) and a live example
(perhaps a [fiddle](http://jsfiddle.net)).
A good bug report shouldn't leave others needing to contact you for more
information. Please try to be as detailed as possible in your report. What is
your environment? What steps will reproduce the issue? What browser(s) and OS
experience the problem? What outcome did you expect, and how did it differ from
what you actually saw? All these details will help people to fix any potential
bugs.
Example:
> Short and descriptive example bug report title
>
> A summary of the issue and the browser/OS environment in which it occurs. If
> suitable, include the steps required to reproduce the bug.
>
> 1. This is the first step
> 2. This is the second step
> 3. Further steps, etc.
>
> `` - a link to the reduced test case
>
> Any other information you want to share that is relevant to the issue being
> reported. This might include the lines of code that you have identified as
> causing the bug, and potential solutions (and your opinions on their
> merits).
## Feature requests
Feature requests are welcome. But take a moment to find out whether your idea
fits with the scope and aims of the project. It's up to *you* to make a strong
case to convince the project's developers of the merits of this feature. Please
provide as much detail and context as possible.
Building something great means choosing features carefully especially because it
is much, much easier to add features than it is to take them away. Additions
to Chosen will be evaluated on a combination of scope (how well it fits into the
project), maintenance burden and general usefulness.
Creating something great often means saying no to seemingly good ideas. Don't
dispair if your feature request isn't accepted, take action! Fork the
repository, build your idea and share it with others. We released Chosen under
the MIT License for this purpose precisely. Open source works best when smart
and dedicated people riff off of each others' ideas to make even greater things.
## Pull requests
Good pull requests - patches, improvements, new features — are a fantastic help.
They should remain focused in scope and avoid containing unrelated commits.
**Please ask first** before embarking on any significant pull request (e.g.
implementing features, refactoring code, porting to a different language),
otherwise you risk spending a lot of time working on something that the
project's developers might not want to merge into the project. You can solicit
feedback and opinions in an open feature request thread or create a new one.
Please use the [git flow for pull requesets](#git-flow) and follow Chosen's
[code conventions](#code-conventions) before submitting your work. Adhering to
these guidelines is the best way to get your work included in Chosen.
#### Git Flow for pull requests
1. [Fork](http://help.github.com/fork-a-repo/) the project, clone your fork,
and configure the remotes:
```bash
# Clone your fork of the repo into the current directory
git clone git@github.com:/chosen.git
# Navigate to the newly cloned directory
cd chosen
# Assign the original repo to a remote called "upstream"
git remote add upstream https://github.com/harvesthq/chosen
```
2. If you cloned a while ago, get the latest changes from upstream:
```bash
git checkout master
git pull upstream master
```
3. Create a new topic branch (off the main project development branch) to
contain your feature, change, or fix:
```bash
git checkout -b
```
4. Commit your changes in logical chunks. Please adhere to these [git commit
message guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
or your code is unlikely be merged into the main project. Use Git's
[interactive rebase](https://help.github.com/articles/interactive-rebase)
feature to tidy up your commits before making them public.
5. Locally merge (or rebase) the upstream development branch into your topic branch:
```bash
git pull [--rebase] upstream master
```
6. Push your topic branch up to your fork:
```bash
git push origin
```
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
with a clear title and description.
**IMPORTANT**: By submitting a patch, you agree to allow the project owner to
license your work under the [MIT License](http://en.wikipedia.org/wiki/MIT_License).
#### Chosen Code Conventions
1. Make all changes in CoffeeScript files, **not** JavaScript files.
2. Use [cake](#cake) to build the JavaScript files.
3. For feature changes, update both jQuery *and* Prototype versions
4. Don't touch the `VERSION` file
#### Using CoffeeScript and Cake
First, make sure you have the proper CoffeeScript / Cake setup in place. We have added a package.json that makes this easy:
```
npm install -d
```
This will install `coffee-script` and `uglifyjs`.
Once you're configured, building the JavaScript from the command line is easy:
cake build # build Chosen from source
cake watch # watch coffee/ for changes and build Chosen
If you're interested, you can find the recipes in [Cakefile](https://github.com/harvesthq/chosen/blob/master/Cakefile).
chosen-0.9.15/coffee/ 0000755 0001750 0001750 00000000000 12153204007 013272 5 ustar ilias ilias chosen-0.9.15/coffee/lib/ 0000755 0001750 0001750 00000000000 12153204007 014040 5 ustar ilias ilias chosen-0.9.15/coffee/lib/select-parser.coffee 0000644 0001750 0001750 00000002730 12153204007 017764 0 ustar ilias ilias class SelectParser
constructor: ->
@options_index = 0
@parsed = []
add_node: (child) ->
if child.nodeName.toUpperCase() is "OPTGROUP"
this.add_group child
else
this.add_option child
add_group: (group) ->
group_position = @parsed.length
@parsed.push
array_index: group_position
group: true
label: group.label
children: 0
disabled: group.disabled
this.add_option( option, group_position, group.disabled ) for option in group.childNodes
add_option: (option, group_position, group_disabled) ->
if option.nodeName.toUpperCase() is "OPTION"
if option.text != ""
if group_position?
@parsed[group_position].children += 1
@parsed.push
array_index: @parsed.length
options_index: @options_index
value: option.value
text: option.text
html: option.innerHTML
selected: option.selected
disabled: if group_disabled is true then group_disabled else option.disabled
group_array_index: group_position
classes: option.className
style: option.style.cssText
else
@parsed.push
array_index: @parsed.length
options_index: @options_index
empty: true
@options_index += 1
SelectParser.select_to_array = (select) ->
parser = new SelectParser()
parser.add_node( child ) for child in select.childNodes
parser.parsed
this.SelectParser = SelectParser
chosen-0.9.15/coffee/lib/abstract-chosen.coffee 0000644 0001750 0001750 00000012130 12153204007 020266 0 ustar ilias ilias ###
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
###
root = this
class AbstractChosen
constructor: (@form_field, @options={}) ->
return unless AbstractChosen.browser_is_supported()
@is_multiple = @form_field.multiple
this.set_default_text()
this.set_default_values()
this.setup()
this.set_up_html()
this.register_observers()
this.finish_setup()
set_default_values: ->
@click_test_action = (evt) => this.test_active_click(evt)
@activate_action = (evt) => this.activate_field(evt)
@active_field = false
@mouse_on_container = false
@results_showing = false
@result_highlighted = null
@result_single_selected = null
@allow_single_deselect = if @options.allow_single_deselect? and @form_field.options[0]? and @form_field.options[0].text is "" then @options.allow_single_deselect else false
@disable_search_threshold = @options.disable_search_threshold || 0
@disable_search = @options.disable_search || false
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
@search_contains = @options.search_contains || false
@single_backstroke_delete = @options.single_backstroke_delete || false
@max_selected_options = @options.max_selected_options || Infinity
@inherit_select_classes = @options.inherit_select_classes || false
set_default_text: ->
if @form_field.getAttribute("data-placeholder")
@default_text = @form_field.getAttribute("data-placeholder")
else if @is_multiple
@default_text = @options.placeholder_text_multiple || @options.placeholder_text || AbstractChosen.default_multiple_text
else
@default_text = @options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text
@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || AbstractChosen.default_no_result_text
mouse_enter: -> @mouse_on_container = true
mouse_leave: -> @mouse_on_container = false
input_focus: (evt) ->
if @is_multiple
setTimeout (=> this.container_mousedown()), 50 unless @active_field
else
@activate_field() unless @active_field
input_blur: (evt) ->
if not @mouse_on_container
@active_field = false
setTimeout (=> this.blur_test()), 100
result_add_option: (option) ->
if not option.disabled
option.dom_id = @container_id + "_o_" + option.array_index
classes = if option.selected and @is_multiple then [] else ["active-result"]
classes.push "result-selected" if option.selected
classes.push "group-option" if option.group_array_index?
classes.push option.classes if option.classes != ""
style = if option.style.cssText != "" then " style=\"#{option.style}\"" else ""
'
' + option.html + '
'
else
""
results_update_field: ->
this.set_default_text()
this.results_reset_cleanup() if not @is_multiple
this.result_clear_highlight()
@result_single_selected = null
this.results_build()
results_toggle: ->
if @results_showing
this.results_hide()
else
this.results_show()
results_search: (evt) ->
if @results_showing
this.winnow_results()
else
this.results_show()
choices_count: ->
return @selected_option_count if @selected_option_count?
@selected_option_count = 0
for option in @form_field.options
@selected_option_count += 1 if option.selected
return @selected_option_count
choices_click: (evt) ->
evt.preventDefault()
this.results_show() unless @results_showing
keyup_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()
switch stroke
when 8
if @is_multiple and @backstroke_length < 1 and this.choices_count() > 0
this.keydown_backstroke()
else if not @pending_backstroke
this.result_clear_highlight()
this.results_search()
when 13
evt.preventDefault()
this.result_select(evt) if this.results_showing
when 27
this.results_hide() if @results_showing
return true
when 9, 38, 40, 16, 91, 17
# don't do anything on these keys
else this.results_search()
generate_field_id: ->
new_id = this.generate_random_id()
@form_field.id = new_id
new_id
generate_random_char: ->
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rand = Math.floor(Math.random() * chars.length)
newchar = chars.substring rand, rand+1
container_width: ->
return if @options.width? then @options.width else "#{@form_field.offsetWidth}px"
# class methods and variables ============================================================
@browser_is_supported: ->
if window.navigator.appName == "Microsoft Internet Explorer"
return null isnt document.documentMode >= 8
return true
@default_multiple_text: "Select Some Options"
@default_single_text: "Select an Option"
@default_no_result_text: "No results match"
root.AbstractChosen = AbstractChosen
chosen-0.9.15/coffee/chosen.proto.coffee 0000644 0001750 0001750 00000047221 12153204007 017072 0 ustar ilias ilias ###
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
###
root = this
class Chosen extends AbstractChosen
setup: ->
@current_selectedIndex = @form_field.selectedIndex
@is_rtl = @form_field.hasClassName "chzn-rtl"
finish_setup: ->
@form_field.addClassName "chzn-done"
set_default_values: ->
super()
# HTML Templates
@single_temp = new Template('#{default}
')
@multi_temp = new Template('
')
@no_results_temp = new Template('
' + @results_none_found + ' "#{terms}"
')
set_up_html: ->
@container_id = @form_field.identify().replace(/[^\w]/g, '_') + "_chzn"
container_classes = ["chzn-container"]
container_classes.push "chzn-container-" + (if @is_multiple then "multi" else "single")
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
container_classes.push "chzn-rtl" if @is_rtl
container_props =
'id': @container_id
'class': container_classes.join ' '
'style': "width: #{this.container_width()};"
'title': @form_field.title
@container = if @is_multiple then new Element('div', container_props).update( @multi_temp.evaluate({ "default": @default_text}) ) else new Element('div', container_props).update( @single_temp.evaluate({ "default":@default_text }) )
@form_field.hide().insert({ after: @container })
@dropdown = @container.down('div.chzn-drop')
@search_field = @container.down('input')
@search_results = @container.down('ul.chzn-results')
this.search_field_scale()
@search_no_results = @container.down('li.no-results')
if @is_multiple
@search_choices = @container.down('ul.chzn-choices')
@search_container = @container.down('li.search-field')
else
@search_container = @container.down('div.chzn-search')
@selected_item = @container.down('.chzn-single')
this.results_build()
this.set_tab_index()
this.set_label_behavior()
@form_field.fire("liszt:ready", {chosen: this})
register_observers: ->
@container.observe "mousedown", (evt) => this.container_mousedown(evt)
@container.observe "mouseup", (evt) => this.container_mouseup(evt)
@container.observe "mouseenter", (evt) => this.mouse_enter(evt)
@container.observe "mouseleave", (evt) => this.mouse_leave(evt)
@search_results.observe "mouseup", (evt) => this.search_results_mouseup(evt)
@search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt)
@search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt)
@search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt)
@search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt)
@form_field.observe "liszt:updated", (evt) => this.results_update_field(evt)
@form_field.observe "liszt:activate", (evt) => this.activate_field(evt)
@form_field.observe "liszt:open", (evt) => this.container_mousedown(evt)
@search_field.observe "blur", (evt) => this.input_blur(evt)
@search_field.observe "keyup", (evt) => this.keyup_checker(evt)
@search_field.observe "keydown", (evt) => this.keydown_checker(evt)
@search_field.observe "focus", (evt) => this.input_focus(evt)
if @is_multiple
@search_choices.observe "click", (evt) => this.choices_click(evt)
else
@container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor
search_field_disabled: ->
@is_disabled = @form_field.disabled
if(@is_disabled)
@container.addClassName 'chzn-disabled'
@search_field.disabled = true
@selected_item.stopObserving "focus", @activate_action if !@is_multiple
this.close_field()
else
@container.removeClassName 'chzn-disabled'
@search_field.disabled = false
@selected_item.observe "focus", @activate_action if !@is_multiple
container_mousedown: (evt) ->
if !@is_disabled
if evt and evt.type is "mousedown" and not @results_showing
evt.stop()
if not (evt? and evt.target.hasClassName "search-choice-close")
if not @active_field
@search_field.clear() if @is_multiple
document.observe "click", @click_test_action
this.results_show()
else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chzn-single"))
this.results_toggle()
this.activate_field()
container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
search_results_mousewheel: (evt) ->
delta = -evt.wheelDelta or evt.detail
if delta?
evt.preventDefault()
delta = delta * 40 if evt.type is 'DOMMouseScroll'
@search_results.scrollTop = delta + @search_results.scrollTop
blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClassName("chzn-container-active")
close_field: ->
document.stopObserving "click", @click_test_action
@active_field = false
this.results_hide()
@container.removeClassName "chzn-container-active"
this.winnow_results_clear()
this.clear_backstroke()
this.show_search_field_default()
this.search_field_scale()
activate_field: ->
@container.addClassName "chzn-container-active"
@active_field = true
@search_field.value = @search_field.value
@search_field.focus()
test_active_click: (evt) ->
if evt.target.up('#' + @container_id)
@active_field = true
else
this.close_field()
results_build: ->
@parsing = true
@selected_option_count = null
@results_data = root.SelectParser.select_to_array @form_field
if @is_multiple and this.choices_count() > 0
@search_choices.select("li.search-choice").invoke("remove")
else if not @is_multiple
@selected_item.addClassName("chzn-default").down("span").update(@default_text)
if @disable_search or @form_field.options.length <= @disable_search_threshold
@container.addClassName "chzn-container-single-nosearch"
else
@container.removeClassName "chzn-container-single-nosearch"
content = ''
for data in @results_data
if data.group
content += this.result_add_group data
else if !data.empty
content += this.result_add_option data
if data.selected and @is_multiple
this.choice_build data
else if data.selected and not @is_multiple
@selected_item.removeClassName("chzn-default").down("span").update( data.html )
this.single_deselect_control_build() if @allow_single_deselect
this.search_field_disabled()
this.show_search_field_default()
this.search_field_scale()
@search_results.update content
@parsing = false
result_add_group: (group) ->
if not group.disabled
group.dom_id = @container_id + "_g_" + group.array_index
'
' + group.label.escapeHTML() + '
'
else
""
result_do_highlight: (el) ->
this.result_clear_highlight()
@result_highlight = el
@result_highlight.addClassName "highlighted"
maxHeight = parseInt @search_results.getStyle('maxHeight'), 10
visible_top = @search_results.scrollTop
visible_bottom = maxHeight + visible_top
high_top = @result_highlight.positionedOffset().top + @search_results.scrollTop
high_bottom = high_top + @result_highlight.getHeight()
if high_bottom >= visible_bottom
@search_results.scrollTop = if (high_bottom - maxHeight) > 0 then (high_bottom - maxHeight) else 0
else if high_top < visible_top
@search_results.scrollTop = high_top
result_clear_highlight: ->
@result_highlight.removeClassName('highlighted') if @result_highlight
@result_highlight = null
results_show: ->
if @result_single_selected?
this.result_do_highlight @result_single_selected
else if @is_multiple and @max_selected_options <= this.choices_count()
@form_field.fire("liszt:maxselected", {chosen: this})
return false
@container.addClassName "chzn-with-drop"
@form_field.fire("liszt:showing_dropdown", {chosen: this})
@results_showing = true
@search_field.focus()
@search_field.value = @search_field.value
this.winnow_results()
results_hide: ->
this.result_clear_highlight()
@container.removeClassName "chzn-with-drop"
@form_field.fire("liszt:hiding_dropdown", {chosen: this})
@results_showing = false
set_tab_index: (el) ->
if @form_field.tabIndex
ti = @form_field.tabIndex
@form_field.tabIndex = -1
@search_field.tabIndex = ti
set_label_behavior: ->
@form_field_label = @form_field.up("label") # first check for a parent label
if not @form_field_label?
@form_field_label = $$("label[for='#{@form_field.id}']").first() #next check for a for=#{id}
if @form_field_label?
@form_field_label.observe "click", (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field()
show_search_field_default: ->
if @is_multiple and this.choices_count() < 1 and not @active_field
@search_field.value = @default_text
@search_field.addClassName "default"
else
@search_field.value = ""
@search_field.removeClassName "default"
search_results_mouseup: (evt) ->
target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
if target
@result_highlight = target
this.result_select(evt)
@search_field.focus()
search_results_mouseover: (evt) ->
target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
this.result_do_highlight( target ) if target
search_results_mouseout: (evt) ->
this.result_clear_highlight() if evt.target.hasClassName('active-result') or evt.target.up('.active-result')
choice_build: (item) ->
choice = new Element('li', { class: "search-choice" }).update("#{item.html}")
if item.disabled
choice.addClassName 'search-choice-disabled'
else
close_link = new Element('a', { href: '#', class: 'search-choice-close', rel: item.array_index })
close_link.observe "click", (evt) => this.choice_destroy_link_click(evt)
choice.insert close_link
@search_container.insert { before: choice }
choice_destroy_link_click: (evt) ->
evt.preventDefault()
evt.stopPropagation()
this.choice_destroy evt.target unless @is_disabled
choice_destroy: (link) ->
if this.result_deselect link.readAttribute("rel")
this.show_search_field_default()
this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.value.length < 1
link.up('li').remove()
this.search_field_scale()
results_reset: ->
@form_field.options[0].selected = true
@selected_option_count = null
@selected_item.down("span").update(@default_text)
@selected_item.addClassName("chzn-default") if not @is_multiple
this.show_search_field_default()
this.results_reset_cleanup()
@form_field.simulate("change") if typeof Event.simulate is 'function'
this.results_hide() if @active_field
results_reset_cleanup: ->
@current_selectedIndex = @form_field.selectedIndex
deselect_trigger = @selected_item.down("abbr")
deselect_trigger.remove() if(deselect_trigger)
result_select: (evt) ->
if @result_highlight
high = @result_highlight
this.result_clear_highlight()
if @is_multiple and @max_selected_options <= this.choices_count()
@form_field.fire("liszt:maxselected", {chosen: this})
return false
if @is_multiple
this.result_deactivate high
else
@search_results.descendants(".result-selected").invoke "removeClassName", "result-selected"
@selected_item.removeClassName("chzn-default")
@result_single_selected = high
high.addClassName("result-selected")
position = high.id.substr(high.id.lastIndexOf("_") + 1 )
item = @results_data[position]
item.selected = true
@form_field.options[item.options_index].selected = true
@selected_option_count = null
if @is_multiple
this.choice_build item
else
@selected_item.down("span").update(item.html)
this.single_deselect_control_build() if @allow_single_deselect
this.results_hide() unless (evt.metaKey or evt.ctrlKey) and @is_multiple
@search_field.value = ""
@form_field.simulate("change") if typeof Event.simulate is 'function' && (@is_multiple || @form_field.selectedIndex != @current_selectedIndex)
@current_selectedIndex = @form_field.selectedIndex
this.search_field_scale()
result_activate: (el) ->
el.addClassName("active-result")
result_deactivate: (el) ->
el.removeClassName("active-result")
result_deselect: (pos) ->
result_data = @results_data[pos]
if not @form_field.options[result_data.options_index].disabled
result_data.selected = false
@form_field.options[result_data.options_index].selected = false
@selected_option_count = null
result = $(@container_id + "_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show()
this.result_clear_highlight()
this.winnow_results()
@form_field.simulate("change") if typeof Event.simulate is 'function'
this.search_field_scale()
return true
else
return false
single_deselect_control_build: ->
@selected_item.down("span").insert { after: "" } if @allow_single_deselect and not @selected_item.down("abbr")
winnow_results: ->
this.no_results_clear()
results = 0
searchText = if @search_field.value is @default_text then "" else @search_field.value.strip().escapeHTML()
regexAnchor = if @search_contains then "" else "^"
regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
for option in @results_data
if not option.disabled and not option.empty
if option.group
$(option.dom_id).hide()
else if not (@is_multiple and option.selected)
found = false
result_id = option.dom_id
if regex.test option.html
found = true
results += 1
else if @enable_split_word_search and (option.html.indexOf(" ") >= 0 or option.html.indexOf("[") == 0)
#TODO: replace this substitution of /\[\]/ with a list of characters to skip.
parts = option.html.replace(/\[|\]/g, "").split(" ")
if parts.length
for part in parts
if regex.test part
found = true
results += 1
if found
if searchText.length
startpos = option.html.search zregex
text = option.html.substr(0, startpos + searchText.length) + '' + option.html.substr(startpos + searchText.length)
text = text.substr(0, startpos) + '' + text.substr(startpos)
else
text = option.html
$(result_id).update text if $(result_id).innerHTML != text
this.result_activate $(result_id)
$(@results_data[option.group_array_index].dom_id).setStyle({display: 'list-item'}) if option.group_array_index?
else
this.result_clear_highlight() if $(result_id) is @result_highlight
this.result_deactivate $(result_id)
if results < 1 and searchText.length
this.no_results(searchText)
else
this.winnow_results_set_highlight()
winnow_results_clear: ->
@search_field.clear()
lis = @search_results.select("li")
for li in lis
if li.hasClassName("group-result")
li.show()
else if not @is_multiple or not li.hasClassName("result-selected")
this.result_activate li
winnow_results_set_highlight: ->
if not @result_highlight
if not @is_multiple
do_high = @search_results.down(".result-selected.active-result")
if not do_high?
do_high = @search_results.down(".active-result")
this.result_do_highlight do_high if do_high?
no_results: (terms) ->
@search_results.insert @no_results_temp.evaluate( terms: terms )
no_results_clear: ->
nr = null
nr.remove() while nr = @search_results.down(".no-results")
keydown_arrow: ->
actives = @search_results.select("li.active-result")
if actives.length
if not @result_highlight
this.result_do_highlight actives.first()
else if @results_showing
sibs = @result_highlight.nextSiblings()
nexts = sibs.intersect(actives)
this.result_do_highlight nexts.first() if nexts.length
this.results_show() if not @results_showing
keyup_arrow: ->
if not @results_showing and not @is_multiple
this.results_show()
else if @result_highlight
sibs = @result_highlight.previousSiblings()
actives = @search_results.select("li.active-result")
prevs = sibs.intersect(actives)
if prevs.length
this.result_do_highlight prevs.first()
else
this.results_hide() if this.choices_count() > 0
this.result_clear_highlight()
keydown_backstroke: ->
if @pending_backstroke
this.choice_destroy @pending_backstroke.down("a")
this.clear_backstroke()
else
next_available_destroy = @search_container.siblings().last()
if next_available_destroy and next_available_destroy.hasClassName("search-choice") and not next_available_destroy.hasClassName("search-choice-disabled")
@pending_backstroke = next_available_destroy
@pending_backstroke.addClassName("search-choice-focus") if @pending_backstroke
if @single_backstroke_delete
@keydown_backstroke()
else
@pending_backstroke.addClassName("search-choice-focus")
clear_backstroke: ->
@pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
@pending_backstroke = null
keydown_checker: (evt) ->
stroke = evt.which ? evt.keyCode
this.search_field_scale()
this.clear_backstroke() if stroke != 8 and this.pending_backstroke
switch stroke
when 8
@backstroke_length = this.search_field.value.length
break
when 9
this.result_select(evt) if this.results_showing and not @is_multiple
@mouse_on_container = false
break
when 13
evt.preventDefault()
break
when 38
evt.preventDefault()
this.keyup_arrow()
break
when 40
this.keydown_arrow()
break
search_field_scale: ->
if @is_multiple
h = 0
w = 0
style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"
styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing']
for style in styles
style_block += style + ":" + @search_field.getStyle(style) + ";"
div = new Element('div', { 'style' : style_block }).update(@search_field.value.escapeHTML())
document.body.appendChild(div)
w = Element.measure(div, 'width') + 25
div.remove()
@f_width = @container.getWidth() unless @f_width
if( w > @f_width-10 )
w = @f_width - 10
@search_field.setStyle({'width': w + 'px'})
root.Chosen = Chosen
chosen-0.9.15/coffee/chosen.jquery.coffee 0000644 0001750 0001750 00000050102 12153204007 017236 0 ustar ilias ilias ###
Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest
###
root = this
$ = jQuery
$.fn.extend({
chosen: (options) ->
# Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7
# Continue on if running IE document type but in compatibility mode
return this unless AbstractChosen.browser_is_supported()
this.each((input_field) ->
$this = $ this
$this.data('chosen', new Chosen(this, options)) unless $this.hasClass "chzn-done"
)
})
class Chosen extends AbstractChosen
setup: ->
@form_field_jq = $ @form_field
@current_selectedIndex = @form_field.selectedIndex
@is_rtl = @form_field_jq.hasClass "chzn-rtl"
finish_setup: ->
@form_field_jq.addClass "chzn-done"
set_up_html: ->
@container_id = if @form_field.id.length then @form_field.id.replace(/[^\w]/g, '_') else this.generate_field_id()
@container_id += "_chzn"
container_classes = ["chzn-container"]
container_classes.push "chzn-container-" + (if @is_multiple then "multi" else "single")
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
container_classes.push "chzn-rtl" if @is_rtl
container_props =
'id': @container_id
'class': container_classes.join ' '
'style': "width: #{this.container_width()};"
'title': @form_field.title
@container = ($ "", container_props)
if @is_multiple
@container.html '