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
@@ -0,0 +1,280 @@
|
|
1
|
+
class AbstractChosen
|
2
|
+
|
3
|
+
constructor: (@form_field, @options={}) ->
|
4
|
+
return unless AbstractChosen.browser_is_supported()
|
5
|
+
@is_multiple = @form_field.multiple
|
6
|
+
this.set_default_text()
|
7
|
+
this.set_default_values()
|
8
|
+
|
9
|
+
this.setup()
|
10
|
+
|
11
|
+
this.set_up_html()
|
12
|
+
this.register_observers()
|
13
|
+
|
14
|
+
set_default_values: ->
|
15
|
+
@click_test_action = (evt) => this.test_active_click(evt)
|
16
|
+
@activate_action = (evt) => this.activate_field(evt)
|
17
|
+
@active_field = false
|
18
|
+
@mouse_on_container = false
|
19
|
+
@results_showing = false
|
20
|
+
@result_highlighted = null
|
21
|
+
@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
|
22
|
+
@disable_search_threshold = @options.disable_search_threshold || 0
|
23
|
+
@disable_search = @options.disable_search || false
|
24
|
+
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true
|
25
|
+
@group_search = if @options.group_search? then @options.group_search else true
|
26
|
+
@search_contains = @options.search_contains || false
|
27
|
+
@single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true
|
28
|
+
@max_selected_options = @options.max_selected_options || Infinity
|
29
|
+
@inherit_select_classes = @options.inherit_select_classes || false
|
30
|
+
@display_selected_options = if @options.display_selected_options? then @options.display_selected_options else true
|
31
|
+
@display_disabled_options = if @options.display_disabled_options? then @options.display_disabled_options else true
|
32
|
+
@create_option = @options.create_option || false
|
33
|
+
@persistent_create_option = @options.persistent_create_option || false
|
34
|
+
@skip_no_results = @options.skip_no_results || false
|
35
|
+
|
36
|
+
set_default_text: ->
|
37
|
+
if @form_field.getAttribute("data-placeholder")
|
38
|
+
@default_text = @form_field.getAttribute("data-placeholder")
|
39
|
+
else if @is_multiple
|
40
|
+
@default_text = @options.placeholder_text_multiple || @options.placeholder_text || AbstractChosen.default_multiple_text
|
41
|
+
else
|
42
|
+
@default_text = @options.placeholder_text_single || @options.placeholder_text || AbstractChosen.default_single_text
|
43
|
+
|
44
|
+
@results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || AbstractChosen.default_no_result_text
|
45
|
+
@create_option_text = @form_field.getAttribute("data-create_option_text") || @options.create_option_text || AbstractChosen.default_create_option_text
|
46
|
+
|
47
|
+
mouse_enter: -> @mouse_on_container = true
|
48
|
+
mouse_leave: -> @mouse_on_container = false
|
49
|
+
|
50
|
+
input_focus: (evt) ->
|
51
|
+
if @is_multiple
|
52
|
+
setTimeout (=> this.container_mousedown()), 50 unless @active_field
|
53
|
+
else
|
54
|
+
@activate_field() unless @active_field
|
55
|
+
|
56
|
+
input_blur: (evt) ->
|
57
|
+
if not @mouse_on_container
|
58
|
+
@active_field = false
|
59
|
+
setTimeout (=> this.blur_test()), 100
|
60
|
+
|
61
|
+
results_option_build: (options) ->
|
62
|
+
content = ''
|
63
|
+
for data in @results_data
|
64
|
+
if data.group
|
65
|
+
content += this.result_add_group data
|
66
|
+
else
|
67
|
+
content += this.result_add_option data
|
68
|
+
|
69
|
+
# this select logic pins on an awkward flag
|
70
|
+
# we can make it better
|
71
|
+
if options?.first
|
72
|
+
if data.selected and @is_multiple
|
73
|
+
this.choice_build data
|
74
|
+
else if data.selected and not @is_multiple
|
75
|
+
this.single_set_selected_text(data.text)
|
76
|
+
|
77
|
+
content
|
78
|
+
|
79
|
+
result_add_option: (option) ->
|
80
|
+
return '' unless option.search_match
|
81
|
+
return '' unless this.include_option_in_results(option)
|
82
|
+
|
83
|
+
classes = []
|
84
|
+
classes.push "active-result" if !option.disabled and !(option.selected and @is_multiple)
|
85
|
+
classes.push "disabled-result" if option.disabled and !(option.selected and @is_multiple)
|
86
|
+
classes.push "result-selected" if option.selected
|
87
|
+
classes.push "group-option" if option.group_array_index?
|
88
|
+
classes.push option.classes if option.classes != ""
|
89
|
+
|
90
|
+
option_el = document.createElement("li")
|
91
|
+
option_el.className = classes.join(" ")
|
92
|
+
option_el.style.cssText = option.style
|
93
|
+
option_el.setAttribute("data-option-array-index", option.array_index)
|
94
|
+
option_el.innerHTML = option.search_text
|
95
|
+
|
96
|
+
this.outerHTML(option_el)
|
97
|
+
|
98
|
+
result_add_group: (group) ->
|
99
|
+
return '' unless group.search_match || group.group_match
|
100
|
+
return '' unless group.active_options > 0
|
101
|
+
|
102
|
+
group_el = document.createElement("li")
|
103
|
+
group_el.className = "group-result"
|
104
|
+
group_el.innerHTML = group.search_text
|
105
|
+
|
106
|
+
this.outerHTML(group_el)
|
107
|
+
|
108
|
+
append_option: (option) ->
|
109
|
+
this.select_append_option(option)
|
110
|
+
|
111
|
+
results_update_field: ->
|
112
|
+
this.set_default_text()
|
113
|
+
this.results_reset_cleanup() if not @is_multiple
|
114
|
+
this.result_clear_highlight()
|
115
|
+
this.results_build()
|
116
|
+
this.winnow_results() if @results_showing
|
117
|
+
|
118
|
+
reset_single_select_options: () ->
|
119
|
+
for result in @results_data
|
120
|
+
result.selected = false if result.selected
|
121
|
+
|
122
|
+
results_toggle: ->
|
123
|
+
if @results_showing
|
124
|
+
this.results_hide()
|
125
|
+
else
|
126
|
+
this.results_show()
|
127
|
+
|
128
|
+
results_search: (evt) ->
|
129
|
+
if @results_showing
|
130
|
+
this.winnow_results()
|
131
|
+
else
|
132
|
+
this.results_show()
|
133
|
+
|
134
|
+
winnow_results: ->
|
135
|
+
this.no_results_clear()
|
136
|
+
|
137
|
+
results = 0
|
138
|
+
exact_result = false
|
139
|
+
|
140
|
+
searchText = this.get_search_text()
|
141
|
+
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
|
142
|
+
regexAnchor = if @search_contains then "" else "^"
|
143
|
+
regex = new RegExp(regexAnchor + escapedSearchText, 'i')
|
144
|
+
zregex = new RegExp(escapedSearchText, 'i')
|
145
|
+
eregex = new RegExp('^' + escapedSearchText + '$', 'i')
|
146
|
+
|
147
|
+
for option in @results_data
|
148
|
+
|
149
|
+
option.search_match = false
|
150
|
+
results_group = null
|
151
|
+
|
152
|
+
if this.include_option_in_results(option)
|
153
|
+
|
154
|
+
if option.group
|
155
|
+
option.group_match = false
|
156
|
+
option.active_options = 0
|
157
|
+
|
158
|
+
if option.group_array_index? and @results_data[option.group_array_index]
|
159
|
+
results_group = @results_data[option.group_array_index]
|
160
|
+
results += 1 if results_group.active_options is 0 and results_group.search_match
|
161
|
+
results_group.active_options += 1
|
162
|
+
|
163
|
+
unless option.group and not @group_search
|
164
|
+
|
165
|
+
option.search_text = if option.group then option.label else option.html
|
166
|
+
option.search_match = this.search_string_match(option.search_text, regex)
|
167
|
+
results += 1 if option.search_match and not option.group
|
168
|
+
|
169
|
+
exact_result = eregex.test option.html
|
170
|
+
|
171
|
+
if option.search_match
|
172
|
+
if searchText.length
|
173
|
+
startpos = option.search_text.search zregex
|
174
|
+
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length)
|
175
|
+
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos)
|
176
|
+
|
177
|
+
results_group.group_match = true if results_group?
|
178
|
+
|
179
|
+
else if option.group_array_index? and @results_data[option.group_array_index].search_match
|
180
|
+
option.search_match = true
|
181
|
+
|
182
|
+
this.result_clear_highlight()
|
183
|
+
|
184
|
+
if results < 1 and searchText.length
|
185
|
+
this.update_results_content ""
|
186
|
+
this.no_results searchText unless @create_option and @skip_no_results
|
187
|
+
else
|
188
|
+
this.update_results_content this.results_option_build()
|
189
|
+
this.winnow_results_set_highlight()
|
190
|
+
|
191
|
+
if @create_option and (results < 1 or (!exact_result and @persistent_create_option)) and searchText.length
|
192
|
+
this.show_create_option( searchText )
|
193
|
+
|
194
|
+
search_string_match: (search_string, regex) ->
|
195
|
+
if regex.test search_string
|
196
|
+
return true
|
197
|
+
else if @enable_split_word_search and (search_string.indexOf(" ") >= 0 or search_string.indexOf("[") == 0)
|
198
|
+
#TODO: replace this substitution of /\[\]/ with a list of characters to skip.
|
199
|
+
parts = search_string.replace(/\[|\]/g, "").split(" ")
|
200
|
+
if parts.length
|
201
|
+
for part in parts
|
202
|
+
if regex.test part
|
203
|
+
return true
|
204
|
+
|
205
|
+
choices_count: ->
|
206
|
+
return @selected_option_count if @selected_option_count?
|
207
|
+
|
208
|
+
@selected_option_count = 0
|
209
|
+
for option in @form_field.options
|
210
|
+
@selected_option_count += 1 if option.selected
|
211
|
+
|
212
|
+
return @selected_option_count
|
213
|
+
|
214
|
+
choices_click: (evt) ->
|
215
|
+
evt.preventDefault()
|
216
|
+
this.results_show() unless @results_showing or @is_disabled
|
217
|
+
|
218
|
+
keyup_checker: (evt) ->
|
219
|
+
stroke = evt.which ? evt.keyCode
|
220
|
+
this.search_field_scale()
|
221
|
+
|
222
|
+
switch stroke
|
223
|
+
when 8
|
224
|
+
if @is_multiple and @backstroke_length < 1 and this.choices_count() > 0
|
225
|
+
this.keydown_backstroke()
|
226
|
+
else if not @pending_backstroke
|
227
|
+
this.result_clear_highlight()
|
228
|
+
this.results_search()
|
229
|
+
when 13
|
230
|
+
evt.preventDefault()
|
231
|
+
this.result_select(evt) if this.results_showing
|
232
|
+
when 27
|
233
|
+
this.results_hide() if @results_showing
|
234
|
+
return true
|
235
|
+
when 9, 38, 40, 16, 91, 17
|
236
|
+
# don't do anything on these keys
|
237
|
+
else this.results_search()
|
238
|
+
|
239
|
+
container_width: ->
|
240
|
+
return if @options.width? then @options.width else "#{@form_field.offsetWidth}px"
|
241
|
+
|
242
|
+
include_option_in_results: (option) ->
|
243
|
+
return false if @is_multiple and (not @display_selected_options and option.selected)
|
244
|
+
return false if not @display_disabled_options and option.disabled
|
245
|
+
return false if option.empty
|
246
|
+
|
247
|
+
return true
|
248
|
+
|
249
|
+
search_results_touchstart: (evt) ->
|
250
|
+
@touch_started = true
|
251
|
+
this.search_results_mouseover(evt)
|
252
|
+
|
253
|
+
search_results_touchmove: (evt) ->
|
254
|
+
@touch_started = false
|
255
|
+
this.search_results_mouseout(evt)
|
256
|
+
|
257
|
+
search_results_touchend: (evt) ->
|
258
|
+
this.search_results_mouseup(evt) if @touch_started
|
259
|
+
|
260
|
+
outerHTML: (element) ->
|
261
|
+
return element.outerHTML if element.outerHTML
|
262
|
+
tmp = document.createElement("div")
|
263
|
+
tmp.appendChild(element)
|
264
|
+
tmp.innerHTML
|
265
|
+
|
266
|
+
# class methods and variables ============================================================
|
267
|
+
|
268
|
+
@browser_is_supported: ->
|
269
|
+
if window.navigator.appName == "Microsoft Internet Explorer"
|
270
|
+
return document.documentMode >= 8
|
271
|
+
if /iP(od|hone)/i.test(window.navigator.userAgent)
|
272
|
+
return false
|
273
|
+
if /Android/i.test(window.navigator.userAgent)
|
274
|
+
return false if /Mobile/i.test(window.navigator.userAgent)
|
275
|
+
return true
|
276
|
+
|
277
|
+
@default_multiple_text: "Select Some Options"
|
278
|
+
@default_single_text: "Select an Option"
|
279
|
+
@default_no_result_text: "No results match"
|
280
|
+
@default_create_option_text: "Add Option"
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class SelectParser
|
2
|
+
|
3
|
+
constructor: ->
|
4
|
+
@options_index = 0
|
5
|
+
@parsed = []
|
6
|
+
|
7
|
+
add_node: (child) ->
|
8
|
+
if child.nodeName.toUpperCase() is "OPTGROUP"
|
9
|
+
this.add_group child
|
10
|
+
else
|
11
|
+
this.add_option child
|
12
|
+
|
13
|
+
add_group: (group) ->
|
14
|
+
group_position = @parsed.length
|
15
|
+
@parsed.push
|
16
|
+
array_index: group_position
|
17
|
+
group: true
|
18
|
+
label: this.escapeExpression(group.label)
|
19
|
+
children: 0
|
20
|
+
disabled: group.disabled
|
21
|
+
this.add_option( option, group_position, group.disabled ) for option in group.childNodes
|
22
|
+
|
23
|
+
add_option: (option, group_position, group_disabled) ->
|
24
|
+
if option.nodeName.toUpperCase() is "OPTION"
|
25
|
+
if option.text != ""
|
26
|
+
if group_position?
|
27
|
+
@parsed[group_position].children += 1
|
28
|
+
@parsed.push
|
29
|
+
array_index: @parsed.length
|
30
|
+
options_index: @options_index
|
31
|
+
value: option.value
|
32
|
+
text: option.text
|
33
|
+
html: option.innerHTML
|
34
|
+
selected: option.selected
|
35
|
+
disabled: if group_disabled is true then group_disabled else option.disabled
|
36
|
+
group_array_index: group_position
|
37
|
+
classes: option.className
|
38
|
+
style: option.style.cssText
|
39
|
+
else
|
40
|
+
@parsed.push
|
41
|
+
array_index: @parsed.length
|
42
|
+
options_index: @options_index
|
43
|
+
empty: true
|
44
|
+
@options_index += 1
|
45
|
+
|
46
|
+
escapeExpression: (text) ->
|
47
|
+
if not text? or text is false
|
48
|
+
return ""
|
49
|
+
unless /[\&\<\>\"\'\`]/.test(text)
|
50
|
+
return text
|
51
|
+
map =
|
52
|
+
"<": "<"
|
53
|
+
">": ">"
|
54
|
+
'"': """
|
55
|
+
"'": "'"
|
56
|
+
"`": "`"
|
57
|
+
unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g
|
58
|
+
text.replace unsafe_chars, (chr) ->
|
59
|
+
map[chr] || "&"
|
60
|
+
|
61
|
+
SelectParser.select_to_array = (select) ->
|
62
|
+
parser = new SelectParser()
|
63
|
+
parser.add_node( child ) for child in select.childNodes
|
64
|
+
parser.parsed
|