OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 WebInspector.OverridesUI = {} | |
6 | |
7 /** | |
8 * @constructor | |
9 * @param {!Element} rotateButton | |
10 * @param {?function(!WebInspector.EmulatedDevice, !WebInspector.EmulatedDevice.
Mode)} callback | |
11 */ | |
12 WebInspector.DeviceSelect = function(rotateButton, callback) | |
13 { | |
14 this._callback = callback; | |
15 this._rotateButton = rotateButton; | |
16 this.element = createElement("p"); | |
17 | |
18 this._deviceSelectElement = this.element.createChild("select", "device-selec
t"); | |
19 this._deviceSelectElement.addEventListener("change", this._deviceSelected.bi
nd(this), false); | |
20 | |
21 var container = this.element.createChild("div", "mode-container"); | |
22 container.appendChild(this._rotateButton); | |
23 this._rotateButton.addEventListener("click", this._rotateButtonClicked.bind(
this), false); | |
24 this._rotateButton.title = WebInspector.UIString("Change orientation"); | |
25 | |
26 var modeSelectContainer = container.createChild("span", "mode-select"); | |
27 this._modeSelectElement = modeSelectContainer.createChild("select"); | |
28 this._modeSelectElement.addEventListener("change", this._modeSelected.bind(t
his), false); | |
29 this._modeLabelElement = modeSelectContainer.createChild("label"); | |
30 this._modeLabelElement.addEventListener("click", this._rotateButtonClicked.b
ind(this), false); | |
31 this._modeLabelElement.title = WebInspector.UIString("Change orientation"); | |
32 | |
33 this._emulatedSettingChangedMuted = false; | |
34 this._lastOrientation = null; | |
35 | |
36 WebInspector.overridesSupport.settings.emulateResolution.addChangeListener(t
his._emulatedSettingChanged, this); | |
37 WebInspector.overridesSupport.settings.deviceWidth.addChangeListener(this._e
mulatedSettingChanged, this); | |
38 WebInspector.overridesSupport.settings.deviceHeight.addChangeListener(this._
emulatedSettingChanged, this); | |
39 WebInspector.overridesSupport.settings.deviceScaleFactor.addChangeListener(t
his._emulatedSettingChanged, this); | |
40 WebInspector.overridesSupport.settings.emulateMobile.addChangeListener(this.
_emulatedSettingChanged, this); | |
41 WebInspector.overridesSupport.settings.emulateTouch.addChangeListener(this._
emulatedSettingChanged, this); | |
42 WebInspector.overridesSupport.settings.userAgent.addChangeListener(this._emu
latedSettingChanged, this); | |
43 | |
44 WebInspector.emulatedDevicesList.addEventListener(WebInspector.EmulatedDevic
esList.Events.CustomDevicesUpdated, this._deviceListChanged, this); | |
45 WebInspector.emulatedDevicesList.addEventListener(WebInspector.EmulatedDevic
esList.Events.StandardDevicesUpdated, this._deviceListChanged, this); | |
46 this._deviceListChanged(); | |
47 } | |
48 | |
49 WebInspector.DeviceSelect.prototype = { | |
50 _deviceListChanged: function() | |
51 { | |
52 this._deviceSelectElement.removeChildren(); | |
53 | |
54 var selectDeviceOption = new Option(WebInspector.UIString("<Select model
>"), WebInspector.UIString("<Select model>")); | |
55 selectDeviceOption.device = null; | |
56 selectDeviceOption.lastSelectedIndex = 0; | |
57 selectDeviceOption.disabled = true; | |
58 this._deviceSelectElement.appendChild(selectDeviceOption); | |
59 | |
60 this._addDeviceGroup(WebInspector.UIString("Custom"), WebInspector.emula
tedDevicesList.custom()); | |
61 this._addDeviceGroup(WebInspector.UIString("Devices"), WebInspector.emul
atedDevicesList.standard()); | |
62 this._emulatedSettingChanged(); | |
63 }, | |
64 | |
65 /** | |
66 * @param {string} name | |
67 * @param {!Array.<!WebInspector.EmulatedDevice>} devices | |
68 */ | |
69 _addDeviceGroup: function(name, devices) | |
70 { | |
71 devices = devices.filter(function (d) { return d.show(); }); | |
72 if (!devices.length) | |
73 return; | |
74 devices.sort(WebInspector.EmulatedDevice.deviceComparator); | |
75 var groupElement = this._deviceSelectElement.createChild("optgroup"); | |
76 groupElement.label = name; | |
77 for (var i = 0; i < devices.length; ++i) { | |
78 var option = new Option(devices[i].title, devices[i].title); | |
79 option.device = devices[i]; | |
80 option.lastSelectedIndex = 0; | |
81 groupElement.appendChild(option); | |
82 } | |
83 }, | |
84 | |
85 _emulatedSettingChanged: function() | |
86 { | |
87 if (this._emulatedSettingChangedMuted) | |
88 return; | |
89 | |
90 for (var i = 1; i < this._deviceSelectElement.options.length; ++i) { | |
91 var option = this._deviceSelectElement.options[i]; | |
92 var device = /** @type {!WebInspector.EmulatedDevice} */ (option.dev
ice); | |
93 for (var j = 0; j < device.modes.length; j++) { | |
94 if (WebInspector.overridesSupport.isEmulatingDevice(device.modeT
oOverridesDevice(device.modes[j]))) { | |
95 this._select(device, device.modes[j]); | |
96 return; | |
97 } | |
98 } | |
99 } | |
100 | |
101 this._select(null, null); | |
102 }, | |
103 | |
104 /** | |
105 * @param {?WebInspector.EmulatedDevice} device | |
106 * @param {?WebInspector.EmulatedDevice.Mode} mode | |
107 */ | |
108 _select: function(device, mode) | |
109 { | |
110 for (var i = 0; i < this._deviceSelectElement.options.length; i++) { | |
111 if (this._deviceSelectElement.options[i].device === device) | |
112 this._deviceSelectElement.selectedIndex = i; | |
113 } | |
114 this._updateModeSelect(); | |
115 for (var i = 0; i < this._modeSelectElement.options.length; i++) { | |
116 if (this._modeSelectElement.options[i].mode === mode) | |
117 this._modeSelectElement.selectedIndex = i; | |
118 } | |
119 this._updateModeControls(); | |
120 this._saveLastSelectedIndex(); | |
121 if (this._callback) { | |
122 var option = this._modeSelectElement.options[this._modeSelectElement
.selectedIndex]; | |
123 this._callback(option.device, option.mode); | |
124 } | |
125 }, | |
126 | |
127 _deviceSelected: function() | |
128 { | |
129 this._updateModeSelect(); | |
130 this._modeSelected(); | |
131 }, | |
132 | |
133 _updateModeSelect: function() | |
134 { | |
135 this._modeSelectElement.removeChildren(); | |
136 var option = this._deviceSelectElement.options[this._deviceSelectElement
.selectedIndex]; | |
137 var device = /** @type {!WebInspector.EmulatedDevice} */ (option.device)
; | |
138 | |
139 if (this._deviceSelectElement.selectedIndex === 0) { | |
140 this._addMode(device, null, ""); | |
141 } else if (device.modes.length === 1) { | |
142 this._addMode(device, device.modes[0], WebInspector.UIString("Defaul
t")); | |
143 } else { | |
144 this._addOrientation(device, WebInspector.EmulatedDevice.Vertical, W
ebInspector.UIString("Portrait")); | |
145 this._addOrientation(device, WebInspector.EmulatedDevice.Horizontal,
WebInspector.UIString("Landscape")); | |
146 } | |
147 this._updateRotateModes(); | |
148 | |
149 var index = option.lastSelectedIndex; | |
150 var modeOption = this._modeSelectElement.options[index]; | |
151 if (modeOption.rotateIndex != -1) { | |
152 var rotateOption = this._modeSelectElement.options[modeOption.rotate
Index]; | |
153 if (rotateOption.mode && rotateOption.mode.orientation === this._las
tOrientation) | |
154 index = modeOption.rotateIndex; | |
155 } | |
156 this._modeSelectElement.selectedIndex = index; | |
157 this._updateModeControls(); | |
158 }, | |
159 | |
160 /** | |
161 * @param {!WebInspector.EmulatedDevice} device | |
162 * @param {string} orientation | |
163 * @param {string} title | |
164 */ | |
165 _addOrientation: function(device, orientation, title) | |
166 { | |
167 var modes = device.modesForOrientation(orientation); | |
168 if (!modes.length) | |
169 return; | |
170 if (modes.length === 1) { | |
171 this._addMode(device, modes[0], title); | |
172 } else { | |
173 for (var index = 0; index < modes.length; index++) | |
174 this._addMode(device, modes[index], title + " \u2013 " + modes[i
ndex].title); | |
175 } | |
176 }, | |
177 | |
178 /** | |
179 * @param {!WebInspector.EmulatedDevice} device | |
180 * @param {?WebInspector.EmulatedDevice.Mode} mode | |
181 * @param {string} title | |
182 */ | |
183 _addMode: function(device, mode, title) | |
184 { | |
185 var option = new Option(title, title); | |
186 option.mode = mode; | |
187 option.device = device; | |
188 this._modeSelectElement.appendChild(option); | |
189 }, | |
190 | |
191 _updateRotateModes: function() | |
192 { | |
193 for (var i = 0; i < this._modeSelectElement.options.length; i++) { | |
194 var modeI = this._modeSelectElement.options[i].mode; | |
195 this._modeSelectElement.options[i].rotateIndex = -1; | |
196 for (var j = 0; j < this._modeSelectElement.options.length; j++) { | |
197 var modeJ = this._modeSelectElement.options[j].mode; | |
198 if (modeI && modeJ && modeI.orientation !== modeJ.orientation &&
modeI.title === modeJ.title) | |
199 this._modeSelectElement.options[i].rotateIndex = j; | |
200 } | |
201 } | |
202 }, | |
203 | |
204 _updateModeControls: function() | |
205 { | |
206 this._modeLabelElement.textContent = this._modeSelectElement.options[thi
s._modeSelectElement.selectedIndex].label; | |
207 | |
208 if (this._modeSelectElement.options.length <= 1) { | |
209 this._modeSelectElement.classList.toggle("hidden", true); | |
210 this._modeLabelElement.classList.toggle("hidden", true); | |
211 } else { | |
212 var showLabel = this._modeSelectElement.options.length === 2 && this
._modeSelectElement.options[0].rotateIndex === 1; | |
213 this._modeSelectElement.classList.toggle("hidden", showLabel); | |
214 this._modeLabelElement.classList.toggle("hidden", !showLabel); | |
215 } | |
216 | |
217 this._rotateButton.classList.toggle("hidden", this._modeSelectElement.op
tions[this._modeSelectElement.selectedIndex].rotateIndex === -1); | |
218 }, | |
219 | |
220 _modeSelected: function() | |
221 { | |
222 this._saveLastSelectedIndex(); | |
223 this._updateModeControls(); | |
224 var option = this._modeSelectElement.options[this._modeSelectElement.sel
ectedIndex]; | |
225 if (this._callback) | |
226 this._callback(option.device, option.mode); | |
227 this._emulatedSettingChangedMuted = true; | |
228 WebInspector.overridesSupport.emulateDevice(option.device.modeToOverride
sDevice(option.mode)); | |
229 this._emulatedSettingChangedMuted = false; | |
230 }, | |
231 | |
232 _saveLastSelectedIndex: function() | |
233 { | |
234 this._deviceSelectElement.options[this._deviceSelectElement.selectedInde
x].lastSelectedIndex = this._modeSelectElement.selectedIndex; | |
235 | |
236 var option = this._modeSelectElement.options[this._modeSelectElement.sel
ectedIndex]; | |
237 if (option.mode && option.rotateIndex != -1) | |
238 this._lastOrientation = option.mode.orientation; | |
239 }, | |
240 | |
241 _rotateButtonClicked: function() | |
242 { | |
243 this._modeSelectElement.selectedIndex = this._modeSelectElement.options[
this._modeSelectElement.selectedIndex].rotateIndex; | |
244 this._modeSelected(); | |
245 } | |
246 } | |
OLD | NEW |