-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy path_Editable.js
More file actions
671 lines (535 loc) · 15.1 KB
/
_Editable.js
File metadata and controls
671 lines (535 loc) · 15.1 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/**
* Handles UX and Events for inline editing of views
*
* Use with a Model's View
* Allows editing model.title field via .llms-editable-title elements
*
* @package LifterLMS/Scripts
*
* @since 3.16.0
* @since 3.25.4 Unknown
* @since 3.37.11 Replace reference to `wp.editor` with `_.getEditor()` helper.
* @version 3.37.11
*/
define( [], function() {
return {
media_lib: null,
/**
* DOM Events
*
* @type {Object}
* @since 3.16.0
* @version 3.17.8
*/
events: {
'click .llms-add-image': 'open_media_lib',
'click a[href="#llms-edit-slug"]': 'make_slug_editable',
'click a[href="#llms-remove-image"]': 'remove_image',
'change .llms-editable-select select': 'on_select',
'change .llms-switch input[type="checkbox"]': 'toggle_switch',
'change .llms-editable-radio input': 'on_radio_select',
'focusin .llms-input': 'on_focus',
'focusout .llms-input': 'on_blur',
'keydown .llms-input': 'on_keydown',
'input .llms-input[type="number"]': 'on_blur',
'paste .llms-input[data-formatting]': 'on_paste',
},
/**
* Retrieve a list of allowed tags for a given element
*
* @param obj $el jQuery selector for the element
* @return array
* @since 3.16.0
* @version 3.17.8
*/
get_allowed_tags: function( $el ) {
if ( $el.attr( 'data-formatting' ) ) {
return _.map( $el.attr( 'data-formatting' ).split( ',' ), function( tag ) {
return tag.trim();
} );
}
return [ 'b', 'i', 'u', 'strong', 'em' ];
},
/**
* Retrieve the content of an element
*
* @param obj $el jQuery object of the element
* @return string
* @since 3.16.0
* @version 3.17.8
*/
get_content: function( $el ) {
if ( 'INPUT' === $el[0].tagName ) {
return $el.val();
}
if ( ! $el.attr( 'data-formatting' ) && ! $el.hasClass( 'ql-editor' ) ) {
return $el.text();
}
return _.stripFormatting( $el.html(), this.get_allowed_tags( $el ) );
},
/**
* Determine if changes have been made to the element
*
* @param {[obj]} event js event object
* @return {Boolean} true when changes have been made, false otherwise
* @since 3.16.0
* @version 3.16.0
*/
has_changed: function( event ) {
var $el = $( event.target );
return ( $el.attr( 'data-original-content' ) !== this.get_content( $el ) );
},
/**
* Ensure that new content is at least 1 character long
*
* @param obj event js event object
* @return boolean
* @since 3.16.0
* @version 3.17.2
*/
is_valid: function( event ) {
var self = this,
$el = $( event.target ),
content = this.get_content( $el ),
type = $el.attr( 'data-type' );
if ( ( $el.attr( 'required' ) || $el.attr( 'data-required' ) ) && content.length < 1 ) {
return false;
}
if ( 'url' === type || 'video' === type ) {
if ( ! this._validate_url( this.get_content( $el ) ) ) {
return false;
}
} else if ( 'permalink' === type ) {
LLMS.Ajax.call( {
data: {
action: 'llms_builder',
action_type: 'get_permalink',
course_id: window.llms_builder.CourseModel.get( 'id' ),
id: self.model.get( 'id' ),
title: self.model.get( 'title' ),
slug: content,
},
beforeSend: function() {
LLMS.Spinner.start( $el.closest( '.llms-editable-toggle-group' ), 'small' );
},
success: function( r ) {
if ( r.permalink && r.slug ) {
self.model.set( 'permalink', r.permalink );
self.model.set( 'name', r.slug );
self.render();
}
}
} );
}
return true;
},
/**
* Initialize datepicker elements
*
* @return void
* @since 3.17.0
* @version 3.17.0
*/
init_datepickers: function() {
this.$el.find( '.llms-editable-date input' ).each( function() {
$( this ).datetimepicker( {
format: $( this ).attr( 'data-date-format' ) || 'Y-m-d h:i A',
datepicker: ( undefined === $( this ).attr( 'data-date-datepicker' ) ) ? true : ( 'true' == $( this ).attr( 'data-date-datepicker' ) ),
timepicker: ( undefined === $( this ).attr( 'data-date-timepicker' ) ) ? true : ( 'true' == $( this ).attr( 'data-date-timepicker' ) ),
onClose: function( current_time, $input ) {
$input.blur();
}
} );
} );
},
/**
* Initialize elements that allow inline formatting
*
* @return void
* @since 3.16.0
* @version 3.16.0
*/
init_formatting_els: function() {
var self = this;
this.$el.find( '.llms-input-formatting[data-formatting]' ).each( function() {
var formatting = $( this ).attr( 'data-formatting' ).split( ',' ),
attr = $( this ).attr( 'data-attribute' );
var ed = new Quill( this, {
modules: {
toolbar: [ formatting ],
keyboard: {
bindings: {
tab: {
key: 9,
handler: function( range, context ) {
return true;
},
},
13: {
key: 13,
handler: function( range, context ) {
ed.root.blur();
return false;
},
},
},
},
},
placeholder: $( this ).attr( 'data-placeholder' ),
theme: 'bubble',
} );
ed.on( 'text-change', function( delta, oldDelta, source ) {
self.model.set( attr, self.get_content( $( ed.root ) ) );
} );
Backbone.pubSub.trigger( 'formatting-ed-init', ed, $( this ), self );
} );
},
/**
* Initialize editable select elements
*
* @return void
* @since 3.16.0
* @version 3.25.4
*/
init_selects: function() {
this.$el.find( '.llms-editable-select select' ).llmsSelect2( {
width: '100%',
} ).trigger( 'change' );
},
/**
* Blur/focusout function for .llms-editable-title elements
* Automatically saves changes if changes have been made
*
* @param obj event js event object
* @return void
* @since 3.16.0
* @version 3.16.6
*/
on_blur: function( event ) {
event.stopPropagation();
this.model.set( '_has_focus', false, { silent: true } );
var self = this,
$el = $( event.target ),
changed = this.has_changed( event );
if ( changed ) {
if ( ! self.is_valid( event ) ) {
self.revert_edits( event );
} else {
this.save_edits( event );
}
}
},
/**
* Focus event for editable inputs
*
* @param obj event js event object
* @return void
* @since 3.16.6
* @version 3.16.6
*/
on_focus: function( event ) {
event.stopPropagation();
this.model.set( '_has_focus', true, { silent: true } );
},
/**
* Handle content pasted into contenteditable fields
* This will ensure that HTML from RTF editors isn't pasted into the dom
*
* @param obj event js event obj
* @return void
* @since 3.17.8
* @version 3.17.8
*/
on_paste: function( event ) {
event.preventDefault();
event.stopPropagation();
var text = ( event.originalEvent || event ).clipboardData.getData( 'text/plain' );
window.document.execCommand( 'insertText', false, text );
},
/**
* Change event for selectables
*
* @param obj event js event object
* @return void
* @since 3.16.0
* @version 3.16.0
*/
on_select: function( event ) {
event.stopPropagation();
var $el = $( event.target ),
multi = ( $el.attr( 'multiple' ) ),
attr = $el.attr( 'name' ),
$selected = $el.find( 'option:selected' ),
val;
if ( multi ) {
val = [];
val = $selected.map( function() {
return this.value;
} ).get();
} else {
val = $selected[0].value;
}
this.model.set( attr, val );
},
/**
* Change event for radio element groups
*
* @param obj event js event object
* @return void
* @since 3.17.6
* @version 3.17.6
*/
on_radio_select: function( event ) {
var $el = $( event.target ),
attr = $el.attr( 'name' ),
val = $el.val();
this.model.set( attr, val );
},
/**
* Keydown function for .llms-editable-title elements
* Blurs
*
* @param {obj} event js event object
* @return void
* @since 3.16.0
* @version 3.17.8
*/
on_keydown: function( event ) {
event.stopPropagation();
var self = this,
key = event.which || event.keyCode,
shift = event.shiftKey;
// ctrl = event.metaKey || event.ctrlKey;
switch ( key ) {
case 13: // enter
// shift + enter should add a return
if ( ! shift ) {
event.preventDefault();
event.target.blur();
}
break;
case 27: // escape
event.preventDefault();
this.revert_edits( event );
event.target.blur();
break;
}
},
/**
* Open the WP media lib
*
* @param obj event js event object
* @return void
* @since 3.16.0
* @version 3.16.6
*/
open_media_lib: function( event ) {
event.stopPropagation();
var self = this,
$el = $( event.currentTarget );
if ( self.media_lib ) {
self.media_lib.uploader.uploader.param( 'post_id' );
} else {
self.media_lib = wp.media.frames.file_frame = wp.media( {
title: LLMS.l10n.translate( 'Select an image' ),
button: {
text: LLMS.l10n.translate( 'Use this image' ),
},
multiple: false // Set to true to allow multiple files to be selected
} );
self.media_lib.on( 'select', function() {
var size = $el.attr( 'data-image-size' ),
attachment = self.media_lib.state().get( 'selection' ).first().toJSON(),
image = self.model.get( $el.attr( 'data-attribute' ) ),
url;
if ( size && attachment.sizes[ size ] ) {
url = attachment.sizes[ size ].url;
} else {
url = attachment.url;
}
image.set( {
id: attachment.id,
src: url,
} );
} );
}
// This flag is used to protect the media files uploaded via places like the Quiz questions (Picture type)
self.media_lib.uploader.options.uploader.params.llms = 1;
self.media_lib.open();
},
/**
* Click event to remove an image
*
* @param obj event js event obj
* @return voids
* @since 3.16.0
* @version 3.16.0
*/
remove_image: function( event ) {
event.preventDefault();
this.model.get( $( event.currentTarget ).attr( 'data-attribute' ) ).set( {
id: '',
src: '',
} );
},
/**
* Helper to undo changes
* Bound to "escape" key via on_keydown function
*
* @param obj event js event object
* @return void
* @since 3.16.0
* @version 3.16.0
*/
revert_edits: function( event ) {
var $el = $( event.target ),
val = $el.attr( 'data-original-content' );
$el.html( val );
},
/**
* Sync changes to the model and DB
*
* @param {obj} event js event object
* @return void
* @since 3.16.0
* @version 3.16.0
*/
save_edits: function( event ) {
var $el = $( event.target ),
val = this.get_content( $el );
this.model.set( $el.attr( 'data-attribute' ), val );
},
/**
* Change event for a switch element
*
* @param obj event js event object
* @return void
* @since 3.16.0
* @version 3.17.0
*/
toggle_switch: function( event ) {
event.stopPropagation();
var $el = $( event.target ),
attr = $el.attr( 'name' ),
rerender = $el.attr( 'data-rerender' ),
val;
if ( $el.is( ':checked' ) ) {
val = $el.attr( 'data-on' ) ? $el.attr( 'data-on' ) : 'yes';
} else {
val = $el.attr( 'data-off' ) ? $el.attr( 'data-off' ) : 'no';
}
if ( -1 !== attr.indexOf( '.' ) ) {
var split = attr.split( '.' );
if ( 'parent' === split[0] ) {
this.model.get_parent().set( split[1], val );
} else {
this.model.get( split[0] ).set( split[1], val );
}
} else {
this.model.set( attr, val );
}
this.trigger( attr.replace( '.', '-' ) + '_toggle', val );
if ( ! rerender || 'yes' === rerender ) {
var self = this;
setTimeout( function() {
self.render();
}, 100 );
}
},
/**
* Initializes a WP Editor on a textarea
*
* @since 3.16.0
* @since 3.37.11 Replace reference to `wp.editor` with `_.getEditor()` helper.
*
* @param {String} id CSS ID of the editor (don't include #).
* @param {Object} settings Optional object of settings to pass to wp.oldEditor.initialize().
* @return {Void}
*/
init_editor: function( id, settings ) {
settings = settings || {};
var editor = _.getEditor();
editor.remove( id );
editor.initialize( id, $.extend( true, editor.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',
setup: _.bind( this.on_editor_ready, this ),
}
}, settings ) );
},
/**
* Setup a permalink editor to allow editing of a permalink
*
* @param obj event js event object
* @return void
* @since 3.16.6
* @version 3.16.6
*/
make_slug_editable: function( event ) {
var self = this,
$btn = $( event.currentTarget ),
$link = $btn.prevAll( 'a' ),
$input = $btn.prev( 'input.permalink' ),
full_url = $link.attr( 'href' ),
slug = $input.val(),
short_url = full_url.replace( slug, '' );
// hide the button
$btn.hide();
// make the link not clickable
$link.css( {
color: '#999',
'pointer-events': 'none',
'text-decoration': 'none',
} );
// remove the current slug & trailing slash from the URL
$link.text( short_url.substring( 0, short_url.length - 1 ) );
// focus in on the field
$input.show().focus();
},
/**
* Callback function called after initialization of an editor
*
* Updates UI if a label is present.
*
* Binds a change event to ensure editor changes are saved to the model.
*
* @since 3.16.0
* @since 3.17.1 Uknown.
* @since 3.37.11 Replace references to `wp.editor` with `_.getEditor()` helper.
*
* @param {Object} editor TinyMCE Editor instance.
* @return {Void}
*/
on_editor_ready: function( editor ) {
var self = this,
$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, _.getEditor().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 );
} );
},
_validate_url: function( str ) {
var a = document.createElement( 'a' );
a.href = str;
return ( a.host && a.host !== window.location.host );
}
};
} );