|
16 | 16 | doc = document
|
17 | 17 |
|
18 | 18 | //对事件集筛选触发
|
| 19 | + /** |
| 20 | + * |
| 21 | + * @param {String} eventname |
| 22 | + * @param {Event} e |
| 23 | + */ |
19 | 24 | let emit = function (eventname, e) {
|
20 | 25 | // console.log(eventname)
|
21 | 26 | let events = this._events,
|
|
38 | 43 | if (rest_events.length === 0) return
|
39 | 44 | }
|
40 | 45 | //用类名代理 判断。
|
| 46 | + /** |
| 47 | + * @param {Boolean} node |
| 48 | + * @param {String} classname |
| 49 | + * @return {Boolean} |
| 50 | + */ |
41 | 51 | function hasp(node = false, classname = '') {
|
42 | 52 | if (!classname) return false
|
43 |
| - var node = node || false |
44 | 53 | while (node) {
|
45 | 54 | if (node === this._ele.parentNode) {
|
46 | 55 | return false;
|
|
53 | 62 | return false
|
54 | 63 | }
|
55 | 64 | //计算方位返回时间类型
|
| 65 | + /** |
| 66 | + * @api public |
| 67 | + * @param {Number} x1 |
| 68 | + * @param {Number} x2 |
| 69 | + * @param {Number} y1 |
| 70 | + * @param {Number} y2 |
| 71 | + * @return {String} |
| 72 | + */ |
56 | 73 | function countposition(x1, x2, y1, y2) {
|
57 | 74 | // 先判断垂直方向水平方向
|
58 | 75 | // 再判断上下左右
|
59 | 76 | return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'swipeleft' : 'swiperight') : (y1 - y2 > 0 ? 'swipeup' : 'swipedown')
|
60 | 77 | }
|
| 78 | + /** |
| 79 | + * @api public |
| 80 | + * @param {Number} x1 |
| 81 | + * @param {Number} x2 |
| 82 | + * @param {Number} y1 |
| 83 | + * @param {Number} y2 |
| 84 | + * @return {String} |
| 85 | + */ |
61 | 86 | function countpositioning(x1, x2, y1, y2) {
|
62 | 87 | return Math.abs(x1 - x2) >= Math.abs(y1 - y2) ? (x1 - x2 > 0 ? 'swipingleft' : 'swipingright') : (y1 - y2 > 0 ? 'swipingup' : 'swipingdown')
|
63 | 88 | }
|
64 | 89 | //事件触发 对事件做二次打包
|
| 90 | + /** |
| 91 | + * |
| 92 | + * @param {String} name |
| 93 | + * @param {Function} fn |
| 94 | + * @param {HTML Element} dom |
| 95 | + * @param {Event} e |
| 96 | + * @return {Boolean|undefined} |
| 97 | + */ |
65 | 98 | let event_pack = function (name, fn, dom, e) {
|
66 | 99 | // e.stopPropagation();
|
67 | 100 | let new_event = {
|
|
78 | 111 | return call_res
|
79 | 112 | }
|
80 | 113 | //事件监听主要逻辑
|
| 114 | + /** |
| 115 | + * @api public |
| 116 | + * @param {HTML Element} dom |
| 117 | + * @return {Function} |
| 118 | + * @description return value is a function to removeEventListener |
| 119 | + */ |
81 | 120 | let eventListener = function (dom) {
|
82 | 121 |
|
83 | 122 | let self = this;
|
|
90 | 129 | eventmark = null,
|
91 | 130 | longtap,//用于长按
|
92 | 131 | tap//用于单击
|
93 |
| - |
| 132 | + /** |
| 133 | + * @description clearTimeout and make isAction change flase |
| 134 | + */ |
94 | 135 | function actionfunc() {
|
95 | 136 | isActive = false;
|
96 | 137 | clearTimeout(touchDelay);
|
97 | 138 | clearTimeout(longtap);
|
98 | 139 | }
|
| 140 | + /** |
| 141 | + * @param {Event} e |
| 142 | + * @description add Event Listener |
| 143 | + */ |
99 | 144 | function touchstart(e) {
|
100 | 145 | var e = e || window.event
|
101 | 146 | if (self.stopPropagation) {
|
102 | 147 | e.stopPropagation();
|
| 148 | + e.preventDefault(); |
103 | 149 | }
|
104 | 150 | eventmark = e || win.event;
|
105 | 151 | starttime = new Date();
|
|
117 | 163 | }, 500)
|
118 | 164 |
|
119 | 165 | }
|
| 166 | + /** |
| 167 | + * @param {Event} e |
| 168 | + * @description add Event Listener |
| 169 | + */ |
120 | 170 | function touchmove(e) {
|
121 | 171 |
|
122 | 172 | var e = e || window.event
|
123 | 173 | if (self.stopPropagation) {
|
124 | 174 | e.stopPropagation();
|
| 175 | + e.preventDefault(); |
125 | 176 | }
|
126 | 177 |
|
127 | 178 | eventmark = e
|
|
135 | 186 | emit.call(self, 'swiping', eventmark);
|
136 | 187 | emit.call(self, countpositioning(x1, x2, y1, y2), eventmark);
|
137 | 188 | } else {
|
138 |
| - if (!isActive) return; |
| 189 | + if (!isActive){ |
| 190 | + return false; |
| 191 | + } |
| 192 | + // console.log('asdasdsa') |
139 | 193 | emit.call(self, 'singletap', e);
|
140 | 194 | actionfunc()
|
141 | 195 | }
|
142 | 196 |
|
143 | 197 | }
|
| 198 | + /** |
| 199 | + * @param {Event} e |
| 200 | + * @description add Event Listener |
| 201 | + */ |
144 | 202 | function touchend(e) {
|
145 | 203 | var e = e || window.event
|
146 | 204 | if (self.stopPropagation) {
|
147 | 205 | e.stopPropagation();
|
| 206 | + e.preventDefault(); |
148 | 207 | }
|
149 | 208 |
|
150 | 209 | if (!isActive) {
|
|
159 | 218 | //断定此次事件为连续两次轻击事件
|
160 | 219 | emit.call(self, 'doubletap', eventmark);
|
161 | 220 | // console.log('123')
|
162 |
| - } else if (y2 > 0 && x2 > 0 && (Math.abs(x1 - x2) > 100 || Math.abs(y1 - y2) > 100)) { |
| 221 | + } else if (y2 > 0 && x2 > 0 && (Math.abs(x1 - x2) > 10 || Math.abs(y1 - y2) > 10)) { |
163 | 222 | // console.log('2223')
|
164 | 223 | // console.log(x1,x2,y1,y2)
|
165 | 224 | emit.call(self, countposition(x1, x2, y1, y2), eventmark);
|
|
190 | 249 | //touch主事件
|
191 | 250 | //内置对象_event存放触发的时间
|
192 | 251 | //ele对应初始化元素节点
|
193 |
| - var _touch = function (element, stopPropagation) { |
| 252 | + /** |
| 253 | + * @param {HTML Element} element |
| 254 | + * @param {Boolean} stopPropagation |
| 255 | + */ |
| 256 | + const _touch = function (element, stopPropagation=true) { |
194 | 257 | if (!(this instanceof _touch)) return new _touch(element, stopPropagation);
|
195 | 258 | this._events = {};
|
196 | 259 | this._ele = element;
|
|
223 | 286 | _touch.prototype._remove = function (eventname, name) {
|
224 | 287 | if (!(this instanceof _touch)) throw new Error('error use');
|
225 | 288 |
|
226 |
| - var _events = this._events; |
| 289 | + let _events = this._events; |
227 | 290 |
|
228 | 291 | if (_events[eventname] === void 0 && (!name)) throw new Error('不存在这个事件')
|
229 | 292 | let len = _events[eventname]
|
|
0 commit comments