define([], function(){ //define(['js!modernizr/modernizr.touch.min.js'], function(MODERNIZR){ //'js!modernizr/modernizr.touch.min.js' function DragBehavior(initialImage, zoomImage, dispatcher){ this._initialImage = initialImage; this._zoomImage = zoomImage; this._dispatcher = dispatcher; this._lastMouseX = 0; this._lastMouseY = 0; this._state = null; } DragBehavior.prototype = { dragInit:function(){ //if(Modernizr.touch){ this.attachAllTouchListeners(); //} else { this.attachAllDesktopListeners(); //} }, attachAllDesktopListeners:function(){ var self = this; //console.log('DragBehavior: attaching listeners', this._dispatcher); //_initialImage RICHFX.jQuery(this._dispatcher).bind('mousedown', RICHFX.jQuery.proxy(function(eventObj){ this.mousedownHandler(eventObj); },this)); RICHFX.jQuery(this._dispatcher).bind('mouseup', RICHFX.jQuery.proxy(function(eventObj){ this.mouseupHandler(eventObj); },this)); RICHFX.jQuery(this._dispatcher).bind('mouseout', RICHFX.jQuery.proxy(function(eventObj){ this.mouseoutHandler(eventObj); },this)); //console.log('DragBehavior: activeElement: ', document.activeElement); /* RICHFX.jQuery(document).bind('keydown', RICHFX.jQuery.proxy(function(eventObj){ this.keyboardHandler(eventObj); //return false; },this)); */ RICHFX.jQuery('#'+this._dispatcher.id).bind('richmedia.external.panleft', RICHFX.jQuery.proxy(function(eventObj){ this.actionDragBehaviour('left'); },this)); RICHFX.jQuery('#'+this._dispatcher.id).bind('richmedia.external.panright', RICHFX.jQuery.proxy(function(eventObj){ this.actionDragBehaviour('right'); },this)); RICHFX.jQuery('#'+this._dispatcher.id).bind('richmedia.external.panup', RICHFX.jQuery.proxy(function(eventObj){ this.actionDragBehaviour('up'); },this)); RICHFX.jQuery('#'+this._dispatcher.id).bind('richmedia.external.pandown', RICHFX.jQuery.proxy(function(eventObj){ this.actionDragBehaviour('down'); },this)); if ($) { $('#'+this._dispatcher.id).bind('richmedia.external.panleft', RICHFX.jQuery.proxy(function(){ this.actionDragBehaviour('left'); },this)); $('#'+this._dispatcher.id).bind('richmedia.external.panright', RICHFX.jQuery.proxy(function(){ this.actionDragBehaviour('right'); },this)); $('#'+this._dispatcher.id).bind('richmedia.external.panup', RICHFX.jQuery.proxy(function(){ this.actionDragBehaviour('up'); },this)); $('#'+this._dispatcher.id).bind('richmedia.external.pandown', RICHFX.jQuery.proxy(function(){ this.actionDragBehaviour('down'); },this)); } }, attachAllTouchListeners:function(){ //make touch device stuff happen RICHFX.jQuery(this._dispatcher).bind('assetstate.zoom.changed assetstate.pinchzoom.changed', RICHFX.jQuery.proxy(function(eventObj){ if ( this._state.getZoomLevel() > 0 ) { //console.log('Binding touchStart - drag'); RICHFX.jQuery(this._dispatcher).bind('touchstart', RICHFX.jQuery.proxy(function(eventObj){ this.touchstartHandler(eventObj); },this)); } else { //console.log('UnBinding touchStart - drag'); RICHFX.jQuery(this._dispatcher).unbind('touchstart'); RICHFX.jQuery(this._dispatcher).unbind('touchmove'); } },this)); /* RICHFX.jQuery(this._dispatcher).bind('touchmove', RICHFX.jQuery.proxy(function(eventObj){ this.touchmoveHandler(eventObj); },this)); */ }, mousedownHandler: function(eventObj) { eventObj.preventDefault(); //console.log('DragBehavior: caught mousedown'); if ( !eventObj.shiftKey ) { this._lastMouseX = eventObj.pageX; this._lastMouseY = eventObj.pageY; RICHFX.jQuery(this._dispatcher).bind('mousemove', RICHFX.jQuery.proxy(function(event){ this.mousemoveHandler(event); },this)); } }, mousemoveHandler: function(eventObj) { var mouseX = eventObj.pageX; var mouseY = eventObj.pageY; var moveLeft = mouseX - this._lastMouseX; var moveTop = mouseY - this._lastMouseY; var xMove = (this._state.getX() + moveLeft); //console.log('DragBehavior: caught mousemove: ' + xMove); // update state this._state.setPosition((this._state.getX() + moveLeft), (this._state.getY() + moveTop)); this._lastMouseX = mouseX; this._lastMouseY = mouseY; }, keyboardHandler: function(eventObj) { //console.log("DragBehavior keyboardHandler: " + this._state.getX() + "|" + this._lastMouseX); var moveLeft = 0; var moveTop = 0; if ( isNaN(this._state.getX()) ) { this._state.setPosition(0,0); } if ( eventObj.keyCode == 38 ) { // up arrow moveTop = 50; } else if ( eventObj.keyCode == 40 ) { // down arrow moveTop = -50; } else if ( eventObj.keyCode == 37 ) { // left arrow moveLeft = 50; } else if ( eventObj.keyCode == 39 ) { // right arrow moveLeft = -50; } else { return; } // update state this._state.setPosition((this._state.getX() + moveLeft), (this._state.getY() + moveTop)); }, actionDragBehaviour: function(direction) { var moveLeft = 0; var moveTop = 0; if ( isNaN(this._state.getX()) ) { this._state.setPosition(0,0); } if ( direction == 'up' ) { // up arrow moveTop = 50; } else if ( direction == 'down' ) { // down arrow moveTop = -50; } else if ( direction == 'left' ) { // left arrow moveLeft = 50; } else if ( direction == 'right' ) { // right arrow moveLeft = -50; } else { return; } // update state this._state.setPosition((this._state.getX() + moveLeft), (this._state.getY() + moveTop)); }, mouseupHandler: function(eventObj) { RICHFX.jQuery(this._dispatcher).unbind('mousemove'); }, mouseoutHandler: function(eventObj) { RICHFX.jQuery(this._dispatcher).unbind('mousemove'); }, touchstartHandler: function(eventObj) { var touch = eventObj.originalEvent; this._lastMouseX = touch.touches[0].pageX; this._lastMouseY = touch.touches[0].pageY; RICHFX.jQuery(this._dispatcher).bind('touchmove', RICHFX.jQuery.proxy(function(eventObj){ this.touchmoveHandler(eventObj); },this)); }, touchmoveHandler: function(eventObj) { eventObj.preventDefault(); var touch = eventObj.originalEvent; if ( touch.touches[1] == undefined ) { mouseX = touch.touches[0].pageX; mouseY = touch.touches[0].pageY; var moveLeft = mouseX - this._lastMouseX; var moveTop = mouseY - this._lastMouseY; // update state this._state.setPosition((this._state.getX() + moveLeft), (this._state.getY() + moveTop)); this._lastMouseX = mouseX; this._lastMouseY = mouseY; } }, setState: function(assetState) { this._state = assetState; } } return DragBehavior; });