Skip to content

Commit 5fd9a89

Browse files
committed
♻ Added changed check
1 parent cf55558 commit 5fd9a89

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Features:
99
- inline field editing
1010
- create links to load some content in a dialog
1111

12-
The easiest way to show how this plugin works is looking the examples [below](#examples-of-dynamic-fields).
12+
The easiest way to show how this plugin works is looking the examples [below](#examples).
1313

1414
## Install
1515

@@ -27,6 +27,7 @@ Options are passed to fields using *input_html* parameter as *data* attributes:
2727
+ **not_checked**: check if a checkbox is not checked
2828
+ **blank**: check if a field is blank
2929
+ **not_blank**: check if a field is not blank
30+
+ **changed**: check if the value of an input is changed (dirty)
3031
- **data-eq**: check if a field has a specific value
3132
- **data-not**: check if a field hasn't a specific value
3233
- **data-target**: target css selector (from parent fieldset, look for the closest match)
@@ -45,22 +46,22 @@ Options are passed to fields using *input_html* parameter as *data* attributes:
4546

4647
### Dynamic fields examples
4748

48-
- A checkbox that hides other fields if false (ex. model *Article*):
49+
- A checkbox that hides other fields if is checked (ex. model *Article*):
4950

5051
```rb
5152
form do |f|
5253
f.inputs 'Article' do
53-
f.input :published, input_html: { data: { if: 'not_checked', action: 'hide', target: '.grp1' } }
54+
f.input :published, input_html: { data: { if: 'checked', action: 'hide', target: '.grp1' } }
5455
f.input :online_date, wrapper_html: { class: 'grp1' }
55-
f.input :position, wrapper_html: { class: 'grp1' }
56+
f.input :draft_notes, wrapper_html: { class: 'grp1' }
5657
end
5758
f.actions
5859
end
5960
```
6061

61-
- Add 3 classes (*first*, *second*, *third*) if a checkbox is true:
62+
- Add 3 classes (*first*, *second*, *third*) if a checkbox is not checked:
6263

63-
`f.input :published, input_html: { data: { if: 'checked', action: 'addClass first second third', target: '.grp1' } }`
64+
`f.input :published, input_html: { data: { if: 'not_checked', action: 'addClass first second third', target: '.grp1' } }`
6465

6566
- Set another field value if a string field is blank:
6667

@@ -183,6 +184,8 @@ The link url is loaded via AJAX before opening the dialog.
183184

184185
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
185186

187+
Take a look at [other ActiveAdmin components](https://github.com/blocknotes?utf8=✓&tab=repositories&q=activeadmin&type=source) that I made if you are curious.
188+
186189
## Contributors
187190

188191
- [Mattia Roccoberton](http://blocknot.es) - creator, maintainer

app/assets/javascripts/activeadmin/dynamic_fields.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Evaluate a condition
2-
function dfEvalCondition( el, args ) {
2+
function dfEvalCondition( el, args, on_change ) {
33
if( args.fn ) {
44
if( args.fn && window[args.fn] ) return !window[args.fn]( el );
55
else console.log( 'Warning - activeadmin_dynamic_fields: ' + args.fn + '() not available [1]' );
@@ -16,6 +16,9 @@ function dfEvalCondition( el, args ) {
1616
else if( args.if == 'not_blank' ) {
1717
return el.val().length !== 0 && el.val().trim();
1818
}
19+
else if( args.if == 'changed' ) {
20+
return on_change;
21+
}
1922
else if( args.eq ) {
2023
return el.val() == args.eq;
2124
}
@@ -36,59 +39,59 @@ function dfSetupField( el ) {
3639
if( el.data( 'target' ) ) target = el.closest( 'fieldset' ).find( el.data( 'target' ) ); // closest find for has many associations
3740
else if( el.data( 'gtarget' ) ) target = $( el.data( 'gtarget' ) );
3841
if( action == 'hide' ) {
39-
if( dfEvalCondition( el, args ) ) target.hide();
42+
if( dfEvalCondition( el, args, false ) ) target.hide();
4043
else target.show();
4144
el.on( 'change', function( event ) {
42-
if( dfEvalCondition( $(this), args ) ) target.hide();
45+
if( dfEvalCondition( $(this), args, true ) ) target.hide();
4346
else target.show();
4447
});
4548
}
4649
else if( action == 'slide' ) {
47-
if( dfEvalCondition( el, args ) ) target.slideDown();
50+
if( dfEvalCondition( el, args, false ) ) target.slideDown();
4851
else target.slideUp();
4952
el.on( 'change', function( event ) {
50-
if( dfEvalCondition( $(this), args ) ) target.slideDown();
53+
if( dfEvalCondition( $(this), args, true ) ) target.slideDown();
5154
else target.slideUp();
5255
});
5356
}
5457
else if( action == 'fade' ) {
55-
if( dfEvalCondition( el, args ) ) target.fadeIn();
58+
if( dfEvalCondition( el, args, false ) ) target.fadeIn();
5659
else target.fadeOut();
5760
el.on( 'change', function( event ) {
58-
if( dfEvalCondition( $(this), args ) ) target.fadeIn();
61+
if( dfEvalCondition( $(this), args, true ) ) target.fadeIn();
5962
else target.fadeOut();
6063
});
6164
}
6265
else if( action.substr( 0, 8 ) == 'setValue' ) {
6366
var val = action.substr( 8 ).trim();
64-
if( dfEvalCondition( el, args ) ) dfSetValue( target, val );
67+
if( dfEvalCondition( el, args, false ) ) dfSetValue( target, val );
6568
el.on( 'change', function( event ) {
66-
if( dfEvalCondition( $(this), args ) ) dfSetValue( target, val );
69+
if( dfEvalCondition( $(this), args, true ) ) dfSetValue( target, val );
6770
});
6871
}
6972
else if( action.substr( 0, 8 ) == 'callback' ) {
7073
var cb = action.substr( 8 ).trim();
7174
if( cb && window[cb] ) {
72-
if( dfEvalCondition( el, args ) ) window[cb]( el.data( 'args' ) );
75+
if( dfEvalCondition( el, args, false ) ) window[cb]( el.data( 'args' ) );
7376
el.on( 'change', function( event ) {
74-
if( dfEvalCondition( $(this), args ) ) window[cb]( el.data( 'args' ) );
77+
if( dfEvalCondition( $(this), args, true ) ) window[cb]( el.data( 'args' ) );
7578
});
7679
}
7780
else console.log( 'Warning - activeadmin_dynamic_fields: ' + cb + '() not available [2]' );
7881
}
7982
else if( action.substr( 0, 8 ) == 'addClass' ) {
8083
var classes = action.substr( 8 ).trim();
81-
if( dfEvalCondition( el, args ) ) target.removeClass( classes );
84+
if( dfEvalCondition( el, args, false ) ) target.removeClass( classes );
8285
else target.addClass( classes );
8386
el.on( 'change', function( event ) {
84-
if( dfEvalCondition( $(this), args ) ) target.removeClass( classes );
87+
if( dfEvalCondition( $(this), args, true ) ) target.removeClass( classes );
8588
else target.addClass( classes );
8689
});
8790
}
8891
else if( args.fn ) { // function without action
89-
dfEvalCondition( el, args );
92+
dfEvalCondition( el, args, false );
9093
el.on( 'change', function( event ) {
91-
dfEvalCondition( el, args );
94+
dfEvalCondition( el, args, true );
9295
});
9396
}
9497
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module ActiveAdmin
22
module DynamicFields
3-
VERSION = '0.2.1'
3+
VERSION = '0.2.2'
44
end
55
end

0 commit comments

Comments
 (0)