Changeset 26035
- Timestamp:
- 11/07/2013 06:13:33 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/js/revisions.js
r24864 r26035 1 /*global _, Backbone, _wpRevisionsSettings, isRtl */ 1 2 window.wp = window.wp || {}; 2 3 … … 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 … … 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 /** … … 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 }); … … 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 … … 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 }); … … 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'); … … 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 … … 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 ) { … … 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() { … … 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 }); … … 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. … … 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; … … 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 }, … … 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, … … 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 ) { … … 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 … … 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') }); … … 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 ); … … 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 ) { … … 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 } … … 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 ); … … 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. … … 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 … … 941 962 leftDragReset = '0', 942 963 rightDragReset = '100%', 964 handles, 943 965 handle = $( ui.handle ); 944 966 … … 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; … … 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] ) ), … … 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 … … 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. … … 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 ) {
Note: See TracChangeset
for help on using the changeset viewer.