1 /** 2 * @fileOverview utils for event 3 * @author yiminghe@gmail.com 4 */ 5 KISSY.add("event/utils", function (S, DOM) { 6 7 /** 8 * whether two event listens are the same 9 * @param h1 已有的 handler 描述 10 * @param h2 用户提供的 handler 描述 11 */ 12 function isIdenticalHandler(h1, h2, el) { 13 var scope1 = h1.scope || el, 14 ret = 1, 15 scope2 = h2.scope || el; 16 if ( 17 h1.fn !== h2.fn || 18 h1.selector !== h2.selector || 19 h1.data !== h2.data || 20 scope1 !== scope2 || 21 h1.originalType !== h2.originalType || 22 h1.groups !== h2.groups || 23 h1.last !== h2.last 24 ) { 25 ret = 0; 26 } 27 return ret; 28 } 29 30 31 function isValidTarget(target) { 32 // 3 - is text node 33 // 8 - is comment node 34 return target && 35 target.nodeType !== DOM.TEXT_NODE && 36 target.nodeType !== DOM.COMMENT_NODE; 37 } 38 39 40 function batchForType(fn, targets, types) { 41 // on(target, 'click focus', fn) 42 if (types && types.indexOf(" ") > 0) { 43 var args = S.makeArray(arguments); 44 S.each(types.split(/\s+/), function (type) { 45 var args2 = [].concat(args); 46 args2.splice(0, 3, targets, type); 47 fn.apply(null, args2); 48 }); 49 return true; 50 } 51 return 0; 52 } 53 54 55 function splitAndRun(type, fn) { 56 S.each(type.split(/\s+/), fn); 57 } 58 59 var doc = S.Env.host.document, 60 simpleAdd = doc.addEventListener ? 61 function (el, type, fn, capture) { 62 if (el.addEventListener) { 63 el.addEventListener(type, fn, !!capture); 64 } 65 } : 66 function (el, type, fn) { 67 if (el.attachEvent) { 68 el.attachEvent('on' + type, fn); 69 } 70 }, 71 simpleRemove = doc.removeEventListener ? 72 function (el, type, fn, capture) { 73 if (el.removeEventListener) { 74 el.removeEventListener(type, fn, !!capture); 75 } 76 } : 77 function (el, type, fn) { 78 if (el.detachEvent) { 79 el.detachEvent('on' + type, fn); 80 } 81 }; 82 83 84 return { 85 // 记录手工 fire(domElement,type) 时的 type 86 // 再在浏览器通知的系统 eventHandler 中检查 87 // 如果相同,那么证明已经 fire 过了,不要再次触发了 88 Event_Triggered:"", 89 TRIGGERED_NONE:"trigger-none-" + S.now(), 90 EVENT_GUID:'ksEventTargetId' + S.now(), 91 splitAndRun:splitAndRun, 92 batchForType:batchForType, 93 isValidTarget:isValidTarget, 94 isIdenticalHandler:isIdenticalHandler, 95 simpleAdd:simpleAdd, 96 simpleRemove:simpleRemove, 97 getTypedGroups:function (type) { 98 if (type.indexOf(".") < 0) { 99 return [type, ""]; 100 } 101 var m = type.match(/([^.]+)?(\..+)?$/), 102 t = m[1], 103 ret = [t], 104 gs = m[2]; 105 if (gs) { 106 gs = gs.split(".").sort(); 107 ret.push(gs.join(".")); 108 } else { 109 ret.push(""); 110 } 111 return ret; 112 }, 113 getGroupsRe:function (groups) { 114 return new RegExp(groups.split(".").join(".*\\.") + "(?:\\.|$)"); 115 } 116 }; 117 118 }, { 119 requires:['dom'] 120 });