Ticket #24425: 24425.draft.20.diff
File 24425.draft.20.diff, 17.2 KB (added by , 12 years ago) |
---|
-
wp-admin/js/revisions.js
3 3 (function($) { 4 4 var revisions; 5 5 6 revisions = wp.revisions = { model: {}, view: {}, controller: {} };6 revisions = wp.revisions = { model: {}, view: {}, controller: {}, router: {} }; 7 7 8 8 // Link settings. 9 9 revisions.settings = typeof _wpRevisionsSettings === 'undefined' ? {} : _wpRevisionsSettings; … … 30 30 31 31 comparator: function( revision ) { 32 32 return revision.id; 33 } ,33 } 34 34 }); 35 35 36 36 revisions.model.Field = Backbone.Model.extend({}); … … 101 101 return this.fetch({ data: { compare: comparisons }, remove: false }); 102 102 }, 103 103 104 /**/105 104 loadLast: function( num ) { 106 105 num = num || 1; 107 106 var ids = this.getProximalDiffIds(); … … 194 193 initialize: function( attributes, options ) { 195 194 this.revisions = options.revisions; 196 195 this.diffs = new revisions.model.Diffs( [], {revisions: this.revisions} ); 196 this.listenTo( this, 'change:from', this.updateDiffFrom ); 197 this.listenTo( this, 'change:to', this.updateDiffTo ); 198 this.revisionsRouter = new revisions.router.Router(); 199 this.revisionsRouter.model = this; 197 200 198 this.listenTo( this, 'change:from change:to', this.updateDiffId );199 201 }, 200 202 201 updateDiff Id: function() {203 updateDiffTo: function() { 202 204 var from = this.get( 'from' ); 203 205 var to = this.get( 'to' ); 204 this.set( 'diffId', (from ? from.id : '0') + ':' + to.id ); 206 this.set( 'diffId', (from ? from.id : '0' ) + ':' + to.id ); 207 }, 208 209 updateDiffFrom: function() { 210 if ( this.get( 'compareTwoMode' ) ) { 211 this.updateDiffTo(); 212 } 205 213 } 214 206 215 }); 207 216 208 217 … … 243 252 this.model.diffs.loadAllBy( 50 ); 244 253 }, this ) ); 245 254 } 255 Backbone.history.start(); 246 256 }, 247 257 248 258 render: function() { … … 266 276 }); 267 277 268 278 // The control view. 269 // This contains the revision slider, previous/next buttons, and the compare checkbox.279 // This contains the revision slider, previous/next buttons, the meta info and the compare checkbox. 270 280 revisions.view.Controls = wp.Backbone.View.extend({ 271 281 tagName: 'div', 272 282 className: 'revisions-controls', 273 283 274 284 initialize: function() { 275 285 // Add the button view 276 this.views.add( new revisions.view.Buttons({ 286 this.views.add( new revisions.view.Buttons({ 277 287 model: this.model 278 288 })); 279 289 … … 314 324 if( this.model.get( 'to' ).attributes.current ) { 315 325 $( '#restore-revision' ).prop( 'disabled', true); 316 326 } else { 317 $( '#restore-revision' ).prop( 'disabled', false) 327 $( '#restore-revision' ).prop( 'disabled', false); 318 328 } 319 329 } 320 330 }); 321 331 322 323 332 // The buttons view. 324 // Encapsulates all of the configuration for the previous /next buttons, and the compare checkbox.333 // Encapsulates all of the configuration for the previous / next buttons, and the compare check-box. 325 334 revisions.view.Buttons = wp.Backbone.View.extend({ 326 335 tagName: 'div', 327 336 className: 'revisions-buttons', 328 337 template: wp.template('revisions-controls'), 329 338 330 339 initialize: function() { 331 this.$el.html( this.template() ) 340 this.$el.html( this.template() ); 341 this.listenTo( this.model, 'change:compareTwoMode', this.updateCompareTwoMode ); 342 332 343 }, 333 344 334 345 events: { 335 346 'click #next': 'nextRevision', 336 'click #previous': 'previousRevision' 347 'click #previous': 'previousRevision', 348 'click .compare-two-revisions': 'compareTwoToggle' 337 349 }, 338 350 351 updateCompareTwoMode: function(){ 352 if ( this.model.get( 'compareTwoMode' ) ){ 353 $( '#revision-diff-container' ).addClass( 'comparing-two-revisions' ); 354 // in RTL mode the 'left handle' is the second in the slider, 'right' is first 355 $( '.wp-slider a.ui-slider-handle' ).first().addClass( isRtl ? 'right-handle' : 'left-handle' ); 356 $( '.wp-slider a.ui-slider-handle' ).last().addClass( isRtl ? 'left-handle' : 'right-handle' ); 357 } else { 358 $( '#revision-diff-container' ).removeClass( 'comparing-two-revisions' ); 359 $( '.wp-slider a.ui-slider-handle' ).removeClass( 'left-handle' ).removeClass( 'right-handle' ); 360 } 361 362 }, 363 364 // Toggle the compare two mode feature when the compare two check-box is checked. 365 compareTwoToggle: function( event ){ 366 // Activate compare two mode? 367 if ( $( '.compare-two-revisions' ).is( ':checked' ) ) { 368 this.model.set( { compareTwoMode: true } ); 369 } else { 370 this.model.set( { compareTwoMode: false } ); 371 } 372 this.model.revisionsRouter.navigateRoute( this.model.get( 'to').id, this.model.get( 'from' ).id ); 373 374 }, 375 376 // Go to a specific modelindex, taking into account Rtl mode. 339 377 gotoModel: function( toIndex ) { 340 378 var attributes = { 341 to: this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl 379 to: this.model.revisions.at( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl. 342 380 }; 343 381 // If we're at the first revision, unset 'from'. 344 382 if ( isRtl ? this.model.revisions.length - toIndex - 1 : toIndex ) // Reverse directions for Rtl … … 347 385 this.model.unset('from', { silent: true }); 348 386 349 387 this.model.set( attributes ); 388 389 this.model.revisionsRouter.navigateRoute( attributes.to.id, attributes.from ? attributes.from.id : 0 ); 390 350 391 }, 351 392 393 // Go to the 'next' revision, direction takes into account Rtl mode. 352 394 nextRevision: function() { 353 var toIndex = this.model.revisions.indexOf( this.model.get( 'to' ) );395 var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1 : this.model.revisions.indexOf( this.model.get( 'to' ) ); 354 396 toIndex = isRtl ? toIndex - 1 : toIndex + 1; 355 397 this.gotoModel( toIndex ); 356 398 }, 357 399 400 // Go to the 'previous' revision, direction takes into account Rtl mode. 358 401 previousRevision: function() { 359 var toIndex = this.model.revisions.indexOf( this.model.get('to') );402 var toIndex = isRtl ? this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1 : this.model.revisions.indexOf( this.model.get( 'to' ) ); 360 403 toIndex = isRtl ? toIndex + 1 : toIndex - 1; 361 404 this.gotoModel( toIndex ); 362 405 }, 363 406 364 407 ready: function() { 365 408 this.listenTo( this.model, 'change:diffId', this.disabledButtonCheck ); 409 410 // Hide compare two mode toggle when fewer than three revisions. 411 if ( this.model.revisions.length < 3 ) { 412 $( '.revision-toggle-compare-mode' ).hide(); 413 } 366 414 }, 367 415 368 // Check to see if the Previous or Next buttons need to be disabled or enabled 416 // Check to see if the Previous or Next buttons need to be disabled or enabled. 369 417 disabledButtonCheck: function() { 370 var maxVal = isRtl ? 0 :this.model.revisions.length - 1,371 minVal = isRtl ? this.model.revisions.length - 1 :0,372 next 418 var maxVal = this.model.revisions.length - 1, 419 minVal = 0, 420 next = $( '.revisions-next .button' ), 373 421 previous = $( '.revisions-previous .button' ), 374 val 422 val = this.model.revisions.indexOf( this.model.get( 'to' ) ); 375 423 376 // Disable "Next" button if you're on the last node 424 // Disable "Next" button if you're on the last node. 377 425 if ( maxVal === val ) 378 426 next.prop( 'disabled', true ); 379 427 else 380 428 next.prop( 'disabled', false ); 381 429 382 // Disable "Previous" button if you're on the first node 430 // Disable "Previous" button if you're on the first node. 383 431 if ( minVal === val ) 384 432 previous.prop( 'disabled', true ); 385 433 else 386 434 previous.prop( 'disabled', false ); 387 } ,435 } 388 436 389 437 390 438 }); … … 404 452 405 453 // Find the initially selected revision 406 454 var initiallySelectedRevisionIndex = 407 this.model.revisions.indexOf( 455 this.model.revisions.indexOf( 408 456 this.model.revisions.findWhere( { id: Number( revisions.settings.selectedRevision ) } ) ); 409 457 410 458 this.settings = new revisions.model.Slider({ … … 418 466 419 467 ready: function() { 420 468 this.$el.slider( this.settings.toJSON() ); 469 470 // Listen for changes in Compare Two Mode setting 471 this.listenTo( this.model, 'change:compareTwoMode', this.updateSliderSettings ); 472 421 473 this.settings.on( 'change', function( model, options ) { 422 // Apply changes to slider settings here. 423 this.$el.slider( { value: this.model.revisions.indexOf( this.model.get( 'to' ) ) } ); // Set handle to current to model 474 this.updateSliderSettings(); 424 475 }, this ); 425 // Reset to the initially selected revision426 this.slide( '', this.settings.attributes );427 476 428 477 // Listen for changes in the diffId 429 478 this.listenTo( this.model, 'change:diffId', this.diffIdChanged ); 430 479 480 // Reset to the initially selected revision 481 this.slide( '', this.settings.attributes ); 482 483 if ( this.model.get( 'compareTwoMode' ) ) 484 $( '.compare-two-revisions' ).trigger( 'click' ); 431 485 }, 432 486 487 updateSliderSettings: function() { 488 if ( isRtl ) { 489 this.$el.slider( { // Order reversed in Rtl mode 490 value: this.model.revisions.length - this.model.revisions.indexOf( this.model.get( 'to' ) ) - 1 491 } ); 492 } else { 493 if ( this.model.get( 'compareTwoMode' ) ) { 494 this.$el.slider( { // Set handles to current from/to models 495 values: [ 496 this.model.revisions.indexOf( this.model.get( 'from' ) ), 497 this.model.revisions.indexOf( this.model.get( 'to' ) ) 498 ], 499 value: null, 500 range: true // Range mode ensures handles can't cross 501 } ); 502 } else { 503 this.$el.slider( { // Set handle to current to model 504 value: this.model.revisions.indexOf( this.model.get( 'to' ) ), 505 values: null, // Clear existing two handled values 506 range: false 507 } ); 508 } 509 } 510 if ( this.model.get( 'compareTwoMode' ) ){ 511 $( '.revisions' ).addClass( 'comparing-two-revisions' ); 512 // in RTL mode the 'left handle' is the second in the slider, 'right' is first 513 514 $( '.wp-slider a.ui-slider-handle' ) 515 .first() 516 .addClass( isRtl ? 'right-handle' : 'left-handle' ) 517 .removeClass( isRtl ? 'left-handle' : 'right-handle' ); 518 $( '.wp-slider a.ui-slider-handle' ) 519 .last() 520 .addClass( isRtl ? 'left-handle' : 'right-handle' ) 521 .removeClass( isRtl ? 'right-handle' : 'left-handle' ); 522 } else { 523 $( '.revisions' ).removeClass( 'comparing-two-revisions' ); 524 } 525 }, 526 433 527 diffIdChanged: function() { 434 528 // Reset the view settings when diffId is changed 435 this.settings.set( { 'value': this.model.revisions.indexOf( this.model.get( 'to' ) ) } ); 529 if ( this.model.get( 'compareTwoMode' ) ) { 530 this.settings.set( { 'values': [ 531 this.model.revisions.indexOf( this.model.get( 'from' ) ), 532 this.model.revisions.indexOf( this.model.get( 'to' ) ) 533 ] } ); 534 } else { 535 this.settings.set( { 'value': this.model.revisions.indexOf( this.model.get( 'to' ) ) } ); 536 } 436 537 }, 437 538 438 539 start: function( event, ui ) { 439 // Track the mouse position to enable smooth dragging, overrides default jquery ui step behaviour 440 $( window ).mousemove( function( e ) { 441 var sliderLeft = $( '.wp-slider' ).offset().left, 442 sliderRight = sliderLeft + $( '.wp-slider' ).width(); 540 if ( true !== this.model.get( 'compareTwoMode' )) { 541 // Track the mouse position to enable smooth dragging, overrides default jquery ui step behaviour . 542 $( window ).mousemove( function( e ) { 543 var sliderLeft = $( '.wp-slider' ).offset().left, 544 sliderRight = sliderLeft + $( '.wp-slider' ).width(); 443 545 444 // Follow mouse movements, as long as handle remains inside slider 445 if ( e.clientX < sliderLeft ) { 446 $( ui.handle ).css( 'left', 0 ); // Mouse to left of slider 447 } else if ( e.clientX > sliderRight ) { 448 $( ui.handle ).css( 'left', sliderRight - sliderLeft); // Mouse to right of slider 449 } else { 450 $( ui.handle ).css( 'left', e.clientX - sliderLeft ); // Mouse in slider 451 } 452 } ); // End mousemove 546 // Follow mouse movements, as long as handle remains inside slider. 547 if ( e.clientX < sliderLeft ) { 548 $( ui.handle ).css( 'left', 0 ); // Mouse to left of slider. 549 } else if ( e.clientX > sliderRight ) { 550 $( ui.handle ).css( 'left', sliderRight - sliderLeft); // Mouse to right of slider. 551 } else { 552 $( ui.handle ).css( 'left', e.clientX - sliderLeft ); // Mouse in slider. 553 } 554 } ); // End mousemove. 555 } 453 556 }, 454 557 558 getSliderPosition: function( ui ){ 559 return isRtl ? this.model.revisions.length - ui.value - 1 : ui.value; 560 }, 561 455 562 slide: function( event, ui ) { 456 var attributes = { 457 to: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.value - 1 : ui.value ) // Reverse directions for Rtl 458 }; 563 var attributes; 564 // Compare two revisions mode 565 if ( 'undefined' !== typeof ui.values && this.model.get( 'compareTwoMode' ) ) { 566 // Prevent sliders from occupying same spot 567 if ( ui.values[1] === ui.values[0] ) 568 return false; 569 attributes = { 570 to: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[1] - 1 : ui.values[1] ), // Reverse directions for Rtl. 571 from: this.model.revisions.at( isRtl ? this.model.revisions.length - ui.values[0] - 1 : ui.values[0] ) // Reverse directions for Rtl. 572 }; 459 573 460 // If we're at the first revision, unset 'from'. 461 if ( isRtl ? this.model.revisions.length - ui.value - 1 : ui.value ) // Reverse directions for Rtl 462 attributes.from = this.model.revisions.at( isRtl ? this.model.revisions.length - ui.value - 2 : ui.value - 1 ); 463 else 464 this.model.unset('from', { silent: true }); 574 } else { 575 // Compare single revision mode 576 var sliderPosition = this.getSliderPosition( ui ); 577 attributes = { 578 to: this.model.revisions.at( sliderPosition ) 579 }; 465 580 581 // If we're at the first revision, unset 'from'. 582 if ( sliderPosition ) // Reverse directions for Rtl. 583 attributes.from = this.model.revisions.at( sliderPosition - 1 ); 584 else 585 this.model.unset('from', { silent: true }); 586 587 } 466 588 this.model.set( attributes ); 589 590 // Maintain state history when dragging 591 this.model.revisionsRouter.navigateRoute( attributes.to.id, ( attributes.from ? attributes.from.id : 0 ) ); 592 467 593 }, 468 594 469 595 stop: function( event, ui ) { 470 $( window ).unbind( 'mousemove' ); // Stop tracking the mouse 471 // Reset settings pops handle back to the step position 472 this.settings.trigger( 'change' ); 596 if ( true !== this.model.get( 'compareTwoMode' )) { 597 $( window ).unbind( 'mousemove' ); // Stop tracking the mouse. 598 // Reset settings pops handle back to the step position. 599 this.settings.trigger( 'change' ); 600 } 473 601 } 474 602 }); 475 603 … … 486 614 } 487 615 }); 488 616 617 // The revisions router 618 // takes URLs with #hash fragments and routes them 619 revisions.router.Router = Backbone.Router.extend({ 620 model: null, 621 622 routes: { 623 'revision/from/:from/to/:to/handles/:handles': 'gotoRevisionId' 624 }, 625 626 navigateRoute: function( to, from ) { 627 var navigateTo = '/revision/from/' + from + '/to/' + to + '/handles/'; 628 if ( this.model.get( 'compareTwoMode' ) ){ 629 navigateTo = navigateTo + '2'; 630 } else { 631 navigateTo = navigateTo + '1'; 632 } 633 this.navigate( navigateTo ); 634 }, 635 636 gotoRevisionId: function( to, from, handles ) { 637 if ( '2' === handles ) { 638 this.model.set( { compareTwoMode: true } ); 639 $( '.compare-two-revisions' ).attr( 'checked', true ); 640 } else { 641 this.model.set( { compareTwoMode: false } ); 642 $( '.compare-two-revisions' ).attr( 'checked', false ); 643 } 644 if ('undefined' !== typeof this.model ) { 645 var selectedToRevision = 646 this.model.revisions.findWhere( { 'id': Number( to ) } ), 647 selectedFromRevision = 648 this.model.revisions.findWhere( { 'id': Number( from ) } ); 649 650 this.model.set( { 651 to: selectedToRevision, 652 from: selectedFromRevision } ); 653 } 654 } 655 656 }); 657 489 658 // Initialize the revisions UI. 490 659 revisions.init = function() { 491 660 revisions.view.frame = new revisions.view.Frame({ 492 661 collection: new revisions.model.Revisions( revisions.settings.revisionData ) 493 662 }).render(); 663 // Only allow compare two mode if three or more revisions 494 664 }; 495 665 496 666 $( revisions.init ); -
wp-admin/css/wp-admin-rtl.css
975 975 left: 6px; 976 976 } 977 977 978 #toggle-revision-compare-mode {978 .revision-toggle-compare-mode { 979 979 right: auto; 980 980 left: 0; 981 981 padding: 9px 0 0 9px; 982 982 } 983 983 984 #diff-next-revision{984 .revisions-next { 985 985 float: left; 986 986 } 987 987 988 #diff-previous-revision{988 .revisions-previous { 989 989 float: right; 990 990 } 991 991 -
wp-admin/css/wp-admin.css
3502 3502 margin-bottom: 10px; 3503 3503 } 3504 3504 3505 .comparing-two-revisions .revisions-controls { 3506 height: 90px; 3507 } 3508 3505 3509 .revisions-meta { 3506 3510 margin-top: 15px; 3507 3511 } … … 3510 3514 top: 0; 3511 3515 right: 0; 3512 3516 } 3517 .comparing-two-revisions .revisions-previous, 3518 .comparing-two-revisions .revisions-next { 3519 display: none; 3520 } 3513 3521 3514 3522 .revisions-previous { 3515 3523 float: left; … … 3655 3663 margin-top: 20px; 3656 3664 } 3657 3665 3658 .comparing-two-revisions#diff-previous-revision,3659 .comparing-two-revisions#diff-next-revision,3666 #diff-previous-revision, 3667 #diff-next-revision, 3660 3668 #diff-header-from { 3661 3669 display: none; 3662 3670 }