chosen-awesome-rails 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 753b8b56b10d8afabb1b00e40537ce896906728f
4
- data.tar.gz: 6a12cdd3920ce72b0c343445397ae463dda8b098
3
+ metadata.gz: 83b18c9dda7c321268a60e99da75a2724585c054
4
+ data.tar.gz: 1b293fb54a12a4ebf435e1a6c0f26ba9bdcadc3a
5
5
  SHA512:
6
- metadata.gz: f08674691cb3adc8ee5206ce8d25085b994c9b7598b5330f3bf4e7a8d01ea562f12c0efdf537ef6d31f85da114ab7e7362be77161b81563fffe36463182c2909
7
- data.tar.gz: 5d705d50f2263136a0307bbe5644946366c618235fc46db7e39786ae8b06721e36dc85f5a3be9635a7bf1089d73afb8ecf3e64e72580475b7d60326131d0481d
6
+ metadata.gz: ce188e12f5e2ac30b1d3d7280bee6e3e0cba2aaf399156fcfbff8aaedc2c73e675262f1f7b1a8391edfd48400eac3efd00b727e839d56661939641a17be118d7
7
+ data.tar.gz: 48743fef272a6ddf06371103e94bc38e32e435dc01f100d65a41e308289afce5ba824a60e97c35add75fcd5462ace2c8deac4d8fdda6067e10223c85929c7448
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "chosen-awesome-rails".freeze
6
- s.version = "1.2.1"
6
+ s.version = "1.3.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -285,7 +285,7 @@ class Chosen
285
285
 
286
286
  @$dropdown.bind "mouseover", "li.chosen-option", (evt) => @dropdown_mouseover(evt)
287
287
  @$dropdown.bind "mousedown", "li.chosen-option", (evt) => @dropdown_mousedown(evt)
288
- @$dropdown.bind "mousewheel DOMMouseScroll", (evt) => @prevent_page_scroll(evt)
288
+ @$dropdown.bind "mousewheel DOMMouseScroll", (evt) => @dropdown_scroll(evt)
289
289
 
290
290
  @$container.addClass("opened")
291
291
  @$dropdown.addClass("opened")
@@ -338,11 +338,20 @@ class Chosen
338
338
  evt.stopImmediatePropagation()
339
339
  return
340
340
 
341
- prevent_page_scroll: (evt) ->
341
+ dropdown_scroll: (evt) ->
342
342
  delta = evt.originalEvent.wheelDelta || -evt.originalEvent.detail
343
343
 
344
- if (delta < 0 and @$dropdown.$list[0].scrollHeight - @$dropdown.scrollTop() == @$dropdown.innerHeight()) or (delta > 0 and @$dropdown.scrollTop() == 0)
344
+ if (delta < 0 and @$dropdown.$list[0].scrollHeight - @$dropdown.scrollTop() <= @$dropdown.innerHeight())
345
345
  evt.preventDefault()
346
+ evt.stopImmediatePropagation()
347
+
348
+ # Try pulling next pages
349
+ if @ajax
350
+ @pull_next_page()
351
+
352
+ else if (delta > 0 and @$dropdown.scrollTop() <= 0)
353
+ evt.preventDefault()
354
+ evt.stopImmediatePropagation()
346
355
 
347
356
  return
348
357
 
@@ -384,9 +393,7 @@ class Chosen
384
393
 
385
394
  return
386
395
 
387
- redraw_dropdown: (data) ->
388
- @parser.update(data) unless data is undefined
389
-
396
+ redraw_dropdown: ->
390
397
  changed = @apply_filter()
391
398
 
392
399
  @update_dropdown_position()
@@ -484,13 +491,19 @@ class Chosen
484
491
 
485
492
  move_selection: (dir) ->
486
493
  cursor = @parser.index_of(@cursor_option) + dir
487
- cursor = @parser.available_options.length - 1 if cursor < 0
488
- cursor = 0 if cursor > @parser.available_options.length - 1
494
+
495
+ if cursor > @parser.available_options.length - 1
496
+ if @ajax and @ajax.has_more isnt false
497
+ # Try pulling next pages
498
+ @pull_next_page(=> @move_selection(dir))
499
+
500
+ return @
501
+
502
+ return @ if cursor < 0
489
503
 
490
504
  @move_selection_to(cursor)
491
505
 
492
506
  if @cursor_option and @cursor_option.selected and @parser.selectable_options.length
493
- # TODO: optimize this, could be slow on large lists
494
507
  return @move_selection(dir)
495
508
 
496
509
  return @
@@ -504,12 +517,15 @@ class Chosen
504
517
  return unless @cursor_option
505
518
 
506
519
  $element = @cursor_option.$listed.addClass("active")
507
- top = $element.position().top + $element.height()
520
+ top = $element.position().top
521
+ bottom = top + $element.outerHeight()
508
522
  list_height = @$dropdown.height()
509
523
  list_scroll = @$dropdown.scrollTop()
510
524
 
511
- if top >= list_height + list_scroll or list_scroll >= top
512
- @$dropdown.scrollTop($element.position().top)
525
+ if bottom >= list_height + list_scroll
526
+ @$dropdown.scrollTop(bottom - list_height)
527
+ else if list_scroll >= top
528
+ @$dropdown.scrollTop(top)
513
529
 
514
530
  return @
515
531
 
@@ -535,13 +551,17 @@ class Chosen
535
551
  clearTimeout(@ajax.pending_update)
536
552
 
537
553
  @ajax.pending_update = setTimeout =>
538
- if @ajax.pending_request and @ajax.pending_request.readyState isnt 4
539
- @ajax.pending_request.abort()
554
+ if @ajax.pending_update_request and @ajax.pending_update_request.readyState isnt 4
555
+ @ajax.pending_update_request.abort()
556
+
557
+ # Reset current page and has_more flag
558
+ delete @ajax.data.page if @ajax.data
559
+ delete @ajax.has_more
540
560
 
541
561
  data =
542
562
  query: @$container.$search.val()
543
563
 
544
- @ajax.pending_request = $.ajax
564
+ @ajax.pending_update_request = $.ajax
545
565
  url: @ajax.url
546
566
  type: @ajax.type or "get"
547
567
  dataType: @ajax.dataType or "json"
@@ -555,7 +575,8 @@ class Chosen
555
575
  success: (data) =>
556
576
  @loaded()
557
577
  @ajax.success?(arguments...)
558
- @redraw_dropdown(data)
578
+ @parser.update(data) unless data is undefined
579
+ @redraw_dropdown()
559
580
  error: =>
560
581
  @error()
561
582
  @ajax.error?(arguments...)
@@ -565,6 +586,40 @@ class Chosen
565
586
 
566
587
  return @
567
588
 
589
+ pull_next_page: (cb) ->
590
+ return @ unless @ajax and @ajax.has_more isnt false
591
+
592
+ if @ajax.pending_next_page_request and @ajax.pending_next_page_request.readyState isnt 4
593
+ return @
594
+
595
+ @ajax.data ||= {}
596
+ @ajax.data.page = if @ajax.data.page then @ajax.data.page + 1 else 2
597
+
598
+ data =
599
+ query: @$container.$search.val()
600
+
601
+ @ajax.pending_next_page_request = $.ajax
602
+ url: @ajax.url
603
+ type: @ajax.type or "get"
604
+ dataType: @ajax.dataType or "json"
605
+ data: $.extend(data, @ajax.data or {})
606
+ async: @ajax.async or true
607
+ xhrFields: @ajax.xhrFields
608
+
609
+ beforeSend: (xhr) =>
610
+ @loading()
611
+ success: (data) =>
612
+ @loaded()
613
+ @ajax.has_more = !!data.length
614
+ @parser.append(data) unless data is undefined
615
+ @redraw_dropdown()
616
+ cb() if cb
617
+ error: =>
618
+ @ajax.data.page = if @ajax.data.page is 2 then null else @ajax.data.page - 1
619
+ @error()
620
+
621
+ return @
622
+
568
623
  @is_ios: ->
569
624
  /iPad|iPhone|iPod/.test(navigator.userAgent) and not window.MSStream
570
625
 
@@ -17,6 +17,42 @@ class Chosen.Parser
17
17
  delete @chosen
18
18
  return
19
19
 
20
+ # Replaces all select options and parses it again
21
+ update: (data) ->
22
+ parser = @chosen.option_parser || @default_parser
23
+ selected_options = []
24
+
25
+ for option in @all_options
26
+ if option.selected or option.blank
27
+ selected_options.push(option)
28
+ else
29
+ option.$option.remove()
30
+
31
+ for attrs in data
32
+ parsed = parser(attrs)
33
+ value = parsed.value.toString()
34
+ text = parsed.text.toString()
35
+
36
+ unless $.grep(selected_options, (o) -> o.value is value and o.label is text ).length
37
+ @chosen.$target.append($("<option />", parsed))
38
+
39
+ @parse(selected_options)
40
+ return @
41
+
42
+ # Appends new options to the end of the list
43
+ append: (data) ->
44
+ parser = @chosen.option_parser || @default_parser
45
+
46
+ for attrs in data
47
+ parsed = parser(attrs)
48
+ value = parsed.value.toString()
49
+ text = parsed.text.toString()
50
+
51
+ unless $.grep(@all_options, (o) -> o.value is value and o.label is text).length
52
+ @insert(attrs, true)
53
+
54
+ return @
55
+
20
56
  parse: (selected_options = []) ->
21
57
  formatter = @chosen.option_formatter || @default_formatter
22
58
  current_group_label = null
@@ -42,90 +78,71 @@ class Chosen.Parser
42
78
  classes += " disabled" if option.disabled
43
79
 
44
80
  $option = $(option)
45
- selected = $.grep(selected_options, (o) => o.value is option.value and o.label is option.label)[0]
81
+ selected = $.grep(selected_options, (o) -> o.value is option.value and o.label is option.label)[0]
46
82
  text = formatter($option)
47
83
 
48
- option_obj =
49
- $group: group
50
- $listed: (selected and selected.$listed) or $("<li />", class: classes, html: text[0])
51
- $choice: (selected and selected.$choice) or $("<li />", class: classes, html: [$reset_link.clone(), text[1]])
52
- $option: (selected and selected.$option) or $option
53
- blank: option.value is "" and index is 0
54
- index: index
55
- score: index * -1
56
- label: option.text
57
- value: option.value
58
- selected: option.selected
59
- disabled: option.disabled
84
+ chosen_option =
85
+ $group: group
86
+ $listed: (selected and selected.$listed) or $("<li />", class: classes, html: text[0])
87
+ $choice: (selected and selected.$choice) or $("<li />", class: classes, html: [$reset_link.clone(), text[1]])
88
+ $option: (selected and selected.$option) or $option
89
+ blank: option.value is "" and index is 0
90
+ index: index
91
+ score: index * -1
92
+ label: option.text
93
+ value: option.value
94
+ selected: option.selected
95
+ disabled: option.disabled
60
96
  match_type: null
61
97
 
62
- @all_options.push option_obj
98
+ @all_options.push chosen_option
63
99
 
64
- unless option_obj.blank
65
- @selected_options.push option_obj if option.selected
66
- @selectable_options.push option_obj unless option.selected
100
+ unless chosen_option.blank
101
+ @selected_options.push chosen_option if option.selected
102
+ @selectable_options.push chosen_option unless option.selected
67
103
 
68
104
  @order()
69
105
  return @
70
106
 
71
- sync: ->
72
- for option in @all_options
73
- if option.$option[0].selected and @selected_options.indexOf(option) < 0
74
- @chosen.select(option, false)
75
- else if not option.$option[0].selected and @selected_options.indexOf(option) >= 0
76
- @chosen.deselect(option, false)
77
-
78
- update: (data) ->
79
- parser = @chosen.option_parser || @default_parser
80
- selected_options = []
81
-
82
- for option in @all_options
83
- if option.selected or option.blank
84
- selected_options.push(option)
85
- else
86
- option.$option.remove()
87
-
88
- for attrs in data
89
- parsed = parser(attrs)
90
- unless $.grep(selected_options, (o) => o.value is parsed.value.toString() and o.label == parsed.text.toString() ).length
91
- @chosen.$target.append($("<option />", parsed))
92
-
93
- @parse(selected_options)
94
- return @
107
+ insert: (option, append = false) ->
108
+ $option = $("<option />", value: option.value, text: option.label)
109
+ $reset_link = @build_reset_link()
95
110
 
96
- insert: (data) ->
97
111
  formatter = @chosen.option_formatter || @default_formatter
98
- classes = "chosen-option"
99
- $option = $("<option />", value: data.value, text: data.label)
100
- $reset_link = @build_reset_link()
101
- text = formatter($option)
112
+ text = formatter($option)
113
+ classes = "chosen-option"
102
114
 
103
115
  @chosen.$target.append($option)
104
116
 
105
- option_obj =
106
- $group: null
107
- $listed: $("<li />", class: classes, html: text[0])
108
- $choice: $("<li />", class: classes, html: [$reset_link, text[1]])
109
- $option: $option
110
- blank: false
111
- index: 0
112
- score: 0
113
- label: $option[0].text
114
- value: $option[0].value
115
- selected: false
116
- disabled: false
117
- match_type: 0
118
-
119
- @all_options.unshift option_obj
120
- @available_options.unshift option_obj
121
- @selectable_options.unshift option_obj
122
-
123
- option_obj
117
+ chosen_option =
118
+ $group: null
119
+ $listed: $("<li />", class: classes, html: text[0])
120
+ $choice: $("<li />", class: classes, html: [$reset_link, text[1]])
121
+ $option: $option
122
+ blank: false
123
+ index: 0
124
+ score: 0
125
+ label: $option[0].text
126
+ value: $option[0].value
127
+ selected: false
128
+ disabled: false
129
+ match_type: if append then null else 0
130
+
131
+ if append
132
+ @all_options.push chosen_option
133
+ @available_options.push chosen_option
134
+ @selectable_options.push chosen_option
135
+ else
136
+ @all_options.unshift chosen_option
137
+ @available_options.unshift chosen_option
138
+ @selectable_options.unshift chosen_option
139
+
140
+ chosen_option
124
141
 
125
142
  restore: (option) ->
126
143
  @select(option)
127
144
 
128
- unless $.grep(@all_options, (o) => option.label is o.label).length
145
+ unless $.grep(@all_options, (o) -> option.label is o.label).length
129
146
  @chosen.$target.append(option.$option)
130
147
 
131
148
  @all_options.unshift option
@@ -145,6 +162,13 @@ class Chosen.Parser
145
162
  index = collection.indexOf(option)
146
163
  collection.splice(index, 1) if index > -1
147
164
 
165
+ sync: ->
166
+ for option in @all_options
167
+ if option.$option[0].selected and @selected_options.indexOf(option) < 0
168
+ @chosen.select(option, false)
169
+ else if not option.$option[0].selected and @selected_options.indexOf(option) >= 0
170
+ @chosen.deselect(option, false)
171
+
148
172
  to_html: ->
149
173
  last_group = null
150
174
  list = []
@@ -237,8 +261,10 @@ class Chosen.Parser
237
261
 
238
262
  expressions_collection = $.map(query, (word, index) ->
239
263
  return [[
240
- new RegExp("^#{word}$", "i"), new RegExp("^#{word}", "i")
241
- new RegExp("#{word}$", "i"), new RegExp(word, "i")
264
+ new RegExp("^#{word}$", "i"),
265
+ new RegExp("^#{word}", "i")
266
+ new RegExp("#{word}$", "i"),
267
+ new RegExp(word, "i")
242
268
  ]]
243
269
  )
244
270
 
@@ -283,8 +309,8 @@ class Chosen.Parser
283
309
 
284
310
  default_parser: (attrs) ->
285
311
  value: attrs.value
286
- text: attrs.label
287
- data: { source: attrs }
312
+ text: attrs.label
313
+ data: { source: attrs }
288
314
 
289
315
  default_formatter: ($option) ->
290
316
  value = $option.contents().text()
@@ -298,5 +324,4 @@ class Chosen.Parser
298
324
  tabindex: @chosen.$target[0].tabindex || "0"
299
325
  )
300
326
 
301
-
302
327
  @escape_exp: /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chosen-awesome-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - heaven