1 | define(["dojo/_base/lang", "../_base", "dojo/_base/config", "dojo/_base/window", "dojo/on", "dojo/touch" |
---|
2 | ], function(lang, dxa, config, window, on, touch){ |
---|
3 | |
---|
4 | |
---|
5 | return (dxa.plugins.touchMove = new (function(){ |
---|
6 | if(config["watchTouch"] !== undefined && !config["watchTouch"]){ |
---|
7 | this.watchTouch = false; |
---|
8 | }else{ |
---|
9 | this.watchTouch = true; |
---|
10 | } |
---|
11 | if(config["showTouchesDetails"] !== undefined && !config["showTouchesDetails"]){ |
---|
12 | this.showTouchesDetails = false; |
---|
13 | }else{ |
---|
14 | this.showTouchesDetails = true; |
---|
15 | } |
---|
16 | this.touchSampleDelay = config["touchSampleDelay"] || 1000; |
---|
17 | this.targetProps = config["targetProps"] || ["id","className","localName","href", "spellcheck", "lang", "textContent", "value" ]; |
---|
18 | this.textContentMaxChars = config["textContentMaxChars"] || 50; |
---|
19 | this.addData = lang.hitch(dxa, "addData", "touch.move"); |
---|
20 | |
---|
21 | this.sampleTouchMove = function(e){ |
---|
22 | if(!this._rateLimited){ |
---|
23 | this.addData("sample",this.trimTouchEvent(e)); |
---|
24 | this._rateLimited = true; |
---|
25 | setTimeout(lang.hitch(this, function(){ |
---|
26 | if(this._rateLimited){ |
---|
27 | this.trimTouchEvent(this._lastTouchEvent); |
---|
28 | delete this._lastTouchEvent; |
---|
29 | delete this._rateLimited; |
---|
30 | } |
---|
31 | }), this.touchSampleDelay); |
---|
32 | } |
---|
33 | this._lastTouchEvent = e; |
---|
34 | return e; |
---|
35 | }; |
---|
36 | |
---|
37 | on(window.doc, touch.move, lang.hitch(this, "sampleTouchMove")); |
---|
38 | |
---|
39 | this.handleTarget = function(t, target, i){ |
---|
40 | var props = this.targetProps; |
---|
41 | t[i] = {}; |
---|
42 | for(var j = 0;j < props.length;j++){ |
---|
43 | if((typeof target == "object" || typeof target == "function") && props[j] in target){ |
---|
44 | |
---|
45 | if(props[j] == "text" || props[j] == "textContent"){ |
---|
46 | if(target["localName"] && (target["localName"] != "HTML") && (target["localName"] != "BODY")){ |
---|
47 | t[i][props[j]] = target[props[j]].substr(0,this.textContentMaxChars); |
---|
48 | } |
---|
49 | }else{ |
---|
50 | t[i][props[j]] = target[props[j]]; |
---|
51 | } |
---|
52 | } |
---|
53 | } |
---|
54 | }; |
---|
55 | |
---|
56 | this.trimTouchEvent = function(e){ |
---|
57 | var t = {}; |
---|
58 | var val; |
---|
59 | for(var i in e){ |
---|
60 | switch(i){ |
---|
61 | case "target": |
---|
62 | this.handleTarget(t, e[i], i) |
---|
63 | break; |
---|
64 | case "touches": |
---|
65 | if(e[i].length !== 0){ |
---|
66 | t["touches.length"] = e[i].length; |
---|
67 | } |
---|
68 | if(this.showTouchesDetails){ |
---|
69 | for(var j = 0;j < e[i].length;j++){ |
---|
70 | for(var s in e[i][j]){ |
---|
71 | switch(s){ |
---|
72 | case "target": |
---|
73 | this.handleTarget(t, e[i][j].target, "touches["+j+"][target]"); |
---|
74 | break; |
---|
75 | case "clientX": |
---|
76 | case "clientY": |
---|
77 | case "screenX": |
---|
78 | case "screenY": |
---|
79 | if(e[i][j]){ |
---|
80 | val = e[i][j][s]; |
---|
81 | t["touches["+j+"]["+s+"]"] = val + ''; |
---|
82 | } |
---|
83 | break; |
---|
84 | default: |
---|
85 | //console.log("Skipping: ", i); |
---|
86 | break; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | break; |
---|
92 | case "clientX": |
---|
93 | case "clientY": |
---|
94 | case "screenX": |
---|
95 | case "screenY": |
---|
96 | if(e[i]){ |
---|
97 | val = e[i]; |
---|
98 | t[i] = val + ''; |
---|
99 | } |
---|
100 | break; |
---|
101 | default: |
---|
102 | //console.log("Skipping: ", i); |
---|
103 | break; |
---|
104 | } |
---|
105 | } |
---|
106 | return t; |
---|
107 | }; |
---|
108 | })()); |
---|
109 | }); |
---|