-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathSettingsFields.js
More file actions
444 lines (356 loc) · 10.5 KB
/
SettingsFields.js
File metadata and controls
444 lines (356 loc) · 10.5 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/**
* Model settings fields view
*
* @since 3.17.0
* @version 4.7.0
*/
define( [], function() {
return Backbone.View.extend( _.defaults( {
/**
* DOM events
*
* @type {Object}
*/
events: {
'click .llms-settings-group-toggle': 'toggle_group',
},
/**
* Processed fields data
* Allows access by ID without traversing the schema
*
* @type {Object}
*/
fields: {},
/**
* Wrapper Tag name
*
* @type {String}
*/
tagName: 'div',
/**
* Get the underscore template
*
* @type {[type]}
*/
template: wp.template( 'llms-settings-fields-template' ),
/**
* Initialization callback func (renders the element on screen)
*
* @return void
* @since 3.17.0
* @version 3.17.0
*/
// initialize: function() {},
/**
* Retrieve an array of all editor fields in all groups
*
* @return array
* @since 3.17.1
* @version 3.17.1
*/
get_editor_fields: function() {
return _.filter( this.fields, function( field ) {
return this.is_editor_field( field.type );
}, this );
},
/**
* Get settings group data from a model
*
* @return {[type]}
* @since 3.17.0
* @version 3.17.0
*/
get_groups: function() {
return this.model.get_settings_fields();
},
/**
* Determine if a settings group is hidden in localStorage
*
* @param string group_id id of the group
* @return {Boolean}
* @since 3.17.0
* @version 3.17.0
*/
is_group_hidden: function( group_id ) {
var id = 'llms-' + this.model.get( 'type' ) + '-settings-group--' + group_id;
if ( 'undefined' !== window.localStorage ) {
return ( 'hidden' === window.localStorage.getItem( id ) );
}
return false;
},
/**
* Get the switch attribute for a field with switches
*
* @param obj field field data obj
* @return string
* @since 3.17.0
* @version 3.17.0
*/
get_switch_attribute: function( field ) {
return field.switch_attribute ? field.switch_attribute : field.attribute;
},
/**
* Determine if a field has a switch
*
* @param string type field type string
* @return {Boolean}
* @since 3.17.0
* @version 3.17.0
*/
has_switch: function( type ) {
return ( -1 !== type.indexOf( 'switch' ) );
},
/**
* Determine if a field is a default (text) field
*
* @param string type field type string
* @return {Boolean}
* @since 3.17.0
* @version 3.17.0
*/
is_default_field: function( type ) {
var types = [ 'audio_embed', 'datepicker', 'number', 'text', 'video_embed' ];
return ( -1 !== types.indexOf( type.replace( 'switch-', '' ) ) );
},
/**
* Determine if a field is a WYSIWYG editor field
*
* @param string type field type string
* @return {Boolean}
* @since 3.17.1
* @version 3.17.1
*/
is_editor_field: function( type ) {
var types = [ 'editor', 'switch-editor' ];
return ( -1 !== types.indexOf( type.replace( 'switch-', '' ) ) );
},
/**
* Determine if a switch is enabled for a field
*
* @param obj field field data object
* @return {Boolean}
* @since 3.17.0
* @version 3.17.6
*/
is_switch_condition_met: function( field ) {
return ( field.switch_on === this.model.get( field.switch_attribute ) );
},
/**
* Compiles the template and renders the view
*
* @return self (for chaining)
* @since 3.17.0
* @version 3.17.1
*/
render: function() {
this.$el.html( this.template( this ) );
// if editors exist, render them
_.each( this.get_editor_fields(), function( field ) {
this.render_editor( field );
}, this );
return this;
},
/**
* Renders an editor field
*
* @since 3.17.1
* @since 3.37.11 Replace references to `wp.editor` with `_.getEditor()` helper.
*
* @param {Object} field Field data object.
* @return {Void}
*/
render_editor: function( field ) {
var self = this,
wpEditor = _.getEditor();
// Exit early if there's no editor to work with.
if ( undefined === wpEditor ) {
console.error( 'Unable to access `wp.oldEditor` or `wp.editor`.' );
return;
}
wpEditor.remove( field.id );
field.settings.tinymce.setup = function( editor ) {
var $ed = $( '#' + editor.id ),
$parent = $ed.closest( '.llms-editable-editor' ),
$label = $parent.find( '.llms-label' ),
prop = $ed.attr( 'data-attribute' )
if ( $label.length ) {
$label.prependTo( $parent.find( '.wp-editor-tools' ) );
}
// save changes to the model via Visual ed
editor.on( 'change', function( event ) {
self.model.set( prop, wpEditor.getContent( editor.id ) );
} );
// save changes via Text ed
$ed.on( 'input', function( event ) {
self.model.set( prop, $ed.val() );
} );
// trigger an input on the Text ed when quicktags buttons are clicked
$parent.on( 'click', '.quicktags-toolbar .ed_button', function() {
setTimeout( function() {
$ed.trigger( 'input' );
}, 10 );
} );
};
wpEditor.initialize( field.id, field.settings );
},
/**
* Get the HTML for a select field
*
* @param obj options flat or multi-dimensional options object
* @param string attribute name of the select field's attribute
* @return string
* @since 3.17.0
* @version 3.17.2
*/
render_select_options: function( options, attribute ) {
var html = '',
selected = this.model.get( attribute );
function option_html( label, val ) {
return '<option value="' + val + '"' + _.selected( val, selected ) + '>' + label.substring( 0, 100 ) + ( label.length > 100 ? '...' : '' ) + '</option>';
}
_.each( options, function( option, index ) {
// this will be an key:val object
if ( 'string' === typeof option ) {
html += option_html( option, index );
// either option group or array of key,val objects
} else if ( 'object' === typeof option ) {
// option group
if ( option.label && option.options ) {
html += '<optgroup label="' + option.label + '">';
html += this.render_select_options( option.options, attribute );
} else {
html += option_html( option.val, option.key );
}
}
}, this );
return html;
},
/**
* Setup and fill fields with default data based on field type
*
* @since 3.17.0
* @since 3.24.0 Unknown.
* @since 3.37.11 Replace reference to `wp.editor` with `_.getEditor()` helper.
* @since 4.7.0 Ensure `switch-number` fields are set with the `number` type attribute.
*
* @param {Object} orig_field Original field as defined in the settings.
* @param {Integer} field_index Index of the field in the current row.
* @return {Object}
*/
setup_field: function( orig_field, field_index ) {
var defaults = {
classes: [],
id: _.uniqueId( orig_field.attribute + '_' ),
input_type: 'text',
label: '',
options: {},
placeholder: '',
tip: '',
tip_position: 'top-right',
settings: {},
};
// check the field condition if set
if ( orig_field.condition && false === _.bind( orig_field.condition, this.model )() ) {
return false;
}
switch ( orig_field.type ) {
case 'audio_embed':
defaults.classes.push( 'llms-editable-audio' );
defaults.placeholder = 'https://';
defaults.tip = LLMS.l10n.translate( 'Use SoundCloud or Spotify audio URLS.' );
defaults.input_type = 'url';
break;
case 'datepicker':
defaults.classes.push( 'llms-editable-date' );
break;
case 'editor':
case 'switch-editor':
var orig_settings = orig_field.settings || {};
defaults.settings = $.extend( true, _.getEditor().getDefaultSettings(), {
mediaButtons: true,
tinymce: {
toolbar1: 'bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_adv',
toolbar2: 'formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help',
}
}, orig_settings );
break;
case 'number':
case 'switch-number':
defaults.input_type = 'number';
break;
case 'permalink':
defaults.label = LLMS.l10n.translate( 'Permalink' );
break;
case 'video_embed':
defaults.classes.push( 'llms-editable-video' );
defaults.placeholder = 'https://';
defaults.tip = LLMS.l10n.translate( 'Use YouTube, Vimeo, or Wistia video URLS.' );
defaults.input_type = 'url';
break;
}
if ( this.has_switch( orig_field.type ) ) {
defaults.switch_on = 'yes';
defaults.switch_off = 'no';
}
var field = _.defaults( _.deepClone( orig_field ), defaults );
// if options is a function run it
if ( _.isFunction( field.options ) ) {
field.options = _.bind( field.options, this.model )();
}
// if it's a radio field options values can be submitted as images
// this will transform those images into <img> html
if ( -1 !== [ 'radio', 'switch-radio' ].indexOf( orig_field.type ) ) {
var has_images = false;
_.each( orig_field.options, function( val, key ) {
if ( -1 !== val.indexOf( '.png' ) || -1 !== val.indexOf( '.jpg' ) ) {
field.options[key] = '<span><img src="' + val + '"></span>';
has_images = true;
}
} );
if ( has_images ) {
field.classes.push( 'has-images' );
}
}
// transform classes array to a css class string
if ( field.classes.length ) {
field.classes = ' ' + field.classes.join( ' ' );
}
this.fields[ field.id ] = field;
return field;
},
/**
* Determine if toggling a switch select should rerender the view
*
* @param string field_type field type string
* @return boolean
* @since 3.17.0
* @version 3.17.0
*/
should_rerender_on_toggle: function( field_type ) {
return ( -1 !== field_type.indexOf( 'switch-' ) ) ? 'yes' : 'no';
},
/**
* Click event for toggling visibility of settings groups
* If localStorage is available, persist state
*
* @param obj event js event object
* @return void
* @since 3.17.0
* @version 3.17.0
*/
toggle_group: function( event ) {
event.preventDefault();
var $el = $( event.currentTarget ),
$group = $el.closest( '.llms-model-settings' );
$group.toggleClass( 'hidden' );
if ( 'undefined' !== window.localStorage ) {
var id = $group.attr( 'id' );
if ( $group.hasClass( 'hidden' ) ) {
window.localStorage.setItem( id, 'hidden' );
} else {
window.localStorage.removeItem( id );
}
}
},
} ) );
} );