OverridesView.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @extends {WebInspector.View}
  33. */
  34. WebInspector.OverridesView = function()
  35. {
  36. WebInspector.View.call(this);
  37. this.registerRequiredCSS("helpScreen.css");
  38. this.element.addStyleClass("fill");
  39. this.element.addStyleClass("help-window-main");
  40. this.element.addStyleClass("settings-tab-container");
  41. var paneContent = this.element.createChild("div", "tabbed-pane-content");
  42. function appendBlockTo(targetElement, contentElement)
  43. {
  44. var blockElement = targetElement.createChild("div", "help-block");
  45. blockElement.appendChild(contentElement);
  46. }
  47. var headerTitle = paneContent.createChild("header").createChild("h3");
  48. headerTitle.appendChild(document.createTextNode(WebInspector.UIString("Overrides")));
  49. var container = paneContent.createChild("div", "help-container-wrapper").createChild("div", "settings-tab help-content help-container");
  50. this.containerElement = container;
  51. appendBlockTo(container, this._createUserAgentControl());
  52. if (Capabilities.canOverrideDeviceMetrics)
  53. appendBlockTo(container, this._createDeviceMetricsControl());
  54. if (Capabilities.canOverrideGeolocation)
  55. appendBlockTo(container, this._createGeolocationOverrideControl());
  56. if (Capabilities.canOverrideDeviceOrientation)
  57. appendBlockTo(container, this._createDeviceOrientationOverrideControl());
  58. appendBlockTo(container, this._createCheckboxSetting(WebInspector.UIString("Emulate touch events"), WebInspector.settings.emulateTouchEvents));
  59. appendBlockTo(container, this._createMediaEmulationElement());
  60. this._statusElement = document.createElement("span");
  61. this._statusElement.textContent = WebInspector.UIString("Overrides");
  62. }
  63. WebInspector.OverridesView.showInDrawer = function()
  64. {
  65. if (!WebInspector.OverridesView._view)
  66. WebInspector.OverridesView._view = new WebInspector.OverridesView();
  67. var view = WebInspector.OverridesView._view;
  68. WebInspector.showViewInDrawer(view._statusElement, view);
  69. }
  70. WebInspector.OverridesView.prototype = {
  71. /**
  72. * @param {boolean=} omitParagraphElement
  73. * @param {Element=} inputElement
  74. */
  75. _createCheckboxSetting: function(name, setting, omitParagraphElement, inputElement)
  76. {
  77. var input = inputElement || document.createElement("input");
  78. input.type = "checkbox";
  79. input.name = name;
  80. input.checked = setting.get();
  81. function listener()
  82. {
  83. setting.set(input.checked);
  84. }
  85. input.addEventListener("click", listener, false);
  86. var label = document.createElement("label");
  87. label.appendChild(input);
  88. label.appendChild(document.createTextNode(name));
  89. if (omitParagraphElement)
  90. return label;
  91. var p = document.createElement("p");
  92. p.appendChild(label);
  93. return p;
  94. },
  95. _createUserAgentControl: function()
  96. {
  97. var userAgent = WebInspector.settings.userAgent.get();
  98. var p = document.createElement("p");
  99. var labelElement = p.createChild("label");
  100. var checkboxElement = labelElement.createChild("input");
  101. checkboxElement.type = "checkbox";
  102. checkboxElement.checked = false;
  103. labelElement.appendChild(document.createTextNode(WebInspector.UIString("User Agent")));
  104. p.appendChild(this._createUserAgentSelectRowElement(checkboxElement));
  105. return p;
  106. },
  107. _createUserAgentSelectRowElement: function(checkboxElement)
  108. {
  109. var userAgent = WebInspector.settings.userAgent.get();
  110. // When present, the third element lists device metrics separated by 'x':
  111. // - screen width,
  112. // - screen height,
  113. // - font scale factor.
  114. const userAgents = [
  115. ["Internet Explorer 9", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"],
  116. ["Internet Explorer 8", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)"],
  117. ["Internet Explorer 7", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"],
  118. ["Firefox 7 \u2014 Windows", "Mozilla/5.0 (Windows NT 6.1; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"],
  119. ["Firefox 7 \u2014 Mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"],
  120. ["Firefox 4 \u2014 Windows", "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"],
  121. ["Firefox 4 \u2014 Mac", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"],
  122. ["Firefox 14 \u2014 Android Mobile", "Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0"],
  123. ["Firefox 14 \u2014 Android Tablet", "Mozilla/5.0 (Android; Tablet; rv:14.0) Gecko/14.0 Firefox/14.0"],
  124. ["Chrome \u2014 Android Mobile", "Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"],
  125. ["Chrome \u2014 Android Tablet", "Mozilla/5.0 (Linux; Android 4.1.2; Nexus 7 Build/JZ054K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19"],
  126. ["iPhone \u2014 iOS 5", "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", "640x960x1"],
  127. ["iPhone \u2014 iOS 4", "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5", "640x960x1"],
  128. ["iPad \u2014 iOS 5", "Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3", "1024x768x1"],
  129. ["iPad \u2014 iOS 4", "Mozilla/5.0 (iPad; CPU OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5", "1024x768x1"],
  130. ["Android 2.3 \u2014 Nexus S", "Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1", "480x800x1.1"],
  131. ["Android 4.0.2 \u2014 Galaxy Nexus", "Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", "720x1280x1.1"],
  132. ["BlackBerry \u2014 PlayBook 2.1", "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+", "1024x600x1"],
  133. ["BlackBerry \u2014 9900", "Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0.187 Mobile Safari/534.11+", "640x480x1"],
  134. ["BlackBerry \u2014 BB10", "Mozilla/5.0 (BB10; Touch) AppleWebKit/537.1+ (KHTML, like Gecko) Version/10.0.0.1337 Mobile Safari/537.1+", "768x1280x1"],
  135. ["MeeGo \u2014 Nokia N9", "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13", "480x854x1"],
  136. [WebInspector.UIString("Other..."), "Other"]
  137. ];
  138. var fieldsetElement = document.createElement("fieldset");
  139. this._selectElement = fieldsetElement.createChild("select");
  140. this._otherUserAgentElement = fieldsetElement.createChild("input");
  141. this._otherUserAgentElement.type = "text";
  142. this._otherUserAgentElement.value = userAgent;
  143. this._otherUserAgentElement.title = userAgent;
  144. this._userAgentFieldsetElement = fieldsetElement;
  145. var selectionRestored = false;
  146. for (var i = 0; i < userAgents.length; ++i) {
  147. var agent = userAgents[i];
  148. var option = new Option(agent[0], agent[1]);
  149. option._metrics = agent[2] ? agent[2] : "";
  150. this._selectElement.add(option);
  151. if (userAgent === agent[1]) {
  152. this._selectElement.selectedIndex = i;
  153. selectionRestored = true;
  154. }
  155. }
  156. if (!selectionRestored) {
  157. if (!userAgent)
  158. this._selectElement.selectedIndex = 0;
  159. else
  160. this._selectElement.selectedIndex = userAgents.length - 1;
  161. }
  162. this._selectElement.addEventListener("change", this._selectionChanged.bind(this, true), false);
  163. fieldsetElement.addEventListener("dblclick", textDoubleClicked.bind(this), false);
  164. this._otherUserAgentElement.addEventListener("blur", textChanged.bind(this), false);
  165. function textDoubleClicked()
  166. {
  167. this._selectElement.selectedIndex = userAgents.length - 1;
  168. this._selectionChanged();
  169. }
  170. function textChanged()
  171. {
  172. WebInspector.settings.userAgent.set(this._otherUserAgentElement.value);
  173. }
  174. function checkboxClicked()
  175. {
  176. if (checkboxElement.checked) {
  177. this._userAgentFieldsetElement.disabled = false;
  178. this._selectionChanged();
  179. } else {
  180. this._userAgentFieldsetElement.disabled = true;
  181. this._otherUserAgentElement.disabled = true;
  182. }
  183. WebInspector.userAgentSupport.toggleUserAgentOverride(checkboxElement.checked);
  184. }
  185. checkboxElement.addEventListener("click", checkboxClicked.bind(this), false);
  186. checkboxClicked.call(this);
  187. return fieldsetElement;
  188. },
  189. /**
  190. * @param {boolean=} isUserGesture
  191. */
  192. _selectionChanged: function(isUserGesture)
  193. {
  194. var value = this._selectElement.options[this._selectElement.selectedIndex].value;
  195. if (value !== "Other") {
  196. WebInspector.settings.userAgent.set(value);
  197. this._otherUserAgentElement.value = value;
  198. this._otherUserAgentElement.title = value;
  199. this._otherUserAgentElement.disabled = true;
  200. } else {
  201. this._otherUserAgentElement.disabled = false;
  202. this._otherUserAgentElement.focus();
  203. }
  204. if (isUserGesture && Capabilities.canOverrideDeviceMetrics) {
  205. var metrics = this._selectElement.options[this._selectElement.selectedIndex]._metrics;
  206. this._setDeviceMetricsOverride(WebInspector.UserAgentSupport.DeviceMetrics.parseSetting(metrics), false, true);
  207. }
  208. },
  209. /**
  210. * Creates an input element under the parentElement with the given id and defaultText.
  211. * It also sets an onblur event listener.
  212. * @param {Element} parentElement
  213. * @param {string} id
  214. * @param {string} defaultText
  215. * @param {function(*)} eventListener
  216. * @param {boolean=} numeric
  217. * @return {Element} element
  218. */
  219. _createInput: function(parentElement, id, defaultText, eventListener, numeric)
  220. {
  221. var element = parentElement.createChild("input");
  222. element.id = id;
  223. element.type = "text";
  224. element.maxLength = 12;
  225. element.style.width = "80px";
  226. element.value = defaultText;
  227. element.align = "right";
  228. if (numeric)
  229. element.className = "numeric";
  230. element.addEventListener("blur", eventListener, false);
  231. return element;
  232. },
  233. _createDeviceMetricsControl: function()
  234. {
  235. const metricsSetting = WebInspector.settings.deviceMetrics.get();
  236. var metrics = WebInspector.UserAgentSupport.DeviceMetrics.parseSetting(metricsSetting);
  237. const p = document.createElement("p");
  238. const labelElement = p.createChild("label");
  239. const checkboxElement = labelElement.createChild("input");
  240. checkboxElement.id = "metrics-override-checkbox";
  241. checkboxElement.type = "checkbox";
  242. checkboxElement.checked = false;
  243. checkboxElement.addEventListener("click", this._onMetricsCheckboxClicked.bind(this), false);
  244. this._metricsCheckboxElement = checkboxElement;
  245. labelElement.appendChild(document.createTextNode(WebInspector.UIString("Device metrics")));
  246. const metricsSectionElement = this._createDeviceMetricsElement(metrics);
  247. p.appendChild(metricsSectionElement);
  248. this._metricsSectionElement = metricsSectionElement;
  249. this._onMetricsCheckboxClicked();
  250. return p;
  251. },
  252. _onMetricsCheckboxClicked: function()
  253. {
  254. var controlsDisabled = !this._metricsCheckboxElement.checked;
  255. this._deviceMetricsFieldsetElement.disabled = controlsDisabled;
  256. if (controlsDisabled) {
  257. WebInspector.userAgentSupport.toggleDeviceMetricsOverride(false);
  258. return;
  259. }
  260. var metrics = WebInspector.UserAgentSupport.DeviceMetrics.parseUserInput(this._widthOverrideElement.value, this._heightOverrideElement.value, this._fontScaleFactorOverrideElement.value);
  261. if (metrics && metrics.isValid() && metrics.width && metrics.height) {
  262. this._setDeviceMetricsOverride(metrics, false, false);
  263. WebInspector.userAgentSupport.toggleDeviceMetricsOverride(true);
  264. }
  265. if (!this._widthOverrideElement.value)
  266. this._widthOverrideElement.focus();
  267. },
  268. _applyDeviceMetricsUserInput: function()
  269. {
  270. this._setDeviceMetricsOverride(WebInspector.UserAgentSupport.DeviceMetrics.parseUserInput(this._widthOverrideElement.value.trim(), this._heightOverrideElement.value.trim(), this._fontScaleFactorOverrideElement.value.trim()), true, false);
  271. },
  272. /**
  273. * @param {?WebInspector.UserAgentSupport.DeviceMetrics} metrics
  274. * @param {boolean} userInputModified
  275. */
  276. _setDeviceMetricsOverride: function(metrics, userInputModified, updateCheckbox)
  277. {
  278. function setValid(condition, element)
  279. {
  280. if (condition)
  281. element.removeStyleClass("error-input");
  282. else
  283. element.addStyleClass("error-input");
  284. }
  285. setValid(metrics && metrics.isWidthValid(), this._widthOverrideElement);
  286. setValid(metrics && metrics.isHeightValid(), this._heightOverrideElement);
  287. setValid(metrics && metrics.isFontScaleFactorValid(), this._fontScaleFactorOverrideElement);
  288. if (!metrics)
  289. return;
  290. if (!userInputModified) {
  291. this._widthOverrideElement.value = metrics.widthToInput();
  292. this._heightOverrideElement.value = metrics.heightToInput();
  293. this._fontScaleFactorOverrideElement.value = metrics.fontScaleFactorToInput();
  294. }
  295. if (metrics.isValid()) {
  296. var value = metrics.toSetting();
  297. if (value !== WebInspector.settings.deviceMetrics.get())
  298. WebInspector.settings.deviceMetrics.set(value);
  299. }
  300. if (this._metricsCheckboxElement && updateCheckbox) {
  301. this._metricsCheckboxElement.checked = !!metrics.toSetting();
  302. this._onMetricsCheckboxClicked();
  303. }
  304. },
  305. /**
  306. * @param {WebInspector.UserAgentSupport.DeviceMetrics} metrics
  307. */
  308. _createDeviceMetricsElement: function(metrics)
  309. {
  310. var fieldsetElement = document.createElement("fieldset");
  311. fieldsetElement.id = "metrics-override-section";
  312. this._deviceMetricsFieldsetElement = fieldsetElement;
  313. function swapDimensionsClicked(event)
  314. {
  315. var widthValue = this._widthOverrideElement.value;
  316. this._widthOverrideElement.value = this._heightOverrideElement.value;
  317. this._heightOverrideElement.value = widthValue;
  318. this._applyDeviceMetricsUserInput();
  319. }
  320. var tableElement = fieldsetElement.createChild("table", "nowrap");
  321. var rowElement = tableElement.createChild("tr");
  322. var cellElement = rowElement.createChild("td");
  323. cellElement.appendChild(document.createTextNode(WebInspector.UIString("Screen resolution:")));
  324. cellElement = rowElement.createChild("td");
  325. this._widthOverrideElement = this._createInput(cellElement, "metrics-override-width", String(metrics.width || screen.width), this._applyDeviceMetricsUserInput.bind(this), true);
  326. cellElement.appendChild(document.createTextNode(" \u00D7 ")); // MULTIPLICATION SIGN.
  327. this._heightOverrideElement = this._createInput(cellElement, "metrics-override-height", String(metrics.height || screen.height), this._applyDeviceMetricsUserInput.bind(this), true);
  328. cellElement.appendChild(document.createTextNode(" \u2014 ")); // EM DASH.
  329. this._swapDimensionsElement = cellElement.createChild("button");
  330. this._swapDimensionsElement.appendChild(document.createTextNode(" \u21C4 ")); // RIGHTWARDS ARROW OVER LEFTWARDS ARROW.
  331. this._swapDimensionsElement.title = WebInspector.UIString("Swap dimensions");
  332. this._swapDimensionsElement.addEventListener("click", swapDimensionsClicked.bind(this), false);
  333. rowElement = tableElement.createChild("tr");
  334. cellElement = rowElement.createChild("td");
  335. cellElement.appendChild(document.createTextNode(WebInspector.UIString("Font scale factor:")));
  336. cellElement = rowElement.createChild("td");
  337. this._fontScaleFactorOverrideElement = this._createInput(cellElement, "metrics-override-font-scale", String(metrics.fontScaleFactor || 1), this._applyDeviceMetricsUserInput.bind(this), true);
  338. rowElement = tableElement.createChild("tr");
  339. cellElement = rowElement.createChild("td");
  340. cellElement.colSpan = 2;
  341. this._fitWindowCheckboxElement = document.createElement("input");
  342. cellElement.appendChild(this._createCheckboxSetting(WebInspector.UIString("Fit in window"), WebInspector.settings.deviceFitWindow, true, this._fitWindowCheckboxElement));
  343. return fieldsetElement;
  344. },
  345. _createGeolocationOverrideControl: function()
  346. {
  347. const geolocationSetting = WebInspector.settings.geolocationOverride.get();
  348. var geolocation = WebInspector.UserAgentSupport.GeolocationPosition.parseSetting(geolocationSetting);
  349. var p = document.createElement("p");
  350. var labelElement = p.createChild("label");
  351. var checkboxElement = labelElement.createChild("input");
  352. checkboxElement.id = "geolocation-override-checkbox";
  353. checkboxElement.type = "checkbox";
  354. checkboxElement.checked = false;
  355. checkboxElement.addEventListener("click", this._onGeolocationOverrideCheckboxClicked.bind(this), false);
  356. this._geolocationOverrideCheckboxElement = checkboxElement;
  357. labelElement.appendChild(document.createTextNode(WebInspector.UIString("Override Geolocation")));
  358. var geolocationSectionElement = this._createGeolocationOverrideElement(geolocation);
  359. p.appendChild(geolocationSectionElement);
  360. this._geolocationSectionElement = geolocationSectionElement;
  361. this._onGeolocationOverrideCheckboxClicked();
  362. return p;
  363. },
  364. _onGeolocationOverrideCheckboxClicked: function()
  365. {
  366. var controlsDisabled = !this._geolocationOverrideCheckboxElement.checked;
  367. this._geolocationFieldsetElement.disabled = controlsDisabled;
  368. if (controlsDisabled) {
  369. WebInspector.userAgentSupport.toggleGeolocationPositionOverride(false);
  370. return;
  371. }
  372. var geolocation = WebInspector.UserAgentSupport.GeolocationPosition.parseUserInput(this._latitudeElement.value, this._longitudeElement.value, this._geolocationErrorElement.checked);
  373. if (geolocation) {
  374. this._setGeolocationPosition(geolocation, false, false);
  375. WebInspector.userAgentSupport.toggleGeolocationPositionOverride(true);
  376. }
  377. if (!this._latitudeElement.value)
  378. this._latitudeElement.focus();
  379. },
  380. _applyGeolocationUserInput: function()
  381. {
  382. this._setGeolocationPosition(WebInspector.UserAgentSupport.GeolocationPosition.parseUserInput(this._latitudeElement.value.trim(), this._longitudeElement.value.trim(), this._geolocationErrorElement.checked), true, false);
  383. },
  384. /**
  385. * @param {?WebInspector.UserAgentSupport.GeolocationPosition} geolocation
  386. * @param {boolean} userInputModified
  387. * @param {boolean} updateCheckbox
  388. */
  389. _setGeolocationPosition: function(geolocation, userInputModified, updateCheckbox)
  390. {
  391. if (!geolocation)
  392. return;
  393. if (!userInputModified) {
  394. this._latitudeElement.value = geolocation.latitude;
  395. this._longitudeElement.value = geolocation.longitude;
  396. }
  397. var value = geolocation.toSetting();
  398. WebInspector.settings.geolocationOverride.set(value);
  399. if (this._geolocationOverrideCheckboxElement && updateCheckbox) {
  400. this._geolocationOverrideCheckboxElement.checked = !!geolocation.toSetting();
  401. this._onGeolocationOverrideCheckboxClicked();
  402. }
  403. },
  404. /**
  405. * @param {WebInspector.UserAgentSupport.GeolocationPosition} geolocation
  406. */
  407. _createGeolocationOverrideElement: function(geolocation)
  408. {
  409. var fieldsetElement = document.createElement("fieldset");
  410. fieldsetElement.id = "geolocation-override-section";
  411. this._geolocationFieldsetElement = fieldsetElement;
  412. var tableElement = fieldsetElement.createChild("table");
  413. var rowElement = tableElement.createChild("tr");
  414. var cellElement = rowElement.createChild("td");
  415. cellElement.appendChild(document.createTextNode(WebInspector.UIString("Geolocation Position") + ":"));
  416. cellElement = rowElement.createChild("td");
  417. cellElement.appendChild(document.createTextNode(WebInspector.UIString("Lat = ")));
  418. this._latitudeElement = this._createInput(cellElement, "geolocation-override-latitude", String(geolocation.latitude), this._applyGeolocationUserInput.bind(this), true);
  419. cellElement.appendChild(document.createTextNode(" , "));
  420. cellElement.appendChild(document.createTextNode(WebInspector.UIString("Lon = ")));
  421. this._longitudeElement = this._createInput(cellElement, "geolocation-override-longitude", String(geolocation.longitude), this._applyGeolocationUserInput.bind(this), true);
  422. rowElement = tableElement.createChild("tr");
  423. cellElement = rowElement.createChild("td");
  424. cellElement.colSpan = 2;
  425. var geolocationErrorLabelElement = document.createElement("label");
  426. var geolocationErrorCheckboxElement = geolocationErrorLabelElement.createChild("input");
  427. geolocationErrorCheckboxElement.id = "geolocation-error";
  428. geolocationErrorCheckboxElement.type = "checkbox";
  429. geolocationErrorCheckboxElement.checked = !geolocation || geolocation.error;
  430. geolocationErrorCheckboxElement.addEventListener("click", this._applyGeolocationUserInput.bind(this), false);
  431. geolocationErrorLabelElement.appendChild(document.createTextNode(WebInspector.UIString("Emulate position unavailable")));
  432. this._geolocationErrorElement = geolocationErrorCheckboxElement;
  433. cellElement.appendChild(geolocationErrorLabelElement);
  434. return fieldsetElement;
  435. },
  436. _createDeviceOrientationOverrideControl: function()
  437. {
  438. const deviceOrientationSetting = WebInspector.settings.deviceOrientationOverride.get();
  439. var deviceOrientation = WebInspector.UserAgentSupport.DeviceOrientation.parseSetting(deviceOrientationSetting);
  440. var p = document.createElement("p");
  441. var labelElement = p.createChild("label");
  442. var checkboxElement = labelElement.createChild("input");
  443. checkboxElement.id = "device-orientation-override-checkbox";
  444. checkboxElement.type = "checkbox";
  445. checkboxElement.checked = false;
  446. checkboxElement.addEventListener("click", this._onDeviceOrientationOverrideCheckboxClicked.bind(this), false);
  447. this._deviceOrientationOverrideCheckboxElement = checkboxElement;
  448. labelElement.appendChild(document.createTextNode(WebInspector.UIString("Override Device Orientation")));
  449. var deviceOrientationSectionElement = this._createDeviceOrientationOverrideElement(deviceOrientation);
  450. p.appendChild(deviceOrientationSectionElement);
  451. this._deviceOrientationSectionElement = deviceOrientationSectionElement;
  452. this._onDeviceOrientationOverrideCheckboxClicked();
  453. return p;
  454. },
  455. _onDeviceOrientationOverrideCheckboxClicked: function()
  456. {
  457. var controlsDisabled = !this._deviceOrientationOverrideCheckboxElement.checked;
  458. this._deviceOrientationFieldsetElement.disabled = controlsDisabled;
  459. if (controlsDisabled) {
  460. WebInspector.userAgentSupport.toggleDeviceOrientationOverride(false);
  461. return;
  462. }
  463. var deviceOrientation = WebInspector.UserAgentSupport.DeviceOrientation.parseUserInput(this._alphaElement.value, this._betaElement.value, this._gammaElement.value);
  464. if (deviceOrientation) {
  465. this._setDeviceOrientation(deviceOrientation, false, false);
  466. WebInspector.userAgentSupport.toggleDeviceOrientationOverride(true);
  467. }
  468. if (!this._alphaElement.value)
  469. this._alphaElement.focus();
  470. },
  471. _applyDeviceOrientationUserInput: function()
  472. {
  473. this._setDeviceOrientation(WebInspector.UserAgentSupport.DeviceOrientation.parseUserInput(this._alphaElement.value.trim(), this._betaElement.value.trim(), this._gammaElement.value.trim()), true, false);
  474. },
  475. /**
  476. * @param {?WebInspector.UserAgentSupport.DeviceOrientation} deviceOrientation
  477. * @param {boolean} userInputModified
  478. * @param {boolean} updateCheckbox
  479. */
  480. _setDeviceOrientation: function(deviceOrientation, userInputModified, updateCheckbox)
  481. {
  482. if (!deviceOrientation)
  483. return;
  484. if (!userInputModified) {
  485. this._alphaElement.value = deviceOrientation.alpha;
  486. this._betaElement.value = deviceOrientation.beta;
  487. this._gammaElement.value = deviceOrientation.gamma;
  488. }
  489. var value = deviceOrientation.toSetting();
  490. WebInspector.settings.deviceOrientationOverride.set(value);
  491. if (this._deviceOrientationOverrideCheckboxElement && updateCheckbox) {
  492. this._deviceOrientationOverrideCheckboxElement.checked = !!deviceOrientation.toSetting();
  493. this._onDeviceOrientationOverrideCheckboxClicked();
  494. }
  495. },
  496. /**
  497. * @param {WebInspector.UserAgentSupport.DeviceOrientation} deviceOrientation
  498. */
  499. _createDeviceOrientationOverrideElement: function(deviceOrientation)
  500. {
  501. var fieldsetElement = document.createElement("fieldset");
  502. fieldsetElement.id = "device-orientation-override-section";
  503. this._deviceOrientationFieldsetElement = fieldsetElement;
  504. var tableElement = fieldsetElement.createChild("table");
  505. var rowElement = tableElement.createChild("tr");
  506. var cellElement = rowElement.createChild("td");
  507. cellElement.appendChild(document.createTextNode("\u03B1: "));
  508. this._alphaElement = this._createInput(cellElement, "device-orientation-override-alpha", String(deviceOrientation.alpha), this._applyDeviceOrientationUserInput.bind(this), true);
  509. cellElement.appendChild(document.createTextNode(" \u03B2: "));
  510. this._betaElement = this._createInput(cellElement, "device-orientation-override-beta", String(deviceOrientation.beta), this._applyDeviceOrientationUserInput.bind(this), true);
  511. cellElement.appendChild(document.createTextNode(" \u03B3: "));
  512. this._gammaElement = this._createInput(cellElement, "device-orientation-override-gamma", String(deviceOrientation.gamma), this._applyDeviceOrientationUserInput.bind(this), true);
  513. return fieldsetElement;
  514. },
  515. _createMediaEmulationElement: function()
  516. {
  517. const p = document.createElement("p");
  518. const labelElement = p.createChild("label");
  519. const checkboxElement = labelElement.createChild("input");
  520. checkboxElement.type = "checkbox";
  521. checkboxElement.checked = false;
  522. labelElement.appendChild(document.createTextNode(WebInspector.UIString("Emulate CSS media")));
  523. var mediaSelectElement = p.createChild("select");
  524. var mediaTypes = WebInspector.CSSStyleModel.MediaTypes;
  525. var defaultMedia = WebInspector.settings.emulatedCSSMedia.get();
  526. for (var i = 0; i < mediaTypes.length; ++i) {
  527. var mediaType = mediaTypes[i];
  528. if (mediaType === "all") {
  529. // "all" is not a device-specific media type.
  530. continue;
  531. }
  532. var option = document.createElement("option");
  533. option.text = mediaType;
  534. option.value = mediaType;
  535. mediaSelectElement.add(option);
  536. if (mediaType === defaultMedia)
  537. mediaSelectElement.selectedIndex = mediaSelectElement.options.length - 1;
  538. }
  539. mediaSelectElement.disabled = true;
  540. var boundListener = this._emulateMediaChanged.bind(this, checkboxElement, mediaSelectElement);
  541. checkboxElement.addEventListener("click", boundListener, false);
  542. mediaSelectElement.addEventListener("change", boundListener, false);
  543. return p;
  544. },
  545. _emulateMediaChanged: function(checkbox, select)
  546. {
  547. select.disabled = !checkbox.checked;
  548. if (checkbox.checked) {
  549. var media = select.options[select.selectedIndex].value;
  550. WebInspector.settings.emulatedCSSMedia.set(media);
  551. PageAgent.setEmulatedMedia(media);
  552. } else
  553. PageAgent.setEmulatedMedia("");
  554. WebInspector.cssModel.mediaQueryResultChanged();
  555. },
  556. __proto__: WebInspector.View.prototype
  557. }