Ticket #25864: 25864.diff
File 25864.diff, 13.9 KB (added by , 10 years ago) |
---|
-
src/wp-admin/js/revisions.js
1 /*global _, Backbone, _wpRevisionsSettings */ 1 2 window.wp = window.wp || {}; 2 3 3 4 (function($) { … … 12 13 revisions.debug = false; 13 14 14 15 revisions.log = function() { 15 if ( window.console && revisions.debug ) 16 console.log.apply( console, arguments ); 16 if ( window.console && revisions.debug ) { 17 window.console.log.apply( window.console, arguments ); 18 } 17 19 }; 18 20 19 21 // Handy functions to help with positioning … … 34 36 }; 35 37 36 38 // wp_localize_script transforms top-level numbers into strings. Undo that. 37 if ( revisions.settings.to ) 39 if ( revisions.settings.to ) { 38 40 revisions.settings.to = parseInt( revisions.settings.to, 10 ); 39 if ( revisions.settings.from ) 41 } 42 if ( revisions.settings.from ) { 40 43 revisions.settings.from = parseInt( revisions.settings.from, 10 ); 44 } 41 45 42 46 // wp_localize_script does not allow for top-level booleans. Fix that. 43 if ( revisions.settings.compareTwoMode ) 47 if ( revisions.settings.compareTwoMode ) { 44 48 revisions.settings.compareTwoMode = revisions.settings.compareTwoMode === '1'; 49 } 45 50 46 51 /** 47 52 * ======================================================================== … … 130 135 // Receives revisions changes from outside the model 131 136 receiveRevisions: function( from, to ) { 132 137 // Bail if nothing changed 133 if ( this.get('from') === from && this.get('to') === to ) 138 if ( this.get('from') === from && this.get('to') === to ) { 134 139 return; 140 } 135 141 136 142 this.set({ from: from, to: to }, { silent: true }); 137 143 this.trigger( 'update:revisions', from, to ); … … 183 189 next: function( revision ) { 184 190 var index = this.indexOf( revision ); 185 191 186 if ( index !== -1 && index !== this.length - 1 ) 192 if ( index !== -1 && index !== this.length - 1 ) { 187 193 return this.at( index + 1 ); 194 } 188 195 }, 189 196 190 197 prev: function( revision ) { 191 198 var index = this.indexOf( revision ); 192 199 193 if ( index !== -1 && index !== 0 ) 200 if ( index !== -1 && index !== 0 ) { 194 201 return this.at( index - 1 ); 202 } 195 203 } 196 204 }); 197 205 … … 202 210 }); 203 211 204 212 revisions.model.Diff = Backbone.Model.extend({ 205 initialize: function( attributes, options) {213 initialize: function() { 206 214 var fields = this.get('fields'); 207 215 this.unset('fields'); 208 216 … … 221 229 model: revisions.model.Diff, 222 230 223 231 ensure: function( id, context ) { 224 var diff = this.get( id ) ;225 var request = this.requests[ id ];226 var deferred = $.Deferred();227 var ids = {};228 var from = id.split(':')[0];229 varto = id.split(':')[1];232 var diff = this.get( id ), 233 request = this.requests[ id ], 234 deferred = $.Deferred(), 235 ids = {}, 236 from = id.split(':')[0], 237 to = id.split(':')[1]; 230 238 ids[id] = true; 231 239 232 240 wp.revisions.log( 'ensure', id ); … … 239 247 this.trigger( 'ensure:load', ids, from, to, deferred.promise() ); 240 248 _.each( ids, _.bind( function( id ) { 241 249 // Remove anything that has an ongoing request 242 if ( this.requests[ id ] ) 250 if ( this.requests[ id ] ) { 243 251 delete ids[ id ]; 252 } 244 253 // Remove anything we already have 245 if ( this.get( id ) ) 254 if ( this.get( id ) ) { 246 255 delete ids[ id ]; 256 } 247 257 }, this ) ); 248 258 if ( ! request ) { 249 259 // Always include the ID that started this ensure … … 274 284 }, 275 285 276 286 _loadAll: function( allRevisionIds, centerId, num ) { 277 var self = this, deferred = $.Deferred() ;278 diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num );287 var self = this, deferred = $.Deferred(), 288 diffs = _.first( this.getClosestUnloaded( allRevisionIds, centerId ), num ); 279 289 if ( _.size( diffs ) > 0 ) { 280 290 this.load( diffs ).done( function() { 281 291 self._loadAll( allRevisionIds, centerId, num ).done( function() { … … 299 309 load: function( comparisons ) { 300 310 wp.revisions.log( 'load', comparisons ); 301 311 // Our collection should only ever grow, never shrink, so remove: false 302 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() {312 return this.fetch({ data: { compare: comparisons }, remove: false }).done( function() { 303 313 wp.revisions.log( 'load:complete', comparisons ); 304 314 }); 305 315 }, … … 313 323 post_id: revisions.settings.postId 314 324 }); 315 325 316 var deferred = wp.ajax.send( options ) ;317 varrequests = this.requests;326 var deferred = wp.ajax.send( options ), 327 requests = this.requests; 318 328 319 329 // Record that we're requesting each diff. 320 330 if ( options.data.compare ) { … … 423 433 diffId = ( from ? from.id : 0 ) + ':' + to.id; 424 434 425 435 // Check if we're actually changing the diff id. 426 if ( this._diffId === diffId ) 436 if ( this._diffId === diffId ) { 427 437 return $.Deferred().reject().promise(); 438 } 428 439 429 440 this._diffId = diffId; 430 441 this.trigger( 'update:revisions', from, to ); … … 448 459 449 460 // A simple wrapper around `updateDiff` to prevent the change event's 450 461 // parameters from being passed through. 451 changeRevisionHandler: function( model, value, options) {462 changeRevisionHandler: function() { 452 463 this.updateDiff(); 453 464 }, 454 465 … … 545 556 var slider = new revisions.model.Slider({ 546 557 frame: this.model, 547 558 revisions: this.model.revisions 548 }) ;559 }), 549 560 550 561 // Prep the tooltip model 551 vartooltip = new revisions.model.Tooltip({562 tooltip = new revisions.model.Tooltip({ 552 563 frame: this.model, 553 564 revisions: this.model.revisions, 554 565 slider: slider … … 579 590 this.top = this.$el.offset().top; 580 591 this.window = $(window); 581 592 this.window.on( 'scroll.wp.revisions', {controls: this}, function(e) { 582 var controls = e.data.controls;583 var container = controls.$el.parent();584 var scrolled = controls.window.scrollTop();585 var frame= controls.views.parent;593 var controls = e.data.controls, 594 container = controls.$el.parent(), 595 scrolled = controls.window.scrollTop(), 596 frame = controls.views.parent; 586 597 587 598 if ( scrolled >= controls.top ) { 588 599 if ( ! frame.$el.hasClass('pinned') ) { … … 724 735 }, 725 736 726 737 ready: function() { 727 if ( this.model.revisions.length < 3 ) 738 if ( this.model.revisions.length < 3 ) { 728 739 $('.revision-toggle-compare-mode').hide(); 740 } 729 741 }, 730 742 731 743 updateCompareTwoMode: function() { … … 733 745 }, 734 746 735 747 // Toggle the compare two mode feature when the compare two checkbox is checked. 736 compareTwoToggle: function( event) {748 compareTwoToggle: function() { 737 749 // Activate compare two mode? 738 750 this.model.set({ compareTwoMode: $('.compare-two-revisions').prop('checked') }); 739 751 } … … 745 757 className: 'revisions-tooltip', 746 758 template: wp.template('revisions-meta'), 747 759 748 initialize: function( options) {760 initialize: function() { 749 761 this.listenTo( this.model, 'change:offset', this.render ); 750 762 this.listenTo( this.model, 'change:hovering', this.toggleVisibility ); 751 763 this.listenTo( this.model, 'change:scrubbing', this.toggleVisibility ); 752 764 }, 753 765 754 766 prepare: function() { 755 if ( _.isNull( this.model.get('revision') ) ) 767 if ( _.isNull( this.model.get('revision') ) ) { 756 768 return; 757 else769 } else { 758 770 return _.extend( { type: 'tooltip' }, { 759 771 attributes: this.model.get('revision').toJSON() 760 772 }); 773 } 761 774 }, 762 775 763 776 render: function() { 764 var direction, directionVal, flipped, css = {}, position = this.model.revisions.indexOf( this.model.get('revision') ) + 1; 777 var otherDirection, 778 direction, 779 directionVal, 780 flipped, 781 css = {}, 782 position = this.model.revisions.indexOf( this.model.get('revision') ) + 1; 783 765 784 flipped = ( position / this.model.revisions.length ) > 0.5; 766 785 if ( isRtl ) { 767 786 direction = flipped ? 'left' : 'right'; … … 781 800 return this.model.get( 'scrubbing' ) || this.model.get( 'hovering' ); 782 801 }, 783 802 784 toggleVisibility: function( options) {785 if ( this.visible() ) 803 toggleVisibility: function() { 804 if ( this.visible() ) { 786 805 this.$el.stop().show().fadeTo( 100 - this.el.style.opacity * 100, 1 ); 787 else806 } else { 788 807 this.$el.stop().fadeTo( this.el.style.opacity * 300, 0, function(){ $(this).hide(); } ); 808 } 789 809 return; 790 810 } 791 811 }); … … 815 835 to: this.model.revisions.at( toIndex ) 816 836 }; 817 837 // If we're at the first revision, unset 'from'. 818 if ( toIndex ) 838 if ( toIndex ) { 819 839 attributes.from = this.model.revisions.at( toIndex - 1 ); 820 else840 } else { 821 841 this.model.unset('from', { silent: true }); 842 } 822 843 823 844 this.model.set( attributes ); 824 845 }, … … 837 858 838 859 // Check to see if the Previous or Next buttons need to be disabled or enabled. 839 860 disabledButtonCheck: function() { 840 var maxVal = this.model.revisions.length - 1,841 minVal = 0,842 next = $('.revisions-next .button'),861 var maxVal = this.model.revisions.length - 1, 862 minVal = 0, 863 next = $('.revisions-next .button'), 843 864 previous = $('.revisions-previous .button'), 844 val = this.model.revisions.indexOf( this.model.get('to') );865 val = this.model.revisions.indexOf( this.model.get('to') ); 845 866 846 867 // Disable "Next" button if you're on the last node. 847 868 next.prop( 'disabled', ( maxVal === val ) ); … … 884 905 }, 885 906 886 907 mouseMove: function( e ) { 887 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models 888 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider 889 sliderWidth = this.$el.width(), // Width of slider 890 tickWidth = sliderWidth / zoneCount, // Calculated width of zone 891 actualX = isRtl? $(window).width() - e.pageX : e.pageX; // Flipped for RTL - sliderFrom; 892 actualX = actualX - sliderFrom; // Offset of mouse position in slider 893 var currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index 908 var zoneCount = this.model.revisions.length - 1, // One fewer zone than models 909 sliderFrom = this.$el.allOffsets()[this.direction], // "From" edge of slider 910 sliderWidth = this.$el.width(), // Width of slider 911 tickWidth = sliderWidth / zoneCount, // Calculated width of zone 912 actualX = ( isRtl ? $(window).width() - e.pageX : e.pageX ) - sliderFrom, // Flipped for RTL - sliderFrom; 913 currentModelIndex = Math.floor( ( actualX + ( tickWidth / 2 ) ) / tickWidth ); // Calculate the model index 894 914 895 915 // Ensure sane value for currentModelIndex. 896 if ( currentModelIndex < 0 ) 916 if ( currentModelIndex < 0 ) { 897 917 currentModelIndex = 0; 898 else if ( currentModelIndex >= this.model.revisions.length )918 } else if ( currentModelIndex >= this.model.revisions.length ) { 899 919 currentModelIndex = this.model.revisions.length - 1; 920 } 900 921 901 922 // Update the tooltip mode 902 923 this.model.set({ hoveredRevision: this.model.revisions.at( currentModelIndex ) }); … … 940 961 rightDragBoundary = sliderRightEdge, 941 962 leftDragReset = '0', 942 963 rightDragReset = '100%', 964 handles, 943 965 handle = $( ui.handle ); 944 966 945 967 // In two handle mode, ensure handles can't be dragged past each other. 946 968 // Adjust left/right boundaries and reset points. 947 969 if ( view.model.get('compareTwoMode') ) { 948 varhandles = handle.parent().find('.ui-slider-handle');970 handles = handle.parent().find('.ui-slider-handle'); 949 971 if ( handle.is( handles.first() ) ) { // We're the left handle 950 972 rightDragBoundary = handles.last().offset().left; 951 973 rightDragReset = rightDragBoundary - sliderOffset; … … 976 998 // Compare two revisions mode 977 999 if ( this.model.get('compareTwoMode') ) { 978 1000 // Prevent sliders from occupying same spot 979 if ( ui.values[1] === ui.values[0] ) 1001 if ( ui.values[1] === ui.values[0] ) { 980 1002 return false; 981 if ( isRtl ) 1003 } 1004 if ( isRtl ) { 982 1005 ui.values.reverse(); 1006 } 983 1007 attributes = { 984 1008 from: this.model.revisions.at( this.getPosition( ui.values[0] ) ), 985 1009 to: this.model.revisions.at( this.getPosition( ui.values[1] ) ) … … 989 1013 to: this.model.revisions.at( this.getPosition( ui.value ) ) 990 1014 }; 991 1015 // If we're at the first revision, unset 'from'. 992 if ( this.getPosition( ui.value ) > 0 ) 1016 if ( this.getPosition( ui.value ) > 0 ) { 993 1017 attributes.from = this.model.revisions.at( this.getPosition( ui.value ) - 1 ); 994 else1018 } else { 995 1019 attributes.from = undefined; 1020 } 996 1021 } 997 1022 movedRevision = this.model.revisions.at( this.getPosition( ui.value ) ); 998 1023 999 1024 // If we are scrubbing, a scrub to a revision is considered a hover 1000 if ( this.model.get('scrubbing') ) 1025 if ( this.model.get('scrubbing') ) { 1001 1026 attributes.hoveredRevision = movedRevision; 1027 } 1002 1028 1003 1029 this.model.set( attributes ); 1004 1030 }, 1005 1031 1006 stop: function( event, ui) {1032 stop: function() { 1007 1033 $( window ).off('mousemove.wp.revisions'); 1008 1034 this.model.updateSliderSettings(); // To snap us back to a tick mark 1009 1035 this.model.set({ scrubbing: false }); … … 1014 1040 // This is the view for the current active diff. 1015 1041 revisions.view.Diff = wp.Backbone.View.extend({ 1016 1042 className: 'revisions-diff', 1017 template: wp.template('revisions-diff'),1043 template: wp.template('revisions-diff'), 1018 1044 1019 1045 // Generate the options to be passed to the template. 1020 1046 prepare: function() { … … 1041 1067 }, 1042 1068 1043 1069 updateUrl: function() { 1044 var from = this.model.has('from') ? this.model.get('from').id : 0 ;1045 var to= this.model.get('to').id;1046 if ( this.model.get('compareTwoMode' ) ) 1070 var from = this.model.has('from') ? this.model.get('from').id : 0, 1071 to = this.model.get('to').id; 1072 if ( this.model.get('compareTwoMode' ) ) { 1047 1073 this.navigate( this.baseUrl( '?from=' + from + '&to=' + to ) ); 1048 else1074 } else { 1049 1075 this.navigate( this.baseUrl( '?revision=' + to ) ); 1076 } 1050 1077 }, 1051 1078 1052 1079 handleRoute: function( a, b ) { 1053 var from, to,compareTwo = _.isUndefined( b );1080 var compareTwo = _.isUndefined( b ); 1054 1081 1055 1082 if ( ! compareTwo ) { 1056 1083 b = this.model.revisions.get( a );