diff --git a/src/js/CanvasView.js b/src/js/CanvasView.js index 79dbd00..19dba37 100644 --- a/src/js/CanvasView.js +++ b/src/js/CanvasView.js @@ -243,10 +243,11 @@ mindmaps.DefaultCanvasView = function() { // horizontal delta when the vertical one is zero, so a sideways two-finger // swipe used to zoom the map. Zoom is a vertical gesture, and a horizontal // one should leave it alone. - this.$getContainer().bind("mousewheel", function(e, delta, deltaX, deltaY) { - // Older builds of the plugin pass no deltaY; fall back to the generic - // delta there so this keeps working rather than silently doing nothing. - var vertical = typeof deltaY === "number" ? deltaY : delta; + this.$getContainer().bind("mousewheel", function(e) { + // e.deltaY rather than the extra callback arguments: the plugin sets the + // normalised deltas on the event itself, and that is the part of its API + // that is not deprecated. + var vertical = e.deltaY; if (!vertical) { return; } diff --git a/src/js/libs/jquery.mousewheel.js b/src/js/libs/jquery.mousewheel.js index 3eadb7e..aec55ba 100644 --- a/src/js/libs/jquery.mousewheel.js +++ b/src/js/libs/jquery.mousewheel.js @@ -1,76 +1,83 @@ /*! - * jQuery Mousewheel 3.1.13 - * - * Copyright jQuery Foundation and other contributors - * Released under the MIT license - * http://jquery.org/license + * jQuery Mousewheel 3.2.2 + * Copyright OpenJS Foundation and other contributors */ -(function (factory) { - if ( typeof define === 'function' && define.amd ) { +( function( factory ) { + "use strict"; + + if ( typeof define === "function" && define.amd ) { + // AMD. Register as an anonymous module. - define(['jquery'], factory); - } else if (typeof exports === 'object') { + define( [ "jquery" ], factory ); + } else if ( typeof exports === "object" ) { + // Node/CommonJS style for Browserify module.exports = factory; } else { + // Browser globals - factory(jQuery); + factory( jQuery ); } -}(function ($) { +} )( function( $ ) { + "use strict"; - var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'], - toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ? - ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'], - slice = Array.prototype.slice, - nullLowestDeltaTimeout, lowestDelta; + var nullLowestDeltaTimeout, lowestDelta, + modernEvents = !!$.fn.on, + toFix = [ "wheel", "mousewheel", "DOMMouseScroll", "MozMousePixelScroll" ], + toBind = ( "onwheel" in window.document || window.document.documentMode >= 9 ) ? + [ "wheel" ] : [ "mousewheel", "DomMouseScroll", "MozMousePixelScroll" ], + slice = Array.prototype.slice; if ( $.event.fixHooks ) { for ( var i = toFix.length; i; ) { - $.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks; + $.event.fixHooks[ toFix[ --i ] ] = $.event.mouseHooks; } } var special = $.event.special.mousewheel = { - version: '3.1.12', + version: "3.2.2", setup: function() { if ( this.addEventListener ) { for ( var i = toBind.length; i; ) { - this.addEventListener( toBind[--i], handler, false ); + this.addEventListener( toBind[ --i ], handler, false ); } } else { this.onmousewheel = handler; } + // Store the line height and page height for this particular element - $.data(this, 'mousewheel-line-height', special.getLineHeight(this)); - $.data(this, 'mousewheel-page-height', special.getPageHeight(this)); + $.data( this, "mousewheel-line-height", special.getLineHeight( this ) ); + $.data( this, "mousewheel-page-height", special.getPageHeight( this ) ); }, teardown: function() { if ( this.removeEventListener ) { for ( var i = toBind.length; i; ) { - this.removeEventListener( toBind[--i], handler, false ); + this.removeEventListener( toBind[ --i ], handler, false ); } } else { this.onmousewheel = null; } + // Clean up the data we added to the element - $.removeData(this, 'mousewheel-line-height'); - $.removeData(this, 'mousewheel-page-height'); + $.removeData( this, "mousewheel-line-height" ); + $.removeData( this, "mousewheel-page-height" ); }, - getLineHeight: function(elem) { - var $elem = $(elem), - $parent = $elem['offsetParent' in $.fn ? 'offsetParent' : 'parent'](); - if (!$parent.length) { - $parent = $('body'); + getLineHeight: function( elem ) { + var $elem = $( elem ), + $parent = $elem[ "offsetParent" in $.fn ? "offsetParent" : "parent" ](); + if ( !$parent.length ) { + $parent = $( "body" ); } - return parseInt($parent.css('fontSize'), 10) || parseInt($elem.css('fontSize'), 10) || 16; + return parseInt( $parent.css( "fontSize" ), 10 ) || + parseInt( $elem.css( "fontSize" ), 10 ) || 16; }, - getPageHeight: function(elem) { - return $(elem).height(); + getPageHeight: function( elem ) { + return $( elem ).height(); }, settings: { @@ -79,56 +86,68 @@ } }; - $.fn.extend({ - mousewheel: function(fn) { - return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel'); + $.fn.extend( { + mousewheel: function( fn ) { + return fn ? + this[ modernEvents ? "on" : "bind" ]( "mousewheel", fn ) : + this.trigger( "mousewheel" ); }, - unmousewheel: function(fn) { - return this.unbind('mousewheel', fn); + unmousewheel: function( fn ) { + return this[ modernEvents ? "off" : "unbind" ]( "mousewheel", fn ); } - }); + } ); - function handler(event) { + function handler( event ) { var orgEvent = event || window.event, - args = slice.call(arguments, 1), + args = slice.call( arguments, 1 ), delta = 0, deltaX = 0, deltaY = 0, - absDelta = 0, - offsetX = 0, - offsetY = 0; - event = $.event.fix(orgEvent); - event.type = 'mousewheel'; + absDelta = 0; + event = $.event.fix( orgEvent ); + event.type = "mousewheel"; // Old school scrollwheel delta - if ( 'detail' in orgEvent ) { deltaY = orgEvent.detail * -1; } - if ( 'wheelDelta' in orgEvent ) { deltaY = orgEvent.wheelDelta; } - if ( 'wheelDeltaY' in orgEvent ) { deltaY = orgEvent.wheelDeltaY; } - if ( 'wheelDeltaX' in orgEvent ) { deltaX = orgEvent.wheelDeltaX * -1; } + if ( "detail" in orgEvent ) { + deltaY = orgEvent.detail * -1; + } + if ( "wheelDelta" in orgEvent ) { + deltaY = orgEvent.wheelDelta; + } + if ( "wheelDeltaY" in orgEvent ) { + deltaY = orgEvent.wheelDeltaY; + } + if ( "wheelDeltaX" in orgEvent ) { + deltaX = orgEvent.wheelDeltaX * -1; + } // Firefox < 17 horizontal scrolling related to DOMMouseScroll event - if ( 'axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) { + if ( "axis" in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) { deltaX = deltaY * -1; deltaY = 0; } - // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy + // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatability delta = deltaY === 0 ? deltaX : deltaY; // New school wheel delta (wheel event) - if ( 'deltaY' in orgEvent ) { + if ( "deltaY" in orgEvent ) { deltaY = orgEvent.deltaY * -1; delta = deltaY; } - if ( 'deltaX' in orgEvent ) { + if ( "deltaX" in orgEvent ) { deltaX = orgEvent.deltaX; - if ( deltaY === 0 ) { delta = deltaX * -1; } + if ( deltaY === 0 ) { + delta = deltaX * -1; + } } // No change actually happened, no reason to go any further - if ( deltaY === 0 && deltaX === 0 ) { return; } + if ( deltaY === 0 && deltaX === 0 ) { + return; + } // Need to convert lines and pages to pixels if we aren't already in pixels // There are three delta modes: @@ -136,31 +155,32 @@ // * deltaMode 1 is by lines // * deltaMode 2 is by pages if ( orgEvent.deltaMode === 1 ) { - var lineHeight = $.data(this, 'mousewheel-line-height'); + var lineHeight = $.data( this, "mousewheel-line-height" ); delta *= lineHeight; deltaY *= lineHeight; deltaX *= lineHeight; } else if ( orgEvent.deltaMode === 2 ) { - var pageHeight = $.data(this, 'mousewheel-page-height'); + var pageHeight = $.data( this, "mousewheel-page-height" ); delta *= pageHeight; deltaY *= pageHeight; deltaX *= pageHeight; } // Store lowest absolute delta to normalize the delta values - absDelta = Math.max( Math.abs(deltaY), Math.abs(deltaX) ); + absDelta = Math.max( Math.abs( deltaY ), Math.abs( deltaX ) ); if ( !lowestDelta || absDelta < lowestDelta ) { lowestDelta = absDelta; // Adjust older deltas if necessary - if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) { + if ( shouldAdjustOldDeltas( orgEvent, absDelta ) ) { lowestDelta /= 40; } } // Adjust older deltas if necessary - if ( shouldAdjustOldDeltas(orgEvent, absDelta) ) { + if ( shouldAdjustOldDeltas( orgEvent, absDelta ) ) { + // Divide all the things by 40! delta /= 40; deltaX /= 40; @@ -168,54 +188,55 @@ } // Get a whole, normalized value for the deltas - delta = Math[ delta >= 1 ? 'floor' : 'ceil' ](delta / lowestDelta); - deltaX = Math[ deltaX >= 1 ? 'floor' : 'ceil' ](deltaX / lowestDelta); - deltaY = Math[ deltaY >= 1 ? 'floor' : 'ceil' ](deltaY / lowestDelta); + delta = Math[ delta >= 1 ? "floor" : "ceil" ]( delta / lowestDelta ); + deltaX = Math[ deltaX >= 1 ? "floor" : "ceil" ]( deltaX / lowestDelta ); + deltaY = Math[ deltaY >= 1 ? "floor" : "ceil" ]( deltaY / lowestDelta ); // Normalise offsetX and offsetY properties if ( special.settings.normalizeOffset && this.getBoundingClientRect ) { var boundingRect = this.getBoundingClientRect(); - offsetX = event.clientX - boundingRect.left; - offsetY = event.clientY - boundingRect.top; + event.offsetX = event.clientX - boundingRect.left; + event.offsetY = event.clientY - boundingRect.top; } // Add information to the event object event.deltaX = deltaX; event.deltaY = deltaY; event.deltaFactor = lowestDelta; - event.offsetX = offsetX; - event.offsetY = offsetY; + // Go ahead and set deltaMode to 0 since we converted to pixels // Although this is a little odd since we overwrite the deltaX/Y // properties with normalized deltas. event.deltaMode = 0; // Add event and delta to the front of the arguments - args.unshift(event, delta, deltaX, deltaY); + args.unshift( event, delta, deltaX, deltaY ); - // Clearout lowestDelta after sometime to better + // Clear out lowestDelta after sometime to better // handle multiple device types that give different // a different lowestDelta // Ex: trackpad = 3 and mouse wheel = 120 - if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); } - nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200); + if ( nullLowestDeltaTimeout ) { + window.clearTimeout( nullLowestDeltaTimeout ); + } + nullLowestDeltaTimeout = window.setTimeout( function() { + lowestDelta = null; + }, 200 ); - return ($.event.dispatch || $.event.handle).apply(this, args); + return ( $.event.dispatch || $.event.handle ).apply( this, args ); } - function nullLowestDelta() { - lowestDelta = null; - } + function shouldAdjustOldDeltas( orgEvent, absDelta ) { - function shouldAdjustOldDeltas(orgEvent, absDelta) { - // If this is an older event and the delta is divisable by 120, + // If this is an older event and the delta is divisible by 120, // then we are assuming that the browser is treating this as an // older mouse wheel event and that we should divide the deltas // by 40 to try and get a more usable deltaFactor. // Side note, this actually impacts the reported scroll distance // in older browsers and can cause scrolling to be slower than native. // Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false. - return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0; + return special.settings.adjustOldDeltas && orgEvent.type === "mousewheel" && + absDelta % 120 === 0; } -})); +} ); diff --git a/test/i18n-hooks.test.js b/test/i18n-hooks.test.js index f13f49d..776a01e 100644 --- a/test/i18n-hooks.test.js +++ b/test/i18n-hooks.test.js @@ -115,14 +115,17 @@ test("vendored jquery plugins expose the APIs the application binds", function() var libs = path.join(srcDir, "libs"); var mousewheel = fs.readFileSync(path.join(libs, "jquery.mousewheel.js"), "utf8"); - // mindmaps binds $(el).bind("mousewheel", function(event, delta) {...}) and reads only - // the sign of delta, so the special event and the delta argument are the whole contract. - assert.ok(/jQuery Mousewheel 3\.1\.13/.test(mousewheel), "mousewheel is the pinned 3.1.13"); + // mindmaps binds the special mousewheel event and reads event.deltaY, zooming on its + // sign. The normalised deltas on the event object are the part of the plugin's API that + // is not deprecated, so that is what the contract pins. + assert.ok(/jQuery Mousewheel 3\.2\.2/.test(mousewheel), "mousewheel is the pinned 3.2.2"); assert.ok(/\$\.event\.special\.mousewheel/.test(mousewheel), "registers the special event"); - assert.ok(/args\.unshift\(event, delta/.test(mousewheel), "still passes delta as the first extra argument"); + assert.ok(/event\.deltaY\s*=/.test(mousewheel), "sets the normalised deltaY on the event"); + assert.ok(/event\.deltaX\s*=/.test(mousewheel), "sets the normalised deltaX on the event"); assert.ok(/\.bind\("mousewheel"/.test(read("CanvasView.js")), "CanvasView still binds mousewheel"); - assert.ok(/delta > 0/.test(read("CanvasPresenter.js")), "zoom still keys off the sign of delta"); + assert.ok(/e\.deltaY/.test(read("CanvasView.js")), "the zoom reads the vertical delta"); + assert.ok(/delta > 0/.test(read("CanvasPresenter.js")), "zoom still keys off the sign"); }); // 8. The build contract: what ships inside the bundle and what the host provides.