jquery_narrows 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +35 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/app/assets/javascripts/jquery.narrows.js +279 -0
- data/app/assets/javascripts/jquery.narrows.min.js +13 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jquery_narrows.gemspec +39 -0
- data/lib/jquery_narrows.rb +13 -0
- data/lib/jquery_narrows/engine.rb +6 -0
- data/lib/jquery_narrows/simple_form.rb +35 -0
- data/lib/jquery_narrows/version.rb +3 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4ee877696a5e565fe13e1b7ca2ac81888e8764ca861b77fcdf647326789f5a3f
|
4
|
+
data.tar.gz: 8bb9efba266cf7e6310a3d14a8927a3ca6249f832fafd72bdaba0c234585ab0a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 91af0421e4218bb389c6c63cbff85fafe5d5dd9eb1b7da4735e2c345f84c060bf4e538023fecc75e87d43af3dfc6cf67440563fa9f3d9537dc5eb099ef55ed6a
|
7
|
+
data.tar.gz: 71f0126794937c03428ec21256824ff183ccf2a5fcc6575cb2a92408a66abace752183282204c6f42f4da746c119a803048acf42d749896686cf2f146f5778a6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at t_katou@kk-unlimited.co.jp. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
jquery_narrows (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.7.0)
|
12
|
+
rspec-core (~> 3.7.0)
|
13
|
+
rspec-expectations (~> 3.7.0)
|
14
|
+
rspec-mocks (~> 3.7.0)
|
15
|
+
rspec-core (3.7.1)
|
16
|
+
rspec-support (~> 3.7.0)
|
17
|
+
rspec-expectations (3.7.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.7.0)
|
20
|
+
rspec-mocks (3.7.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.7.0)
|
23
|
+
rspec-support (3.7.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
jquery_narrows!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.16.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 katatema
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 taku katou
|
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,41 @@
|
|
1
|
+
# jquery_narrows
|
2
|
+
|
3
|
+
jquery.narrows(https://github.com/monmonmon/jquery.narrows) for rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'jquery_narrows'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install jquery_narrows
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/katatema/jquery_narrows. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
38
|
+
|
39
|
+
## Code of Conduct
|
40
|
+
|
41
|
+
Everyone interacting in the JqueryNarrows project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/katatema/jquery_narrows/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,279 @@
|
|
1
|
+
/*!
|
2
|
+
* jQuery narrowing plugin
|
3
|
+
* Version 0.0.2
|
4
|
+
* @requires jQuery v1.7.0 or later
|
5
|
+
*
|
6
|
+
* Copyright 2013 Simon Yamada 山田史門
|
7
|
+
* Dual licensed under the MIT and GPL licenses:
|
8
|
+
* http://www.opensource.org/licenses/mit-license.php
|
9
|
+
* http://www.gnu.org/licenses/gpl.html
|
10
|
+
*
|
11
|
+
* Date: 2013-09-11
|
12
|
+
*/
|
13
|
+
(function ($) {
|
14
|
+
var methods = {
|
15
|
+
__relations: {}, // 親子関係を保持する配列
|
16
|
+
__options: {}, // 子selectごとにoptionを全て保持
|
17
|
+
__ukeys: {}, // selectに与えるユニークキーのリストを保持
|
18
|
+
__ukey_name: 'jquery-narrows-unique-key', // select要素に付与するユニークキーの属性名
|
19
|
+
__default_params: {
|
20
|
+
disable_if_parent_is_null: true,
|
21
|
+
null_value: '',
|
22
|
+
allow_multiple_parent_values: false,
|
23
|
+
multiple_parent_values_separator: ' *, *',
|
24
|
+
},
|
25
|
+
// 初期化
|
26
|
+
init: function (selector, params) {
|
27
|
+
var $parents = this;
|
28
|
+
var $children = $(selector);
|
29
|
+
// 引数チェック
|
30
|
+
methods.validate_selects($parents, $children);
|
31
|
+
// 設定のデフォルト値をオーバーライド
|
32
|
+
var __params = {};
|
33
|
+
for (var key in methods.__default_params) {
|
34
|
+
__params[key] = params && params[key] !== void 0 ? params[key] : methods.__default_params[key];
|
35
|
+
}
|
36
|
+
if (__params.allow_multiple_parent_values) {
|
37
|
+
__params.regexp_separator = new RegExp(__params.multiple_parent_values_separator);
|
38
|
+
}
|
39
|
+
params = __params;
|
40
|
+
// この親子関係を登録
|
41
|
+
methods.register_new_relations($parents, $children, params);
|
42
|
+
// 子selectはとりあえず全てdisable(直後に適宜enable)
|
43
|
+
if (params.disable_if_parent_is_null) {
|
44
|
+
$children.attr('disabled', 'disabled');
|
45
|
+
}
|
46
|
+
// 親selectのイベントハンドラを設定
|
47
|
+
$parents.on('change', methods.onchange_handler);
|
48
|
+
// 絞り込みを初期化
|
49
|
+
var relations = methods.get_relations_of($parents);
|
50
|
+
for (var relation_i = 0, len = relations.length; relation_i < len; relation_i++) {
|
51
|
+
var relation = relations[relation_i];
|
52
|
+
if (relation.initialized) {
|
53
|
+
// 初期化は1回だけ
|
54
|
+
continue;
|
55
|
+
}
|
56
|
+
relation.initialized = true;
|
57
|
+
methods.manage_this_relation(relation);
|
58
|
+
}
|
59
|
+
return $parents;
|
60
|
+
},
|
61
|
+
// 親子それぞれの select, option が正しく記述されているかをチェック
|
62
|
+
validate_selects: function ($parents, $children) {
|
63
|
+
// 親子select共通のチェック
|
64
|
+
var arr = [$parents, $children];
|
65
|
+
for (var arr_i = 0, len = arr.length; arr_i < len; arr_i++) {
|
66
|
+
var $selects = arr[arr_i];
|
67
|
+
if (0 === $selects.size()) {
|
68
|
+
$.error('selector '+$selects.selector+': element not found');
|
69
|
+
}
|
70
|
+
$selects.each(function () {
|
71
|
+
if ('SELECT' != $(this).prop('tagName')) {
|
72
|
+
$.error('selector '+$selects.selector+': includes non-select element(s)');
|
73
|
+
}
|
74
|
+
if (!$(this).attr('id')) {
|
75
|
+
$.error('id is required for all select elements');
|
76
|
+
}
|
77
|
+
});
|
78
|
+
}
|
79
|
+
// 親子関係を data-x 属性で正しく記述しているかチェック
|
80
|
+
var parent_ids = $parents.map(function () { return $(this).attr('id'); });
|
81
|
+
$children.each(function () {
|
82
|
+
for (var p_i = 0, len = parent_ids.length; p_i < len; p_i++) {
|
83
|
+
var attr = 'data-' + parent_ids[p_i];
|
84
|
+
var $select = $(this);
|
85
|
+
$select.find('option').each(function () {
|
86
|
+
// value="" の option 以外は data-<parent_id> 属性必須
|
87
|
+
var $option = $(this);
|
88
|
+
if ($option.val() && !$option.attr(attr)) {
|
89
|
+
var id = $select.attr('id');
|
90
|
+
$.error('options of select "#'+id+'" require "'+attr+'" attribute');
|
91
|
+
}
|
92
|
+
});
|
93
|
+
}
|
94
|
+
});
|
95
|
+
},
|
96
|
+
// 親子関係を新規登録
|
97
|
+
register_new_relations: function ($parents, $children, params) {
|
98
|
+
// 親子それぞれの select にランダムなユニークキーを割り当てる
|
99
|
+
var parent_keys = [];
|
100
|
+
$parents.each(function () {
|
101
|
+
var parent_key = methods.unique_key($(this));
|
102
|
+
parent_keys.push(parent_key);
|
103
|
+
});
|
104
|
+
var child_keys = [];
|
105
|
+
$children.each(function () {
|
106
|
+
var child_key = methods.unique_key($(this));
|
107
|
+
child_keys.push(child_key);
|
108
|
+
// 子selectのoptionを配列で全部持つ
|
109
|
+
methods.__options[child_key] = $(this).find('option').get();
|
110
|
+
});
|
111
|
+
// 親selectのユニークキーをキーとして、親子関係を __relations に保持する
|
112
|
+
for (var p_i = 0, len = parent_keys.length; p_i < len; p_i++) {
|
113
|
+
var parent_key = parent_keys[p_i];
|
114
|
+
if (!methods.__relations[parent_key])
|
115
|
+
methods.__relations[parent_key] = [];
|
116
|
+
methods.__relations[parent_key].push({
|
117
|
+
$parents:$parents,
|
118
|
+
$children:$children,
|
119
|
+
params:params,
|
120
|
+
initialized:false
|
121
|
+
});
|
122
|
+
}
|
123
|
+
},
|
124
|
+
// ユニークなキーを生成して $element に属性として与える
|
125
|
+
unique_key: function ($element) {
|
126
|
+
// 割り当て済みのunique keyがあればそれを返す
|
127
|
+
var ukey = $element.data(methods.__ukey_name);
|
128
|
+
if (ukey) {
|
129
|
+
return ukey;
|
130
|
+
}
|
131
|
+
// ユニークなキーを生成
|
132
|
+
do {
|
133
|
+
ukey = (Math.random() * 10000000 | 0).toString(16);
|
134
|
+
} while (methods.__ukeys[ukey] !== void 0);
|
135
|
+
$element.data(methods.__ukey_name, ukey);
|
136
|
+
// キャッシュ
|
137
|
+
methods.__ukeys[ukey] = 1;
|
138
|
+
return ukey;
|
139
|
+
},
|
140
|
+
// 親selectのchangeイベントハンドラ
|
141
|
+
onchange_handler: function (e) {
|
142
|
+
var relations = methods.get_relations_of($(this));
|
143
|
+
for (var relation_i = 0, len = relations.length; relation_i < len; relation_i++) {
|
144
|
+
var relation = relations[relation_i];
|
145
|
+
methods.manage_this_relation(relation);
|
146
|
+
}
|
147
|
+
},
|
148
|
+
// 親selectで選択された値に基づいて子selectを絞り込む
|
149
|
+
manage_this_relation: function (relation) {
|
150
|
+
// 全ての親selectで選択したvalueを調べる
|
151
|
+
var parent_selected_values = {};
|
152
|
+
var all_parents_selected = true;
|
153
|
+
relation.$parents.each(function () {
|
154
|
+
var value = $(this).val();
|
155
|
+
if (value == relation.params.null_value) {
|
156
|
+
all_parents_selected = false;
|
157
|
+
return false;
|
158
|
+
}
|
159
|
+
parent_selected_values[$(this).attr('id')] = value;
|
160
|
+
});
|
161
|
+
if (all_parents_selected) {
|
162
|
+
// この親子関係の全ての親selectで value="" 以外の値が選択された。
|
163
|
+
// 対応するoptionを子selectに追加
|
164
|
+
relation.$children.each(function () {
|
165
|
+
methods.enable_relevant_options($(this), relation, parent_selected_values);
|
166
|
+
});
|
167
|
+
} else {
|
168
|
+
// この親子関係の親selectのどれかで value="" が選択された。
|
169
|
+
if (relation.params.disable_if_parent_is_null) {
|
170
|
+
// 子selectのoptionを全て削除してdisable
|
171
|
+
relation.$children.each(function () {
|
172
|
+
methods.disable_all_options($(this), relation);
|
173
|
+
});
|
174
|
+
} else {
|
175
|
+
// 子selectのoptionを全部表示
|
176
|
+
relation.$children.each(function () {
|
177
|
+
methods.enable_all_options($(this), relation);
|
178
|
+
});
|
179
|
+
}
|
180
|
+
}
|
181
|
+
// 子孫selectへイベントを伝播
|
182
|
+
relation.$children.trigger('change');
|
183
|
+
},
|
184
|
+
// 親selectで選択された値に対応するoptionを有効にする
|
185
|
+
enable_relevant_options: function ($select, relation, parent_selected_values) {
|
186
|
+
var previously_selected_value = $select.val();
|
187
|
+
$select
|
188
|
+
// disabled解除
|
189
|
+
.removeAttr('disabled')
|
190
|
+
// option要素をDOMから一旦べしっと削除
|
191
|
+
.find('option[value!=""]').remove();
|
192
|
+
// 全てのoptionのうち、親selectの選択結果に適合するもののみ追加
|
193
|
+
var all_options = methods.get_all_options_of($select);
|
194
|
+
for (var option_i = 0, len = all_options.length; option_i < len; option_i++) {
|
195
|
+
var $option = $(all_options[option_i]);
|
196
|
+
if (!$option.val()) {
|
197
|
+
continue;
|
198
|
+
}
|
199
|
+
// このoptionが全ての親selectの選択結果にマッチするか確認
|
200
|
+
var relevant_option = true;
|
201
|
+
for (var parent_id in parent_selected_values) {
|
202
|
+
var parent_selected_value = parent_selected_values[parent_id];
|
203
|
+
var relevant_value = $option.data(parent_id);
|
204
|
+
if (relation.params.allow_multiple_parent_values) {
|
205
|
+
// セパレータ区切りで複数の親にマッチ
|
206
|
+
var relevant_values = relevant_value.toString().split(relation.params.regexp_separator);
|
207
|
+
var matched_any = false;
|
208
|
+
for (var r_i = 0, r_len = relevant_values.length; r_i < r_len; r_i++) {
|
209
|
+
if (relevant_values[r_i] == parent_selected_value) {
|
210
|
+
matched_any = true; break;
|
211
|
+
}
|
212
|
+
}
|
213
|
+
if (!matched_any) {
|
214
|
+
relevant_option = false; break;
|
215
|
+
}
|
216
|
+
} else {
|
217
|
+
// 単一の親にマッチ
|
218
|
+
if (relevant_value != parent_selected_value) {
|
219
|
+
relevant_option = false; break;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
}
|
223
|
+
if (relevant_option) {
|
224
|
+
// このoptionを子selectに追加
|
225
|
+
// さっきまで選択されてたoptionであればselectedに
|
226
|
+
if ($option.val() == previously_selected_value) {
|
227
|
+
$option.prop('selected', true);
|
228
|
+
}
|
229
|
+
$select.append($option);
|
230
|
+
}
|
231
|
+
}
|
232
|
+
},
|
233
|
+
// 全てのoptionを有効にする
|
234
|
+
enable_all_options: function ($select, relation) {
|
235
|
+
var previously_selected_child_value = $select.val();
|
236
|
+
$select
|
237
|
+
// disabled解除
|
238
|
+
.removeAttr('disabled')
|
239
|
+
// option要素をDOMから一旦べしっと削除
|
240
|
+
.find('option[value!=""]').remove();
|
241
|
+
// 全てのoptionを追加
|
242
|
+
var all_options = methods.get_all_options_of($select);
|
243
|
+
for (var option_i = 0, len = all_options.length; option_i < len; option_i++) {
|
244
|
+
var $option = $(all_options[option_i]);
|
245
|
+
// さっきまで選択されてたoptionであればselectedに
|
246
|
+
if ($option.val() == previously_selected_child_value) {
|
247
|
+
$option.prop('selected', true);
|
248
|
+
}
|
249
|
+
$select.append($option);
|
250
|
+
}
|
251
|
+
},
|
252
|
+
// 全てのoptionを無効にする
|
253
|
+
disable_all_options: function ($select, relation) {
|
254
|
+
$select
|
255
|
+
.attr('disabled', 'disabled').val(relation.params.null_value)
|
256
|
+
.find('option[value!=""]').remove();
|
257
|
+
},
|
258
|
+
// $select を親とする全ての親子関係を取得する
|
259
|
+
get_relations_of: function ($select) {
|
260
|
+
var ukey = $select.data(methods.__ukey_name);
|
261
|
+
return methods.__relations[ukey];
|
262
|
+
},
|
263
|
+
// $select の全ての option 要素を返す
|
264
|
+
get_all_options_of: function ($select) {
|
265
|
+
var ukey = $select.data(methods.__ukey_name);
|
266
|
+
return methods.__options[ukey];
|
267
|
+
}
|
268
|
+
};
|
269
|
+
$.fn.narrows = function (method) {
|
270
|
+
// Method calling logic
|
271
|
+
if (methods[method]) {
|
272
|
+
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
|
273
|
+
} else if (typeof method === 'object' || typeof method === 'string' || !method) {
|
274
|
+
return methods.init.apply(this, arguments);
|
275
|
+
} else {
|
276
|
+
$.error('Method ' + method + ' does not exist on jQuery.narrows');
|
277
|
+
}
|
278
|
+
};
|
279
|
+
})(jQuery);
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*!
|
2
|
+
* jQuery narrowing plugin
|
3
|
+
* Version 0.0.2
|
4
|
+
* @requires jQuery v1.7.0 or later
|
5
|
+
*
|
6
|
+
* Copyright 2013 Simon Yamada 山田史門
|
7
|
+
* Dual licensed under the MIT and GPL licenses:
|
8
|
+
* http://www.opensource.org/licenses/mit-license.php
|
9
|
+
* http://www.gnu.org/licenses/gpl.html
|
10
|
+
*
|
11
|
+
* Date: 2013-09-11
|
12
|
+
*/
|
13
|
+
!function(a){var b={__relations:{},__options:{},__ukeys:{},__ukey_name:"jquery-narrows-unique-key",__default_params:{disable_if_parent_is_null:!0,null_value:"",allow_multiple_parent_values:!1,multiple_parent_values_separator:" *, *"},init:function(c,d){var e=this,f=a(c);b.validate_selects(e,f);var g={};for(var h in b.__default_params)g[h]=d&&void 0!==d[h]?d[h]:b.__default_params[h];g.allow_multiple_parent_values&&(g.regexp_separator=new RegExp(g.multiple_parent_values_separator)),d=g,b.register_new_relations(e,f,d),d.disable_if_parent_is_null&&f.attr("disabled","disabled"),e.on("change",b.onchange_handler);for(var i=b.get_relations_of(e),j=0,k=i.length;k>j;j++){var l=i[j];l.initialized||(l.initialized=!0,b.manage_this_relation(l))}return e},validate_selects:function(b,c){for(var d=[b,c],e=0,f=d.length;f>e;e++){var g=d[e];0===g.size()&&a.error("selector "+g.selector+": element not found"),g.each(function(){"SELECT"!=a(this).prop("tagName")&&a.error("selector "+g.selector+": includes non-select element(s)"),a(this).attr("id")||a.error("id is required for all select elements")})}var h=b.map(function(){return a(this).attr("id")});c.each(function(){for(var b=0,c=h.length;c>b;b++){var d="data-"+h[b],e=a(this);e.find("option").each(function(){var b=a(this);if(b.val()&&!b.attr(d)){var c=e.attr("id");a.error('options of select "#'+c+'" require "'+d+'" attribute')}})}})},register_new_relations:function(c,d,e){var f=[];c.each(function(){var c=b.unique_key(a(this));f.push(c)});var g=[];d.each(function(){var c=b.unique_key(a(this));g.push(c),b.__options[c]=a(this).find("option").get()});for(var h=0,i=f.length;i>h;h++){var j=f[h];b.__relations[j]||(b.__relations[j]=[]),b.__relations[j].push({$parents:c,$children:d,params:e,initialized:!1})}},unique_key:function(a){var c=a.data(b.__ukey_name);if(c)return c;do c=(1e7*Math.random()|0).toString(16);while(void 0!==b.__ukeys[c]);return a.data(b.__ukey_name,c),b.__ukeys[c]=1,c},onchange_handler:function(c){for(var d=b.get_relations_of(a(this)),e=0,f=d.length;f>e;e++){var g=d[e];b.manage_this_relation(g)}},manage_this_relation:function(c){var d={},e=!0;c.$parents.each(function(){var b=a(this).val();return b==c.params.null_value?(e=!1,!1):void(d[a(this).attr("id")]=b)}),e?c.$children.each(function(){b.enable_relevant_options(a(this),c,d)}):c.params.disable_if_parent_is_null?c.$children.each(function(){b.disable_all_options(a(this),c)}):c.$children.each(function(){b.enable_all_options(a(this),c)}),c.$children.trigger("change")},enable_relevant_options:function(c,d,e){var f=c.val();c.removeAttr("disabled").find('option[value!=""]').remove();for(var g=b.get_all_options_of(c),h=0,i=g.length;i>h;h++){var j=a(g[h]);if(j.val()){var k=!0;for(var l in e){var m=e[l],n=j.data(l);if(d.params.allow_multiple_parent_values){for(var o=n.toString().split(d.params.regexp_separator),p=!1,q=0,r=o.length;r>q;q++)if(o[q]==m){p=!0;break}if(!p){k=!1;break}}else if(n!=m){k=!1;break}}k&&(j.val()==f&&j.prop("selected",!0),c.append(j))}}},enable_all_options:function(c,d){var e=c.val();c.removeAttr("disabled").find('option[value!=""]').remove();for(var f=b.get_all_options_of(c),g=0,h=f.length;h>g;g++){var i=a(f[g]);i.val()==e&&i.prop("selected",!0),c.append(i)}},disable_all_options:function(a,b){a.attr("disabled","disabled").val(b.params.null_value).find('option[value!=""]').remove()},get_relations_of:function(a){var c=a.data(b.__ukey_name);return b.__relations[c]},get_all_options_of:function(a){var c=a.data(b.__ukey_name);return b.__options[c]}};a.fn.narrows=function(c){return b[c]?b[c].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof c&&"string"!=typeof c&&c?void a.error("Method "+c+" does not exist on jQuery.narrows"):b.init.apply(this,arguments)}}(jQuery);
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jquery_narrows"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "jquery_narrows/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jquery_narrows"
|
8
|
+
spec.version = JqueryNarrows::VERSION
|
9
|
+
spec.authors = ["taku katou"]
|
10
|
+
spec.email = ["t_katou@kk-unlimited.co.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{Add assets of jquery_narrows to ruby on rails}
|
13
|
+
spec.description = %q{Add assets of jquery_narrows to ruby on rails}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
#if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
#end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'simple_form'
|
2
|
+
module JqueryNarrows
|
3
|
+
class NarrowsSelectInput < ::SimpleForm::Inputs::CollectionInput
|
4
|
+
def input(wrapper_options = nil)
|
5
|
+
label_method, value_method = detect_collection_methods
|
6
|
+
|
7
|
+
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
|
8
|
+
|
9
|
+
if merged_input_options[:id].blank?
|
10
|
+
merged_input_options[:id] = collection.first.class.to_s.underscore.gsub(/_/, "-")
|
11
|
+
end
|
12
|
+
|
13
|
+
parent = input_options[:parent]
|
14
|
+
if parent.present?
|
15
|
+
@builder.select(
|
16
|
+
attribute_name, collection.map{ |c|
|
17
|
+
[
|
18
|
+
c.send(label_method), c.send(value_method),
|
19
|
+
{data: {"#{parent.to_s}": "#{c.parent_id}"}}
|
20
|
+
]},
|
21
|
+
input_options, merged_input_options
|
22
|
+
)
|
23
|
+
else
|
24
|
+
@builder.collection_select(
|
25
|
+
attribute_name, collection, value_method, label_method,
|
26
|
+
input_options, merged_input_options
|
27
|
+
)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
SimpleForm::FormBuilder.class_eval do
|
33
|
+
map_type :narrows_select, :to => JqueryNarrows::NarrowsSelectInput
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jquery_narrows
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- taku katou
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-06-01 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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
|
+
description: Add assets of jquery_narrows to ruby on rails
|
56
|
+
email:
|
57
|
+
- t_katou@kk-unlimited.co.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- LICENSE
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- app/assets/javascripts/jquery.narrows.js
|
73
|
+
- app/assets/javascripts/jquery.narrows.min.js
|
74
|
+
- bin/console
|
75
|
+
- bin/setup
|
76
|
+
- jquery_narrows.gemspec
|
77
|
+
- lib/jquery_narrows.rb
|
78
|
+
- lib/jquery_narrows/engine.rb
|
79
|
+
- lib/jquery_narrows/simple_form.rb
|
80
|
+
- lib/jquery_narrows/version.rb
|
81
|
+
homepage: ''
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.7.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Add assets of jquery_narrows to ruby on rails
|
105
|
+
test_files: []
|