/
fieldmanager.js
270 lines (248 loc) · 8.24 KB
/
fieldmanager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var fm = {};
( function( $ ) {
var dynamic_seq = 0;
var init_sortable_container = function( el ) {
if ( !$( el ).hasClass( 'ui-sortable' ) ) {
$( el ).sortable( {
handle: '.fmjs-drag',
items: '> .fm-item',
placeholder: "sortable-placeholder",
forcePlaceholderSize: true,
start: function( e, ui ) {
$( document ).trigger( 'fm_sortable_drag', el );
},
stop: function( e, ui ) {
var $parent = ui.item.parents( '.fm-wrapper' ).first();
fm_renumber( $parent );
$( document ).trigger( 'fm_sortable_drop', el );
}
} );
}
}
var init_sortable = function() {
$( '.fmjs-sortable' ).each( function() {
if ( $( this ).is( ':visible' ) ) {
init_sortable_container( this );
} else {
var sortable = this;
$( sortable ).parents( '.fm-group' ).first().bind( 'fm_collapsible_toggle', function() {
init_sortable_container( sortable );
} );
}
} );
}
var init_label_macros = function() {
// Label macro magic.
$( '.fm-label-with-macro' ).each( function( label ) {
$( this ).data( 'label-original', $( this ).html() );
var src = $( this ).parents( '.fm-group' ).first().find( $( this ).data( 'label-token' ) );
if ( src.length > 0 ) {
var $src = $( src[0] );
if ( typeof $src.val === 'function' ) {
var $label = $( this );
var title_macro = function() {
var token = '';
if ( $src.prop( 'tagName' ) == 'SELECT' ) {
var $option = $src.find( 'option:selected' );
if ( $option.val() ) {
token = $option.text();
}
} else {
token = $src.val();
}
if ( token.length > 0 ) {
$label.html( $label.data( 'label-format' ).replace( '%s', token ) );
} else {
$label.html( $label.data( 'label-original' ) );
}
};
$src.on( 'change keyup', title_macro );
title_macro();
}
}
} );
}
var fm_renumber = function( $wrappers ) {
$wrappers.each( function() {
var level_pos = $( this ).data( 'fm-array-position' ) - 0;
var order = 0;
if ( level_pos > 0 ) {
$( this ).find( '> .fm-item' ).each( function() {
if ( $( this ).hasClass( 'fmjs-proto' ) ) {
return; // continue
}
$( this ).find( '.fm-element, .fm-incrementable' ).each( function() {
var fname = $(this).attr( 'name' );
if ( fname ) {
fname = fname.replace( /\]/g, '' );
parts = fname.split( '[' );
if ( parts[ level_pos ] != order ) {
parts[ level_pos ] = order;
var new_fname = parts[ 0 ] + '[' + parts.slice( 1 ).join( '][' ) + ']';
$( this ).attr( 'name', new_fname );
if ( $( this ).attr( 'id' ) && $( this ).attr( 'id' ).match( '-proto' ) && ! new_fname.match( 'proto' ) ) {
$( this ).attr( 'id', 'fm-edit-dynamic-' + dynamic_seq );
if ( $( this ).parent().hasClass( 'fm-option' ) ) {
$( this ).parent().find( 'label' ).attr( 'for', 'fm-edit-dynamic-' + dynamic_seq );
} else {
var parent = $( this ).closest( '.fm-item' );
if ( parent.length && parent.find( '.fm-label label' ).length ) {
parent.find( '.fm-label label' ).attr( 'for', 'fm-edit-dynamic-' + dynamic_seq );
}
}
dynamic_seq++;
return; // continue;
}
}
}
if ( $( this ).hasClass( 'fm-incrementable' ) ) {
$( this ).attr( 'id', 'fm-edit-dynamic-' + dynamic_seq );
dynamic_seq++;
}
} );
order++;
} );
}
$( this ).find( '.fm-wrapper' ).each( function() {
fm_renumber( $( this ) );
} );
} );
}
/**
* Get data attribute display-value(s).
*
* Accounts for jQuery converting string to number automatically.
*
* @param HTMLDivElement el Wrapper with the data attribute.
* @return string|number|array Single string or number, or array if data attr contains CSV.
*/
var getCompareValues = function( el ) {
var values = $( el ).data( 'display-value' );
try {
values = values.split( ',' );
} catch( e ) {
// If jQuery already converted string to number.
values = [ values ];
}
return values;
};
var match_value = function( values, match_string ) {
for ( var index in values ) {
if ( values[index] == match_string ) {
return true;
}
}
return false;
}
fm_add_another = function( $element ) {
var el_name = $element.data( 'related-element' )
, limit = $element.data( 'limit' ) - 0
, siblings = $element.parent().siblings( '.fm-item' ).not( '.fmjs-proto' )
, add_more_position = $element.data( 'add-more-position' ) || "bottom";
if ( limit > 0 && siblings.length >= limit ) {
return;
}
var $new_element = $( '.fmjs-proto.fm-' + el_name, $element.closest( '.fm-wrapper' ) ).first().clone();
$new_element.removeClass( 'fmjs-proto' );
$new_element = add_more_position == "bottom" ? $new_element.insertBefore( $element.parent() ) :
$new_element.insertAfter( $element.parent() ) ;
fm_renumber( $element.parents( '.fm-wrapper' ) );
// Trigger for subclasses to do any post-add event handling for the new element
$element.parent().siblings().last().trigger( 'fm_added_element' );
init_label_macros();
init_sortable();
}
fm_remove = function( $element ) {
$wrapper = $( this ).parents( '.fm-wrapper' ).first();
$element.parents( '.fm-item' ).first().remove();
fm_renumber( $wrapper );
}
$( document ).ready( function () {
$( document ).on( 'click', '.fm-add-another', function( e ) {
e.preventDefault();
fm_add_another( $( this ) );
} );
// Handle remove events
$( document ).on( 'click', '.fmjs-remove', function( e ) {
e.preventDefault();
fm_remove( $( this ) );
} );
// Handle collapse events
$( document ).on( 'click', '.fmjs-collapsible-handle', function() {
$( this ).parents( '.fm-group' ).first().children( '.fm-group-inner' ).slideToggle( 'fast' );
fm_renumber( $( this ).parents( '.fm-wrapper' ).first() );
$( this ).parents( '.fm-group' ).first().trigger( 'fm_collapsible_toggle' );
$( this ).toggleClass( 'closed' );
if ( $( this ).hasClass( 'closed' ) ) {
$( this ).attr( 'aria-expanded', 'false' );
} else {
$( this ).attr( 'aria-expanded', 'true' );
}
} );
$( '.fm-collapsed > .fm-group:not(.fmjs-proto) > .fm-group-inner' ).hide();
// Initializes triggers to conditionally hide or show fields
$( '.display-if' ).each( function() {
var val;
var src = $( this ).data( 'display-src' );
var values = getCompareValues( this );
// Wrapper divs sometimes receive .fm-element, but don't use them as
// triggers. Also don't use autocomplete inputs as triggers, because the
// value is in their sibling hidden fields (which this still matches).
var trigger = $( this ).siblings( '.fm-' + src + '-wrapper' ).find( '.fm-element' ).not( 'div, .fm-autocomplete' );
if ( trigger.is( ':checkbox' ) ) {
if ( trigger.is( ':checked' ) ) {
// If checked, use the checkbox value.
val = trigger.val();
} else {
// Otherwise, use the hidden sibling field with the "unchecked" value.
val = trigger.siblings( 'input[type=hidden][name="' + trigger.attr( 'name' ) + '"]' ).val();
}
} else if ( trigger.is( ':radio' ) ) {
if ( trigger.filter( ':checked' ).length ) {
val = trigger.filter( ':checked' ).val();
} else {
// On load, there might not be any selected radio, in which case call the value blank.
val = '';
}
} else {
val = trigger.val().split( ',' );
}
trigger.addClass( 'display-trigger' );
if ( ! match_value( values, val ) ) {
$( this ).hide();
}
} );
// Controls the trigger to show or hide fields
$( document ).on( 'change', '.display-trigger', function() {
var val;
var $this = $( this );
var name = $this.attr( 'name' );
if ( $this.is( ':checkbox' ) ) {
if ( $this.is( ':checked' ) ) {
val = $this.val();
} else {
val = $this.siblings( 'input[type=hidden][name="' + name + '"]' ).val();
}
} else if ( $this.is( ':radio' ) ) {
val = $this.filter( ':checked' ).val();
} else {
val = $this.val().split( ',' );
}
$( this ).closest( '.fm-wrapper' ).siblings().each( function() {
if ( $( this ).hasClass( 'display-if' ) ) {
if ( name && name.match( $( this ).data( 'display-src' ) ) != null ) {
if ( match_value( getCompareValues( this ), val ) ) {
$( this ).show();
} else {
$( this ).hide();
}
$( this ).trigger( 'fm_displayif_toggle' );
}
}
} );
} );
init_label_macros();
init_sortable();
$( document ).on( 'fm_activate_tab', init_sortable );
} );
} )( jQuery );