Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/emulation/TouchModel.js

Issue 1739533005: [DevTools] Add touch to sensors view. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /**
6 * @constructor
7 * @implements {WebInspector.TargetManager.Observer}
8 */
9 WebInspector.MultitargetTouchModel = function()
10 {
11 this._touchEnabled = false;
12 this._touchMobile = false;
13 this._customTouchEnabled = false;
14
15 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Type.Pag e);
16 }
17
18 WebInspector.MultitargetTouchModel._symbol = Symbol("MultitargetTouchModel.symbo l");
19
20 WebInspector.MultitargetTouchModel.prototype = {
21 /**
22 * @param {boolean} enabled
23 * @param {boolean} mobile
24 */
25 setTouchEnabled: function(enabled, mobile)
26 {
27 this._touchEnabled = enabled;
28 this._touchMobile = mobile;
29 this._updateAllTargets();
30 },
31
32 /**
33 * @param {boolean} enabled
34 */
35 setCustomTouchEnabled: function(enabled)
36 {
37 this._customTouchEnabled = enabled;
38 this._updateAllTargets();
39 },
40
41 _updateAllTargets: function()
42 {
43 for (var target of WebInspector.targetManager.targets(WebInspector.Targe t.Type.Page))
44 this._applyToTarget(target);
45 },
46
47 /**
48 * @param {!WebInspector.Target} target
49 */
50 _applyToTarget: function(target)
51 {
52 var current = {enabled: this._touchEnabled, configuration : this._touchM obile ? "mobile" : "desktop"};
53 if (this._customTouchEnabled)
54 current = {enabled: true, configuration: "mobile"};
55
56 var domModel = WebInspector.DOMModel.fromTarget(target);
57 var inspectModeEnabled = domModel ? domModel.inspectModeEnabled() : fals e;
58 if (inspectModeEnabled)
59 current = {enabled: false, configuration: "mobile"};
60
61 /**
62 * @suppressGlobalPropertiesCheck
63 */
64 const injectedFunction = function() {
65 const touchEvents = ["ontouchstart", "ontouchend", "ontouchmove", "o ntouchcancel"];
66 var recepients = [window.__proto__, document.__proto__];
67 for (var i = 0; i < touchEvents.length; ++i) {
68 for (var j = 0; j < recepients.length; ++j) {
69 if (!(touchEvents[i] in recepients[j]))
70 Object.defineProperty(recepients[j], touchEvents[i], { v alue: null, writable: true, configurable: true, enumerable: true });
71 }
72 }
73 };
74
75 var symbol = WebInspector.MultitargetTouchModel._symbol;
76 var previous = target[symbol] || {enabled: false, configuration: "mobile ", scriptId: ""};
77
78 if (previous.enabled === current.enabled && (!current.enabled || previou s.configuration === current.configuration))
79 return;
80
81 if (previous.scriptId) {
82 target.pageAgent().removeScriptToEvaluateOnLoad(previous.scriptId);
83 target[symbol].scriptId = "";
84 }
85
86 target[symbol] = current;
87 target[symbol].scriptId = "";
88
89 if (current.enabled)
90 target.pageAgent().addScriptToEvaluateOnLoad("(" + injectedFunction. toString() + ")()", scriptAddedCallback);
91
92 /**
93 * @param {?Protocol.Error} error
94 * @param {string} scriptId
95 */
96 function scriptAddedCallback(error, scriptId)
97 {
98 (target[symbol] || {}).scriptId = error ? "" : scriptId;
99 }
100
101 target.emulationAgent().setTouchEmulationEnabled(current.enabled, curren t.configuration);
102 },
103
104 /**
105 * @param {!WebInspector.Event} event
106 */
107 _inspectModeToggled: function(event)
108 {
109 var domModel = /** @type {!WebInspector.DOMModel} */ (event.target);
110 this._applyToTarget(domModel.target());
111 },
112
113 /**
114 * @override
115 * @param {!WebInspector.Target} target
116 */
117 targetAdded: function(target)
118 {
119 var domModel = WebInspector.DOMModel.fromTarget(target);
120 if (domModel)
121 domModel.addEventListener(WebInspector.DOMModel.Events.InspectModeWi llBeToggled, this._inspectModeToggled, this);
122 this._applyToTarget(target);
123 },
124
125 /**
126 * @override
127 * @param {!WebInspector.Target} target
128 */
129 targetRemoved: function(target)
130 {
131 var domModel = WebInspector.DOMModel.fromTarget(target);
132 if (domModel)
133 domModel.removeEventListener(WebInspector.DOMModel.Events.InspectMod eWillBeToggled, this._inspectModeToggled, this);
134 }
135 }
136
137
138 /** @type {?WebInspector.MultitargetTouchModel} */
139 WebInspector.MultitargetTouchModel._instance = null;
140
141 /**
142 * @return {!WebInspector.MultitargetTouchModel}
143 */
144 WebInspector.MultitargetTouchModel.instance = function()
145 {
146 if (!WebInspector.MultitargetTouchModel._instance)
147 WebInspector.MultitargetTouchModel._instance = new WebInspector.Multitar getTouchModel();
148 return /** @type {!WebInspector.MultitargetTouchModel} */ (WebInspector.Mult itargetTouchModel._instance);
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698