Changeset 47120
- Timestamp:
- 01/29/2020 12:35:39 AM (6 years ago)
- File:
-
- 1 edited
-
trunk/src/js/_enqueues/wp/revisions.js (modified) (42 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/js/_enqueues/wp/revisions.js
r43347 r47120 20 20 revisions.settings = window._wpRevisionsSettings || {}; 21 21 22 // For debugging 22 // For debugging. 23 23 revisions.debug = false; 24 24 … … 35 35 }; 36 36 37 // Handy functions to help with positioning 37 // Handy functions to help with positioning. 38 38 $.fn.allOffsets = function() { 39 39 var offset = this.offset() || {top: 0, left: 0}, win = $(window); … … 72 72 this.revisions = options.revisions; 73 73 74 // Listen for changes to the revisions or mode from outside 74 // Listen for changes to the revisions or mode from outside. 75 75 this.listenTo( this.frame, 'update:revisions', this.receiveRevisions ); 76 76 this.listenTo( this.frame, 'change:compareTwoMode', this.updateMode ); 77 77 78 // Listen for internal changes 78 // Listen for internal changes. 79 79 this.on( 'change:from', this.handleLocalChanges ); 80 80 this.on( 'change:to', this.handleLocalChanges ); … … 82 82 this.on( 'update:revisions', this.updateSliderSettings ); 83 83 84 // Listen for changes to the hovered revision 84 // Listen for changes to the hovered revision. 85 85 this.on( 'change:hoveredRevision', this.hoverRevision ); 86 86 … … 106 106 ], 107 107 value: null, 108 range: true // ensures handles cannot cross108 range: true // Ensures handles cannot cross. 109 109 }); 110 110 } else { … … 118 118 }, 119 119 120 // Called when a revision is hovered 120 // Called when a revision is hovered. 121 121 hoverRevision: function( model, value ) { 122 122 this.trigger( 'hovered:revision', value ); 123 123 }, 124 124 125 // Called when `compareTwoMode` changes 125 // Called when `compareTwoMode` changes. 126 126 updateMode: function( model, value ) { 127 127 this.set({ compareTwoMode: value }); 128 128 }, 129 129 130 // Called when `from` or `to` changes in the local model 130 // Called when `from` or `to` changes in the local model. 131 131 handleLocalChanges: function() { 132 132 this.frame.set({ … … 136 136 }, 137 137 138 // Receives revisions changes from outside the model 138 // Receives revisions changes from outside the model. 139 139 receiveRevisions: function( from, to ) { 140 // Bail if nothing changed 140 // Bail if nothing changed. 141 141 if ( this.get('from') === from && this.get('to') === to ) { 142 142 return; … … 153 153 revision: null, 154 154 offset: {}, 155 hovering: false, // Whether the mouse is hovering 156 scrubbing: false // Whether the mouse is scrubbing 155 hovering: false, // Whether the mouse is hovering. 156 scrubbing: false // Whether the mouse is scrubbing. 157 157 }, 158 158 … … 256 256 this.trigger( 'ensure:load', ids, from, to, deferred.promise() ); 257 257 _.each( ids, _.bind( function( id ) { 258 // Remove anything that has an ongoing request 258 // Remove anything that has an ongoing request. 259 259 if ( this.requests[ id ] ) { 260 260 delete ids[ id ]; 261 261 } 262 // Remove anything we already have 262 // Remove anything we already have. 263 263 if ( this.get( id ) ) { 264 264 delete ids[ id ]; … … 266 266 }, this ) ); 267 267 if ( ! request ) { 268 // Always include the ID that started this ensure 268 // Always include the ID that started this ensure. 269 269 ids[ id ] = true; 270 270 request = this.load( _.keys( ids ) ); … … 281 281 }, 282 282 283 // Returns an array of proximal diffs 283 // Returns an array of proximal diffs. 284 284 getClosestUnloaded: function( ids, centerId ) { 285 285 var self = this; … … 304 304 if ( 1 === num ) { // Already tried 1. This just isn't working. Give up. 305 305 deferred.reject(); 306 } else { // Request fewer diffs this time 306 } else { // Request fewer diffs this time. 307 307 self._loadAll( allRevisionIds, centerId, Math.ceil( num / 2 ) ).done( function() { 308 308 deferred.resolve(); … … 318 318 load: function( comparisons ) { 319 319 wp.revisions.log( 'load', comparisons ); 320 // Our collection should only ever grow, never shrink, so remove: false320 // Our collection should only ever grow, never shrink, so `remove: false`. 321 321 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() { 322 322 wp.revisions.log( 'load:complete', comparisons ); … … 395 395 this.diffs.set( this.get( 'diffData' ) ); 396 396 397 // Set up internal listeners 397 // Set up internal listeners. 398 398 this.listenTo( this, 'change:from', this.changeRevisionHandler ); 399 399 this.listenTo( this, 'change:to', this.changeRevisionHandler ); … … 411 411 } ); 412 412 413 // Start the router if browser supports History API 413 // Start the router if browser supports History API. 414 414 if ( window.history && window.history.pushState ) { 415 415 this.router = new revisions.Router({ model: this }); … … 430 430 431 431 // If we were on the first revision before switching to two-handled mode, 432 // bump the 'to' position over one 432 // bump the 'to' position over one. 433 433 if ( value && 0 === toIndex ) { 434 434 this.set({ … … 439 439 440 440 // When switching back to single-handled mode, reset 'from' model to 441 // one position before the 'to' model 442 if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode 441 // one position before the 'to' model. 442 if ( ! value && 0 !== toIndex ) { // '! value' means switching to single-handled mode. 443 443 this.set({ 444 444 from: this.revisions.at( toIndex - 1 ), … … 450 450 updatedRevisions: function( from, to ) { 451 451 if ( this.get( 'compareTwoMode' ) ) { 452 // TODO: compare-two loading strategy452 // @todo Compare-two loading strategy. 453 453 } else { 454 454 this.diffs.loadAll( this.revisions.pluck('id'), to.id, 40 ); … … 461 461 }, 462 462 463 // So long as `from` and `to` are changed at the same time, the diff 464 // will only be updated once. This is because Backbone updates all of 465 // the changed attributes in `set`, and then fires the `change` events. 463 /* 464 * So long as `from` and `to` are changed at the same time, the diff 465 * will only be updated once. This is because Backbone updates all of 466 * the changed attributes in `set`, and then fires the `change` events. 467 */ 466 468 updateDiff: function( options ) { 467 469 var from, to, diffId, diff; … … 510 512 error: true 511 513 }); 512 } else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change 514 } else if ( this._diffId === diff.id ) { // Make sure the current diff didn't change. 513 515 this.trigger( 'update:diff', diff ); 514 516 } … … 594 596 _.bindAll( this, 'setWidth' ); 595 597 596 // Add the button view 598 // Add the button view. 597 599 this.views.add( new revisions.view.Buttons({ 598 600 model: this.model 599 601 }) ); 600 602 601 // Add the checkbox view 603 // Add the checkbox view. 602 604 this.views.add( new revisions.view.Checkbox({ 603 605 model: this.model 604 606 }) ); 605 607 606 // Prep the slider model 608 // Prep the slider model. 607 609 var slider = new revisions.model.Slider({ 608 610 frame: this.model, … … 610 612 }), 611 613 612 // Prep the tooltip model 614 // Prep the tooltip model. 613 615 tooltip = new revisions.model.Tooltip({ 614 616 frame: this.model, … … 617 619 }); 618 620 619 // Add the tooltip view 621 // Add the tooltip view. 620 622 this.views.add( new revisions.view.Tooltip({ 621 623 model: tooltip 622 624 }) ); 623 625 624 // Add the tickmarks view 626 // Add the tickmarks view. 625 627 this.views.add( new revisions.view.Tickmarks({ 626 628 model: tooltip 627 629 }) ); 628 630 629 // Add the slider view 631 // Add the slider view. 630 632 this.views.add( new revisions.view.Slider({ 631 633 model: slider 632 634 }) ); 633 635 634 // Add the Metabox view 636 // Add the Metabox view. 635 637 this.views.add( new revisions.view.Metabox({ 636 638 model: this.model … … 673 675 }); 674 676 675 // The tickmarks view 677 // The tickmarks view. 676 678 revisions.view.Tickmarks = wp.Backbone.View.extend({ 677 679 className: 'revisions-tickmarks', … … 687 689 parentOffset = this.$el.parent().allOffsets(); 688 690 if ( index === this.model.revisions.length - 1 ) { 689 // Last one 691 // Last one. 690 692 offset = { 691 693 rightPlusWidth: thisOffset.left - parentOffset.left + 1, … … 693 695 }; 694 696 } else { 695 // Normal tick 697 // Normal tick. 696 698 tick = this.$('div:nth-of-type(' + (index + 1) + ')'); 697 699 offset = tick.allPositions(); … … 720 722 }); 721 723 722 // The metabox view 724 // The metabox view. 723 725 revisions.view.Metabox = wp.Backbone.View.extend({ 724 726 className: 'revisions-meta', 725 727 726 728 initialize: function() { 727 // Add the 'from' view 729 // Add the 'from' view. 728 730 this.views.add( new revisions.view.MetaFrom({ 729 731 model: this.model, … … 731 733 }) ); 732 734 733 // Add the 'to' view 735 // Add the 'to' view. 734 736 this.views.add( new revisions.view.MetaTo({ 735 737 model: this.model … … 738 740 }); 739 741 740 // The revision meta view (to be extended) 742 // The revision meta view (to be extended). 741 743 revisions.view.Meta = wp.Backbone.View.extend({ 742 744 template: wp.template('revisions-meta'), … … 761 763 }); 762 764 763 // The revision meta 'from' view 765 // The revision meta 'from' view. 764 766 revisions.view.MetaFrom = revisions.view.Meta.extend({ 765 767 className: 'diff-meta diff-meta-from', … … 767 769 }); 768 770 769 // The revision meta 'to' view 771 // The revision meta 'to' view. 770 772 revisions.view.MetaTo = revisions.view.Meta.extend({ 771 773 className: 'diff-meta diff-meta-to', … … 881 883 }, 882 884 883 // Go to a specific model index 885 // Go to a specific model index. 884 886 gotoModel: function( toIndex ) { 885 887 var attributes = { … … 896 898 }, 897 899 898 // Go to the 'next' revision 900 // Go to the 'next' revision. 899 901 nextRevision: function() { 900 902 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) + 1; … … 902 904 }, 903 905 904 // Go to the 'previous' revision 906 // Go to the 'previous' revision. 905 907 previousRevision: function() { 906 908 var toIndex = this.model.revisions.indexOf( this.model.get('to') ) - 1; … … 957 959 958 960 mouseMove: function( e ) { 959 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models960 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider 961 sliderWidth = this.$el.width(), // Width of slider962 tickWidth = sliderWidth / zoneCount, // Calculated width of zone963 actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom ;964 currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index961 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models. 962 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider. 963 sliderWidth = this.$el.width(), // Width of slider. 964 tickWidth = sliderWidth / zoneCount, // Calculated width of zone. 965 actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom. 966 currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index. 965 967 966 968 // Ensure sane value for currentModelIndex. … … 971 973 } 972 974 973 // Update the tooltip mode 975 // Update the tooltip mode. 974 976 this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) }); 975 977 }, … … 988 990 989 991 if ( this.model.get('compareTwoMode') ) { 990 // in RTL mode the 'left handle' is the second in the slider, 'right' is first992 // In RTL mode the 'left handle' is the second in the slider, 'right' is first. 991 993 handles.first() 992 994 .toggleClass( 'to-handle', !! isRtl ) … … 1020 1022 if ( view.model.get('compareTwoMode') ) { 1021 1023 handles = handle.parent().find('.ui-slider-handle'); 1022 if ( handle.is( handles.first() ) ) { // We're the left handle 1024 if ( handle.is( handles.first() ) ) { 1025 // We're the left handle. 1023 1026 rightDragBoundary = handles.last().offset().left; 1024 1027 rightDragReset = rightDragBoundary - sliderOffset; 1025 } else { // We're the right handle 1028 } else { 1029 // We're the right handle. 1026 1030 leftDragBoundary = handles.first().offset().left + handles.first().width(); 1027 1031 leftDragReset = leftDragBoundary - sliderOffset; … … 1044 1048 }, 1045 1049 1046 // Responds to slide events 1050 // Responds to slide events. 1047 1051 slide: function( event, ui ) { 1048 1052 var attributes, movedRevision; 1049 // Compare two revisions mode 1053 // Compare two revisions mode. 1050 1054 if ( this.model.get('compareTwoMode') ) { 1051 // Prevent sliders from occupying same spot 1055 // Prevent sliders from occupying same spot. 1052 1056 if ( ui.values[1] === ui.values[0] ) { 1053 1057 return false; … … 1073 1077 movedRevision = this.model.revisions.at( this.getPosition( ui.value ) ); 1074 1078 1075 // If we are scrubbing, a scrub to a revision is considered a hover 1079 // If we are scrubbing, a scrub to a revision is considered a hover. 1076 1080 if ( this.model.get('scrubbing') ) { 1077 1081 attributes.hoveredRevision = movedRevision; … … 1083 1087 stop: function() { 1084 1088 $( window ).off('mousemove.wp.revisions'); 1085 this.model.updateSliderSettings(); // To snap us back to a tick mark 1089 this.model.updateSliderSettings(); // To snap us back to a tick mark. 1086 1090 this.model.set({ scrubbing: false }); 1087 1091 } … … 1106 1110 this.model = options.model; 1107 1111 1108 // Maintain state and history when navigating 1112 // Maintain state and history when navigating. 1109 1113 this.listenTo( this.model, 'update:diff', _.debounce( this.updateUrl, 250 ) ); 1110 1114 this.listenTo( this.model, 'change:compareTwoMode', this.updateUrl );
Note: See TracChangeset
for help on using the changeset viewer.