Skip to content

Commit

Permalink
双击事件支持
Browse files Browse the repository at this point in the history
  • Loading branch information
kener committed Aug 19, 2014
1 parent a518915 commit 1d158bd
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
57 changes: 57 additions & 0 deletions doc/asset/js/zDocShape.js
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,63 @@ zr.on(config.EVENT.CLICK, function(params) {
else {
alert('Global catch! None shape, but i catch you!');
}
});
}).toString().slice(13, -10),
cantry : true
},
{
name : 'ondblclick',
des : '默认为null,当前图形双击响应,回传参数和有效返回值见下',
params : [
['eventPacket', '{Object}','事件包,结构如下'],
['eventPacket.type', '{string}','事件类型为EVENT.DBLCLICK'],
['eventPacket.event', '{event}','原始dom事件对象'],
['eventPacket.target', '{Object}','当前图形shape对象']
],
res : [
'true | false ', '{boolean}','回调返回,true(阻塞全局zrender事件)| false(不阻塞全局zrender事件)<br/> 无返回值,等同返回false'
],
pre : (function(){
var CircleShape = require('zrender/shape/Circle');
zr.addShape(new CircleShape({
style : {
x : 200,
y : 200,
r : 80,
color : 'red',
text : 'dblclick and block!',
textPosition : 'inside'
},
clickable : true,
ondblclick : function() {
alert('dblclick on red shape!')
return true; // 阻塞全局zrender事件
}
}));
zr.addShape(new CircleShape({
style : {
x : 400,
y : 200,
r : 80,
color : 'green',
text : 'dblclick!',
textPosition : 'inside'
},
clickable : true,
ondblclick : function() {
alert('dblclick on green shape!')
}
}));
zr.render();

var config = require('zrender/config');
zr.on(config.EVENT.DBLCLICK, function(params) {
if (params.target) {
alert('Global catch! Double Click on shape!');
}
else {
alert('Global catch! None shape, but i catch you!');
}
});
}).toString().slice(13, -10),
cantry : true
Expand Down
37 changes: 33 additions & 4 deletions src/Handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ define(
var EVENT = config.EVENT;

var domHandlerNames = [
'resize', 'click',
'resize', 'click', 'dblclick',
'mousewheel', 'mousemove', 'mouseout', 'mouseup', 'mousedown',
'touchstart', 'touchend', 'touchmove'
];
Expand Down Expand Up @@ -55,6 +55,26 @@ define(

this._mousemoveHandler(event);
},

/**
* 双击响应函数
*
* @param {event} event dom事件对象
*/
dblclick: function (event) {
event = this._zrenderEventFixed(event);

//分发config.EVENT.DBLCLICK事件
var _lastHover = this._lastHover;
if (( _lastHover && _lastHover.clickable )
|| !_lastHover
) {
this._dispatchAgency(_lastHover, EVENT.DBLCLICK, event);
}

this._mousemoveHandler(event);
},


/**
* 鼠标滚轮响应函数
Expand Down Expand Up @@ -252,10 +272,15 @@ define(
//eventTool.stop(event);// 阻止浏览器默认事件,重要
event = this._zrenderEventFixed(event, true);
this._mouseupHandler(event);

if (new Date() - this._lastTouchMoment < EVENT.touchClickDelay) {

var now = new Date();
if (now - this._lastTouchMoment < EVENT.touchClickDelay) {
this._mobildFindFixed(event);
this._clickHandler(event);
if (now - this._lastClickMoment < EVENT.touchClickDelay / 2) {
this._dblclickHandler(event);
}
this._lastClickMoment = now;
}
this.painter.clearHover();
}
Expand Down Expand Up @@ -339,6 +364,7 @@ define(
else {
// mobile的click/move/up/down自己模拟
root.addEventListener('click', this._clickHandler);
root.addEventListener('dblclick', this._dblclickHandler);
root.addEventListener('mousewheel', this._mousewheelHandler);
root.addEventListener('mousemove', this._mousemoveHandler);
root.addEventListener('mousedown', this._mousedownHandler);
Expand All @@ -351,6 +377,7 @@ define(
window.attachEvent('onresize', this._resizeHandler);

root.attachEvent('onclick', this._clickHandler);
root.attachEvent('ondblclick ', this._dblclickHandler);
root.attachEvent('onmousewheel', this._mousewheelHandler);
root.attachEvent('onmousemove', this._mousemoveHandler);
root.attachEvent('onmouseout', this._mouseoutHandler);
Expand Down Expand Up @@ -388,6 +415,7 @@ define(
switch (eventName) {
case EVENT.RESIZE:
case EVENT.CLICK:
case EVENT.DBLCLICK:
case EVENT.MOUSEWHEEL:
case EVENT.MOUSEMOVE:
case EVENT.MOUSEDOWN:
Expand Down Expand Up @@ -627,7 +655,8 @@ define(
}

while (el) {
el[eventHandler] && el[eventHandler](eventPacket);
el[eventHandler]
&& (eventPacket.cancelBubble = el[eventHandler](eventPacket));
el.dispatch(eventName, eventPacket);

el = el.parent;
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ define(
EVENT : { // 支持事件列表
RESIZE : 'resize', // 窗口大小变化
CLICK : 'click', // 鼠标按钮被(手指)按下,事件对象是:目标图形元素或空
DBLCLICK : 'dblclick', // 双击事件

MOUSEWHEEL : 'mousewheel', // 鼠标滚轮变化,事件对象是:目标图形元素或空
MOUSEMOVE : 'mousemove', // 鼠标(手指)被移动,事件对象是:目标图形元素或空
Expand Down
1 change: 1 addition & 0 deletions src/tool/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ define(
? function (e) {
e.preventDefault();
e.stopPropagation();
e.cancelBubble = true;
}
: function (e) {
e.returnValue = false;
Expand Down

0 comments on commit 1d158bd

Please sign in to comment.