effective_form_inputs 0.7.5 → 0.7.6

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: dfe1f299c36eb92e01218a5cfa7fd64cbef52398
4
- data.tar.gz: 0ace3f60cc9f9790719fb07c3be576f4d0f82cf7
3
+ metadata.gz: 956155606d7d1da40fcca46e7424a36477b3829b
4
+ data.tar.gz: 6b6ecf49a907ba2803120cd4624b560f17706e3f
5
5
  SHA512:
6
- metadata.gz: a94bfe9d38b9bf0ba806c2bd6088fbac47df703bbbc0c3702da11485c81fdef311fa32dea86cf5b86b0bb454533218d4dd95769ce3e357e5e320c54fd2b6b36e
7
- data.tar.gz: 27b5de98a8a592ad3efafa7c6f68704b5926e5c0b523baf197dfd5c8585ed83fa9fc3ae0cc8ac4fc09510eae2ac3738aa5a7cb27091545764ab7e94945dec483
6
+ metadata.gz: fec09f2fffabc7b3d827ab26d72ad5f3b81b772f5e815ac0671b5fe094a6ba3e1e45f86774f96cf221fedd8fad0c7431e9f49066a6610ffa73e0509aa884b3d2
7
+ data.tar.gz: dc8e1de8a5b8391d89f07b66b16db57e7f645eed9d59a2f5eafda11a44e2beea2418b8fbb2f1e5e51a2ce3c2790f20b5f3a96ffc690a0028832fb72683dc2061
data/README.md CHANGED
@@ -329,10 +329,44 @@ To hide the search box entirely:
329
329
  :minimumResultsForSearch => 'Infinity'
330
330
  ```
331
331
 
332
- For a full list of options, please refer to:
332
+ For a full list of options, please refer to: https://select2.github.io/options.html
333
+
334
+
335
+ The following `:input_js => options` are not part of the standard select2 API, and are custom `effective_select` functionality only:
336
+
337
+ To add a css class to the select2 container or dropdown:
338
+
339
+ ```ruby
340
+ :containerClass => 'custom-container-class'
341
+ :dropdownClass => 'custom-dropdown-class'
342
+ ```
343
+
344
+ ### Working with dynamic options
333
345
 
334
- https://select2.github.io/options.html
346
+ The following information applies to `effective_select` only, and is not part of the standard select2 API.
335
347
 
348
+ To totally hide (instead of just grey out) any disabled options from the select2 dropdown, initialize the input with:
349
+
350
+ ```ruby
351
+ = f.input :category, :as => :effective_select, :collection => ..., :hide_disabled => true
352
+ ```
353
+
354
+ If you want to dynamically add/remove options from the select field after page load, you must use the `select2:reinitialize` event:
355
+
356
+ ```coffeescript
357
+ # When something on my page changes
358
+ $(document).on 'change', '.something', (event) ->
359
+ $select = $(event.target).closest('form').find('select.i-want-to-change') # Find the select2 input to be updated
360
+
361
+ # Go through its options, and modify some of them.
362
+ # Using the above 'hide_disabled true' functionality, the following code hides the options from being displayed,
363
+ # but you could actually remove the options, add new ones, or update the values/texts. whatever.
364
+ $select.find('option').each (index, option) ->
365
+ $(option).prop('disabled', true) if index > 10
366
+
367
+ # Whenever the underlying options change, you need to manually trigger the following event:
368
+ $select.select2().trigger('select2:reinitialize')
369
+ ```
336
370
 
337
371
  ### AJAX Support
338
372
 
@@ -5,7 +5,13 @@ initialize = ->
5
5
  element = $(element)
6
6
  options = element.data('input-js-options') || {}
7
7
 
8
- element.addClass('initialized').select2(options)
8
+ $select = element.addClass('initialized').select2(options)
9
+
10
+ # effective_select custom class functionality
11
+ # select2 doesn't provide an initializer to add css classes to its input, so we manually support this feature
12
+ # js_options[:containerClass] and js_options[:dropdownClass]
13
+ $select.data('select2').$container.addClass(options['containerClass']) if options['containerClass']
14
+ $select.data('select2').$dropdown.addClass(options['dropdownClass']) if options['dropdownClass']
9
15
 
10
16
  $ -> initialize()
11
17
  $(document).on 'page:change', -> initialize()
@@ -4,8 +4,41 @@
4
4
  $(document).on 'select2:unselecting', (event) -> $(event.target).data('state', 'unselected')
5
5
 
6
6
  $(document).on 'select2:open', (event) ->
7
- obj = $(event.target)
7
+ $select = $(event.target)
8
+
9
+ if $select.data('state') == 'unselected'
10
+ $select.removeData('state')
11
+ setTimeout ( => $select.select2('close') ), 0
12
+
13
+
14
+ # effective_select custom reinitialization functionality
15
+ # This is a custom event intended to be manually triggered when the underlying options change
16
+ # You can use this to dynamically disable options (with or without the effective_select hide_disabled: true)
17
+ # https://github.com/select2/select2/issues/2830
18
+
19
+ # To trigger this event,
20
+ # $(document).on 'change', "select[name$='[something_id]']", (event) ->
21
+ # ...add/remove/disable this select field's options...
22
+ # $(event.target).select2().trigger('select2:reinitialize')
23
+
24
+ $(document).on 'select2:reinitialize', (event) ->
25
+ $select = $(event.target)
26
+
27
+ # Get the existing options and value
28
+ options = $select.data('select2').options.options['inputJsOptions'] || {}
29
+ value = $select.find('option:selected').val()
30
+
31
+ # Clear/Restore the value appropriately
32
+ if value && $select.find("option:enabled[value='#{value}']").length > 0
33
+ $select.val(value)
34
+ else
35
+ $select.val('')
36
+
37
+ # Reinitialize select2
38
+ $select.select2('destroy').select2(options)
39
+
40
+ # Restore effective_select custom class functionality
41
+ $select.data('select2').$container.addClass(options['containerClass']) if options['containerClass']
42
+ $select.data('select2').$dropdown.addClass(options['dropdownClass']) if options['dropdownClass']
43
+
8
44
 
9
- if obj.data('state') == 'unselected'
10
- obj.removeData('state')
11
- setTimeout ( => obj.select2('close') ), 0
@@ -20,3 +20,8 @@
20
20
  .select2-container--focus > .selection > .select2-selection {
21
21
  border: 1px solid #9cd5fe !important;
22
22
  }
23
+
24
+ // effective_select option "hide_disabled: true"
25
+ .select2-container.hide-disabled .select2-results__option[aria-disabled=true] {
26
+ display: none;
27
+ }
@@ -8,7 +8,7 @@ module Inputs
8
8
  end
9
9
 
10
10
  def default_input_js
11
- {theme: 'bootstrap', minimumResultsForSearch: 6, tokenSeparators: [',', ' '], width: 'style', placeholder: 'Please choose'}
11
+ { theme: 'bootstrap', minimumResultsForSearch: 6, tokenSeparators: [',', ' '], width: 'style', placeholder: 'Please choose' }
12
12
  end
13
13
 
14
14
  def default_input_html
@@ -131,14 +131,27 @@ module Inputs
131
131
  html_options[:multiple] = options[:multiple]
132
132
  html_options[:class] << 'polymorphic' if options[:polymorphic]
133
133
  html_options[:class] << 'grouped' if options[:grouped]
134
+ html_options[:class] << 'hide-disabled' if options[:hide_disabled]
134
135
  end
135
136
  end
136
137
 
137
138
  def js_options
138
- super().tap do |js_options|
139
+ @effective_select_js_options ||= super().tap do |js_options|
139
140
  js_options[:allowClear] = (options[:multiple] != true)
140
141
  js_options[:tags] = (options[:tags] == true)
141
142
  js_options[:tokenSeparators] = nil if options[:tags] != true
143
+
144
+ # select2 doesn't support adding css classes to its input, so we support it through the
145
+ # js_options[:containerClass] and js_options[:dropdownClass] methods
146
+ # When we use options[:hide_disabled], we add the 'hide-disabled' class to both the container and the dropdown
147
+ if options[:hide_disabled]
148
+ js_options[:containerClass] = (arrayize_html_class_key(js_options[:containerClass]) + ['hide-disabled']).join(' ')
149
+ end
150
+
151
+ if options[:hide_disabled]
152
+ js_options[:dropdownClass] = (arrayize_html_class_key(js_options[:dropdownClass]) + ['hide-disabled']).join(' ')
153
+ end
154
+
142
155
  end
143
156
  end
144
157
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveFormInputs
2
- VERSION = '0.7.5'.freeze
2
+ VERSION = '0.7.6'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_form_inputs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
4
+ version: 0.7.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-17 00:00:00.000000000 Z
11
+ date: 2015-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails