intesys_asset_manager 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,498 @@
1
+ class @Chosen extends AbstractChosen
2
+
3
+ setup: ->
4
+ @current_selectedIndex = @form_field.selectedIndex
5
+ @is_rtl = @form_field.hasClassName "chosen-rtl"
6
+
7
+ set_default_values: ->
8
+ super()
9
+
10
+ # HTML Templates
11
+ @single_temp = new Template('<a class="chosen-single chosen-default" tabindex="-1"><span>#{default}</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>')
12
+ @multi_temp = new Template('<ul class="chosen-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chosen-drop"><ul class="chosen-results"></ul></div>')
13
+ @no_results_temp = new Template('<li class="no-results">' + @results_none_found + ' "<span>#{terms}</span>"</li>')
14
+
15
+ set_up_html: ->
16
+ container_classes = ["chosen-container"]
17
+ container_classes.push "chosen-container-" + (if @is_multiple then "multi" else "single")
18
+ container_classes.push @form_field.className if @inherit_select_classes && @form_field.className
19
+ container_classes.push "chosen-rtl" if @is_rtl
20
+
21
+ container_props =
22
+ 'class': container_classes.join ' '
23
+ 'style': "width: #{this.container_width()};"
24
+ 'title': @form_field.title
25
+
26
+ container_props.id = @form_field.id.replace(/[^\w]/g, '_') + "_chosen" if @form_field.id.length
27
+
28
+ @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 }) )
29
+
30
+ @form_field.hide().insert({ after: @container })
31
+ @dropdown = @container.down('div.chosen-drop')
32
+
33
+ @search_field = @container.down('input')
34
+ @search_results = @container.down('ul.chosen-results')
35
+ this.search_field_scale()
36
+
37
+ @search_no_results = @container.down('li.no-results')
38
+
39
+ if @is_multiple
40
+ @search_choices = @container.down('ul.chosen-choices')
41
+ @search_container = @container.down('li.search-field')
42
+ else
43
+ @search_container = @container.down('div.chosen-search')
44
+ @selected_item = @container.down('.chosen-single')
45
+
46
+ this.results_build()
47
+ this.set_tab_index()
48
+ this.set_label_behavior()
49
+ @form_field.fire("chosen:ready", {chosen: this})
50
+
51
+ register_observers: ->
52
+ @container.observe "mousedown", (evt) => this.container_mousedown(evt)
53
+ @container.observe "mouseup", (evt) => this.container_mouseup(evt)
54
+ @container.observe "mouseenter", (evt) => this.mouse_enter(evt)
55
+ @container.observe "mouseleave", (evt) => this.mouse_leave(evt)
56
+
57
+ @search_results.observe "mouseup", (evt) => this.search_results_mouseup(evt)
58
+ @search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt)
59
+ @search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt)
60
+ @search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt)
61
+ @search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt)
62
+
63
+ @search_results.observe "touchstart", (evt) => this.search_results_touchstart(evt)
64
+ @search_results.observe "touchmove", (evt) => this.search_results_touchmove(evt)
65
+ @search_results.observe "touchend", (evt) => this.search_results_touchend(evt)
66
+
67
+ @form_field.observe "chosen:updated", (evt) => this.results_update_field(evt)
68
+ @form_field.observe "chosen:activate", (evt) => this.activate_field(evt)
69
+ @form_field.observe "chosen:open", (evt) => this.container_mousedown(evt)
70
+ @form_field.observe "chosen:close", (evt) => this.input_blur(evt)
71
+
72
+ @search_field.observe "blur", (evt) => this.input_blur(evt)
73
+ @search_field.observe "keyup", (evt) => this.keyup_checker(evt)
74
+ @search_field.observe "keydown", (evt) => this.keydown_checker(evt)
75
+ @search_field.observe "focus", (evt) => this.input_focus(evt)
76
+ @search_field.observe "cut", (evt) => this.clipboard_event_checker(evt)
77
+ @search_field.observe "paste", (evt) => this.clipboard_event_checker(evt)
78
+
79
+ if @is_multiple
80
+ @search_choices.observe "click", (evt) => this.choices_click(evt)
81
+ else
82
+ @container.observe "click", (evt) => evt.preventDefault() # gobble click of anchor
83
+
84
+ destroy: ->
85
+ @container.ownerDocument.stopObserving "click", @click_test_action
86
+
87
+ @form_field.stopObserving()
88
+ @container.stopObserving()
89
+ @search_results.stopObserving()
90
+ @search_field.stopObserving()
91
+ @form_field_label.stopObserving() if @form_field_label?
92
+
93
+ if @is_multiple
94
+ @search_choices.stopObserving()
95
+ @container.select(".search-choice-close").each (choice) ->
96
+ choice.stopObserving()
97
+ else
98
+ @selected_item.stopObserving()
99
+
100
+ if @search_field.tabIndex
101
+ @form_field.tabIndex = @search_field.tabIndex
102
+
103
+ @container.remove()
104
+ @form_field.show()
105
+
106
+ search_field_disabled: ->
107
+ @is_disabled = @form_field.disabled
108
+ if(@is_disabled)
109
+ @container.addClassName 'chosen-disabled'
110
+ @search_field.disabled = true
111
+ @selected_item.stopObserving "focus", @activate_action if !@is_multiple
112
+ this.close_field()
113
+ else
114
+ @container.removeClassName 'chosen-disabled'
115
+ @search_field.disabled = false
116
+ @selected_item.observe "focus", @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.stop()
122
+
123
+ if not (evt? and evt.target.hasClassName "search-choice-close")
124
+ if not @active_field
125
+ @search_field.clear() if @is_multiple
126
+ @container.ownerDocument.observe "click", @click_test_action
127
+ this.results_show()
128
+ else if not @is_multiple and evt and (evt.target is @selected_item || evt.target.up("a.chosen-single"))
129
+ this.results_toggle()
130
+
131
+ this.activate_field()
132
+
133
+ container_mouseup: (evt) ->
134
+ this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled
135
+
136
+ search_results_mousewheel: (evt) ->
137
+ delta = -evt.wheelDelta or evt.detail
138
+ if delta?
139
+ evt.preventDefault()
140
+ delta = delta * 40 if evt.type is 'DOMMouseScroll'
141
+ @search_results.scrollTop = delta + @search_results.scrollTop
142
+
143
+ blur_test: (evt) ->
144
+ this.close_field() if not @active_field and @container.hasClassName("chosen-container-active")
145
+
146
+ close_field: ->
147
+ @container.ownerDocument.stopObserving "click", @click_test_action
148
+
149
+ @active_field = false
150
+ this.results_hide()
151
+
152
+ @container.removeClassName "chosen-container-active"
153
+ this.clear_backstroke()
154
+
155
+ this.show_search_field_default()
156
+ this.search_field_scale()
157
+
158
+ activate_field: ->
159
+ @container.addClassName "chosen-container-active"
160
+ @active_field = true
161
+
162
+ @search_field.value = @search_field.value
163
+ @search_field.focus()
164
+
165
+ test_active_click: (evt) ->
166
+ if evt.target.up('.chosen-container') is @container
167
+ @active_field = true
168
+ else
169
+ this.close_field()
170
+
171
+ results_build: ->
172
+ @parsing = true
173
+ @selected_option_count = null
174
+
175
+ @results_data = SelectParser.select_to_array @form_field
176
+
177
+ if @is_multiple
178
+ @search_choices.select("li.search-choice").invoke("remove")
179
+ else if not @is_multiple
180
+ this.single_set_selected_text()
181
+ if @disable_search or @form_field.options.length <= @disable_search_threshold
182
+ @search_field.readOnly = true
183
+ @container.addClassName "chosen-container-single-nosearch"
184
+ else
185
+ @search_field.readOnly = false
186
+ @container.removeClassName "chosen-container-single-nosearch"
187
+
188
+ this.update_results_content this.results_option_build({first:true})
189
+
190
+ this.search_field_disabled()
191
+ this.show_search_field_default()
192
+ this.search_field_scale()
193
+
194
+ @parsing = false
195
+
196
+ result_do_highlight: (el) ->
197
+ this.result_clear_highlight()
198
+
199
+ @result_highlight = el
200
+ @result_highlight.addClassName "highlighted"
201
+
202
+ maxHeight = parseInt @search_results.getStyle('maxHeight'), 10
203
+ visible_top = @search_results.scrollTop
204
+ visible_bottom = maxHeight + visible_top
205
+
206
+ high_top = @result_highlight.positionedOffset().top
207
+ high_bottom = high_top + @result_highlight.getHeight()
208
+
209
+ if high_bottom >= visible_bottom
210
+ @search_results.scrollTop = if (high_bottom - maxHeight) > 0 then (high_bottom - maxHeight) else 0
211
+ else if high_top < visible_top
212
+ @search_results.scrollTop = high_top
213
+
214
+ result_clear_highlight: ->
215
+ @result_highlight.removeClassName('highlighted') if @result_highlight
216
+ @result_highlight = null
217
+
218
+ results_show: ->
219
+ if @is_multiple and @max_selected_options <= this.choices_count()
220
+ @form_field.fire("chosen:maxselected", {chosen: this})
221
+ return false
222
+
223
+ @container.addClassName "chosen-with-drop"
224
+ @results_showing = true
225
+
226
+ @search_field.focus()
227
+ @search_field.value = @search_field.value
228
+
229
+ this.winnow_results()
230
+ @form_field.fire("chosen:showing_dropdown", {chosen: this})
231
+
232
+ update_results_content: (content) ->
233
+ @search_results.update content
234
+
235
+ results_hide: ->
236
+ if @results_showing
237
+ this.result_clear_highlight()
238
+
239
+ @container.removeClassName "chosen-with-drop"
240
+ @form_field.fire("chosen:hiding_dropdown", {chosen: this})
241
+
242
+ @results_showing = false
243
+
244
+
245
+ set_tab_index: (el) ->
246
+ if @form_field.tabIndex
247
+ ti = @form_field.tabIndex
248
+ @form_field.tabIndex = -1
249
+ @search_field.tabIndex = ti
250
+
251
+ set_label_behavior: ->
252
+ @form_field_label = @form_field.up("label") # first check for a parent label
253
+ if not @form_field_label?
254
+ @form_field_label = $$("label[for='#{@form_field.id}']").first() #next check for a for=#{id}
255
+
256
+ if @form_field_label?
257
+ @form_field_label.observe "click", (evt) => if @is_multiple then this.container_mousedown(evt) else this.activate_field()
258
+
259
+ show_search_field_default: ->
260
+ if @is_multiple and this.choices_count() < 1 and not @active_field
261
+ @search_field.value = @default_text
262
+ @search_field.addClassName "default"
263
+ else
264
+ @search_field.value = ""
265
+ @search_field.removeClassName "default"
266
+
267
+ search_results_mouseup: (evt) ->
268
+ target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
269
+ if target
270
+ @result_highlight = target
271
+ this.result_select(evt)
272
+ @search_field.focus()
273
+
274
+ search_results_mouseover: (evt) ->
275
+ target = if evt.target.hasClassName("active-result") then evt.target else evt.target.up(".active-result")
276
+ this.result_do_highlight( target ) if target
277
+
278
+ search_results_mouseout: (evt) ->
279
+ this.result_clear_highlight() if evt.target.hasClassName('active-result') or evt.target.up('.active-result')
280
+
281
+ choice_build: (item) ->
282
+ choice = new Element('li', { class: "search-choice" }).update("<span>#{item.html}</span>")
283
+
284
+ if item.disabled
285
+ choice.addClassName 'search-choice-disabled'
286
+ else
287
+ close_link = new Element('a', { href: '#', class: 'search-choice-close', rel: item.array_index })
288
+ close_link.observe "click", (evt) => this.choice_destroy_link_click(evt)
289
+ choice.insert close_link
290
+
291
+ @search_container.insert { before: choice }
292
+
293
+ choice_destroy_link_click: (evt) ->
294
+ evt.preventDefault()
295
+ evt.stopPropagation()
296
+ this.choice_destroy evt.target unless @is_disabled
297
+
298
+ choice_destroy: (link) ->
299
+ if this.result_deselect link.readAttribute("rel")
300
+ this.show_search_field_default()
301
+
302
+ this.results_hide() if @is_multiple and this.choices_count() > 0 and @search_field.value.length < 1
303
+
304
+ link.up('li').remove()
305
+
306
+ this.search_field_scale()
307
+
308
+ results_reset: ->
309
+ this.reset_single_select_options()
310
+ @form_field.options[0].selected = true
311
+ this.single_set_selected_text()
312
+ this.show_search_field_default()
313
+ this.results_reset_cleanup()
314
+ @form_field.simulate("change") if typeof Event.simulate is 'function'
315
+ this.results_hide() if @active_field
316
+
317
+ results_reset_cleanup: ->
318
+ @current_selectedIndex = @form_field.selectedIndex
319
+ deselect_trigger = @selected_item.down("abbr")
320
+ deselect_trigger.remove() if(deselect_trigger)
321
+
322
+ result_select: (evt) ->
323
+ if @result_highlight
324
+ high = @result_highlight
325
+ this.result_clear_highlight()
326
+
327
+ if @is_multiple and @max_selected_options <= this.choices_count()
328
+ @form_field.fire("chosen:maxselected", {chosen: this})
329
+ return false
330
+
331
+ if @is_multiple
332
+ high.removeClassName("active-result")
333
+ else
334
+ this.reset_single_select_options()
335
+
336
+ high.addClassName("result-selected")
337
+
338
+ item = @results_data[ high.getAttribute("data-option-array-index") ]
339
+ item.selected = true
340
+
341
+ @form_field.options[item.options_index].selected = true
342
+ @selected_option_count = null
343
+
344
+ if @is_multiple
345
+ this.choice_build item
346
+ else
347
+ this.single_set_selected_text(item.text)
348
+
349
+ this.results_hide() unless (evt.metaKey or evt.ctrlKey) and @is_multiple
350
+
351
+ @search_field.value = ""
352
+
353
+ @form_field.simulate("change") if typeof Event.simulate is 'function' && (@is_multiple || @form_field.selectedIndex != @current_selectedIndex)
354
+ @current_selectedIndex = @form_field.selectedIndex
355
+
356
+ this.search_field_scale()
357
+
358
+ single_set_selected_text: (text=@default_text) ->
359
+ if text is @default_text
360
+ @selected_item.addClassName("chosen-default")
361
+ else
362
+ this.single_deselect_control_build()
363
+ @selected_item.removeClassName("chosen-default")
364
+
365
+ @selected_item.down("span").update(text)
366
+
367
+ result_deselect: (pos) ->
368
+ result_data = @results_data[pos]
369
+
370
+ if not @form_field.options[result_data.options_index].disabled
371
+ result_data.selected = false
372
+
373
+ @form_field.options[result_data.options_index].selected = false
374
+ @selected_option_count = null
375
+
376
+ this.result_clear_highlight()
377
+ this.winnow_results() if @results_showing
378
+
379
+ @form_field.simulate("change") if typeof Event.simulate is 'function'
380
+ this.search_field_scale()
381
+ return true
382
+ else
383
+ return false
384
+
385
+ single_deselect_control_build: ->
386
+ return unless @allow_single_deselect
387
+ @selected_item.down("span").insert { after: "<abbr class=\"search-choice-close\"></abbr>" } unless @selected_item.down("abbr")
388
+ @selected_item.addClassName("chosen-single-with-deselect")
389
+
390
+ get_search_text: ->
391
+ if @search_field.value is @default_text then "" else @search_field.value.strip().escapeHTML()
392
+
393
+ winnow_results_set_highlight: ->
394
+ if not @is_multiple
395
+ do_high = @search_results.down(".result-selected.active-result")
396
+
397
+ if not do_high?
398
+ do_high = @search_results.down(".active-result")
399
+
400
+ this.result_do_highlight do_high if do_high?
401
+
402
+ no_results: (terms) ->
403
+ @search_results.insert @no_results_temp.evaluate( terms: terms )
404
+ @form_field.fire("chosen:no_results", {chosen: this})
405
+
406
+ no_results_clear: ->
407
+ nr = null
408
+ nr.remove() while nr = @search_results.down(".no-results")
409
+
410
+
411
+ keydown_arrow: ->
412
+ if @results_showing and @result_highlight
413
+ next_sib = @result_highlight.next('.active-result')
414
+ this.result_do_highlight next_sib if next_sib
415
+ else
416
+ this.results_show()
417
+
418
+ keyup_arrow: ->
419
+ if not @results_showing and not @is_multiple
420
+ this.results_show()
421
+ else if @result_highlight
422
+ sibs = @result_highlight.previousSiblings()
423
+ actives = @search_results.select("li.active-result")
424
+ prevs = sibs.intersect(actives)
425
+
426
+ if prevs.length
427
+ this.result_do_highlight prevs.first()
428
+ else
429
+ this.results_hide() if this.choices_count() > 0
430
+ this.result_clear_highlight()
431
+
432
+ keydown_backstroke: ->
433
+ if @pending_backstroke
434
+ this.choice_destroy @pending_backstroke.down("a")
435
+ this.clear_backstroke()
436
+ else
437
+ next_available_destroy = @search_container.siblings().last()
438
+ if next_available_destroy and next_available_destroy.hasClassName("search-choice") and not next_available_destroy.hasClassName("search-choice-disabled")
439
+ @pending_backstroke = next_available_destroy
440
+ @pending_backstroke.addClassName("search-choice-focus") if @pending_backstroke
441
+ if @single_backstroke_delete
442
+ @keydown_backstroke()
443
+ else
444
+ @pending_backstroke.addClassName("search-choice-focus")
445
+
446
+ clear_backstroke: ->
447
+ @pending_backstroke.removeClassName("search-choice-focus") if @pending_backstroke
448
+ @pending_backstroke = null
449
+
450
+ keydown_checker: (evt) ->
451
+ stroke = evt.which ? evt.keyCode
452
+ this.search_field_scale()
453
+
454
+ this.clear_backstroke() if stroke != 8 and this.pending_backstroke
455
+
456
+ switch stroke
457
+ when 8
458
+ @backstroke_length = this.search_field.value.length
459
+ break
460
+ when 9
461
+ this.result_select(evt) if this.results_showing and not @is_multiple
462
+ @mouse_on_container = false
463
+ break
464
+ when 13
465
+ evt.preventDefault()
466
+ break
467
+ when 38
468
+ evt.preventDefault()
469
+ this.keyup_arrow()
470
+ break
471
+ when 40
472
+ evt.preventDefault()
473
+ this.keydown_arrow()
474
+ break
475
+
476
+ search_field_scale: ->
477
+ if @is_multiple
478
+ h = 0
479
+ w = 0
480
+
481
+ style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"
482
+ styles = ['font-size','font-style', 'font-weight', 'font-family','line-height', 'text-transform', 'letter-spacing']
483
+
484
+ for style in styles
485
+ style_block += style + ":" + @search_field.getStyle(style) + ";"
486
+
487
+ div = new Element('div', { 'style' : style_block }).update(@search_field.value.escapeHTML())
488
+ document.body.appendChild(div)
489
+
490
+ w = Element.measure(div, 'width') + 25
491
+ div.remove()
492
+
493
+ f_width = @container.getWidth()
494
+
495
+ if( w > f_width-10 )
496
+ w = f_width - 10
497
+
498
+ @search_field.setStyle({'width': w + 'px'})