From f738dcbbcc40e5f08d3d701cb8f37a7cc484964a Mon Sep 17 00:00:00 2001 From: Kin Blas Date: Fri, 28 Jan 2011 15:39:51 -0800 Subject: [PATCH] More work-in-progress changes. --- experiments/fast-click/jquery.mobile.click.js | 71 ++++++++++++++++--- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/experiments/fast-click/jquery.mobile.click.js b/experiments/fast-click/jquery.mobile.click.js index c5211b9c..94e347ef 100644 --- a/experiments/fast-click/jquery.mobile.click.js +++ b/experiments/fast-click/jquery.mobile.click.js @@ -4,31 +4,74 @@ * Dual licensed under the MIT or GPL Version 2 licenses. * http://jquery.org/license */ + +// This plugin is an experient for abstracting away the touch and mouse +// events so that developers don't have to worry about which method of input +// the device their document is loaded on supports. +// +// The idea here is to allow the developer to register listeners for the +// basic mouse events, such as mousedown, mousemove, mouseup, and click, +// and the plugin will take care of registering the correct listeners +// behind the scenes to invoke the listener at the fastest possible time +// for that device, while still retaining the order of event firing in +// the traditional mouse environment, should multiple handlers be registered +// on the same element for different events. +// +// The current version simply adds mBind and mUnbind to the $.fn space, +// but we're considering other methods for making this easier. One alternative +// would be to allow users to use virtual mouse event names, such as +// "vmousedown", "vmouseup", etc, to trigger the traditional jQuery special/custom +// event api, which would then trigger this same code. + (function($, window, document, undefined) { var nextSequencerID = 0; +var eventToSequencerName = { + touchstart: "mmousedown", + touchmove: "mmousemove", + touchend: "mmouseup", + mousedown: "mmousedown", + mousemove: "mmousemove", + mouseup: "mmouseup", + mouseover: "mmouseover", + mouseout: "mmouseout", + click: "mclick" +}; + +var downEvents = "touchstart mousedown"; +var upEvents = "touchend mouseup"; +var moveEvents = "touchmove mousemove"; + function MouseEventSequencer() { this.seqID = "MSE-" + nextSequencerID++; this.handlerDict = {}; this.mode = null; + this.downFunc = null; } $.extend(MouseEventSequencer.prototype, { bind: function(ele, eventType, handler) { - var $ele = $(ele), - hArray = handlerDict[eventType]; - if (!hArray) - hArray = handlerDict[eventType] = []; + var self = this, + seqName = eventToSequencerName[eventType], + $ele = $(ele), + hArray = handlerDict[seqName]; + + if (!hArray) { + hArray = handlerDict[seqName] = []; + } hArray.push(handler); - // We always register for touchstart and mousedown because - // we may need to synthesize some events when dealing with - // touch devices. + if (!this.downFunc){ + // We always register for touchstart and mousedown because + // we may need to synthesize some events when dealing with + // touch devices. - $ele.bind("touchstart." + this.seqID + " mousedown." + this.seqID, function(e,d){ self.handleDown(e, d); }); + this.downFunc = function(e,d){ self.handleDown(e, d); } + $ele.bind("touchstart." + this.seqID + " mousedown." + this.seqID, this.downFunc); + } }, unbind: function(ele, eventType, handler) @@ -50,8 +93,10 @@ $.extend(MouseEventSequencer.prototype, { handleDown: function(event, data) { - this.mode = isTouchEvent(event.type) ? "touch" : "mouse"; - this.trigger("mmouseover"); + this.mode = this.getModeFromType(event.type); + if (this.mode == "touch") { + this.trigger("mmouseover"); + } this.trigger("mmousedown"); }, @@ -79,6 +124,11 @@ $.extend(MouseEventSequencer.prototype, { { if (this.mode != "touch") this.trigger("mmouseout"); + }, + + getModeFromType: function (eventType) + { + return isTouchEvent(eventType) ? "touch" : "mouse"; } }); @@ -94,6 +144,7 @@ function isTouchEvent(eventType) return eventType.search(/^touch(start|end|move|cancel)$/) != -1; } + $.extend($.fn, { mBind: function(eventType, handler) {