| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @implements {SDK.TargetManager.Observer} | 5 * @implements {SDK.TargetManager.Observer} |
| 6 * @unrestricted | 6 * @unrestricted |
| 7 */ | 7 */ |
| 8 Console.ConsoleContextSelector = class { | 8 Console.ConsoleContextSelector = class { |
| 9 /** | 9 /** |
| 10 * @param {!Element} selectElement | 10 * @param {!Element} selectElement |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 SDK.targetManager.addModelListener( | 22 SDK.targetManager.addModelListener( |
| 23 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this.
_onExecutionContextChanged, this); | 23 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextChanged, this.
_onExecutionContextChanged, this); |
| 24 SDK.targetManager.addModelListener( | 24 SDK.targetManager.addModelListener( |
| 25 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi
s._onExecutionContextDestroyed, this); | 25 SDK.RuntimeModel, SDK.RuntimeModel.Events.ExecutionContextDestroyed, thi
s._onExecutionContextDestroyed, this); |
| 26 | 26 |
| 27 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); | 27 this._selectElement.addEventListener('change', this._executionContextChanged
.bind(this), false); |
| 28 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont
extChangedExternally, this); | 28 UI.context.addFlavorChangeListener(SDK.ExecutionContext, this._executionCont
extChangedExternally, this); |
| 29 } | 29 } |
| 30 | 30 |
| 31 /** | 31 /** |
| 32 * @param {?SDK.ExecutionContext} executionContext |
| 33 * @return {boolean} |
| 34 */ |
| 35 static isTopContext(executionContext) { |
| 36 if (!executionContext || !executionContext.isDefault) |
| 37 return false; |
| 38 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContext.ta
rget()); |
| 39 var frame = executionContext.frameId && resourceTreeModel && resourceTreeMod
el.frameForId(executionContext.frameId); |
| 40 if (!frame) |
| 41 return false; |
| 42 return frame.isMainFrame(); |
| 43 } |
| 44 |
| 45 /** |
| 32 * @param {!SDK.ExecutionContext} executionContext | 46 * @param {!SDK.ExecutionContext} executionContext |
| 33 * @return {string} | 47 * @return {string} |
| 34 */ | 48 */ |
| 35 _titleFor(executionContext) { | 49 static titleForContext(executionContext) { |
| 36 var target = executionContext.target(); | 50 var target = executionContext.target(); |
| 37 var depth = 0; | 51 var depth = 0; |
| 38 var label = executionContext.label() ? target.decorateLabel(executionContext
.label()) : ''; | 52 var label = executionContext.label() ? target.decorateLabel(executionContext
.label()) : ''; |
| 39 if (!executionContext.isDefault) | 53 if (!executionContext.isDefault) |
| 40 depth++; | 54 depth++; |
| 41 if (executionContext.frameId) { | 55 if (executionContext.frameId) { |
| 42 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); | 56 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(target); |
| 43 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon
text.frameId); | 57 var frame = resourceTreeModel && resourceTreeModel.frameForId(executionCon
text.frameId); |
| 44 if (frame) { | 58 if (frame) { |
| 45 label = label || frame.displayName(); | 59 label = label || frame.displayName(); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 72 * @param {!SDK.ExecutionContext} executionContext | 86 * @param {!SDK.ExecutionContext} executionContext |
| 73 */ | 87 */ |
| 74 _executionContextCreated(executionContext) { | 88 _executionContextCreated(executionContext) { |
| 75 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. | 89 // FIXME(413886): We never want to show execution context for the main threa
d of shadow page in service/shared worker frontend. |
| 76 // This check could be removed once we do not send this context to frontend. | 90 // This check could be removed once we do not send this context to frontend. |
| 77 if (!executionContext.target().hasJSCapability()) | 91 if (!executionContext.target().hasJSCapability()) |
| 78 return; | 92 return; |
| 79 | 93 |
| 80 var newOption = createElement('option'); | 94 var newOption = createElement('option'); |
| 81 newOption.__executionContext = executionContext; | 95 newOption.__executionContext = executionContext; |
| 82 newOption.text = this._titleFor(executionContext); | 96 newOption.text = Console.ConsoleContextSelector.titleForContext(executionCon
text); |
| 83 this._optionByExecutionContext.set(executionContext, newOption); | 97 this._optionByExecutionContext.set(executionContext, newOption); |
| 84 var options = this._selectElement.options; | 98 var options = this._selectElement.options; |
| 85 var contexts = Array.prototype.map.call(options, mapping); | 99 var contexts = Array.prototype.map.call(options, mapping); |
| 86 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); | 100 var index = contexts.lowerBound(executionContext, executionContext.runtimeMo
del.executionContextComparator()); |
| 87 this._selectElement.insertBefore(newOption, options[index]); | 101 this._selectElement.insertBefore(newOption, options[index]); |
| 88 | 102 |
| 89 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) | 103 if (executionContext === UI.context.flavor(SDK.ExecutionContext)) |
| 90 this._select(newOption); | 104 this._select(newOption); |
| 91 | 105 |
| 92 /** | 106 /** |
| (...skipping 14 matching lines...) Expand all Loading... |
| 107 this._updateSelectionWarning(); | 121 this._updateSelectionWarning(); |
| 108 } | 122 } |
| 109 | 123 |
| 110 /** | 124 /** |
| 111 * @param {!Common.Event} event | 125 * @param {!Common.Event} event |
| 112 */ | 126 */ |
| 113 _onExecutionContextChanged(event) { | 127 _onExecutionContextChanged(event) { |
| 114 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); | 128 var executionContext = /** @type {!SDK.ExecutionContext} */ (event.data); |
| 115 var option = this._optionByExecutionContext.get(executionContext); | 129 var option = this._optionByExecutionContext.get(executionContext); |
| 116 if (option) | 130 if (option) |
| 117 option.text = this._titleFor(executionContext); | 131 option.text = Console.ConsoleContextSelector.titleForContext(executionCont
ext); |
| 118 this._updateSelectionWarning(); | 132 this._updateSelectionWarning(); |
| 119 } | 133 } |
| 120 | 134 |
| 121 /** | 135 /** |
| 122 * @param {!SDK.ExecutionContext} executionContext | 136 * @param {!SDK.ExecutionContext} executionContext |
| 123 */ | 137 */ |
| 124 _executionContextDestroyed(executionContext) { | 138 _executionContextDestroyed(executionContext) { |
| 125 var option = this._optionByExecutionContext.remove(executionContext); | 139 var option = this._optionByExecutionContext.remove(executionContext); |
| 126 option.remove(); | 140 option.remove(); |
| 127 } | 141 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 153 _executionContextChanged() { | 167 _executionContextChanged() { |
| 154 var option = this._selectedOption(); | 168 var option = this._selectedOption(); |
| 155 var newContext = option ? option.__executionContext : null; | 169 var newContext = option ? option.__executionContext : null; |
| 156 UI.context.setFlavor(SDK.ExecutionContext, newContext); | 170 UI.context.setFlavor(SDK.ExecutionContext, newContext); |
| 157 this._updateSelectionWarning(); | 171 this._updateSelectionWarning(); |
| 158 } | 172 } |
| 159 | 173 |
| 160 _updateSelectionWarning() { | 174 _updateSelectionWarning() { |
| 161 var executionContext = UI.context.flavor(SDK.ExecutionContext); | 175 var executionContext = UI.context.flavor(SDK.ExecutionContext); |
| 162 this._selectElement.parentElement.classList.toggle( | 176 this._selectElement.parentElement.classList.toggle( |
| 163 'warning', !this._isTopContext(executionContext) && this._hasTopContext(
)); | 177 'warning', !Console.ConsoleContextSelector.isTopContext(executionContext
) && this._hasTopContext()); |
| 164 } | 178 } |
| 165 | 179 |
| 166 /** | 180 /** |
| 167 * @param {?SDK.ExecutionContext} executionContext | |
| 168 * @return {boolean} | |
| 169 */ | |
| 170 _isTopContext(executionContext) { | |
| 171 if (!executionContext || !executionContext.isDefault) | |
| 172 return false; | |
| 173 var resourceTreeModel = SDK.ResourceTreeModel.fromTarget(executionContext.ta
rget()); | |
| 174 var frame = executionContext.frameId && resourceTreeModel && resourceTreeMod
el.frameForId(executionContext.frameId); | |
| 175 if (!frame) | |
| 176 return false; | |
| 177 return frame.isMainFrame(); | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * @return {boolean} | 181 * @return {boolean} |
| 182 */ | 182 */ |
| 183 _hasTopContext() { | 183 _hasTopContext() { |
| 184 var options = this._selectElement.options; | 184 var options = this._selectElement.options; |
| 185 for (var i = 0; i < options.length; i++) { | 185 for (var i = 0; i < options.length; i++) { |
| 186 if (this._isTopContext(options[i].__executionContext)) | 186 if (Console.ConsoleContextSelector.isTopContext(options[i].__executionCont
ext)) |
| 187 return true; | 187 return true; |
| 188 } | 188 } |
| 189 return false; | 189 return false; |
| 190 } | 190 } |
| 191 | 191 |
| 192 /** | 192 /** |
| 193 * @override | 193 * @override |
| 194 * @param {!SDK.Target} target | 194 * @param {!SDK.Target} target |
| 195 */ | 195 */ |
| 196 targetAdded(target) { | 196 targetAdded(target) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 219 | 219 |
| 220 /** | 220 /** |
| 221 * @return {?Element} | 221 * @return {?Element} |
| 222 */ | 222 */ |
| 223 _selectedOption() { | 223 _selectedOption() { |
| 224 if (this._selectElement.selectedIndex >= 0) | 224 if (this._selectElement.selectedIndex >= 0) |
| 225 return this._selectElement[this._selectElement.selectedIndex]; | 225 return this._selectElement[this._selectElement.selectedIndex]; |
| 226 return null; | 226 return null; |
| 227 } | 227 } |
| 228 }; | 228 }; |
| OLD | NEW |