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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/ui/UIUtils.js

Issue 2377193004: [DevTools] Rework some focus code. (Closed)
Patch Set: Created 4 years, 2 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
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com). 4 * Copyright (C) 2007 Matt Lilek (pewtermoose@gmail.com).
5 * Copyright (C) 2009 Joseph Pecoraro 5 * Copyright (C) 2009 Joseph Pecoraro
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 10 *
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 * @param {boolean=} dimmed 277 * @param {boolean=} dimmed
278 */ 278 */
279 WebInspector.GlassPane = function(document, dimmed) 279 WebInspector.GlassPane = function(document, dimmed)
280 { 280 {
281 this.element = createElement("div"); 281 this.element = createElement("div");
282 var background = dimmed ? "rgba(255, 255, 255, 0.5)" : "transparent"; 282 var background = dimmed ? "rgba(255, 255, 255, 0.5)" : "transparent";
283 this._zIndex = WebInspector._glassPane ? WebInspector._glassPane._zIndex + 1 000 : 3000; // Deliberately starts with 3000 to hide other z-indexed elements be low. 283 this._zIndex = WebInspector._glassPane ? WebInspector._glassPane._zIndex + 1 000 : 3000; // Deliberately starts with 3000 to hide other z-indexed elements be low.
284 this.element.style.cssText = "position:absolute;top:0;bottom:0;left:0;right: 0;background-color:" + background + ";z-index:" + this._zIndex + ";overflow:hidd en;"; 284 this.element.style.cssText = "position:absolute;top:0;bottom:0;left:0;right: 0;background-color:" + background + ";z-index:" + this._zIndex + ";overflow:hidd en;";
285 document.body.appendChild(this.element); 285 document.body.appendChild(this.element);
286 WebInspector._glassPane = this; 286 WebInspector._glassPane = this;
287 // TODO(dgozman): disallow focus outside of glass pane?
287 } 288 }
288 289
289 WebInspector.GlassPane.prototype = { 290 WebInspector.GlassPane.prototype = {
290 dispose: function() 291 dispose: function()
291 { 292 {
292 delete WebInspector._glassPane; 293 delete WebInspector._glassPane;
293 this.element.remove(); 294 this.element.remove();
294 } 295 }
295 } 296 }
296 297
(...skipping 18 matching lines...) Expand all
315 while (element) { 316 while (element) {
316 if (element.__editing) 317 if (element.__editing)
317 return true; 318 return true;
318 element = element.parentElementOrShadowHost(); 319 element = element.parentElementOrShadowHost();
319 } 320 }
320 return false; 321 return false;
321 } 322 }
322 323
323 /** 324 /**
324 * @return {boolean} 325 * @return {boolean}
326 * @suppressGlobalPropertiesCheck
325 */ 327 */
326 WebInspector.isEditing = function() 328 WebInspector.isEditing = function()
327 { 329 {
328 if (WebInspector.__editingCount) 330 if (WebInspector.__editingCount)
329 return true; 331 return true;
330 332
331 var element = WebInspector.currentFocusElement(); 333 var focused = document.deepActiveElement();
332 if (!element) 334 if (!focused)
333 return false; 335 return false;
334 return element.classList.contains("text-prompt") || element.nodeName === "IN PUT" || element.nodeName === "TEXTAREA"; 336 return focused.classList.contains("text-prompt") || focused.nodeName === "IN PUT" || focused.nodeName === "TEXTAREA";
335 } 337 }
336 338
337 /** 339 /**
338 * @param {!Element} element 340 * @param {!Element} element
339 * @param {boolean} value 341 * @param {boolean} value
340 * @return {boolean} 342 * @return {boolean}
341 */ 343 */
342 WebInspector.markBeingEdited = function(element, value) 344 WebInspector.markBeingEdited = function(element, value)
343 { 345 {
344 if (value) { 346 if (value) {
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 * @param {!Document} document 787 * @param {!Document} document
786 * @param {!Event} event 788 * @param {!Event} event
787 */ 789 */
788 WebInspector._windowBlurred = function(document, event) 790 WebInspector._windowBlurred = function(document, event)
789 { 791 {
790 if (event.target.document.nodeType === Node.DOCUMENT_NODE) 792 if (event.target.document.nodeType === Node.DOCUMENT_NODE)
791 document.body.classList.add("inactive"); 793 document.body.classList.add("inactive");
792 } 794 }
793 795
794 /** 796 /**
795 * @return {!Element}
796 */
797 WebInspector.previousFocusElement = function()
798 {
799 return WebInspector._previousFocusElement;
800 }
801
802 /**
803 * @return {!Element}
804 */
805 WebInspector.currentFocusElement = function()
806 {
807 return WebInspector._currentFocusElement;
808 }
809
810 /**
811 * @param {!Event} event 797 * @param {!Event} event
812 */ 798 */
813 WebInspector._focusChanged = function(event) 799 WebInspector._focusChanged = function(event)
814 { 800 {
815 var node = event.deepActiveElement(); 801 var node = event.deepActiveElement();
816 WebInspector.Widget.focusWidgetForNode(node); 802 WebInspector.Widget.focusWidgetForNode(node);
817 WebInspector.setCurrentFocusElement(node);
818 } 803 }
819 804
820 /** 805 /**
821 * @param {!Document} document 806 * @param {!Element|!WebInspector.Widget} toFocus
822 * @param {!Event} event 807 * @return {?Element}
823 */ 808 */
824 WebInspector._documentBlurred = function(document, event) 809 WebInspector.saveFocusedElement = function(toFocus)
825 { 810 {
826 // We want to know when currentFocusElement loses focus to nowhere. 811 var element = null;
827 // This is the case when event.relatedTarget is null (no element is being fo cused) 812 var widget = null;
828 // and document.activeElement is reset to default (this is not a window blur ). 813 if (toFocus instanceof WebInspector.Widget) {
829 if (!event.relatedTarget && document.activeElement === document.body) 814 widget = /** @type {!WebInspector.Widget} */ (toFocus);
830 WebInspector.setCurrentFocusElement(null); 815 element = widget.element;
831 } 816 } else {
817 element = /** @type {!Element} */ (toFocus);
818 }
832 819
833 WebInspector._textInputTypes = new Set(["text", "search", "tel", "url", "email", "password"]); 820 if (!element.isConnected)
834 WebInspector._isTextEditingElement = function(element) 821 return null;
835 { 822 var result = element.ownerDocument.deepActiveElement();
836 if (element instanceof HTMLInputElement) 823 if (widget)
837 return WebInspector._textInputTypes.has(element.type); 824 widget.focus();
838 825 else
839 if (element instanceof HTMLTextAreaElement) 826 element.focus();
840 return true; 827 return result;
841
842 return false;
843 } 828 }
844 829
845 /** 830 /**
846 * @param {?Node} x 831 * @param {!Element|!WebInspector.Widget} fromFocused
832 * @param {?Element} savedElement
833 * @return {?Element}
luoe 2016/09/30 18:25:36 Should this be @return {null}, since this fn() alw
dgozman 2016/09/30 20:27:19 This will mess up with jscompiler, unfortunately.
847 */ 834 */
848 WebInspector.setCurrentFocusElement = function(x) 835 WebInspector.restoreFocusedElement = function(fromFocused, savedElement)
849 { 836 {
850 if (WebInspector._glassPane && x && !WebInspector._glassPane.element.isAnces tor(x)) 837 if (!savedElement || !savedElement.isConnected)
851 return; 838 return null;
852 if (x && !x.ownerDocument.isAncestor(x)) 839 var fromNode = fromFocused instanceof WebInspector.Widget ? /** @type {!WebI nspector.Widget} */(fromFocused).element : fromFocused;
853 return; 840 if (fromNode.hasFocus())
854 if (WebInspector._currentFocusElement !== x) 841 savedElement.focus();
855 WebInspector._previousFocusElement = WebInspector._currentFocusElement; 842 return null;
856 WebInspector._currentFocusElement = x;
857
858 if (x) {
859 x.focus();
860
861 // Make a caret selection inside the new element if there isn't a range selection and there isn't already a caret selection inside.
862 // This is needed (at least) to remove caret from console when focus is moved to some element in the panel.
863 // The code below should not be applied to text fields and text areas, h ence _isTextEditingElement check.
864 var selection = x.getComponentSelection();
865 if (!WebInspector._isTextEditingElement(x) && selection.isCollapsed && ! x.isInsertionCaretInside()) {
866 var selectionRange = x.ownerDocument.createRange();
867 selectionRange.setStart(x, 0);
868 selectionRange.setEnd(x, 0);
869
870 selection.removeAllRanges();
871 selection.addRange(selectionRange);
872 }
873 } else if (WebInspector._previousFocusElement)
874 WebInspector._previousFocusElement.blur();
875 }
876
877 WebInspector.restoreFocusFromElement = function(element)
878 {
879 if (element && element.isSelfOrAncestor(WebInspector.currentFocusElement()))
880 WebInspector.setCurrentFocusElement(WebInspector.previousFocusElement()) ;
881 } 843 }
882 844
883 /** 845 /**
884 * @param {!Element} element 846 * @param {!Element} element
885 * @param {number} offset 847 * @param {number} offset
886 * @param {number} length 848 * @param {number} length
887 * @param {!Array.<!Object>=} domChanges 849 * @param {!Array.<!Object>=} domChanges
888 * @return {?Element} 850 * @return {?Element}
889 */ 851 */
890 WebInspector.highlightSearchResult = function(element, offset, length, domChange s) 852 WebInspector.highlightSearchResult = function(element, offset, length, domChange s)
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 1226
1265 /** 1227 /**
1266 * @param {!Document} document 1228 * @param {!Document} document
1267 * @param {!WebInspector.Setting} themeSetting 1229 * @param {!WebInspector.Setting} themeSetting
1268 */ 1230 */
1269 WebInspector.initializeUIUtils = function(document, themeSetting) 1231 WebInspector.initializeUIUtils = function(document, themeSetting)
1270 { 1232 {
1271 document.defaultView.addEventListener("focus", WebInspector._windowFocused.b ind(WebInspector, document), false); 1233 document.defaultView.addEventListener("focus", WebInspector._windowFocused.b ind(WebInspector, document), false);
1272 document.defaultView.addEventListener("blur", WebInspector._windowBlurred.bi nd(WebInspector, document), false); 1234 document.defaultView.addEventListener("blur", WebInspector._windowBlurred.bi nd(WebInspector, document), false);
1273 document.addEventListener("focus", WebInspector._focusChanged.bind(WebInspec tor), true); 1235 document.addEventListener("focus", WebInspector._focusChanged.bind(WebInspec tor), true);
1274 document.addEventListener("blur", WebInspector._documentBlurred.bind(WebInsp ector, document), true);
1275 1236
1276 if (!WebInspector.themeSupport) 1237 if (!WebInspector.themeSupport)
1277 WebInspector.themeSupport = new WebInspector.ThemeSupport(themeSetting); 1238 WebInspector.themeSupport = new WebInspector.ThemeSupport(themeSetting);
1278 WebInspector.themeSupport.applyTheme(document); 1239 WebInspector.themeSupport.applyTheme(document);
1279 1240
1280 var body = /** @type {!Element} */ (document.body); 1241 var body = /** @type {!Element} */ (document.body);
1281 WebInspector.appendStyle(body, "ui/inspectorStyle.css"); 1242 WebInspector.appendStyle(body, "ui/inspectorStyle.css");
1282 WebInspector.appendStyle(body, "ui/popover.css"); 1243 WebInspector.appendStyle(body, "ui/popover.css");
1283 } 1244 }
1284 1245
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after
2012 * @param {string} title 1973 * @param {string} title
2013 * @return {!Element} 1974 * @return {!Element}
2014 */ 1975 */
2015 WebInspector.linkifyDocumentationURLAsNode = function(article, title) 1976 WebInspector.linkifyDocumentationURLAsNode = function(article, title)
2016 { 1977 {
2017 return WebInspector.linkifyURLAsNode("https://developers.google.com/web/tool s/chrome-devtools/" + article, title, undefined, true); 1978 return WebInspector.linkifyURLAsNode("https://developers.google.com/web/tool s/chrome-devtools/" + article, title, undefined, true);
2018 } 1979 }
2019 1980
2020 /** @type {!WebInspector.ThemeSupport} */ 1981 /** @type {!WebInspector.ThemeSupport} */
2021 WebInspector.themeSupport; 1982 WebInspector.themeSupport;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698