-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathSync.js
More file actions
767 lines (577 loc) · 20.1 KB
/
Sync.js
File metadata and controls
767 lines (577 loc) · 20.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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
/**
* Sync builder data to the server
*
* @since 3.16.0
* @version 4.17.0
*/
define( [], function() {
return function( Course, settings ) {
this.saving = false;
var self = this,
autosave = ( 'yes' === window.llms_builder.autosave ),
check_interval = null,
check_interval_ms = settings.check_interval_ms || ( ( 'yes' === window.llms_builder.autosave ) ? 10000 : 1000 ),
detached = new Backbone.Collection(),
trashed = new Backbone.Collection();
/**
* init
*
* @since 3.16.7
*
* @return {Void}
*/
function init() {
// determine if autosaving is possible
if ( 'undefined' === typeof wp.heartbeat ) {
window.llms_builder.debug.log( 'WordPress Heartbeat disabled. Autosaving is disabled!' );
autosave = false;
}
// setup the check interval
if ( check_interval_ms ) {
self.set_check_interval( check_interval_ms );
}
// warn when users attempt to leave the page
$( window ).on( 'beforeunload', function() {
if ( self.has_unsaved_changes() ) {
check_for_changes();
return 'Are you sure you want to abandon your changes?';
}
} );
};
/*
/$$ /$$ /$$ /$$
|__/ | $$ | $$ |__/
/$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ | $$ /$$$$$$ /$$$$$$ /$$
| $$| $$__ $$|_ $$_/ /$$__ $$ /$$__ $$| $$__ $$ |____ $$| $$ |____ $$ /$$__ $$| $$
| $$| $$ \ $$ | $$ | $$$$$$$$| $$ \__/| $$ \ $$ /$$$$$$$| $$ /$$$$$$$| $$ \ $$| $$
| $$| $$ | $$ | $$ /$$| $$_____/| $$ | $$ | $$ /$$__ $$| $$ /$$__ $$| $$ | $$| $$
| $$| $$ | $$ | $$$$/| $$$$$$$| $$ | $$ | $$| $$$$$$$| $$ | $$$$$$$| $$$$$$$/| $$
|__/|__/ |__/ \___/ \_______/|__/ |__/ |__/ \_______/|__/ \_______/| $$____/ |__/
| $$
| $$
|__/
*/
/**
* Adds error message(s) to the data object returned by heartbeat-tick
*
* @param obj data llms_builder data object from heartbeat-tick
* @param string|array err error messages array or string
* @return obj
* @since 3.16.0
* @version 3.16.0
*/
function add_error_msg( data, err ) {
if ( 'success' === data.status ) {
data.message = [];
}
data.status = 'error';
if ( 'string' === typeof err ) {
err = [ err ];
}
data.message = data.message.concat( err );
return data;
};
/**
* Publish sync status so other areas of the application can see what's happening here
*
* @return void
* @since 3.16.0
* @version 3.16.0
*/
function check_for_changes() {
var data = {};
data.changes = self.get_unsaved_changes();
data.has_unsaved_changes = self.has_unsaved_changes( data.changes );
data.saving = self.saving;
window.llms_builder.debug.log( '==== start changes check ====', data, '==== finish changes check ====' );
Backbone.pubSub.trigger( 'current-save-status', data );
};
/**
* Manually Save data via Admin AJAX when the heartbeat API has been disabled
*
* @since 3.16.7
* @since 4.17.0 Fixed undefined variable error when logging an error response.
*
* @return void
*/
function do_ajax_save() {
// prevent simultaneous saves
if ( self.saving ) {
return;
}
var changes = self.get_unsaved_changes();
// only send data if we have data to send
if ( self.has_unsaved_changes( changes ) ) {
changes.id = Course.get( 'id' );
LLMS.Ajax.call( {
data: {
action: 'llms_builder',
action_type: 'ajax_save',
course_id: changes.id,
llms_builder: JSON.stringify( changes ),
},
beforeSend: function() {
window.llms_builder.debug.log( '==== start do_ajax_save before ====', changes, '==== finish do_ajax_save before ====' );
self.saving = true;
Backbone.pubSub.trigger( 'heartbeat-send', self );
},
error: function( xhr, status, error ) {
window.llms_builder.debug.log( '==== start do_ajax_save error ====', xhr, '==== finish do_ajax_save error ====' );
self.saving = false;
Backbone.pubSub.trigger( 'heartbeat-tick', self, {
status: 'error',
message: xhr.responseText + ' (' + error + ' ' + status + ')',
} );
},
success: function( res ) {
if ( ! res.llms_builder ) {
return;
}
window.llms_builder.debug.log( '==== start do_ajax_save success ====', res, '==== finish do_ajax_save success ====' );
res.llms_builder = process_removals( res.llms_builder );
res.llms_builder = process_updates( res.llms_builder );
self.saving = false;
Backbone.pubSub.trigger( 'heartbeat-tick', self, res.llms_builder );
}
} );
}
};
/**
* Retrieve all the attributes changed on a model since the last sync
*
* For a new model (a model with a temp ID) or a model where _forceSync has been defined ALL atts will be returned
* For an existing model (without a temp ID) only retrieves changed attributes as tracked by Backbone.TrackIt
*
* This function excludes any attributes defined as child attributes via the models relationship settings
*
* @param obj model instance of a Backbone.Model
* @return obj
* @since 3.16.0
* @version 3.16.6
*/
function get_changed_attributes( model ) {
var atts = {},
sync_type;
// don't save mid editing
if ( model.get( '_has_focus' ) ) {
return atts;
}
// model hasn't been persisted to the database to get a real ID yet
// send *all* of it's atts
if ( has_temp_id( model ) || true === model.get( '_forceSync' ) ) {
atts = _.clone( model.attributes );
sync_type = 'full';
// only send the changed atts
} else {
atts = model.unsavedAttributes();
sync_type = 'partial';
}
var exclude = ( model.get_relationships ) ? model.get_child_props() : [];
atts = _.omit( atts, function( val, key ) {
// exclude keys that start with an underscore which are used by the
// application but don't need to be stored in the database
if ( 0 === key.indexOf( '_' ) ) {
return true;
} else if ( -1 !== exclude.indexOf( key ) ) {
return true;
}
return false;
} );
if ( model.before_save ) {
atts = model.before_save( atts, sync_type );
}
return atts;
};
/**
* Get all the changes to an object (either a Model or a Collection of models)
* Returns only changes to models and the IDs of that model (should changes exist)
* Uses get_changed_attributes() to determine if all atts or only changed atts are needed
* Processes children intelligently to only return changed children rather than the entire collection of children
*
* @param obj object instance of a Backbone.Model or Backbone.Collection
* @return obj|array if object is a model, returns an object
* if object is a collection, returns an array of objects
* @since 3.16.0
* @version 3.16.11
*/
function get_changes_to_object( object ) {
var changed_atts;
if ( object instanceof Backbone.Model ) {
changed_atts = get_changed_attributes( object );
if ( object.get_relationships ) {
_.each( object.get_child_props(), function( prop ) {
var children = get_changes_to_object( object.get( prop ) );
if ( ! _.isEmpty( children ) ) {
changed_atts[ prop ] = children;
}
} );
}
// if we have any data, add the id to the model
if ( ! _.isEmpty( changed_atts ) ) {
changed_atts.id = object.get( 'id' );
}
} else if ( object instanceof Backbone.Collection ) {
changed_atts = [];
object.each( function( model ) {
var model_changes = get_changes_to_object( model );
if ( ! _.isEmpty( model_changes ) ) {
changed_atts.push( model_changes );
}
} );
}
return changed_atts;
};
/**
* Determines if a model has a temporary ID or a real persisted ID
*
* @param obj model instance of a model
* @return boolean
* @since 3.16.0
* @version 3.16.0
*/
function has_temp_id( model ) {
return ( ! _.isNumber( model.id ) && 0 === model.id.indexOf( 'temp_' ) );
};
/**
* Compares changes synced to the server against current model and restarts
* tracking on elements that haven't changed since the last sync
*
* @param obj model instance of a Backbone.Model
* @param obj data data set that was processed by the server
* @return void
* @since 3.16.11
* @version 3.19.4
*/
function maybe_restart_tracking( model, data ) {
Backbone.pubSub.trigger( model.get( 'type' ) + '-maybe-restart-tracking', model, data );
var omit = [ 'id', 'orig_id' ];
if ( model.get_relationships ) {
omit.concat( model.get_child_props() );
}
_.each( _.omit( data, omit ), function( val, prop ) {
if ( _.isEqual( model.get( prop ), val ) ) {
delete model._unsavedChanges[ prop ];
model._originalAttrs[ prop ] = val;
}
} );
// if syncing was forced, allow tracking to move forward as normal moving forward
model.unset( '_forceSync' );
};
/**
* Processes response data from heartbeat-tick related to trashing & detaching models
* On success, removes from local removal collection
* On error, appends error messages to the data object returned to UI for on-screen feedback
*
* @param obj data data.llms_builder object from heartbeat-tick response
* @return obj
* @since 3.16.0
* @version 3.17.1
*/
function process_removals( data ) {
// check removals for errors
var removals = {
detach: detached,
trash: trashed,
};
_.each( removals, function( coll, key ) {
if ( data[ key ] ) {
var errors = [];
_.each( data[ key ] , function( info ) {
// successfully detached, remove it from the detached collection
if ( ! info.error ) {
coll.remove( info.id );
} else {
errors.push( info.error );
}
} );
if ( errors.length ) {
_.extend( data, add_error_msg( data, errors ) );
}
}
} );
return data;
}
/**
* Processes response data from heartbeat-tick related to creating / updating a single object
* Handles both collections and models as a recursive function
*
* @param {[type]} data [description]
* @param {[type]} type [description]
* @param {[type]} parent [description]
* @param {[type]} main_data [description]
* @return {[type]}
* @since 3.16.0
* @version 3.16.11
*/
function process_object_updates( data, type, parent, main_data ) {
if ( ! data[ type ] ) {
return data;
}
if ( parent.get( type ) instanceof Backbone.Model ) {
var info = data[ type ];
if ( info.error ) {
_.extend( main_data, add_error_msg( main_data, info.error ) );
} else {
var model = parent.get( type );
// update temp ids with the real id
if ( info.id != info.orig_id ) {
model.set( 'id', info.id );
delete model._unsavedChanges.id;
}
maybe_restart_tracking( model, info );
// check children
if ( model.get_relationships ) {
_.each( model.get_child_props(), function( child_key ) {
_.extend( data[ type ], process_object_updates( data[ type ], child_key, model, main_data ) );
} );
}
}
} else if ( parent.get( type ) instanceof Backbone.Collection ) {
_.each( data[ type ], function( info, index ) {
if ( info.error ) {
_.extend( main_data, add_error_msg( main_data, info.error ) );
} else {
var model = parent.get( type ).get( info.orig_id );
// update temp ids with the real id
if ( info.id != info.orig_id ) {
model.set( 'id', info.id );
delete model._unsavedChanges.id;
}
maybe_restart_tracking( model, info );
// check children
if ( model.get_relationships ) {
_.each( model.get_child_props(), function( child_key ) {
_.extend( data[ type ], process_object_updates( data[ type ][ index ], child_key, model, main_data ) );
} );
}
}
} );
}
return main_data;
};
/**
* Processes response data from heartbeat-tick related to updating & creating new models
* On success, removes from local removal collection
* On error, appends error messages to the data object returned to UI for on-screen feedback
*
* @param obj data data.llms_builder object from heartbeat-tick response
* @return obj
* @since 3.16.0
* @version 3.16.0
*/
function process_updates( data ) {
// only mess with updates data
if ( ! data.updates ) {
return data;
}
if ( data.updates ) {
data = process_object_updates( data.updates, 'sections', Course, data );
}
return data;
};
/*
/$$ /$$ /$$ /$$
| $$ | $$|__/ |__/
/$$$$$$ /$$ /$$| $$$$$$$ | $$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$
/$$__ $$| $$ | $$| $$__ $$| $$| $$ /$$_____/ |____ $$ /$$__ $$| $$
| $$ \ $$| $$ | $$| $$ \ $$| $$| $$| $$ /$$$$$$$| $$ \ $$| $$
| $$ | $$| $$ | $$| $$ | $$| $$| $$| $$ /$$__ $$| $$ | $$| $$
| $$$$$$$/| $$$$$$/| $$$$$$$/| $$| $$| $$$$$$$ | $$$$$$$| $$$$$$$/| $$
| $$____/ \______/ |_______/ |__/|__/ \_______/ \_______/| $$____/ |__/
| $$ | $$
| $$ | $$
|__/ |__/
*/
/**
* Retrieve all unsaved changes for the builder instance
*
* @return obj
* @since 3.16.0
* @version 3.17.1
*/
this.get_unsaved_changes = function() {
return {
detach: detached.pluck( 'id' ),
trash: trashed.pluck( 'id' ),
updates: get_changes_to_object( Course ),
}
};
/**
* Check if the builder instance has unsaved changes
*
* @param obj changes optionally pass in an object from the return of this.get_unsaved_changes()
* save some resources by not running the check twice during heartbeats
* @return boolean
* @since 3.16.0
* @version 3.16.0
*/
this.has_unsaved_changes = function( changes ) {
if ( 'undefined' === typeof changes ) {
changes = self.get_unsaved_changes();
}
// check all possible keys, once we find one with content we have some changes to persist
var found = _.find( changes, function( data ) {
return ( false === _.isEmpty( data ) );
} );
return found ? true : false;
};
/**
* Save changes right now.
*
* @return void
* @since 3.16.0
* @version 3.16.7
*/
this.save_now = function() {
if ( autosave ) {
wp.heartbeat.connectNow();
} else {
do_ajax_save();
}
};
/**
* Update the interval that checks for changes to the builder instance
*
* @param int ms time (in milliseconds) to run the check on
* pass 0 to disable the check
* @return void
* @since 3.16.0
* @version 3.16.0
*/
this.set_check_interval = function( ms ) {
check_interval_ms = ms;
if ( check_interval ) {
clearInterval( check_interval );
}
if ( check_interval_ms ) {
check_interval = setInterval( check_for_changes, check_interval_ms );
}
};
/*
/$$ /$$ /$$
| $$|__/ | $$
| $$ /$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$
| $$| $$ /$$_____/|_ $$_/ /$$__ $$| $$__ $$ /$$__ $$ /$$__ $$ /$$_____/
| $$| $$| $$$$$$ | $$ | $$$$$$$$| $$ \ $$| $$$$$$$$| $$ \__/| $$$$$$
| $$| $$ \____ $$ | $$ /$$| $$_____/| $$ | $$| $$_____/| $$ \____ $$
| $$| $$ /$$$$$$$/ | $$$$/| $$$$$$$| $$ | $$| $$$$$$$| $$ /$$$$$$$/
|__/|__/|_______/ \___/ \_______/|__/ |__/ \_______/|__/ |_______/
*/
/**
* Listen for detached models and send them to the server for persistence
*
* @since 3.16.0
* @version 3.16.0
*/
Backbone.pubSub.on( 'model-detached', function( model ) {
// detached models with temp ids haven't been persisted so we don't care
if ( has_temp_id( model ) ) {
return;
}
detached.add( _.clone( model.attributes ) );
} );
/**
* Listen for trashed models and send them to the server for deletion
*
* @since 3.16.0
* @version 3.17.1
*/
Backbone.pubSub.on( 'model-trashed', function( model ) {
// if the model has a temp ID we don't have to persist the deletion
if ( has_temp_id( model ) ) {
return;
}
var data = _.clone( model.attributes );
if ( model.get_trash_id ) {
data.id = model.get_trash_id();
}
trashed.add( data );
} );
/*
/$$ /$$ /$$ /$$
| $$ | $$ | $$ | $$
| $$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ | $$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$
| $$__ $$ /$$__ $$ |____ $$ /$$__ $$|_ $$_/ | $$__ $$ /$$__ $$ |____ $$|_ $$_/
| $$ \ $$| $$$$$$$$ /$$$$$$$| $$ \__/ | $$ | $$ \ $$| $$$$$$$$ /$$$$$$$ | $$
| $$ | $$| $$_____/ /$$__ $$| $$ | $$ /$$| $$ | $$| $$_____/ /$$__ $$ | $$ /$$
| $$ | $$| $$$$$$$| $$$$$$$| $$ | $$$$/| $$$$$$$/| $$$$$$$| $$$$$$$ | $$$$/
|__/ |__/ \_______/ \_______/|__/ \___/ |_______/ \_______/ \_______/ \___/
*/
/**
* Add data to the WP heartbeat to persist new models, changes, and deletions to the DB
*
* @since 3.16.0
* @since 3.16.7 Unknown
* @since 4.14.0 Return early when autosaving is disabled.
*/
$( document ).on( 'heartbeat-send', function( event, data ) {
// Autosaving is disabled.
if ( ! autosave ) {
return;
}
// prevent simultaneous saves
if ( self.saving ) {
return;
}
var changes = self.get_unsaved_changes();
// only send data if we have data to send
if ( self.has_unsaved_changes( changes ) ) {
changes.id = Course.get( 'id' );
self.saving = true;
data.llms_builder = JSON.stringify( changes );
}
window.llms_builder.debug.log( '==== start heartbeat-send ====', data, '==== finish heartbeat-send ====' );
Backbone.pubSub.trigger( 'heartbeat-send', self );
} );
/**
* Confirm detachments & deletions and replace temp IDs with new persisted IDs
*
* @since 3.16.0
* @since 4.14.0 Return early when autosaving is disabled.
*/
$( document ).on( 'heartbeat-tick', function( event, data ) {
// Autosaving is disabled.
if ( ! autosave ) {
return;
}
if ( ! data.llms_builder ) {
return;
}
window.llms_builder.debug.log( '==== start heartbeat-tick ====', data, '==== finish heartbeat-tick ====' );
data.llms_builder = process_removals( data.llms_builder );
data.llms_builder = process_updates( data.llms_builder );
self.saving = false;
Backbone.pubSub.trigger( 'heartbeat-tick', self, data.llms_builder );
} );
/**
* On heartbeat errors publish an error to the main builder application
*
* @since 3.16.0
* @since 4.14.0 Return early when autosaving is disabled.
*/
$( document ).on( 'heartbeat-error', function( event, data ) {
// Autosaving is disabled.
if ( ! autosave ) {
return;
}
window.llms_builder.debug.log( '==== start heartbeat-error ====', data, '==== finish heartbeat-error ====' );
self.saving = false;
Backbone.pubSub.trigger( 'heartbeat-tick', self, {
status: 'error',
message: data.responseText + ' (' + data.status + ' ' + data.statusText + ')',
} );
} );
/*
/$$ /$$ /$$
|__/ |__/ | $$
/$$ /$$$$$$$ /$$ /$$$$$$
| $$| $$__ $$| $$|_ $$_/
| $$| $$ \ $$| $$ | $$
| $$| $$ | $$| $$ | $$ /$$
| $$| $$ | $$| $$ | $$$$/
|__/|__/ |__/|__/ \___/
*/
init();
return this;
};
} );