selectize-ajax 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +95 -0
- data/LICENSE.txt +21 -0
- data/README.md +157 -0
- data/Rakefile +6 -0
- data/app/assets/javascripts/selectize-ajax.coffee +133 -0
- data/app/assets/stylesheets/selectize-ajax.css +31 -0
- data/app/views/selectize_ajax/_tag.html.erb +53 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +4 -0
- data/config/locales/ru.yml +4 -0
- data/lib/selectize/ajax.rb +29 -0
- data/lib/selectize/ajax/configuration.rb +6 -0
- data/lib/selectize/ajax/core/control.rb +91 -0
- data/lib/selectize/ajax/core/script.rb +47 -0
- data/lib/selectize/ajax/core/settings.rb +40 -0
- data/lib/selectize/ajax/engine.rb +6 -0
- data/lib/selectize/ajax/railtie.rb +11 -0
- data/lib/selectize/ajax/version.rb +5 -0
- data/lib/selectize/ajax/view_helpers.rb +9 -0
- data/selectize-ajax.gemspec +29 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b8bf67e05fd5e3b2961919f5b8e411f587b32883
|
4
|
+
data.tar.gz: da4fe12d22a3f424c696faab0f314b75e7e278dc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 02d4a9b7f4561394b5b90567fac7bd8501ac24b1c4e111558e621e48583f0109591a769b4fabcdba81ffffd715d6f309e187275d1c8dc06f6870906d5a20f668
|
7
|
+
data.tar.gz: dc6fc895cee33cac0ddb8af4021778ddaf424255abcedeadcfe8074e3856baf79ab81200c973e5db6538eaff1991656c5ae93bb53c88ee4ee056384500772c23
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
selectize-ajax (0.1.4)
|
5
|
+
coffee-rails (~> 4.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (5.1.3)
|
11
|
+
actionview (= 5.1.3)
|
12
|
+
activesupport (= 5.1.3)
|
13
|
+
rack (~> 2.0)
|
14
|
+
rack-test (~> 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
17
|
+
actionview (5.1.3)
|
18
|
+
activesupport (= 5.1.3)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
23
|
+
activesupport (5.1.3)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (~> 0.7)
|
26
|
+
minitest (~> 5.1)
|
27
|
+
tzinfo (~> 1.1)
|
28
|
+
builder (3.2.3)
|
29
|
+
coffee-rails (4.2.2)
|
30
|
+
coffee-script (>= 2.2.0)
|
31
|
+
railties (>= 4.0.0)
|
32
|
+
coffee-script (2.4.1)
|
33
|
+
coffee-script-source
|
34
|
+
execjs
|
35
|
+
coffee-script-source (1.12.2)
|
36
|
+
concurrent-ruby (1.0.5)
|
37
|
+
diff-lcs (1.3)
|
38
|
+
erubi (1.6.1)
|
39
|
+
execjs (2.7.0)
|
40
|
+
i18n (0.8.6)
|
41
|
+
loofah (2.0.3)
|
42
|
+
nokogiri (>= 1.5.9)
|
43
|
+
method_source (0.8.2)
|
44
|
+
mini_portile2 (2.2.0)
|
45
|
+
minitest (5.10.3)
|
46
|
+
nokogiri (1.8.0)
|
47
|
+
mini_portile2 (~> 2.2.0)
|
48
|
+
rack (2.0.3)
|
49
|
+
rack-test (0.6.3)
|
50
|
+
rack (>= 1.0)
|
51
|
+
rails-dom-testing (2.0.3)
|
52
|
+
activesupport (>= 4.2.0)
|
53
|
+
nokogiri (>= 1.6)
|
54
|
+
rails-html-sanitizer (1.0.3)
|
55
|
+
loofah (~> 2.0)
|
56
|
+
railties (5.1.3)
|
57
|
+
actionpack (= 5.1.3)
|
58
|
+
activesupport (= 5.1.3)
|
59
|
+
method_source
|
60
|
+
rake (>= 0.8.7)
|
61
|
+
thor (>= 0.18.1, < 2.0)
|
62
|
+
rake (10.4.2)
|
63
|
+
rspec (3.6.0)
|
64
|
+
rspec-core (~> 3.6.0)
|
65
|
+
rspec-expectations (~> 3.6.0)
|
66
|
+
rspec-mocks (~> 3.6.0)
|
67
|
+
rspec-core (3.6.0)
|
68
|
+
rspec-support (~> 3.6.0)
|
69
|
+
rspec-expectations (3.6.0)
|
70
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
71
|
+
rspec-support (~> 3.6.0)
|
72
|
+
rspec-mocks (3.6.0)
|
73
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
+
rspec-support (~> 3.6.0)
|
75
|
+
rspec-support (3.6.0)
|
76
|
+
selectize-rails (0.12.4)
|
77
|
+
thor (0.20.0)
|
78
|
+
thread_safe (0.3.6)
|
79
|
+
tzinfo (1.2.3)
|
80
|
+
thread_safe (~> 0.1)
|
81
|
+
|
82
|
+
PLATFORMS
|
83
|
+
ruby
|
84
|
+
|
85
|
+
DEPENDENCIES
|
86
|
+
bundler (~> 1.13)
|
87
|
+
coffee-rails (~> 4.2)
|
88
|
+
railties
|
89
|
+
rake (~> 10.0)
|
90
|
+
rspec (~> 3.0)
|
91
|
+
selectize-ajax!
|
92
|
+
selectize-rails
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
1.13.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ryabov Ruslan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
# Selectize::Ajax
|
2
|
+
|
3
|
+
Useful [Selectize.js](https://selectize.github.io/selectize.js/) form control tag with autocomplete, create and edit items by ajax.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'selectize-ajax'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install selectize-ajax
|
20
|
+
|
21
|
+
In your application.js, include the following:
|
22
|
+
|
23
|
+
//= require selectize-ajax
|
24
|
+
|
25
|
+
In your application.css, include the following:
|
26
|
+
|
27
|
+
*= require selectize-ajax
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
For example you want create dropdown control for choosing post category
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
selectize_ajax_tag f.object, :category_id, collection: Category.collection
|
35
|
+
```
|
36
|
+
|
37
|
+
This code generate simple selectize dropdown.
|
38
|
+
The collection should be the following:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
[
|
42
|
+
...
|
43
|
+
{ value: <id>, label: <title> },
|
44
|
+
...
|
45
|
+
]
|
46
|
+
```
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
def self.collection
|
50
|
+
Category.map do |category|
|
51
|
+
{ label: category.title, value: category.id }
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
### Autocomplete
|
56
|
+
|
57
|
+
For use ajax autocomplete you must add path for search:
|
58
|
+
```ruby
|
59
|
+
selectize_ajax_tag f.object, :category_id, collection_path: categories_autocomplete_path
|
60
|
+
```
|
61
|
+
|
62
|
+
By default search param is `q`, if you want use other param you need set `search_param` for control.
|
63
|
+
|
64
|
+
### Add new item
|
65
|
+
|
66
|
+
You can add new item from modal window. For this you need:
|
67
|
+
|
68
|
+
1. Add path and modal target to selectize control
|
69
|
+
2. Create modal and action on controller
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
<%= selectize_ajax_tag f.object, :category_id,
|
73
|
+
collection: Category.collection,
|
74
|
+
add_path: new_category_path,
|
75
|
+
add_modal: '#new-category-modal'
|
76
|
+
%>
|
77
|
+
```
|
78
|
+
|
79
|
+
Bootstrap modal window
|
80
|
+
```haml
|
81
|
+
...
|
82
|
+
.modal-header
|
83
|
+
%h4.modal-title
|
84
|
+
Create new category
|
85
|
+
|
86
|
+
.modal-body
|
87
|
+
= simple_form_for(@category_form, url: categories_path,
|
88
|
+
data: { target: '#new-category-modal' }, remote: true) do |f|
|
89
|
+
...
|
90
|
+
```
|
91
|
+
|
92
|
+
Controller action after success create new record should return json:
|
93
|
+
```ruby
|
94
|
+
def create
|
95
|
+
...
|
96
|
+
render json: { label: record.title, value: record.id }
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
After that, the modal will close and the new record will be selected on dropdown.
|
101
|
+
|
102
|
+
### Edit selected item
|
103
|
+
|
104
|
+
For edit selected item you should add new modal and edit action path.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
<%= selectize_ajax_tag f.object, :category_id,
|
108
|
+
collection: Category.collection,
|
109
|
+
add_path: new_category_path,
|
110
|
+
add_modal: '#new-category-modal',
|
111
|
+
edit_path: edit_category_path(@category),
|
112
|
+
edit_modal: '#edit-category-modal'
|
113
|
+
%>
|
114
|
+
```
|
115
|
+
|
116
|
+
**WARNING**: if you want use `edit_path` and do not have record id for generate link path you need use following templates:
|
117
|
+
|
118
|
+
- Replace ID to string `{{id}}` - `edit_category_path(id: '{{id}}')`
|
119
|
+
- Or use `edit_category_path(id: f.object.category_id || '{{id}}')`
|
120
|
+
- Or write hardcoded path without rails hepler `'/category/{{id}}/edit'` **(not recomended)**
|
121
|
+
|
122
|
+
Script automaticly will be replace `{{id}}` param to selected value.
|
123
|
+
|
124
|
+
|
125
|
+
## All options
|
126
|
+
|
127
|
+
Parameter | Values | Default
|
128
|
+
--------------------|:------------------|:----------------
|
129
|
+
`label` | string | From object
|
130
|
+
`value` | mixed | From object
|
131
|
+
`placeholder` | string | --
|
132
|
+
`wrap_class` | string \| false | --
|
133
|
+
`wrap_class_only` | true \| false | false
|
134
|
+
`label_class` | string | --
|
135
|
+
`input_html[class]` | string | --
|
136
|
+
`required` | true \| false | From object
|
137
|
+
`collection` | array | []
|
138
|
+
`add_modal` | string | --
|
139
|
+
`add_path` | string | --
|
140
|
+
`add_button_text` | string | I18n.t('selectize_ajax.add_button_text')
|
141
|
+
`add_button_class` | string | --
|
142
|
+
`edit_path` | string | --
|
143
|
+
`edit_modal` | string | --
|
144
|
+
`edit_button_text` | string | I18n.t('selectize_ajax.edit_button_text')
|
145
|
+
`edit_button_class` | string | --
|
146
|
+
`horizontal` | true \| false | true
|
147
|
+
`collection_path` | string | --
|
148
|
+
`search_param` | string | `q`
|
149
|
+
|
150
|
+
## Contributing
|
151
|
+
|
152
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/distroid/selectize-ajax.
|
153
|
+
|
154
|
+
|
155
|
+
## License
|
156
|
+
|
157
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
window.clearSelectizeModal = (modal) ->
|
2
|
+
modal.find('.modal-header').empty()
|
3
|
+
modal.find('.modal-body').empty()
|
4
|
+
modal.data 'bs.modal', null
|
5
|
+
|
6
|
+
window.SJCollection = {}
|
7
|
+
|
8
|
+
window.SelectizeAjax = class SelectizeAjax
|
9
|
+
constructor: (options = {}) ->
|
10
|
+
@options = options
|
11
|
+
@initialize()
|
12
|
+
|
13
|
+
initialize: () ->
|
14
|
+
return unless @options.resource_id?
|
15
|
+
|
16
|
+
@selectize_control()
|
17
|
+
@control_buttons()
|
18
|
+
|
19
|
+
selectize_control: () ->
|
20
|
+
$("##{@options.resource_id}").selectize
|
21
|
+
persist: false
|
22
|
+
maxItems: 1
|
23
|
+
valueField: 'value'
|
24
|
+
labelField: 'label'
|
25
|
+
sortField: 'label'
|
26
|
+
searchField: 'label'
|
27
|
+
options: @options.collection
|
28
|
+
render: item: (item, escape) ->
|
29
|
+
[
|
30
|
+
'<div>',
|
31
|
+
(if item.label then '<span>' + escape(item.label) + '</span>' else ''),
|
32
|
+
'</div>'
|
33
|
+
].join(' ')
|
34
|
+
|
35
|
+
load: (query, callback) ->
|
36
|
+
options = SJCollection[this.$input.attr('id')].options
|
37
|
+
return callback() if !query.length || !options.collection_path?
|
38
|
+
|
39
|
+
self = this
|
40
|
+
$.ajax
|
41
|
+
url: options.collection_path
|
42
|
+
type: 'GET'
|
43
|
+
dataType: 'json'
|
44
|
+
data: { "#{options.search_param}": query }
|
45
|
+
error: ->
|
46
|
+
callback()
|
47
|
+
success: (res) ->
|
48
|
+
self.clearOptions()
|
49
|
+
callback res
|
50
|
+
|
51
|
+
control_buttons: () ->
|
52
|
+
if typeof @options.add_modal != 'undefined'
|
53
|
+
@ajax_add_complete_script()
|
54
|
+
@clear_add_form_script()
|
55
|
+
|
56
|
+
if typeof @options.edit_modal != 'undefined'
|
57
|
+
@ajax_edit_complete_script()
|
58
|
+
@edit_button_script()
|
59
|
+
|
60
|
+
ajax_add_complete_script: () ->
|
61
|
+
$(@options.add_modal).data('resource_id', @options.resource_id)
|
62
|
+
$(@options.add_modal).on 'ajax:complete', (evt, data, status, errors) ->
|
63
|
+
return console.error 'Somthing went wrong, form submit return empty response.' unless data?
|
64
|
+
return if data.status != 200 && data.status != 201
|
65
|
+
|
66
|
+
if data.responseJSON == null || typeof data.responseJSON == 'undefined'
|
67
|
+
$(this).find('.modal-content').html data.responseText
|
68
|
+
$(this).trigger 'error'
|
69
|
+
else
|
70
|
+
provider = JSON.parse(data.responseText)
|
71
|
+
resource_id = $(this).data('resource_id')
|
72
|
+
|
73
|
+
if $("##{resource_id}").length > 0
|
74
|
+
selectizer = $("##{resource_id}")[0].selectize
|
75
|
+
selectizer.addOption provider
|
76
|
+
selectizer.addItem provider.value
|
77
|
+
|
78
|
+
$(this).modal('hide')
|
79
|
+
$(this).find('form')[0].reset() if $(this).find('form').length > 0
|
80
|
+
|
81
|
+
|
82
|
+
clear_add_form_script: () ->
|
83
|
+
$(@options.add_modal).on 'hidden.bs.modal, show.bs.modal', ->
|
84
|
+
$(this).find('form')[0].reset() if $(this).find('form').length > 0
|
85
|
+
|
86
|
+
$(this).find('.error').each ->
|
87
|
+
$(this).remove()
|
88
|
+
|
89
|
+
$(this).find('.field_with_errors').each ->
|
90
|
+
$(this).removeClass 'field_with_errors'
|
91
|
+
|
92
|
+
ajax_edit_complete_script: () ->
|
93
|
+
$(@options.edit_modal).data('resource_id', @options.resource_id)
|
94
|
+
$(document).on 'hidden.bs.modal', @options.edit_modal, ->
|
95
|
+
clearSelectizeModal $(this)
|
96
|
+
|
97
|
+
$(document).on 'ajax:complete', @options.edit_modal, (e, data) ->
|
98
|
+
if data.responseJSON == null || typeof data.responseJSON == 'undefined'
|
99
|
+
$(this).find('.modal-content').html data.responseText
|
100
|
+
$(this).trigger 'error'
|
101
|
+
else
|
102
|
+
data = data.responseJSON
|
103
|
+
$(this).modal 'hide'
|
104
|
+
if data.value != null and data.label != null
|
105
|
+
$('div[data-value=\'' + data.value + '\']').find('span').text data.label
|
106
|
+
$('div.selected[data-value=\'' + data.value + '\']').text data.label
|
107
|
+
|
108
|
+
edit_button_script: () ->
|
109
|
+
if !$("##{@options.resource_id}").val()
|
110
|
+
$(".edit-#{@options.resource_id}").hide()
|
111
|
+
$("##{@options.resource_id}")
|
112
|
+
.closest('.selectize-ajax-wrapper')
|
113
|
+
.addClass('selectize-ajax-wrapper--empty')
|
114
|
+
else
|
115
|
+
$(".edit-#{@options.resource_id}").show()
|
116
|
+
$(".edit-#{@options.resource_id}").attr(
|
117
|
+
'href',
|
118
|
+
@options.edit_resource_template.replace('{{id}}', $("##{@options.resource_id}").val())
|
119
|
+
)
|
120
|
+
|
121
|
+
$(document).on 'change', "##{@options.resource_id}", ->
|
122
|
+
resource_id = $(this).attr('id')
|
123
|
+
if !$(this).val()
|
124
|
+
$(this).closest('.selectize-ajax-wrapper').addClass('selectize-ajax-wrapper--empty')
|
125
|
+
return $(".edit-#{resource_id}").hide()
|
126
|
+
|
127
|
+
options = SJCollection[resource_id].options
|
128
|
+
$(".edit-#{resource_id}").show()
|
129
|
+
$(".edit-#{resource_id}").attr(
|
130
|
+
'href',
|
131
|
+
options.edit_resource_template.replace('{{id}}', $(this).val())
|
132
|
+
)
|
133
|
+
$(this).closest('.selectize-ajax-wrapper').removeClass('selectize-ajax-wrapper--empty')
|
@@ -0,0 +1,31 @@
|
|
1
|
+
.selectize-input-group {
|
2
|
+
position: relative;
|
3
|
+
display: table;
|
4
|
+
border-collapse: separate;
|
5
|
+
}
|
6
|
+
|
7
|
+
.selectize-ajax-wrapper.form-group {
|
8
|
+
flex: 1;
|
9
|
+
margin-left: 0;
|
10
|
+
margin-right: 0;
|
11
|
+
}
|
12
|
+
|
13
|
+
.selectize-ajax-wrapper {
|
14
|
+
margin-bottom: 15px;
|
15
|
+
margin-left: 10px;
|
16
|
+
align-items: flex-start;
|
17
|
+
}
|
18
|
+
|
19
|
+
.selectize-ajax-wrapper--empty {
|
20
|
+
margin-left: 0;
|
21
|
+
}
|
22
|
+
|
23
|
+
.selectize-ajax-wrapper--empty a[class*='add-'] {
|
24
|
+
border-bottom-right-radius: 3px !important;
|
25
|
+
border-top-right-radius: 3px !important;
|
26
|
+
}
|
27
|
+
|
28
|
+
.selectize-ajax-wrapper .selectize-input,
|
29
|
+
.selectize-ajax-wrapper .selectize-control .selectize-input.input-active {
|
30
|
+
display: block;
|
31
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
<div class="<%= ('has-error' if control.has_error?) %> form-group selectize-ajax-wrapper">
|
2
|
+
<% if control.label && control.options.horizontal %>
|
3
|
+
<%= label_tag "#{control.resource_name}[#{control.field}]",
|
4
|
+
class: "col-sm-3 control-label #{control.options.label_class}" do %>
|
5
|
+
<%= '*' if control.field_required? %>
|
6
|
+
<%= control.label %>
|
7
|
+
<% end %>
|
8
|
+
<% end %>
|
9
|
+
|
10
|
+
<div class="<%= control.wrap_classes %>">
|
11
|
+
<% if control.label && !control.options.horizontal %>
|
12
|
+
<%= label_tag "#{control.resource_name}[#{control.field}]", class: control.options.label_class do %>
|
13
|
+
<%= '*' if control.field_required? %>
|
14
|
+
<%= control.label %>
|
15
|
+
<% end %>
|
16
|
+
<% end %>
|
17
|
+
|
18
|
+
<%= text_field_tag "#{control.resource_name}[#{control.field}]", control.value, id: control.resource_id,
|
19
|
+
placeholder: control.options.placeholder, class: control.options.input_html[:class] %>
|
20
|
+
|
21
|
+
<% if control.can_add? || control.can_edit? %>
|
22
|
+
<span class="input-group-btn">
|
23
|
+
<% if control.can_add? %>
|
24
|
+
<%= link_to control.options.add_path,
|
25
|
+
class: "btn btn-primary btn-remote add-#{control.resource_id} #{control.options.add_button_class}",
|
26
|
+
data: { toggle: 'modal', target: control.options.add_modal } do %>
|
27
|
+
<span class="glyphicon glyphicon-plus"></span>
|
28
|
+
<%= control.options.add_button_text %>
|
29
|
+
<% end %>
|
30
|
+
<% end %>
|
31
|
+
|
32
|
+
<% if control.can_edit? %>
|
33
|
+
<%= link_to control.options.edit_path,
|
34
|
+
class: "btn btn-primary btn-remote edit-#{control.resource_id} #{control.options.edit_button_class}",
|
35
|
+
data: { toggle: 'modal', target: control.options.edit_modal },
|
36
|
+
style: ('display:none;' if control.value.blank?) do %>
|
37
|
+
<span class="glyphicon glyphicon-edit"></span>
|
38
|
+
<%= control.options.edit_button_text %>
|
39
|
+
<% end %>
|
40
|
+
<% end %>
|
41
|
+
</span>
|
42
|
+
<% end %>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<% if control.has_error? %>
|
46
|
+
<span class="<%= (control.options.horizontal ? 'col-sm-9 col-sm-offset-3' : 'col-sm-12') %> help-block error">
|
47
|
+
<%= control.resource_object.errors[control.field].first %>
|
48
|
+
</span>
|
49
|
+
<% end %>
|
50
|
+
|
51
|
+
<%= script.html_safe %>
|
52
|
+
</div>
|
53
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'selectize/ajax'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'selectize/ajax/version'
|
2
|
+
require 'selectize/ajax/configuration'
|
3
|
+
require 'selectize/ajax/view_helpers'
|
4
|
+
require 'selectize/ajax/core/control'
|
5
|
+
require 'selectize/ajax/core/script'
|
6
|
+
require 'selectize/ajax/core/settings'
|
7
|
+
|
8
|
+
if defined?(Rails)
|
9
|
+
require 'selectize/ajax/railtie'
|
10
|
+
require 'selectize/ajax/engine'
|
11
|
+
end
|
12
|
+
|
13
|
+
module Selectize
|
14
|
+
class << self
|
15
|
+
attr_accessor :configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= Ajax::Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset
|
23
|
+
@configuration = Ajax::Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.configure
|
27
|
+
yield(configuration)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module Selectize::Ajax::Core
|
2
|
+
class Control
|
3
|
+
attr_accessor :resource, :field, :options
|
4
|
+
|
5
|
+
def initialize(resource, field, options = {})
|
6
|
+
@options = Selectize::Ajax::Core::Settings.new(options).call
|
7
|
+
@resource = resource
|
8
|
+
@field = field
|
9
|
+
end
|
10
|
+
|
11
|
+
def resource_name
|
12
|
+
return resource.to_s.underscore.to_sym unless resource_object?
|
13
|
+
|
14
|
+
class_name = resource.class.name.split('::').last
|
15
|
+
return :form if class_name == 'Form'
|
16
|
+
class_name.chomp('Form').underscore.to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def resource_id
|
20
|
+
[resource_name, field].join('_')
|
21
|
+
end
|
22
|
+
|
23
|
+
def value
|
24
|
+
return options.value if options.value.present?
|
25
|
+
return resource.send(field) if resource_object? && resource.respond_to?(field)
|
26
|
+
end
|
27
|
+
|
28
|
+
def label
|
29
|
+
(options.label || field.to_s.titleize) if options.label != false
|
30
|
+
end
|
31
|
+
|
32
|
+
def edit_resource_template
|
33
|
+
return unless can_edit?
|
34
|
+
|
35
|
+
@edit_resource ||= options.edit_path if options.edit_path.index('{{id}}').blank?
|
36
|
+
@edit_resource ||= options.edit_path.split('/').reverse.map do |part|
|
37
|
+
break '{{id}}' unless part.to_i.zero?
|
38
|
+
part
|
39
|
+
end.reverse.join('/')
|
40
|
+
URI.unescape(@edit_resource)
|
41
|
+
end
|
42
|
+
|
43
|
+
def resource_object
|
44
|
+
@model_object ||= if resource_object?
|
45
|
+
resource
|
46
|
+
else
|
47
|
+
resource.to_s.classify.constantize rescue nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def field_required?
|
52
|
+
return false unless options.required
|
53
|
+
return false if resource_object.blank? || !resource_object.respond_to?(:_validators)
|
54
|
+
validators = resource_object._validators[field].map(&:class) rescue []
|
55
|
+
[
|
56
|
+
validators.include?(ActiveModel::Validations::PresenceValidator),
|
57
|
+
validators.include?(ActiveRecord::Validations::PresenceValidator)
|
58
|
+
].any?
|
59
|
+
rescue
|
60
|
+
false
|
61
|
+
end
|
62
|
+
|
63
|
+
def wrap_classes
|
64
|
+
return if options.wrap_class == false
|
65
|
+
return options.wrap_class if options.wrap_class_only
|
66
|
+
|
67
|
+
classes = [options.wrap_class]
|
68
|
+
classes << 'selectize-input-group' if can_add?
|
69
|
+
classes << (options.horizontal ? 'col-sm-9' : 'col-sm-12')
|
70
|
+
classes.compact.reject(&:blank?).join(' ')
|
71
|
+
end
|
72
|
+
|
73
|
+
def has_error?
|
74
|
+
@has_error ||= resource_object? ? resource_object&.errors&.include?(field) : false
|
75
|
+
end
|
76
|
+
|
77
|
+
def can_add?
|
78
|
+
options.add_path.present? && options.add_modal.present?
|
79
|
+
end
|
80
|
+
|
81
|
+
def can_edit?
|
82
|
+
options.edit_path.present? && options.edit_modal.present?
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def resource_object?
|
88
|
+
@_is_object ||= [resource.is_a?(String), resource.is_a?(Symbol)].none?
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Selectize::Ajax::Core
|
2
|
+
class Script
|
3
|
+
attr_accessor :control
|
4
|
+
|
5
|
+
def initialize(control)
|
6
|
+
@control = control
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
js_script_tag
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def js_script_tag
|
16
|
+
"<script type=\"text/javascript\" id=\"sj-#{control.resource_id}-script\">
|
17
|
+
$(function() {
|
18
|
+
setTimeout(function() {
|
19
|
+
obj = new window.SelectizeAjax(#{tag_options.to_json});
|
20
|
+
SJCollection['#{control.resource_id}'] = obj;
|
21
|
+
$('#sj-#{control.resource_id}-script').remove();
|
22
|
+
});
|
23
|
+
});
|
24
|
+
</script>"
|
25
|
+
end
|
26
|
+
|
27
|
+
def tag_options
|
28
|
+
options = {
|
29
|
+
resource_id: control.resource_id,
|
30
|
+
resource_name: control.resource_name
|
31
|
+
}
|
32
|
+
if control.options.collection.present?
|
33
|
+
options[:collection] = control.options.collection
|
34
|
+
end
|
35
|
+
if control.options.collection_path.present?
|
36
|
+
options[:search_param] = control.options.search_param.presence
|
37
|
+
options[:collection_path] = control.options.collection_path
|
38
|
+
end
|
39
|
+
if control.can_edit?
|
40
|
+
options[:edit_modal] = control.options.edit_modal
|
41
|
+
options[:edit_resource_template] = control.edit_resource_template
|
42
|
+
end
|
43
|
+
options[:add_modal] = control.options.add_modal if control.can_add?
|
44
|
+
options
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Selectize::Ajax::Core
|
2
|
+
class Settings
|
3
|
+
def initialize(options = {})
|
4
|
+
@options = options
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
::OpenStruct.new(default_options.merge(@options))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def default_options
|
14
|
+
{
|
15
|
+
collection: [],
|
16
|
+
required: true,
|
17
|
+
add_modal: nil,
|
18
|
+
add_path: nil,
|
19
|
+
add_button_text: I18n.t('selectize_ajax.add_button_text'),
|
20
|
+
edit_path: nil,
|
21
|
+
edit_modal: nil,
|
22
|
+
edit_button_text: I18n.t('selectize_ajax.edit_button_text'),
|
23
|
+
horizontal: true,
|
24
|
+
collection_path: nil,
|
25
|
+
search_param: 'q',
|
26
|
+
placeholder: nil,
|
27
|
+
wrap_class: '',
|
28
|
+
wrap_class_only: false,
|
29
|
+
label_class: '',
|
30
|
+
add_button_class: '',
|
31
|
+
edit_button_class: '',
|
32
|
+
input_html: {
|
33
|
+
class: ''
|
34
|
+
},
|
35
|
+
label: nil,
|
36
|
+
value: nil
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Selectize::Ajax
|
2
|
+
module ViewHelpers
|
3
|
+
def selectize_ajax_tag(resource, field, options = {})
|
4
|
+
control = Selectize::Ajax::Core::Control.new(resource, field, options)
|
5
|
+
script = Selectize::Ajax::Core::Script.new(control).call
|
6
|
+
render 'selectize_ajax/tag', control: control, script: script
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'selectize/ajax/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'selectize-ajax'
|
8
|
+
spec.version = Selectize::Ajax::VERSION
|
9
|
+
spec.authors = ['Ryabov Ruslan']
|
10
|
+
spec.email = ['diserve.it@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Useful selectize form control.}
|
13
|
+
spec.description = %q{Useful selectize form control with autocomplete, create and edit items by ajax.}
|
14
|
+
spec.homepage = 'https://github.com/distroid/selectize-ajax'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
|
28
|
+
spec.add_dependency('coffee-rails', '~> 4.2')
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: selectize-ajax
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.8
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryabov Ruslan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coffee-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.2'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.2'
|
69
|
+
description: Useful selectize form control with autocomplete, create and edit items
|
70
|
+
by ajax.
|
71
|
+
email:
|
72
|
+
- diserve.it@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- app/assets/javascripts/selectize-ajax.coffee
|
86
|
+
- app/assets/stylesheets/selectize-ajax.css
|
87
|
+
- app/views/selectize_ajax/_tag.html.erb
|
88
|
+
- bin/console
|
89
|
+
- bin/setup
|
90
|
+
- config/locales/en.yml
|
91
|
+
- config/locales/ru.yml
|
92
|
+
- lib/selectize/ajax.rb
|
93
|
+
- lib/selectize/ajax/configuration.rb
|
94
|
+
- lib/selectize/ajax/core/control.rb
|
95
|
+
- lib/selectize/ajax/core/script.rb
|
96
|
+
- lib/selectize/ajax/core/settings.rb
|
97
|
+
- lib/selectize/ajax/engine.rb
|
98
|
+
- lib/selectize/ajax/railtie.rb
|
99
|
+
- lib/selectize/ajax/version.rb
|
100
|
+
- lib/selectize/ajax/view_helpers.rb
|
101
|
+
- selectize-ajax.gemspec
|
102
|
+
homepage: https://github.com/distroid/selectize-ajax
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.6.8
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Useful selectize form control.
|
126
|
+
test_files: []
|