chosen-awesome-rails 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b30c32c3adbe56fdddb65b10d1e2f33faf899b13
4
- data.tar.gz: a3b0deb605bdbea00946c186e05f79a0bd5251f8
3
+ metadata.gz: 59a43784c3980859f42f15ccd3077693e28db11c
4
+ data.tar.gz: 5886a19651b3be6efe230a71a6a7599059ec2204
5
5
  SHA512:
6
- metadata.gz: 4cc52728accbc2ee66a1ac8c557e7f68eda5b937d88c4d6ebafdd18681ec6a1b4def66e6c29b7f74a9ae2686c7d28064144e960b1637b244a395debf180c17f6
7
- data.tar.gz: e89d44fa81544a6c89a013763b26cf9a634871d43d100609c03753de25a452ca3d12bfe58827fc7659cb2688f63f1b9dffae07cde6c310a1a8bdf58927e15799
6
+ metadata.gz: 01c686077aa10c1d2b88201464f040dc33f490c2b951561c04a91f4da558954a671ea9e00c1299e44c45c6f7eb551c4f90e2a2842a8356b01b42667d2f72b1d9
7
+ data.tar.gz: 5d6d5939e55161b78fc4751511bdbb478dd843f238296864044bfb9cdd35e8a5546f17c9788b929331cae9f810ff36c4d66c9a770fed16e5f237eaadc43f7938
@@ -1,42 +1,44 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: chosen-awesome-rails 0.0.1 ruby lib
2
+ # stub: chosen-awesome-rails 1.0.3 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
- s.name = "chosen-awesome-rails"
6
- s.version = "1.0.3"
5
+ s.name = "chosen-awesome-rails".freeze
6
+ s.version = "1.1.0"
7
7
 
8
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
- s.require_paths = ["lib"]
10
- s.authors = ["heaven"]
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib".freeze]
10
+ s.authors = ["heaven".freeze]
11
11
  s.date = "2015-03-24"
12
- s.description = "Chosen is a javascript library of select box enhancer for jQuery, integrates with Rails asset pipeline for ease of use."
13
- s.email = ["alex@codeart.pw"]
14
- s.files = [".gitignore", "Gemfile", "Gemfile.lock", "LICENSE.txt", "README.md", "Rakefile", "chosen-awesome-rails.gemspec", "lib/chosen-awesome-rails.rb", "lib/chosen-awesome-rails/engine.rb", "lib/chosen-awesome-rails/engine3.rb", "lib/chosen-awesome-rails/version.rb", "vendor/assets/images/chosen-arrow.gif", "vendor/assets/javascripts/chosen.js.coffee", "vendor/assets/javascripts/chosen/chosen.js.coffee", "vendor/assets/javascripts/chosen/multiple.js.coffee", "vendor/assets/javascripts/chosen/parser.js.coffee", "vendor/assets/javascripts/chosen/single.js.coffee", "vendor/assets/stylesheets/chosen.css.scss", "vendor/assets/stylesheets/chosen/bootstrap2.css.scss", "vendor/assets/stylesheets/chosen/bootstrap.css.scss", "vendor/assets/stylesheets/chosen/default.css.scss"]
15
- s.homepage = "https://github.com/heaven/chosen-awesome-rails"
16
- s.rubygems_version = "2.2.1"
17
- s.summary = "Integrate Chosen javascript library with Rails asset pipeline"
12
+ s.description = "Chosen is a javascript library of select box enhancer for jQuery, integrates with Rails asset pipeline for ease of use.".freeze
13
+ s.email = ["alex@codeart.pw".freeze]
14
+ s.files = [".gitignore".freeze, "Gemfile".freeze, "Gemfile.lock".freeze, "LICENSE.txt".freeze, "README.md".freeze, "Rakefile".freeze, "chosen-awesome-rails.gemspec".freeze, "lib/chosen-awesome-rails.rb".freeze, "lib/chosen-awesome-rails/engine.rb".freeze, "lib/chosen-awesome-rails/engine3.rb".freeze, "lib/chosen-awesome-rails/version.rb".freeze, "vendor/assets/images/chosen-arrow.gif".freeze, "vendor/assets/javascripts/chosen.coffee".freeze, "vendor/assets/javascripts/chosen/chosen.coffee".freeze, "vendor/assets/javascripts/chosen/multiple.coffee".freeze, "vendor/assets/javascripts/chosen/parser.coffee".freeze, "vendor/assets/javascripts/chosen/single.coffee".freeze, "vendor/assets/stylesheets/chosen.scss".freeze, "vendor/assets/stylesheets/chosen/_bootstrap2.scss".freeze, "vendor/assets/stylesheets/chosen/_bootstrap3.scss".freeze, "vendor/assets/stylesheets/chosen/_default.scss".freeze]
15
+ s.homepage = "https://github.com/heaven/chosen-awesome-rails".freeze
16
+ s.rubygems_version = "2.6.10".freeze
17
+ s.summary = "Integrate Chosen javascript library with Rails asset pipeline".freeze
18
+
19
+ s.installed_by_version = "2.6.10" if s.respond_to? :installed_by_version
18
20
 
19
21
  if s.respond_to? :specification_version then
20
22
  s.specification_version = 3
21
23
 
22
24
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
- s.add_runtime_dependency(%q<railties>, [">= 3.0"])
24
- s.add_runtime_dependency(%q<coffee-rails>, [">= 3.2"])
25
- s.add_runtime_dependency(%q<sass-rails>, [">= 3.2"])
26
- s.add_development_dependency(%q<bundler>, [">= 1.0"])
27
- s.add_development_dependency(%q<rails>, [">= 3.0"])
25
+ s.add_runtime_dependency(%q<railties>.freeze, [">= 3.0"])
26
+ s.add_runtime_dependency(%q<coffee-rails>.freeze, [">= 3.2"])
27
+ s.add_runtime_dependency(%q<sass-rails>.freeze, [">= 3.2"])
28
+ s.add_development_dependency(%q<bundler>.freeze, [">= 1.0"])
29
+ s.add_development_dependency(%q<rails>.freeze, [">= 3.0"])
28
30
  else
29
- s.add_dependency(%q<railties>, [">= 3.0"])
30
- s.add_dependency(%q<coffee-rails>, [">= 3.2"])
31
- s.add_dependency(%q<sass-rails>, [">= 3.2"])
32
- s.add_dependency(%q<bundler>, [">= 1.0"])
33
- s.add_dependency(%q<rails>, [">= 3.0"])
31
+ s.add_dependency(%q<railties>.freeze, [">= 3.0"])
32
+ s.add_dependency(%q<coffee-rails>.freeze, [">= 3.2"])
33
+ s.add_dependency(%q<sass-rails>.freeze, [">= 3.2"])
34
+ s.add_dependency(%q<bundler>.freeze, [">= 1.0"])
35
+ s.add_dependency(%q<rails>.freeze, [">= 3.0"])
34
36
  end
35
37
  else
36
- s.add_dependency(%q<railties>, [">= 3.0"])
37
- s.add_dependency(%q<coffee-rails>, [">= 3.2"])
38
- s.add_dependency(%q<sass-rails>, [">= 3.2"])
39
- s.add_dependency(%q<bundler>, [">= 1.0"])
40
- s.add_dependency(%q<rails>, [">= 3.0"])
38
+ s.add_dependency(%q<railties>.freeze, [">= 3.0"])
39
+ s.add_dependency(%q<coffee-rails>.freeze, [">= 3.2"])
40
+ s.add_dependency(%q<sass-rails>.freeze, [">= 3.2"])
41
+ s.add_dependency(%q<bundler>.freeze, [">= 1.0"])
42
+ s.add_dependency(%q<rails>.freeze, [">= 3.0"])
41
43
  end
42
44
  end
@@ -6,7 +6,7 @@ end
6
6
  require 'chosen-awesome-rails/version'
7
7
 
8
8
  case ::Rails.version.to_s
9
- when /^4/
9
+ when /^4|5/
10
10
  require 'chosen-awesome-rails/engine'
11
11
  when /^3\.[12]/
12
12
  require 'chosen-awesome-rails/engine3'
@@ -5,6 +5,7 @@ class Chosen
5
5
  $.extend(@, $.extend($.extend($.extend({}, Chosen.defaults), @constructor.defaults), options))
6
6
 
7
7
  @$body = $("body")
8
+ @$window = $(window)
8
9
  @target = @$target[0]
9
10
  @parser = new Chosen.Parser(@)
10
11
  @default_values = $.map @parser.selected_options, (option) -> option
@@ -18,15 +19,15 @@ class Chosen
18
19
 
19
20
  @build()
20
21
  @load()
22
+ @refresh()
21
23
 
22
24
  Chosen.pool.push(@)
23
25
 
24
- @set_state()
25
-
26
- @$target.addClass("chosen").after(@$container).bind("chosen:update", (evt) => @set_state())
26
+ @$target.before(@$container)
27
+ .on("change chosen:update", (evt, data) => @refresh(evt, data))
27
28
 
28
29
  destroy: ->
29
- @$target.unbind "chosen:update"
30
+ @$target.unbind "change chosen:update"
30
31
 
31
32
  @unbind_events()
32
33
 
@@ -36,6 +37,9 @@ class Chosen
36
37
  @$dropdown.remove()
37
38
  @$target.removeData("chosen").removeClass("chosen").show()
38
39
 
40
+ if @$container.$search[0].required
41
+ @target.required = true
42
+
39
43
  delete @$container
40
44
  delete @$dropdown
41
45
  delete @$target
@@ -48,22 +52,6 @@ class Chosen
48
52
  index = Chosen.pool.indexOf(@)
49
53
  Chosen.pool.splice(index, 1) if index >= 0
50
54
 
51
- set_state: ->
52
- if @target.disabled
53
- @disabled = false
54
- @disable()
55
- else
56
- @disabled = true
57
- @enable()
58
-
59
- reset: ->
60
- @deselect_all()
61
-
62
- $.each @default_values, (index, option) =>
63
- @parser.restore(option)
64
-
65
- @load()
66
-
67
55
  build: ->
68
56
  select_classes = ["chosen-container"]
69
57
 
@@ -73,7 +61,9 @@ class Chosen
73
61
 
74
62
  container_props =
75
63
  class: select_classes.join ' '
76
- css: if @width? then { width: @width } else @getCSSProperties(@target, ["width", "min-width", "max-width"])
64
+ css: if @width then { width: @width } else @getCSSProperties(@target, ["width", "min-width", "max-width"])
65
+
66
+ @$target.addClass("chosen")
77
67
 
78
68
  attrs = @getCSSProperties(@target, ["height"])
79
69
 
@@ -82,13 +72,22 @@ class Chosen
82
72
 
83
73
  container_props.title = @target.title if @target.title.length
84
74
  container_props.id = @target.id.replace(/[^\w]/g, '-') + "-chosen" if @target.id
85
- placeholder = @$target.attr("placeholder") || @placeholder
75
+
76
+ input_attrs =
77
+ autocomplete: "off"
78
+ tabindex: @target.tabindex || "0"
79
+ placeholder: @$target.attr("placeholder") || @placeholder
80
+
81
+ if @target.required
82
+ input_attrs.required = @target.required and not @parser.selected().length
83
+ input_attrs["data-required"] = @target.required
84
+ @target.required = false
86
85
 
87
86
  @$container = $("<div />", container_props)
88
- @$container.html "<ul class=\"chosen-choices\"><li class=\"chosen-search-field\"><input type=\"text\" placeholder=\"#{placeholder}\" autocomplete=\"off\" /></li></ul>"
87
+ @$container.html "<ul class=\"chosen-choices\"><li class=\"chosen-search-field\"><input type=\"text\" /></li></ul>"
89
88
  @$container.$choices = @$container.find("ul.chosen-choices")
90
89
  @$container.$search_container = @$container.$choices.find("li.chosen-search-field")
91
- @$container.$search = @$container.$choices.find("input[type=text]").attr(tabindex: @target.tabindex || "0")
90
+ @$container.$search = @$container.$choices.find("input[type=text]").attr(input_attrs)
92
91
 
93
92
  dropdown_classes = ["chosen-dropdown"]
94
93
 
@@ -106,17 +105,25 @@ class Chosen
106
105
  @$dropdown.$list.suggestion = null
107
106
 
108
107
  getCSSProperties: (node, properties)->
108
+ attrs = {}
109
+ return attrs if Chosen.is_crappy_browser(8)
110
+
109
111
  sheets = document.styleSheets
110
112
  node.matches = node.matches or node.webkitMatchesSelector or node.mozMatchesSelector or node.msMatchesSelector or node.oMatchesSelector
111
113
  matches = []
112
- attrs = {}
113
114
 
114
115
  for i of sheets
115
- rules = sheets[i].rules or sheets[i].cssRules
116
+ try
117
+ rules = sheets[i].rules or sheets[i].cssRules
118
+ catch
119
+ continue
116
120
 
117
121
  for r of rules
118
- if node.matches(rules[r].selectorText)
119
- matches.push rules[r].cssText
122
+ try
123
+ if node.matches(rules[r].selectorText)
124
+ matches.push rules[r].cssText
125
+ catch
126
+ continue
120
127
 
121
128
  for p in properties
122
129
  for m in matches
@@ -144,10 +151,12 @@ class Chosen
144
151
  @open()
145
152
  evt.stopImmediatePropagation()
146
153
 
147
- @$container.$search.bind "keydown", (evt) => @keydown(evt)
148
- @$container.$search.bind "keyup", (evt) => @keyup(evt)
149
- @$container.$search.bind "focus", (evt) => @focus(evt)
150
- @$container.$search.bind "blur", (evt) => @blur(evt)
154
+ @$container.$search.bind "keydown", $.proxy(@keydown, @)
155
+ @$container.$search.bind "keyup", $.proxy(@keyup, @)
156
+ @$container.$search.bind "focus", $.proxy(@focus, @)
157
+ @$container.$search.bind "blur", $.proxy(@blur, @)
158
+
159
+ @$target.bind "change", $.proxy(@change, @)
151
160
 
152
161
  return true
153
162
 
@@ -155,12 +164,49 @@ class Chosen
155
164
  if @target.id.length
156
165
  $("label[for=#{@target.id}]").unbind "click"
157
166
 
167
+ @$target.unbind "change", @change
168
+
158
169
  @$dropdown.unbind()
159
170
  @$container.$search.unbind()
160
171
  @$container.unbind()
161
172
 
162
173
  return true
163
174
 
175
+ reset: ->
176
+ @deselect_all()
177
+
178
+ $.each @default_values, (index, option) =>
179
+ @parser.restore(option)
180
+
181
+ @load()
182
+
183
+ refresh: (evt, data) ->
184
+ # Event triggered inside chosen
185
+ if typeof data is "object" and data.chosen
186
+ return true
187
+
188
+ if @target.disabled
189
+ @disabled = false
190
+ @disable()
191
+ else
192
+ @disabled = true
193
+ @enable()
194
+
195
+ change: (evt, data) ->
196
+ # Event triggered outside of chosen, sync with external changes
197
+ if not data or not data.chosen
198
+ @parser.sync()
199
+
200
+ if @parser.selected().length
201
+ @$container.$search.removeAttr("required")
202
+ else if @$container.$search.data("required")
203
+ @$container.$search.attr("required", "required")
204
+
205
+ required: (value) ->
206
+ @$container.$search.attr
207
+ "required": value
208
+ "data-required": value
209
+
164
210
  activate: ->
165
211
  @$container.$search.trigger("focus")
166
212
  return @
@@ -347,16 +393,43 @@ class Chosen
347
393
  update_dropdown_position: ->
348
394
  return unless @opened
349
395
 
396
+ rect = @$container[0].getBoundingClientRect()
397
+ rect.width ||= rect.right - rect.left
398
+ rect.height ||= rect.bottom - rect.top
399
+
350
400
  offsets = @$container.offset()
351
- height = @$container.innerHeight()
352
- width = @$container.innerWidth()
401
+ offsets.bottom = @$body.height() - (offsets.top + rect.height)
353
402
 
354
- @$dropdown.css
355
- left: "#{offsets.left}px",
356
- top: "#{offsets.top + height}px"
357
- width: "#{width}px"
403
+ border_width = parseInt(@$container.css("border-bottom-width"))
404
+ upside = false
358
405
 
359
- return
406
+ viewport_top = rect.top;
407
+ viewport_bottom = @$window.height() - rect.bottom;
408
+ viewport_height = viewport_bottom
409
+
410
+ if viewport_bottom < 250 and viewport_top > viewport_bottom
411
+ viewport_height = viewport_top
412
+ upside = true
413
+
414
+ options =
415
+ top: "initial"
416
+ bottom: "initial"
417
+ left: "#{offsets.left}px"
418
+ width: "#{rect.width}px"
419
+ maxHeight: "#{(if viewport_height > 300 then 300 else viewport_height) + border_width}px"
420
+
421
+ if upside
422
+ @$dropdown.addClass("upside").removeClass("downside")
423
+ @$container.addClass("upside").removeClass("downside")
424
+
425
+ options.bottom = "#{offsets.bottom + rect.height - border_width}px"
426
+ else
427
+ @$dropdown.addClass("downside").removeClass("upside")
428
+ @$container.addClass("downside").removeClass("upside")
429
+
430
+ options.top = "#{offsets.top + rect.height - border_width}px"
431
+
432
+ @$dropdown.css(options)
360
433
 
361
434
  update_dropdown_content: ->
362
435
  @insert_suggestion()
@@ -412,7 +485,7 @@ class Chosen
412
485
  @move_selection_to(cursor)
413
486
 
414
487
  if @cursor_option and @cursor_option.selected and @parser.selectable_options.length
415
- # TODO: optimize this, could be slow on big lists
488
+ # TODO: optimize this, could be slow on large lists
416
489
  return @move_selection(dir)
417
490
 
418
491
  return @
@@ -451,7 +524,7 @@ class Chosen
451
524
  0
452
525
 
453
526
  pull_updates: ->
454
- return @ if not @ajax or not @$container.$search[0].value
527
+ return @ unless @ajax
455
528
 
456
529
  if @ajax.pending_update
457
530
  clearTimeout(@ajax.pending_update)
@@ -461,25 +534,28 @@ class Chosen
461
534
  @ajax.pending_request.abort()
462
535
 
463
536
  data =
464
- query: @$container.$search[0].value
537
+ query: @$container.$search.val()
465
538
 
466
539
  @ajax.pending_request = $.ajax
467
- url: @ajax.url
468
- type: @ajax.type or "get"
469
- dataType: @ajax.dataType or "json"
470
- data: $.extend(@ajax.data || {}, data)
471
- async: @ajax.async or true
540
+ url: @ajax.url
541
+ type: @ajax.type or "get"
542
+ dataType: @ajax.dataType or "json"
543
+ data: $.extend(data, @ajax.data or {})
544
+ async: @ajax.async or true
472
545
  xhrFields: @ajax.xhrFields
473
546
 
474
547
  beforeSend: (xhr) =>
475
548
  @loading()
476
- @ajax.beforeSend(xhr) if typeof @ajax.beforeSend is "function"
549
+ @ajax.beforeSend?(arguments...)
477
550
  success: (data) =>
478
551
  @loaded()
552
+ @ajax.success?(arguments...)
479
553
  @redraw_dropdown(data)
480
554
  error: =>
481
555
  @error()
482
-
556
+ @ajax.error?(arguments...)
557
+ complete: =>
558
+ @ajax.complete?(arguments...)
483
559
  , 300
484
560
 
485
561
  return @
@@ -491,15 +567,15 @@ class Chosen
491
567
  not Chosen.is_crappy_browser(6)
492
568
 
493
569
  @defaults:
494
- allow_insertion: false
495
- inherit_classes: true
496
- is_rtl: false
497
- option_parser: null
570
+ allow_insertion: false
571
+ inherit_classes: true
572
+ is_rtl: false
573
+ option_parser: null
498
574
  option_formatter: null
499
575
  locale:
500
- no_results: "No results found"
576
+ no_results: "No results found"
501
577
  start_typing: "Please start typing"
502
- add_new: "add new"
578
+ add_new: "add new"
503
579
 
504
580
  @pool: []
505
581
 
@@ -6,12 +6,28 @@ class Chosen.Multiple extends Chosen
6
6
 
7
7
  @pending_option = null
8
8
 
9
+ bind_events: ->
10
+ super
11
+
12
+ for option in @parser.selected()
13
+ @bind_option_events(option)
14
+
15
+ unbind_events: ->
16
+ super
17
+
18
+ for option in @parser.selected()
19
+ @unbind_option_events(option)
20
+
9
21
  bind_option_events: (option) ->
10
22
  option.$choice.find("a").unbind().
11
23
  bind("mousedown", -> false).
12
24
  bind("click", (evt) => @deselect_choice(evt))
13
25
  return
14
26
 
27
+ unbind_option_events: (option) ->
28
+ option.$choice.find("a").unbind()
29
+ return
30
+
15
31
  set_default_value: ->
16
32
  @$container.$search[0].value = ""
17
33
  return
@@ -89,16 +105,16 @@ class Chosen.Multiple extends Chosen
89
105
 
90
106
  return @
91
107
 
92
- deselect: (option) ->
108
+ deselect: (option, trigger_event = true) ->
93
109
  @parser.deselect(option)
94
110
 
95
111
  option.$choice.remove()
96
112
  @update_dropdown_position()
97
113
 
98
- @$target.trigger("change")
114
+ @$target.trigger("change", [{ chosen: true }]) if trigger_event
99
115
  return @
100
116
 
101
- select: (option) ->
117
+ select: (option, trigger_event = true) ->
102
118
  return @ if not option or option.disabled or option.selected
103
119
 
104
120
  @parser.select(option)
@@ -109,7 +125,7 @@ class Chosen.Multiple extends Chosen
109
125
  @bind_option_events(option)
110
126
  @close()
111
127
 
112
- @$target.trigger("change")
128
+ @$target.trigger("change", [{ chosen: true }]) if trigger_event
113
129
  return @
114
130
 
115
131
  deselect_all: ->
@@ -126,7 +142,6 @@ class Chosen.Multiple extends Chosen
126
142
 
127
143
  load: ->
128
144
  for option in @parser.selected()
129
- @bind_option_events(option)
130
145
  @$container.$search_container.before(option.$choice)
131
146
 
132
147
  return @