rails_depends_on 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 +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +1 -0
- data/app/assets/javascripts/dependsOn.js +601 -0
- data/app/assets/javascripts/rails_depends_on.js.coffee +19 -0
- data/lib/rails_depends_on.rb +6 -0
- data/lib/rails_depends_on/version.rb +3 -0
- data/rails_depends_on.gemspec +25 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13b8e843efedd99dfdaa7c8f1b5076afe95a817c
|
4
|
+
data.tar.gz: 2489e82ea49a004772280476deae0cdb943dbbf8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7ae7f86885b5285d55fa933c318b62785b12d70352ab3ee2f5c91fc56144d6f9dda55ef7daf91500bf8cadb33970dc9398cc59dec91ae64422f28f349d1209df
|
7
|
+
data.tar.gz: fb86d50b1ac18f26a2ca21981bebbf34d8bf607b82fbe1c7866aab46e344eebacc4f071aedcbf7cc95de0d188a78a8c174b4300a5251a4ae46a245ee95831e7b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Francesco Brunetti
|
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,61 @@
|
|
1
|
+
# RailsDependsOn
|
2
|
+
|
3
|
+
A simple rails engine to make it easier to use the great DependsOn jQuery Plugin:
|
4
|
+
|
5
|
+
http://dstreet.github.io/dependsOn/
|
6
|
+
|
7
|
+
Note: not all the features of the original plugin are implemented for now
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'rails_depends_on'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install rails_depends_on
|
25
|
+
|
26
|
+
Include the assets in your javascript manifest file:
|
27
|
+
$ //= require dependent-fields to your Javascript manifest file (usually found at app/assets/javascripts/application.js).
|
28
|
+
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```haml
|
33
|
+
= f.input 'a_checkbox'
|
34
|
+
.depends-on{"data-id" => 'a_checkbox', 'data-value' =>'true'}
|
35
|
+
= f.input 'another_field'
|
36
|
+
```
|
37
|
+
|
38
|
+
```haml
|
39
|
+
= f.input 'a_text_field'
|
40
|
+
.depends-on{"data-id" => 'a_text_field', 'data-value' =>[['1'],['2']]}
|
41
|
+
= f.input 'another_field'
|
42
|
+
```
|
43
|
+
|
44
|
+
## TODO
|
45
|
+
|
46
|
+
* Multiple dependencies
|
47
|
+
* *NOT* qualifier
|
48
|
+
* *Contains* qualifier
|
49
|
+
* *Regexp* qualifier
|
50
|
+
* *url* and *email* qualifier
|
51
|
+
* *Custom* Qualifiers
|
52
|
+
* implement all dependsOn options (callbacks etc..)
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/francescob/rails_depends_on.
|
57
|
+
|
58
|
+
|
59
|
+
## License
|
60
|
+
|
61
|
+
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 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,601 @@
|
|
1
|
+
/*!
|
2
|
+
* dependsOn v1.0.2
|
3
|
+
* a jQuery plugin to facilitate the handling of form field dependencies.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2015 David Street
|
6
|
+
* @license the MIT license.
|
7
|
+
*/
|
8
|
+
|
9
|
+
(function ($) {
|
10
|
+
|
11
|
+
/**
|
12
|
+
* Creates a new Dependency
|
13
|
+
*
|
14
|
+
* @class Dependency
|
15
|
+
* @param {String} selector The jQuery selector.
|
16
|
+
* @param {Object} qualifiers An object representing the qualifiers for the Dependency.
|
17
|
+
*/
|
18
|
+
var Dependency = function ( selector, qualifiers ) {
|
19
|
+
this.selector = selector;
|
20
|
+
this.$dependencyObj = $(selector);
|
21
|
+
this.qualifiers = qualifiers;
|
22
|
+
};
|
23
|
+
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Qualifier method which checks for the `disabled` attribute.
|
27
|
+
*
|
28
|
+
* @function enabled
|
29
|
+
* @param checkAgainst The value we are checking.
|
30
|
+
* @returns {Boolean}
|
31
|
+
*/
|
32
|
+
Dependency.prototype.enabled = function ( checkAgainst ) {
|
33
|
+
if ( $(this.selector + '[disabled]').length > 0 ) {
|
34
|
+
|
35
|
+
// Dependency is disabled and `enabled` qualifier is set to true
|
36
|
+
if ( checkAgainst ) {
|
37
|
+
return false;
|
38
|
+
}
|
39
|
+
} else {
|
40
|
+
|
41
|
+
// Dependency is not disabled and `enabled` qualifier is set to false
|
42
|
+
if ( !checkAgainst ) {
|
43
|
+
return false;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
return true;
|
48
|
+
};
|
49
|
+
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Qualifier method which checks for the `checked` attribute on checkboxes and radio buttons.
|
53
|
+
*
|
54
|
+
* @function checked
|
55
|
+
* @param checkAgainst The value we are checking.
|
56
|
+
* @returns {Boolean}
|
57
|
+
*/
|
58
|
+
Dependency.prototype.checked = function ( checkAgainst ) {
|
59
|
+
|
60
|
+
// Dependency must be a checkbox for this qualifier
|
61
|
+
if ( this.$dependencyObj.attr('type') === 'checkbox') {
|
62
|
+
|
63
|
+
// Checkbox is not checked and the `checked` qualifier is set to true
|
64
|
+
// or the checkbox is checked and the `checked` qualifier is set to false
|
65
|
+
if ( (!this.$dependencyObj.is(':checked') && checkAgainst) ||
|
66
|
+
(this.$dependencyObj.is(':checked') && !checkAgainst ) ) {
|
67
|
+
|
68
|
+
return false;
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
return true;
|
73
|
+
};
|
74
|
+
|
75
|
+
|
76
|
+
/**
|
77
|
+
* Qualifier method which checks the dependency input value against an array of
|
78
|
+
* whitelisted values.
|
79
|
+
*
|
80
|
+
* @function values
|
81
|
+
* @param checkAgainst The value we are checking.
|
82
|
+
* @returns {Boolean}
|
83
|
+
*/
|
84
|
+
Dependency.prototype.values = function ( checkAgainst ) {
|
85
|
+
var dependencyValue = this.$dependencyObj.val()
|
86
|
+
, length = checkAgainst.length
|
87
|
+
, i = 0
|
88
|
+
, match = false;
|
89
|
+
|
90
|
+
// If dependency is a radio group, then filter by `:checked`
|
91
|
+
if ( this.$dependencyObj.attr('type') === 'radio' ) {
|
92
|
+
dependencyValue = this.$dependencyObj.filter(':checked').val();
|
93
|
+
}
|
94
|
+
|
95
|
+
// Loop through list of accepted values. Break when we find a match.
|
96
|
+
for ( i; i < length; i += 1 ) {
|
97
|
+
if ( typeof(dependencyValue) === 'array' || typeof(dependencyValue) === 'object' ) {
|
98
|
+
|
99
|
+
// If `dependencyValue` is an array then check to see if arrays match
|
100
|
+
if ( $(this.$dependencyObj.val()).not($(checkAgainst[i])).length === 0 &&
|
101
|
+
$(checkAgainst[i]).not($(this.$dependencyObj.val())).length === 0 ) {
|
102
|
+
match = true;
|
103
|
+
break;
|
104
|
+
}
|
105
|
+
} else {
|
106
|
+
if ( checkAgainst[i] === dependencyValue ) {
|
107
|
+
match = true;
|
108
|
+
break;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
return match;
|
114
|
+
};
|
115
|
+
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Qualifier method which the dependency input value against an array of
|
119
|
+
* blacklisted values.
|
120
|
+
*
|
121
|
+
* @function not
|
122
|
+
* @param checkAgainst The value we are checking.
|
123
|
+
* @returns {Boolean}
|
124
|
+
*/
|
125
|
+
Dependency.prototype.not = function ( checkAgainst ) {
|
126
|
+
var dependencyValue = this.$dependencyObj.val()
|
127
|
+
, length = checkAgainst.length
|
128
|
+
, i = 0;
|
129
|
+
|
130
|
+
if ( this.$dependencyObj.attr('type') === 'radio' ) {
|
131
|
+
dependencyValue = this.$dependencyObj.filter(':checked').val();
|
132
|
+
}
|
133
|
+
// Loop through list of blacklisted values. Break when we find a match.
|
134
|
+
for ( i; i < length; i += 1 ) {
|
135
|
+
if ( checkAgainst[i] === dependencyValue ) {
|
136
|
+
return false;
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
return true;
|
141
|
+
};
|
142
|
+
|
143
|
+
|
144
|
+
/**
|
145
|
+
* Qualifier method which runs a RegEx pattern match on the dependency input value.
|
146
|
+
*
|
147
|
+
* @function match
|
148
|
+
* @param checkAgainst The value we are checking.
|
149
|
+
* @returns {Boolean}
|
150
|
+
*/
|
151
|
+
Dependency.prototype.match = function ( checkAgainst ) {
|
152
|
+
var dependencyValue = this.$dependencyObj.val()
|
153
|
+
, pattern = checkAgainst;
|
154
|
+
|
155
|
+
return pattern.test( dependencyValue );
|
156
|
+
};
|
157
|
+
|
158
|
+
|
159
|
+
/**
|
160
|
+
* Qualifier method which checks if a whitelisted value exists in an array of dependencyValues.
|
161
|
+
* Used for select inputs with the `multiple` attribute.
|
162
|
+
* If `dependencyValue` is not an array or object, then method will fallback
|
163
|
+
* to the `values` method.
|
164
|
+
*
|
165
|
+
* @function contains
|
166
|
+
* @param checkAgainst The value we are checking.
|
167
|
+
* @returns {Boolean}
|
168
|
+
*/
|
169
|
+
Dependency.prototype.contains = function ( checkAgainst ) {
|
170
|
+
var dependencyValue = this.$dependencyObj.val()
|
171
|
+
, i = 0;
|
172
|
+
|
173
|
+
// Dependency value must be an array or object
|
174
|
+
if ( typeof(dependencyValue) === 'array' || typeof(dependencyValue) === 'object' ) {
|
175
|
+
for ( i in checkAgainst ) {
|
176
|
+
if ( $.inArray(checkAgainst[i], dependencyValue) !== -1 ) {
|
177
|
+
return true;
|
178
|
+
}
|
179
|
+
|
180
|
+
}
|
181
|
+
} else {
|
182
|
+
return this.values( checkAgainst );
|
183
|
+
}
|
184
|
+
|
185
|
+
return false;
|
186
|
+
};
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
* Qualifier method which is a shortcut qualifier uses `match` method to check if value is an
|
191
|
+
* email address.
|
192
|
+
*
|
193
|
+
* @function email
|
194
|
+
* @param checkAgainst The value we are checking.
|
195
|
+
* @returns {Boolean}
|
196
|
+
*/
|
197
|
+
Dependency.prototype.email = function ( checkAgainst ) {
|
198
|
+
var emailPattern = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]+)*@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$/;
|
199
|
+
|
200
|
+
return ( this.match(emailPattern) === checkAgainst );
|
201
|
+
};
|
202
|
+
|
203
|
+
|
204
|
+
/**
|
205
|
+
* Qualifier method which is a shortcut qualifier uses `match` method to check if value is a
|
206
|
+
* URL.
|
207
|
+
*
|
208
|
+
* @function url
|
209
|
+
* @param checkAgainst The value we are checking.
|
210
|
+
* @returns {Boolean}
|
211
|
+
*/
|
212
|
+
Dependency.prototype.url = function ( checkAgainst ) {
|
213
|
+
var urlPattern = /(((http|ftp|https):\/\/)|www\.)[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?\^=%&:\/~\+#!]*[\w\-\@?\^=%&\/~\+#])?/g;
|
214
|
+
|
215
|
+
return ( this.match(urlPattern) === checkAgainst );
|
216
|
+
};
|
217
|
+
|
218
|
+
/**
|
219
|
+
* Checks the Dependency against all of it's qualifiers.
|
220
|
+
*
|
221
|
+
* @function doesQualify
|
222
|
+
* @returns {Boolean}
|
223
|
+
*/
|
224
|
+
Dependency.prototype.doesQualify = function () {
|
225
|
+
var q = 0;
|
226
|
+
|
227
|
+
// Loop through qualifiers
|
228
|
+
for ( q in this.qualifiers ) {
|
229
|
+
|
230
|
+
// Check if qualifier is a method of the Dependency object; if so,
|
231
|
+
// execute it.
|
232
|
+
if ( Dependency.prototype.hasOwnProperty( q ) &&
|
233
|
+
typeof(Dependency.prototype[q]) === 'function' ) {
|
234
|
+
|
235
|
+
if ( !this[q](this.qualifiers[q]) ) {
|
236
|
+
return false;
|
237
|
+
}
|
238
|
+
} else {
|
239
|
+
|
240
|
+
// Custom qualifier method
|
241
|
+
if ( typeof(this.qualifiers[q] === 'function') ) {
|
242
|
+
return this.qualifiers[q]( this.$dependencyObj.val() );
|
243
|
+
}
|
244
|
+
}
|
245
|
+
}
|
246
|
+
|
247
|
+
return true;
|
248
|
+
};
|
249
|
+
|
250
|
+
|
251
|
+
/**
|
252
|
+
* Creates a new DependencySet
|
253
|
+
*
|
254
|
+
* @class DependencySet
|
255
|
+
* @param {Object} dependencies An object containing key-value pairs of selectors and qualifiers.
|
256
|
+
*/
|
257
|
+
var DependencySet = function ( dependencies ) {
|
258
|
+
var d = 0;
|
259
|
+
|
260
|
+
this.dependencies = [];
|
261
|
+
|
262
|
+
for ( d in dependencies ) {
|
263
|
+
this.dependencies.push( new Dependency(d, dependencies[d]) );
|
264
|
+
}
|
265
|
+
};
|
266
|
+
|
267
|
+
|
268
|
+
/**
|
269
|
+
* Checks if each Dependency in the set qualifies.
|
270
|
+
*
|
271
|
+
* @function doesQualify
|
272
|
+
* @returns {Boolean}
|
273
|
+
*/
|
274
|
+
DependencySet.prototype.doesQualify = function () {
|
275
|
+
var length = this.dependencies.length
|
276
|
+
, d = 0
|
277
|
+
, qualifies = true;
|
278
|
+
|
279
|
+
// Execute `doesQualify` method on each dependency
|
280
|
+
for ( d; d < length; d += 1 ) {
|
281
|
+
if ( !this.dependencies[d].doesQualify() ) {
|
282
|
+
qualifies = false;
|
283
|
+
break;
|
284
|
+
}
|
285
|
+
}
|
286
|
+
|
287
|
+
return qualifies;
|
288
|
+
};
|
289
|
+
|
290
|
+
|
291
|
+
/**
|
292
|
+
* Creates a new DependencyCollection
|
293
|
+
*
|
294
|
+
* @class DependencyCollection
|
295
|
+
* @param {Object} subject A jQuery object which is the subject of the dependency.
|
296
|
+
* @param {Object} initialSet An object of key-value pairs of selectors and qualifiers
|
297
|
+
* representing the inital DependencySet.
|
298
|
+
* @param {Object} options An object for key-value pairs of options.
|
299
|
+
*/
|
300
|
+
var DependencyCollection = function ( $subject, initalSet, options ) {
|
301
|
+
this.dependencySets = [];
|
302
|
+
this.$subject = $subject;
|
303
|
+
|
304
|
+
// Extend default settings with the provided options
|
305
|
+
this.settings = $.extend({
|
306
|
+
disable: true,
|
307
|
+
hide: true,
|
308
|
+
duration: 200,
|
309
|
+
onEnable: function() {},
|
310
|
+
onDisable: function() {}
|
311
|
+
}, options);
|
312
|
+
|
313
|
+
/**
|
314
|
+
* Misc. settings not enabled by default
|
315
|
+
* -------------------------------------
|
316
|
+
* valueOnEnable: string
|
317
|
+
* valueOnDisable: string
|
318
|
+
* checkOnDisable: boolean
|
319
|
+
* checkOnEnable: boolean
|
320
|
+
* valueTarget: selector (string)
|
321
|
+
* toggleClass: string
|
322
|
+
*/
|
323
|
+
|
324
|
+
this.enableCallback = function() {};
|
325
|
+
this.disableCallback = function() {};
|
326
|
+
|
327
|
+
this.init( initalSet );
|
328
|
+
};
|
329
|
+
|
330
|
+
|
331
|
+
/**
|
332
|
+
* Initaialize the collection by adding the intial dependencySet
|
333
|
+
* and running the first check.
|
334
|
+
*
|
335
|
+
* @function init
|
336
|
+
* @param {Object} dependencies An object of key-value pairs of selectors and qualifiers.
|
337
|
+
*/
|
338
|
+
DependencyCollection.prototype.init = function ( dependencies ) {
|
339
|
+
this.addSet( dependencies );
|
340
|
+
this.check( true );
|
341
|
+
};
|
342
|
+
|
343
|
+
|
344
|
+
/**
|
345
|
+
* Add a new DependencySet and register the `change` event for each
|
346
|
+
* Dependency in that set.
|
347
|
+
*
|
348
|
+
* @function addSet
|
349
|
+
* @param {Object} set An object of key-value pairs of selectors and qualifiers.
|
350
|
+
*/
|
351
|
+
DependencyCollection.prototype.addSet = function ( set ) {
|
352
|
+
var self = this
|
353
|
+
, thisSet = 0
|
354
|
+
, numDependencies = 0
|
355
|
+
, d = 0
|
356
|
+
, dependency;
|
357
|
+
|
358
|
+
// Create a new DependencySet and add it to the stack
|
359
|
+
this.dependencySets.push( new DependencySet(set) );
|
360
|
+
|
361
|
+
thisSet = this.dependencySets.length - 1;
|
362
|
+
numDependencies = this.dependencySets[thisSet].dependencies.length;
|
363
|
+
|
364
|
+
// Loop through each of the Dependencies in the newly added DependencySet
|
365
|
+
for ( d; d < numDependencies; d += 1 ) {
|
366
|
+
dependency = this.dependencySets[thisSet].dependencies[d];
|
367
|
+
|
368
|
+
// Register change event
|
369
|
+
dependency.$dependencyObj.on('change', function(e) {
|
370
|
+
self.triggeredEvent = e;
|
371
|
+
self.triggeredDependency = this;
|
372
|
+
self.check();
|
373
|
+
});
|
374
|
+
|
375
|
+
// Handle on enter key event (fix for IE which doesn't register a change event when user
|
376
|
+
// hits the enter key for text fields)
|
377
|
+
if ( dependency.$dependencyObj.attr('type') === 'text' ) {
|
378
|
+
dependency.$dependencyObj.on('keypress', function(e) {
|
379
|
+
if ( e.which && dependency.$dependencyObj.is(':focus') ) {
|
380
|
+
if ( self.check() ) {
|
381
|
+
self.triggeredEvent = e;
|
382
|
+
self.triggeredDependency = this;
|
383
|
+
self.check();
|
384
|
+
}
|
385
|
+
}
|
386
|
+
});
|
387
|
+
}
|
388
|
+
}
|
389
|
+
};
|
390
|
+
|
391
|
+
|
392
|
+
/**
|
393
|
+
* Public method to add a new DependencySet to the stack.
|
394
|
+
*
|
395
|
+
* @function or
|
396
|
+
* @param {Object} dependencies An object of key-value pairs of selectors and qualifiers.
|
397
|
+
* @returns {DependencyCollection} Returns this DependencyCollection in order to maintain
|
398
|
+
* chainability.
|
399
|
+
*/
|
400
|
+
DependencyCollection.prototype.or = function ( dependencies ) {
|
401
|
+
this.addSet( dependencies );
|
402
|
+
this.check( false );
|
403
|
+
|
404
|
+
return this;
|
405
|
+
};
|
406
|
+
|
407
|
+
/**
|
408
|
+
* Enable the subject.
|
409
|
+
*
|
410
|
+
* @function enable
|
411
|
+
* @param {Boolean} noFade Whether or not to fade the subject or immediately show it.
|
412
|
+
*/
|
413
|
+
DependencyCollection.prototype.enable = function ( noFade ) {
|
414
|
+
var valueSubject = this.$subject
|
415
|
+
, subjectId = this.$subject.attr( 'id' )
|
416
|
+
, $hideObject;
|
417
|
+
|
418
|
+
// If the value target has been specified by the user
|
419
|
+
if ( this.settings.hasOwnProperty('valueTarget') && this.settings.valueTarget !== undefined) {
|
420
|
+
valueSubject = $( this.settings.valueTarget );
|
421
|
+
|
422
|
+
// If the subject is not a form field, then look for one within the subject
|
423
|
+
} else if ( this.$subject[0].nodeName.toLowerCase() !== 'input' &&
|
424
|
+
this.$subject[0].nodeName.toLowerCase() !== 'textarea' &&
|
425
|
+
this.$subject[0].nodeName.toLowerCase() !== 'select') {
|
426
|
+
|
427
|
+
valueSubject = this.$subject.find( 'input, textarea, select' );
|
428
|
+
}
|
429
|
+
|
430
|
+
// Remove the disabled attribute from the subject if allowed by the settings
|
431
|
+
if ( this.settings.disable ) {
|
432
|
+
this.$subject.removeAttr( 'disabled' );
|
433
|
+
}
|
434
|
+
|
435
|
+
// Show the subject if allowed by the settings
|
436
|
+
if ( this.settings.hide ) {
|
437
|
+
|
438
|
+
// If the subject's parent is a label
|
439
|
+
if ( this.$subject.parent()[0].nodeName.toLowerCase() === 'label' ) {
|
440
|
+
$hideObject = this.$subject.parent();
|
441
|
+
} else {
|
442
|
+
$hideObject = this.$subject.add( 'label[for="' + subjectId + '"]' )
|
443
|
+
}
|
444
|
+
|
445
|
+
if ( $hideObject.css('display') === 'none' ) {
|
446
|
+
if ( noFade ) {
|
447
|
+
|
448
|
+
// Show the input and it's label (if exists)
|
449
|
+
$hideObject.show();
|
450
|
+
} else {
|
451
|
+
|
452
|
+
// Fade in the input and it's label (if exists)
|
453
|
+
$hideObject.fadeIn( this.settings.duration );
|
454
|
+
}
|
455
|
+
}
|
456
|
+
}
|
457
|
+
|
458
|
+
// Set the value of the subject if allowed by the settings
|
459
|
+
if ( this.settings.hasOwnProperty('valueOnEnable') && this.settings.valueOnEnable !== undefined ) {
|
460
|
+
valueSubject.val( this.settings.valueOnEnable );
|
461
|
+
}
|
462
|
+
|
463
|
+
// Add/remove the checked attribute of the subject if allowed by the settings
|
464
|
+
if ( this.settings.hasOwnProperty('checkOnEnable') ) {
|
465
|
+
if ( this.settings.checkOnEnable ) {
|
466
|
+
valueSubject.attr( 'checked', 'checked' );
|
467
|
+
} else {
|
468
|
+
valueSubject.removeAttr( 'checked' );
|
469
|
+
}
|
470
|
+
}
|
471
|
+
|
472
|
+
// Add a class to the subject if allowed by the settings
|
473
|
+
if ( this.settings.hasOwnProperty('toggleClass') && this.settings.toggleClass !== undefined ) {
|
474
|
+
this.$subject.addClass( this.settings.toggleClass );
|
475
|
+
}
|
476
|
+
|
477
|
+
// User callback
|
478
|
+
this.settings.onEnable.call(this.triggeredDependency, this.triggeredEvent);
|
479
|
+
};
|
480
|
+
|
481
|
+
/**
|
482
|
+
* Disable the subject.
|
483
|
+
*
|
484
|
+
* @function disable
|
485
|
+
* @param {Boolean} noFade Whether or not to fade the subject or immediately hide it.
|
486
|
+
*/
|
487
|
+
DependencyCollection.prototype.disable = function ( noFade ) {
|
488
|
+
var valueSubject = this.$subject
|
489
|
+
, subjectId = this.$subject.attr( 'id' )
|
490
|
+
, $hideObject;
|
491
|
+
|
492
|
+
// If the value target has been specified by the user
|
493
|
+
if ( this.settings.hasOwnProperty('valueTarget') && this.settings.valueTarget !== undefined) {
|
494
|
+
valueSubject = $( this.settings.valueTarget );
|
495
|
+
|
496
|
+
// If the subject is not a form field, then look for one within the subject
|
497
|
+
} else if ( this.$subject[0].nodeName.toLowerCase() !== 'input' &&
|
498
|
+
this.$subject[0].nodeName.toLowerCase() !== 'textarea' &&
|
499
|
+
this.$subject[0].nodeName.toLowerCase() !== 'select') {
|
500
|
+
|
501
|
+
valueSubject = this.$subject.find( 'input, textarea, select' );
|
502
|
+
}
|
503
|
+
|
504
|
+
// Add the disabled attribute from the subject if allowed by the settings
|
505
|
+
if ( this.settings.disable ) {
|
506
|
+
this.$subject.attr( 'disabled', 'disabled' );
|
507
|
+
}
|
508
|
+
|
509
|
+
// Hide the subject if allowed by the settings
|
510
|
+
if ( this.settings.hide ) {
|
511
|
+
|
512
|
+
// If the subject's parent is a label
|
513
|
+
if ( this.$subject.parent()[0].nodeName.toLowerCase() === 'label' ) {
|
514
|
+
$hideObject = this.$subject.parent();
|
515
|
+
} else {
|
516
|
+
$hideObject = this.$subject.add( 'label[for="' + subjectId + '"]' )
|
517
|
+
}
|
518
|
+
|
519
|
+
if ( noFade ) {
|
520
|
+
|
521
|
+
// Hide the input and it's label (if exists)
|
522
|
+
$hideObject.css({ 'display': 'none' });
|
523
|
+
} else {
|
524
|
+
|
525
|
+
// Fade out the input and it's label (if exists)
|
526
|
+
$hideObject.fadeOut( this.settings.duration );
|
527
|
+
}
|
528
|
+
}
|
529
|
+
|
530
|
+
// Set the value of the subject if allowed by the settings
|
531
|
+
if ( this.settings.hasOwnProperty('valueOnDisable') && this.settings.valueOnDisable !== undefined ) {
|
532
|
+
valueSubject.val( this.settings.valueOnDisable );
|
533
|
+
}
|
534
|
+
|
535
|
+
// Add/remove the checked attribute of the subject if allowed by the settings
|
536
|
+
if ( this.settings.hasOwnProperty('checkOnDisable') ) {
|
537
|
+
if ( this.settings.checkOnDisable ) {
|
538
|
+
valueSubject.attr( 'checked', 'checked' );
|
539
|
+
} else {
|
540
|
+
valueSubject.removeAttr( 'checked' );
|
541
|
+
}
|
542
|
+
}
|
543
|
+
|
544
|
+
// Remove a class of the subject if allowed by the settings
|
545
|
+
if ( this.settings.hasOwnProperty('toggleClass') && this.settings.toggleClass !== undefined ) {
|
546
|
+
this.$subject.removeClass( this.settings.toggleClass );
|
547
|
+
}
|
548
|
+
|
549
|
+
// User callback
|
550
|
+
this.settings.onDisable.call(this.triggeredDependency, this.triggeredEvent);
|
551
|
+
};
|
552
|
+
|
553
|
+
|
554
|
+
/**
|
555
|
+
* Check each DependencySet's `doesQualify` method. If any of the sets qualify then enable
|
556
|
+
* the input, othewise disable it.
|
557
|
+
*
|
558
|
+
* @function check
|
559
|
+
* @param {Boolean} firstRun Whether or not this is the initial check.
|
560
|
+
* @returns {Boolean} Whether or not the event qualifies.
|
561
|
+
*/
|
562
|
+
DependencyCollection.prototype.check = function ( firstRun ) {
|
563
|
+
var length = this.dependencySets.length
|
564
|
+
, i = 0
|
565
|
+
, qualifies = false;
|
566
|
+
|
567
|
+
// Loop through each DependencySet
|
568
|
+
for ( i; i < length; i += 1 ) {
|
569
|
+
if ( this.dependencySets[i].doesQualify() ) {
|
570
|
+
qualifies = true;
|
571
|
+
break;
|
572
|
+
}
|
573
|
+
}
|
574
|
+
|
575
|
+
if (qualifies) {
|
576
|
+
this.enable( firstRun );
|
577
|
+
return true;
|
578
|
+
} else {
|
579
|
+
this.disable( firstRun );
|
580
|
+
return false;
|
581
|
+
}
|
582
|
+
};
|
583
|
+
|
584
|
+
|
585
|
+
/**
|
586
|
+
* Plugin access point.
|
587
|
+
*
|
588
|
+
* @function dependsOn
|
589
|
+
* @param {Object} initialSet An object of key-value pairs of selectors and qualifiers
|
590
|
+
* representing the inital DependencySet.
|
591
|
+
* @param {Object} options An object for key-value pairs of options.
|
592
|
+
* @returns {DependencyCollection} The DependencyCollection object.
|
593
|
+
*/
|
594
|
+
$.fn.dependsOn = function ( dependencies, options ) {
|
595
|
+
var dependencyCollection = new DependencyCollection( this, dependencies, options );
|
596
|
+
|
597
|
+
return dependencyCollection;
|
598
|
+
|
599
|
+
};
|
600
|
+
|
601
|
+
})( jQuery );
|
@@ -0,0 +1,19 @@
|
|
1
|
+
//= require dependsOn
|
2
|
+
window.prepareDependsOn = ->
|
3
|
+
|
4
|
+
for element in $('.depends-on')
|
5
|
+
field_to_check = $(element).data('id')
|
6
|
+
field_to_check = "##{field_to_check}"
|
7
|
+
type = ($(field_to_check).attr('type'))
|
8
|
+
switch type
|
9
|
+
when 'checkbox' then depend_type = 'checked'
|
10
|
+
when 'text', 'number', 'select' then depend_type = 'values'
|
11
|
+
value = $(element).data('value')
|
12
|
+
object = {}
|
13
|
+
object[field_to_check] = {
|
14
|
+
"#{depend_type}": value
|
15
|
+
}
|
16
|
+
$(element).dependsOn object
|
17
|
+
|
18
|
+
# ---
|
19
|
+
# generated by js2coffee 2.1.0
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_depends_on/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_depends_on"
|
8
|
+
spec.version = RailsDependsOn::VERSION
|
9
|
+
spec.authors = ["Francesco Brunetti"]
|
10
|
+
spec.email = ["francescob@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A jQuery plugin to facilitate the handling of form field dependencies."
|
13
|
+
spec.homepage = "https://github.com/francescob/rails_depends_on"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_dependency 'jquery-rails'
|
23
|
+
spec.add_dependency 'coffee-rails'
|
24
|
+
spec.add_dependency "railties", ">= 3.1"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_depends_on
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Brunetti
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-22 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: jquery-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '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: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: railties
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.1'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.1'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- francescob@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- app/assets/javascripts/dependsOn.js
|
97
|
+
- app/assets/javascripts/rails_depends_on.js.coffee
|
98
|
+
- lib/rails_depends_on.rb
|
99
|
+
- lib/rails_depends_on/version.rb
|
100
|
+
- rails_depends_on.gemspec
|
101
|
+
homepage: https://github.com/francescob/rails_depends_on
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.4.5
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: A jQuery plugin to facilitate the handling of form field dependencies.
|
125
|
+
test_files: []
|
126
|
+
has_rdoc:
|