UNPKG

3.9 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports["default"] = void 0;
7
8// 图片点击事件
9function octoParseImgTap(e) {
10 var that = this;
11 var nowImgUrl = e.currentTarget.dataset.src;
12 var tagFrom = e.currentTarget.dataset.from;
13
14 if (typeof tagFrom != 'undefined' && tagFrom.length > 0) {
15 wx.previewImage({
16 current: nowImgUrl,
17 // 当前显示图片的http链接
18 // urls: that.data[tagFrom].imageUrls // 需要预览的图片http链接列表
19 urls: [nowImgUrl]
20 });
21 }
22} /// a 跳转事件
23
24
25function octoParseTagATap(e) {
26 var href = e.currentTarget.dataset.src;
27} /// 默认事件
28
29
30function defaultEvent(e) {}
31
32function mountDefaultAction(wx) {
33 wx.octoParseImgTap = octoParseImgTap;
34 wx.octoParseTagATap = octoParseTagATap;
35 wx.defaultEvent = defaultEvent;
36} /////eventMap增加事件
37
38
39function addEvent2EventMap(eventMap, opt) {
40 if (opt.filter) {
41 if (opt.filter.indexOf('#') === 0) {
42 var id = opt.filter.slice(1);
43
44 if (!eventMap['id'][id]) {
45 eventMap['id'][id] = {}; // eventMap['id'][id] = Object.assign(eventMap['id'][id], opt.events);
46 }
47
48 eventMap['id'][id] = Object.assign(eventMap['id'][id], opt.events);
49 } else if (opt.filter.indexOf('.') === 0) {
50 var className = opt.filter.slice(1);
51
52 if (!eventMap['class'][className]) {
53 eventMap['class'][className] = {}; // eventMap['class'][className] = Object.assign(eventMap['class'][className], opt.events);
54 }
55
56 eventMap['class'][className] = Object.assign(eventMap['class'][className], opt.events);
57 }
58 } else if (!opt.filter) {
59 eventMap['base'] = Object.assign(eventMap['base'], opt.events);
60 }
61}
62
63function findClassInRule(ruleClass, classStr) {
64 var res = '';
65 if (!classStr) return res;
66 var classArray = classStr.split(' ');
67 var length = classArray.length;
68
69 for (var i = length - 1; i >= 0; i--) {
70 var className = classArray[i].slice(1);
71
72 if (ruleClass[className]) {
73 res = className;
74 break;
75 }
76 }
77
78 return res;
79} /////返回默认方法
80
81
82function defaultEventName(e) {
83 switch (e.type) {
84 case 'tap':
85 if (e.currentTarget.dataset.tag == 'img') {
86 octoParseImgTap(e);
87 } else if (e.currentTarget.dataset.tag == 'a') {
88 octoParseTagATap(e);
89 }
90
91 break;
92
93 default:
94 defaultEvent(e);
95 }
96} ////生成挂载代理事件
97
98
99function generateAgentFunc(optionArray) {
100 var rule = {};
101
102 if (Array.isArray(optionArray)) {
103 optionArray.forEach(function (opt) {
104 if (!rule[opt.tag]) {
105 rule[opt.tag] = {
106 base: {},
107 "class": {},
108 id: {}
109 };
110 addEvent2EventMap(rule[opt.tag], opt);
111 } else {
112 addEvent2EventMap(rule[opt.tag], opt);
113 }
114 });
115 }
116
117 return function (e) {
118 var dataset = e.currentTarget.dataset;
119
120 if (dataset && rule[dataset.tag]) {
121 if (rule[dataset.tag]['id'][dataset.id]) {
122 rule[dataset.tag]['id'][dataset.id][e.type](e);
123 } else if (findClassInRule(rule[dataset.tag]['class'], dataset["class"])) {
124 var className = findClassInRule(rule[dataset.tag]['class'], dataset["class"]);
125 rule[dataset.tag]['class'][className][e.type](e);
126 } else if (typeof rule[dataset.tag]['base'][e.type] == 'function') {
127 rule[dataset.tag]['base'][e.type](e);
128 } else {
129 defaultEventName(e);
130 }
131 } else {
132 return defaultEventName(e);
133 }
134 };
135} /////事件挂载函数
136
137
138function mountAction(wx, optionArray) {
139 // mountDefaultAction(wx); ////挂载默认事件
140 // registerEvent2Page(wx, optionArray); ////在page上注册所有的事件
141 var agentFunc = generateAgentFunc(optionArray);
142 wx.agentFunc = agentFunc; ////注册代理事件
143} // module.exports = {
144// mountAction: mountAction
145// }
146
147
148var _default = mountAction;
149exports["default"] = _default;
\No newline at end of file