chosen-koenpunt-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/chosen-koenpunt-rails.gemspec +19 -0
- data/lib/chosen-koenpunt-rails/version.rb +3 -0
- data/lib/chosen-koenpunt-rails.rb +5 -0
- data/vendor/assets/javascripts/chosen.jquery.coffee +520 -0
- data/vendor/assets/javascripts/chosen.proto.coffee +528 -0
- data/vendor/assets/javascripts/lib/abstract-chosen.coffee +280 -0
- data/vendor/assets/javascripts/lib/select-parser.coffee +64 -0
- data/vendor/assets/stylesheets/chosen.scss +411 -0
- metadata +59 -0
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 tnarik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ChosenKoenpuntRails
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'chosen-koenpunt-rails'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install chosen-koenpunt-rails
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'chosen-koenpunt-rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "chosen-koenpunt-rails"
|
8
|
+
gem.version = ChosenKoenpuntRails::VERSION
|
9
|
+
gem.authors = ["tnarik"]
|
10
|
+
gem.email = ["tnarik@gmail.com"]
|
11
|
+
gem.description = %q{Integrate Koenpunt branch of the Chosen javascript library with the Rails asset pipeline}
|
12
|
+
gem.summary = %q{Integrate Koenpunt branch of the Chosen javascript library with the Rails asset pipeline}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,520 @@
|
|
1
|
+
$ = jQuery
|
2
|
+
|
3
|
+
$.fn.extend({
|
4
|
+
chosen: (options) ->
|
5
|
+
# Do no harm and return as soon as possible for unsupported browsers, namely IE6 and IE7
|
6
|
+
# Continue on if running IE document type but in compatibility mode
|
7
|
+
return this unless AbstractChosen.browser_is_supported()
|
8
|
+
this.each (input_field) ->
|
9
|
+
$this = $ this
|
10
|
+
chosen = $this.data('chosen')
|
11
|
+
if options is 'destroy' && chosen
|
12
|
+
chosen.destroy()
|
13
|
+
else unless chosen
|
14
|
+
$this.data('chosen', new Chosen(this, options))
|
15
|
+
|
16
|
+
return
|
17
|
+
|
18
|
+
})
|
19
|
+
|
20
|
+
class Chosen extends AbstractChosen
|
21
|
+
|
22
|
+
setup: ->
|
23
|
+
@form_field_jq = $ @form_field
|
24
|
+
@current_selectedIndex = @form_field.selectedIndex
|
25
|
+
@is_rtl = @form_field_jq.hasClass "chosen-rtl"
|
26
|
+
|
27
|
+
set_up_html: ->
|
28
|
+
container_classes = ["chosen-container"]
|
29
|
+
container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
|
30
|
+
container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
|
31
|
+
container_classes.push "chosen-rtl" if @is_rtl
|
32
|
+
|
33
|
+
container_props =
|
34
|
+
'class': container_classes.join ' '
|
35
|
+
'style': "width: #{this.container_width()};"
|
36
|
+
'title': @form_field.title
|
37
|
+
|
38
|
+
container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chosen" if @form_field.id.length
|
39
|
+
|
40
|
+
@container = ($ "<div />", container_props)
|
41
|
+
|
42
|
+
if @is_multiple
|
43
|
+
@container.html '<ul class="chosen-choices"><li class="search-field"><input type="text" value="' + @default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>'
|
44
|
+
else
|
45
|
+
@container.html '<a class="chosen-single chosen-default" tabindex="-1"><span>' + @default_text + '</span><div><b></b></div></a><div class="chosen-drop"><div class="chosen-search"><input type="text" autocomplete="off" /></div><ul class="chosen-results"></ul></div>'
|
46
|
+
|
47
|
+
@form_field_jq.hide().after @container
|
48
|
+
@dropdown = @container.find('div.chosen-drop').first()
|
49
|
+
|
50
|
+
@search_field = @container.find('input').first()
|
51
|
+
@search_results = @container.find('ul.chosen-results').first()
|
52
|
+
this.search_field_scale()
|
53
|
+
|
54
|
+
@search_no_results = @container.find('li.no-results').first()
|
55
|
+
|
56
|
+
if @is_multiple
|
57
|
+
@search_choices = @container.find('ul.chosen-choices').first()
|
58
|
+
@search_container = @container.find('li.search-field').first()
|
59
|
+
else
|
60
|
+
@search_container = @container.find('div.chosen-search').first()
|
61
|
+
@selected_item = @container.find('.chosen-single').first()
|
62
|
+
|
63
|
+
this.results_build()
|
64
|
+
this.set_tab_index()
|
65
|
+
this.set_label_behavior()
|
66
|
+
@form_field_jq.trigger("chosen:ready", {chosen: this})
|
67
|
+
|
68
|
+
register_observers: ->
|
69
|
+
@container.bind 'mousedown.chosen', (evt) => this.container_mousedown(evt); return
|
70
|
+
@container.bind 'mouseup.chosen', (evt) => this.container_mouseup(evt); return
|
71
|
+
@container.bind 'mouseenter.chosen', (evt) => this.mouse_enter(evt); return
|
72
|
+
@container.bind 'mouseleave.chosen', (evt) => this.mouse_leave(evt); return
|
73
|
+
|
74
|
+
@search_results.bind 'mouseup.chosen', (evt) => this.search_results_mouseup(evt); return
|
75
|
+
@search_results.bind 'mouseover.chosen', (evt) => this.search_results_mouseover(evt); return
|
76
|
+
@search_results.bind 'mouseout.chosen', (evt) => this.search_results_mouseout(evt); return
|
77
|
+
@search_results.bind 'mousewheel.chosen DOMMouseScroll.chosen', (evt) => this.search_results_mousewheel(evt); return
|
78
|
+
|
79
|
+
@search_results.bind 'touchstart.chosen', (evt) => this.search_results_touchstart(evt); return
|
80
|
+
@search_results.bind 'touchmove.chosen', (evt) => this.search_results_touchmove(evt); return
|
81
|
+
@search_results.bind 'touchend.chosen', (evt) => this.search_results_touchend(evt); return
|
82
|
+
|
83
|
+
@form_field_jq.bind "chosen:updated.chosen", (evt) => this.results_update_field(evt); return
|
84
|
+
@form_field_jq.bind "chosen:activate.chosen", (evt) => this.activate_field(evt); return
|
85
|
+
@form_field_jq.bind "chosen:open.chosen", (evt) => this.container_mousedown(evt); return
|
86
|
+
|
87
|
+
@search_field.bind 'blur.chosen', (evt) => this.input_blur(evt); return
|
88
|
+
@search_field.bind 'keyup.chosen', (evt) => this.keyup_checker(evt); return
|
89
|
+
@search_field.bind 'keydown.chosen', (evt) => this.keydown_checker(evt); return
|
90
|
+
@search_field.bind 'focus.chosen', (evt) => this.input_focus(evt); return
|
91
|
+
|
92
|
+
if @is_multiple
|
93
|
+
@search_choices.bind 'click.chosen', (evt) => this.choices_click(evt); return
|
94
|
+
else
|
95
|
+
@container.bind 'click.chosen', (evt) -> evt.preventDefault(); return # gobble click of anchor
|
96
|
+
|
97
|
+
destroy: ->
|
98
|
+
$(document).unbind "click.chosen", @click_test_action
|
99
|
+
if @search_field[0].tabIndex
|
100
|
+
@form_field_jq[0].tabIndex = @search_field[0].tabIndex
|
101
|
+
|
102
|
+
@container.remove()
|
103
|
+
@form_field_jq.removeData('chosen')
|
104
|
+
@form_field_jq.show()
|
105
|
+
|
106
|
+
search_field_disabled: ->
|
107
|
+
@is_disabled = @form_field_jq[0].disabled
|
108
|
+
if(@is_disabled)
|
109
|
+
@container.addClass 'chosen-disabled'
|
110
|
+
@search_field[0].disabled = true
|
111
|
+
@selected_item.unbind "focus.chosen", @activate_action if !@is_multiple
|
112
|
+
this.close_field()
|
113
|
+
else
|
114
|
+
@container.removeClass 'chosen-disabled'
|
115
|
+
@search_field[0].disabled = false
|
116
|
+
@selected_item.bind "focus.chosen", @activate_action if !@is_multiple
|
117
|
+
|
118
|
+
container_mousedown: (evt) ->
|
119
|
+
if !@is_disabled
|
120
|
+
if evt and evt.type is "mousedown" and not @results_showing
|
121
|
+
evt.preventDefault()
|
122
|
+
|
123
|
+
if not (evt? and ($ evt.target).hasClass "search-choice-close")
|
124
|
+
if not @active_field
|
125
|
+
@search_field.val "" if @is_multiple
|
126
|
+
$(document).bind 'click.chosen', @click_test_action
|
127
|
+
this.results_show()
|
128
|
+
else if not @is_multiple and evt and (($(evt.target)[0] == @selected_item[0]) || $(evt.target).parents("a.chosen-single").length)
|
129
|
+
evt.preventDefault()
|
130
|
+
this.results_toggle()
|
131
|
+
|
132
|
+
this.activate_field()
|
133
|
+
|
134
|
+
container_mouseup: (evt) ->
|
135
|
+
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
|
136
|
+
|
137
|
+
search_results_mousewheel: (evt) ->
|
138
|
+
delta = -evt.originalEvent?.wheelDelta or evt.originialEvent?.detail
|
139
|
+
if delta?
|
140
|
+
evt.preventDefault()
|
141
|
+
delta = delta * 40 if evt.type is 'DOMMouseScroll'
|
142
|
+
@search_results.scrollTop(delta + @search_results.scrollTop())
|
143
|
+
|
144
|
+
blur_test: (evt) ->
|
145
|
+
this.close_field() if not @active_field and @container.hasClass "chosen-container-active"
|
146
|
+
|
147
|
+
close_field: ->
|
148
|
+
$(document).unbind "click.chosen", @click_test_action
|
149
|
+
|
150
|
+
@active_field = false
|
151
|
+
this.results_hide()
|
152
|
+
|
153
|
+
@container.removeClass "chosen-container-active"
|
154
|
+
this.clear_backstroke()
|
155
|
+
|
156
|
+
this.show_search_field_default()
|
157
|
+
this.search_field_scale()
|
158
|
+
|
159
|
+
activate_field: ->
|
160
|
+
@container.addClass "chosen-container-active"
|
161
|
+
@active_field = true
|
162
|
+
|
163
|
+
@search_field.val(@search_field.val())
|
164
|
+
@search_field.focus()
|
165
|
+
|
166
|
+
|
167
|
+
test_active_click: (evt) ->
|
168
|
+
if @container.is($(evt.target).closest('.chosen-container'))
|
169
|
+
@active_field = true
|
170
|
+
else
|
171
|
+
this.close_field()
|
172
|
+
|
173
|
+
results_build: ->
|
174
|
+
@parsing = true
|
175
|
+
@selected_option_count = null
|
176
|
+
|
177
|
+
@results_data = SelectParser.select_to_array @form_field
|
178
|
+
|
179
|
+
if @is_multiple
|
180
|
+
@search_choices.find("li.search-choice").remove()
|
181
|
+
else if not @is_multiple
|
182
|
+
this.single_set_selected_text()
|
183
|
+
if @disable_search or @form_field.options.length <= @disable_search_threshold and not @create_option
|
184
|
+
@search_field[0].readOnly = true
|
185
|
+
@container.addClass "chosen-container-single-nosearch"
|
186
|
+
else
|
187
|
+
@search_field[0].readOnly = false
|
188
|
+
@container.removeClass "chosen-container-single-nosearch"
|
189
|
+
|
190
|
+
this.update_results_content this.results_option_build({first:true})
|
191
|
+
|
192
|
+
this.search_field_disabled()
|
193
|
+
this.show_search_field_default()
|
194
|
+
this.search_field_scale()
|
195
|
+
|
196
|
+
@parsing = false
|
197
|
+
|
198
|
+
result_do_highlight: (el) ->
|
199
|
+
if el.length
|
200
|
+
this.result_clear_highlight()
|
201
|
+
|
202
|
+
@result_highlight = el
|
203
|
+
@result_highlight.addClass "highlighted"
|
204
|
+
|
205
|
+
maxHeight = parseInt @search_results.css("maxHeight"), 10
|
206
|
+
visible_top = @search_results.scrollTop()
|
207
|
+
visible_bottom = maxHeight + visible_top
|
208
|
+
|
209
|
+
high_top = @result_highlight.position().top + @search_results.scrollTop()
|
210
|
+
high_bottom = high_top + @result_highlight.outerHeight()
|
211
|
+
|
212
|
+
if high_bottom >= visible_bottom
|
213
|
+
@search_results.scrollTop if (high_bottom - maxHeight) > 0 then (high_bottom - maxHeight) else 0
|
214
|
+
else if high_top < visible_top
|
215
|
+
@search_results.scrollTop high_top
|
216
|
+
|
217
|
+
result_clear_highlight: ->
|
218
|
+
@result_highlight.removeClass "highlighted" if @result_highlight
|
219
|
+
@result_highlight = null
|
220
|
+
|
221
|
+
results_show: ->
|
222
|
+
if @is_multiple and @max_selected_options <= this.choices_count()
|
223
|
+
@form_field_jq.trigger("chosen:maxselected", {chosen: this})
|
224
|
+
return false
|
225
|
+
|
226
|
+
@container.addClass "chosen-with-drop"
|
227
|
+
@form_field_jq.trigger("chosen:showing_dropdown", {chosen: this})
|
228
|
+
|
229
|
+
@results_showing = true
|
230
|
+
|
231
|
+
@search_field.focus()
|
232
|
+
@search_field.val @search_field.val()
|
233
|
+
|
234
|
+
this.winnow_results()
|
235
|
+
|
236
|
+
update_results_content: (content) ->
|
237
|
+
@search_results.html content
|
238
|
+
|
239
|
+
results_hide: ->
|
240
|
+
if @results_showing
|
241
|
+
this.result_clear_highlight()
|
242
|
+
|
243
|
+
@container.removeClass "chosen-with-drop"
|
244
|
+
@form_field_jq.trigger("chosen:hiding_dropdown", {chosen: this})
|
245
|
+
|
246
|
+
@results_showing = false
|
247
|
+
|
248
|
+
|
249
|
+
set_tab_index: (el) ->
|
250
|
+
if @form_field.tabIndex
|
251
|
+
ti = @form_field.tabIndex
|
252
|
+
@form_field.tabIndex = -1
|
253
|
+
@search_field[0].tabIndex = ti
|
254
|
+
|
255
|
+
set_label_behavior: ->
|
256
|
+
@form_field_label = @form_field_jq.parents("label") # first check for a parent label
|
257
|
+
if not @form_field_label.length and @form_field.id.length
|
258
|
+
@form_field_label = $("label[for='#{@form_field.id}']") #next check for a for=#{id}
|
259
|
+
|
260
|
+
if @form_field_label.length > 0
|
261
|
+
@form_field_label.bind 'click.chosen', (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field()
|
262
|
+
|
263
|
+
show_search_field_default: ->
|
264
|
+
if @is_multiple and this.choices_count() < 1 and not @active_field
|
265
|
+
@search_field.val(@default_text)
|
266
|
+
@search_field.addClass "default"
|
267
|
+
else
|
268
|
+
@search_field.val("")
|
269
|
+
@search_field.removeClass "default"
|
270
|
+
|
271
|
+
search_results_mouseup: (evt) ->
|
272
|
+
target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first()
|
273
|
+
if target.length
|
274
|
+
@result_highlight = target
|
275
|
+
this.result_select(evt)
|
276
|
+
@search_field.focus()
|
277
|
+
|
278
|
+
search_results_mouseover: (evt) ->
|
279
|
+
target = if $(evt.target).hasClass "active-result" then $(evt.target) else $(evt.target).parents(".active-result").first()
|
280
|
+
this.result_do_highlight( target ) if target
|
281
|
+
|
282
|
+
search_results_mouseout: (evt) ->
|
283
|
+
this.result_clear_highlight() if $(evt.target).hasClass "active-result" or $(evt.target).parents('.active-result').first()
|
284
|
+
|
285
|
+
choice_build: (item) ->
|
286
|
+
choice = $('<li />', { class: "search-choice" }).html("<span>#{item.html}</span>")
|
287
|
+
|
288
|
+
if item.disabled
|
289
|
+
choice.addClass 'search-choice-disabled'
|
290
|
+
else
|
291
|
+
close_link = $('<a />', { class: 'search-choice-close', 'data-option-array-index': item.array_index })
|
292
|
+
close_link.bind 'click.chosen', (evt) => this.choice_destroy_link_click(evt)
|
293
|
+
choice.append close_link
|
294
|
+
|
295
|
+
@search_container.before choice
|
296
|
+
|
297
|
+
choice_destroy_link_click: (evt) ->
|
298
|
+
evt.preventDefault()
|
299
|
+
evt.stopPropagation()
|
300
|
+
this.choice_destroy $(evt.target) unless @is_disabled
|
301
|
+
|
302
|
+
choice_destroy: (link) ->
|
303
|
+
if this.result_deselect( link[0].getAttribute("data-option-array-index") )
|
304
|
+
this.show_search_field_default()
|
305
|
+
|
306
|
+
this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.val().length < 1
|
307
|
+
|
308
|
+
link.parents('li').first().remove()
|
309
|
+
|
310
|
+
this.search_field_scale()
|
311
|
+
|
312
|
+
results_reset: ->
|
313
|
+
this.reset_single_select_options()
|
314
|
+
@form_field.options[0].selected = true
|
315
|
+
this.single_set_selected_text()
|
316
|
+
this.show_search_field_default()
|
317
|
+
this.results_reset_cleanup()
|
318
|
+
@form_field_jq.trigger "change"
|
319
|
+
this.results_hide() if @active_field
|
320
|
+
|
321
|
+
results_reset_cleanup: ->
|
322
|
+
@current_selectedIndex = @form_field.selectedIndex
|
323
|
+
@selected_item.find("abbr").remove()
|
324
|
+
|
325
|
+
result_select: (evt) ->
|
326
|
+
if @result_highlight
|
327
|
+
high = @result_highlight
|
328
|
+
|
329
|
+
if high.hasClass "create-option"
|
330
|
+
this.select_create_option(@search_field.val())
|
331
|
+
return this.results_hide()
|
332
|
+
|
333
|
+
this.result_clear_highlight()
|
334
|
+
|
335
|
+
if @is_multiple and @max_selected_options <= this.choices_count()
|
336
|
+
@form_field_jq.trigger("chosen:maxselected", {chosen: this})
|
337
|
+
return false
|
338
|
+
|
339
|
+
if @is_multiple
|
340
|
+
high.removeClass("active-result")
|
341
|
+
else
|
342
|
+
this.reset_single_select_options()
|
343
|
+
|
344
|
+
item = @results_data[ high[0].getAttribute("data-option-array-index") ]
|
345
|
+
item.selected = true
|
346
|
+
|
347
|
+
@form_field.options[item.options_index].selected = true
|
348
|
+
@selected_option_count = null
|
349
|
+
|
350
|
+
if @is_multiple
|
351
|
+
this.choice_build item
|
352
|
+
else
|
353
|
+
this.single_set_selected_text(item.text)
|
354
|
+
|
355
|
+
this.results_hide() unless (evt.metaKey or evt.ctrlKey) and @is_multiple
|
356
|
+
|
357
|
+
@search_field.val ""
|
358
|
+
|
359
|
+
@form_field_jq.trigger "change", {'selected': @form_field.options[item.options_index].value} if @is_multiple || @form_field.selectedIndex != @current_selectedIndex
|
360
|
+
@current_selectedIndex = @form_field.selectedIndex
|
361
|
+
this.search_field_scale()
|
362
|
+
|
363
|
+
single_set_selected_text: (text=@default_text) ->
|
364
|
+
if text is @default_text
|
365
|
+
@selected_item.addClass("chosen-default")
|
366
|
+
else
|
367
|
+
this.single_deselect_control_build()
|
368
|
+
@selected_item.removeClass("chosen-default")
|
369
|
+
|
370
|
+
@selected_item.find("span").text(text)
|
371
|
+
|
372
|
+
result_deselect: (pos) ->
|
373
|
+
result_data = @results_data[pos]
|
374
|
+
|
375
|
+
if not @form_field.options[result_data.options_index].disabled
|
376
|
+
result_data.selected = false
|
377
|
+
|
378
|
+
@form_field.options[result_data.options_index].selected = false
|
379
|
+
@selected_option_count = null
|
380
|
+
|
381
|
+
this.result_clear_highlight()
|
382
|
+
this.winnow_results() if @results_showing
|
383
|
+
|
384
|
+
@form_field_jq.trigger "change", {deselected: @form_field.options[result_data.options_index].value}
|
385
|
+
this.search_field_scale()
|
386
|
+
|
387
|
+
return true
|
388
|
+
else
|
389
|
+
return false
|
390
|
+
|
391
|
+
single_deselect_control_build: ->
|
392
|
+
return unless @allow_single_deselect
|
393
|
+
@selected_item.find("span").first().after "<abbr class=\"search-choice-close\"></abbr>" unless @selected_item.find("abbr").length
|
394
|
+
@selected_item.addClass("chosen-single-with-deselect")
|
395
|
+
|
396
|
+
get_search_text: ->
|
397
|
+
if @search_field.val() is @default_text then "" else $('<div/>').text($.trim(@search_field.val())).html()
|
398
|
+
|
399
|
+
winnow_results_set_highlight: ->
|
400
|
+
selected_results = if not @is_multiple then @search_results.find(".result-selected.active-result") else []
|
401
|
+
do_high = if selected_results.length then selected_results.first() else @search_results.find(".active-result").first()
|
402
|
+
|
403
|
+
this.result_do_highlight do_high if do_high?
|
404
|
+
|
405
|
+
no_results: (terms) ->
|
406
|
+
no_results_html = $('<li class="no-results">' + @results_none_found + ' "<span></span>"</li>')
|
407
|
+
no_results_html.find("span").first().html(terms)
|
408
|
+
@search_results.append no_results_html
|
409
|
+
|
410
|
+
show_create_option: (terms) ->
|
411
|
+
create_option_html = $('<li class="create-option active-result"><a href="javascript:void(0);">' + @create_option_text + '</a>: "' + terms + '"</li>')
|
412
|
+
@search_results.append create_option_html
|
413
|
+
|
414
|
+
create_option_clear: ->
|
415
|
+
@search_results.find(".create-option").remove()
|
416
|
+
|
417
|
+
select_create_option: (terms) ->
|
418
|
+
if $.isFunction(@create_option)
|
419
|
+
@create_option.call this, terms
|
420
|
+
else
|
421
|
+
this.select_append_option( {value: terms, text: terms} )
|
422
|
+
|
423
|
+
select_append_option: ( options ) ->
|
424
|
+
option = $('<option />', options ).attr('selected', 'selected')
|
425
|
+
@form_field_jq.append option
|
426
|
+
@form_field_jq.trigger "chosen:updated"
|
427
|
+
@form_field_jq.trigger "change"
|
428
|
+
@search_field.trigger "focus"
|
429
|
+
|
430
|
+
no_results_clear: ->
|
431
|
+
@search_results.find(".no-results").remove()
|
432
|
+
|
433
|
+
keydown_arrow: ->
|
434
|
+
if @results_showing and @result_highlight
|
435
|
+
next_sib = @result_highlight.nextAll("li.active-result").first()
|
436
|
+
this.result_do_highlight next_sib if next_sib
|
437
|
+
else if @results_showing and @create_option
|
438
|
+
this.result_do_highlight(@search_results.find('.create-option'))
|
439
|
+
else
|
440
|
+
this.results_show()
|
441
|
+
|
442
|
+
keyup_arrow: ->
|
443
|
+
if not @results_showing and not @is_multiple
|
444
|
+
this.results_show()
|
445
|
+
else if @result_highlight
|
446
|
+
prev_sibs = @result_highlight.prevAll("li.active-result")
|
447
|
+
|
448
|
+
if prev_sibs.length
|
449
|
+
this.result_do_highlight prev_sibs.first()
|
450
|
+
else
|
451
|
+
this.results_hide() if this.choices_count() > 0
|
452
|
+
this.result_clear_highlight()
|
453
|
+
|
454
|
+
keydown_backstroke: ->
|
455
|
+
if @pending_backstroke
|
456
|
+
this.choice_destroy @pending_backstroke.find("a").first()
|
457
|
+
this.clear_backstroke()
|
458
|
+
else
|
459
|
+
next_available_destroy = @search_container.siblings("li.search-choice").last()
|
460
|
+
if next_available_destroy.length and not next_available_destroy.hasClass("search-choice-disabled")
|
461
|
+
@pending_backstroke = next_available_destroy
|
462
|
+
if @single_backstroke_delete
|
463
|
+
@keydown_backstroke()
|
464
|
+
else
|
465
|
+
@pending_backstroke.addClass "search-choice-focus"
|
466
|
+
|
467
|
+
clear_backstroke: ->
|
468
|
+
@pending_backstroke.removeClass "search-choice-focus" if @pending_backstroke
|
469
|
+
@pending_backstroke = null
|
470
|
+
|
471
|
+
keydown_checker: (evt) ->
|
472
|
+
stroke = evt.which ? evt.keyCode
|
473
|
+
this.search_field_scale()
|
474
|
+
|
475
|
+
this.clear_backstroke() if stroke != 8 and this.pending_backstroke
|
476
|
+
|
477
|
+
switch stroke
|
478
|
+
when 8
|
479
|
+
@backstroke_length = this.search_field.val().length
|
480
|
+
break
|
481
|
+
when 9
|
482
|
+
this.result_select(evt) if this.results_showing and not @is_multiple
|
483
|
+
@mouse_on_container = false
|
484
|
+
break
|
485
|
+
when 13
|
486
|
+
evt.preventDefault()
|
487
|
+
break
|
488
|
+
when 38
|
489
|
+
evt.preventDefault()
|
490
|
+
this.keyup_arrow()
|
491
|
+
break
|
492
|
+
when 40
|
493
|
+
evt.preventDefault()
|
494
|
+
this.keydown_arrow()
|
495
|
+
break
|
496
|
+
|
497
|
+
search_field_scale: ->
|
498
|
+
if @is_multiple
|
499
|
+
h = 0
|
500
|
+
w = 0
|
501
|
+
|
502
|
+
style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"
|
503
|
+
styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing']
|
504
|
+
|
505
|
+
for style in styles
|
506
|
+
style_block += style + ":" + @search_field.css(style) + ";"
|
507
|
+
|
508
|
+
div = $('<div />', { 'style' : style_block })
|
509
|
+
div.text @search_field.val()
|
510
|
+
$('body').append div
|
511
|
+
|
512
|
+
w = div.width() + 25
|
513
|
+
div.remove()
|
514
|
+
|
515
|
+
f_width = @container.outerWidth()
|
516
|
+
|
517
|
+
if( w > f_width - 10 )
|
518
|
+
w = f_width - 10
|
519
|
+
|
520
|
+
@search_field.css({'width': w + 'px'})
|