| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 | |
| 5 /** | 4 /** |
| 6 * @constructor | 5 * @unrestricted |
| 7 */ | 6 */ |
| 8 WebInspector.ActionRegistry = function() | 7 WebInspector.ActionRegistry = class { |
| 9 { | 8 constructor() { |
| 10 /** @type {!Map.<string, !WebInspector.Action>} */ | 9 /** @type {!Map.<string, !WebInspector.Action>} */ |
| 11 this._actionsById = new Map(); | 10 this._actionsById = new Map(); |
| 12 this._registerActions(); | 11 this._registerActions(); |
| 13 }; | 12 } |
| 14 | 13 |
| 15 WebInspector.ActionRegistry.prototype = { | 14 _registerActions() { |
| 16 _registerActions: function() | 15 self.runtime.extensions(WebInspector.ActionDelegate).forEach(registerExtensi
on, this); |
| 17 { | |
| 18 self.runtime.extensions(WebInspector.ActionDelegate).forEach(registerExt
ension, this); | |
| 19 | |
| 20 /** | |
| 21 * @param {!Runtime.Extension} extension | |
| 22 * @this {WebInspector.ActionRegistry} | |
| 23 */ | |
| 24 function registerExtension(extension) | |
| 25 { | |
| 26 var actionId = extension.descriptor()["actionId"]; | |
| 27 console.assert(actionId); | |
| 28 console.assert(!this._actionsById.get(actionId)); | |
| 29 this._actionsById.set(actionId, new WebInspector.Action(extension)); | |
| 30 } | |
| 31 }, | |
| 32 | 16 |
| 33 /** | 17 /** |
| 34 * @return {!Array.<!WebInspector.Action>} | 18 * @param {!Runtime.Extension} extension |
| 19 * @this {WebInspector.ActionRegistry} |
| 35 */ | 20 */ |
| 36 availableActions: function() | 21 function registerExtension(extension) { |
| 37 { | 22 var actionId = extension.descriptor()['actionId']; |
| 38 return this.applicableActions(this._actionsById.keysArray(), WebInspecto
r.context); | 23 console.assert(actionId); |
| 39 }, | 24 console.assert(!this._actionsById.get(actionId)); |
| 25 this._actionsById.set(actionId, new WebInspector.Action(extension)); |
| 26 } |
| 27 } |
| 28 |
| 29 /** |
| 30 * @return {!Array.<!WebInspector.Action>} |
| 31 */ |
| 32 availableActions() { |
| 33 return this.applicableActions(this._actionsById.keysArray(), WebInspector.co
ntext); |
| 34 } |
| 35 |
| 36 /** |
| 37 * @param {!Array.<string>} actionIds |
| 38 * @param {!WebInspector.Context} context |
| 39 * @return {!Array.<!WebInspector.Action>} |
| 40 */ |
| 41 applicableActions(actionIds, context) { |
| 42 var extensions = []; |
| 43 actionIds.forEach(function(actionId) { |
| 44 var action = this._actionsById.get(actionId); |
| 45 if (action) |
| 46 extensions.push(action._extension); |
| 47 }, this); |
| 48 return context.applicableExtensions(extensions).valuesArray().map(extensionT
oAction.bind(this)); |
| 40 | 49 |
| 41 /** | 50 /** |
| 42 * @param {!Array.<string>} actionIds | 51 * @param {!Runtime.Extension} extension |
| 43 * @param {!WebInspector.Context} context | 52 * @return {!WebInspector.Action} |
| 44 * @return {!Array.<!WebInspector.Action>} | 53 * @this {WebInspector.ActionRegistry} |
| 45 */ | 54 */ |
| 46 applicableActions: function(actionIds, context) | 55 function extensionToAction(extension) { |
| 47 { | 56 return /** @type {!WebInspector.Action} */ (this.action(extension.descript
or()['actionId'])); |
| 48 var extensions = []; | 57 } |
| 49 actionIds.forEach(function(actionId) { | 58 } |
| 50 var action = this._actionsById.get(actionId); | 59 |
| 51 if (action) | 60 /** |
| 52 extensions.push(action._extension); | 61 * @param {string} actionId |
| 53 }, this); | 62 * @return {?WebInspector.Action} |
| 54 return context.applicableExtensions(extensions).valuesArray().map(extens
ionToAction.bind(this)); | 63 */ |
| 55 | 64 action(actionId) { |
| 56 /** | 65 return this._actionsById.get(actionId) || null; |
| 57 * @param {!Runtime.Extension} extension | 66 } |
| 58 * @return {!WebInspector.Action} | |
| 59 * @this {WebInspector.ActionRegistry} | |
| 60 */ | |
| 61 function extensionToAction(extension) | |
| 62 { | |
| 63 return /** @type {!WebInspector.Action} */(this.action(extension.des
criptor()["actionId"])); | |
| 64 } | |
| 65 }, | |
| 66 | |
| 67 /** | |
| 68 * @param {string} actionId | |
| 69 * @return {?WebInspector.Action} | |
| 70 */ | |
| 71 action: function(actionId) | |
| 72 { | |
| 73 return this._actionsById.get(actionId) || null; | |
| 74 } | |
| 75 }; | 67 }; |
| 76 | 68 |
| 77 /** | 69 /** |
| 78 * @constructor | 70 * @unrestricted |
| 79 * @extends {WebInspector.Object} | |
| 80 * @param {!Runtime.Extension} extension | |
| 81 */ | 71 */ |
| 82 WebInspector.Action = function(extension) | 72 WebInspector.Action = class extends WebInspector.Object { |
| 83 { | 73 /** |
| 84 WebInspector.Object.call(this); | 74 * @param {!Runtime.Extension} extension |
| 75 */ |
| 76 constructor(extension) { |
| 77 super(); |
| 85 this._extension = extension; | 78 this._extension = extension; |
| 86 this._enabled = true; | 79 this._enabled = true; |
| 87 this._toggled = false; | 80 this._toggled = false; |
| 81 } |
| 82 |
| 83 /** |
| 84 * @return {string} |
| 85 */ |
| 86 id() { |
| 87 return this._extension.descriptor()['actionId']; |
| 88 } |
| 89 |
| 90 /** |
| 91 * @return {!Promise.<boolean>} |
| 92 */ |
| 93 execute() { |
| 94 return this._extension.instance().then(handleAction.bind(this)); |
| 95 |
| 96 /** |
| 97 * @param {!Object} actionDelegate |
| 98 * @return {boolean} |
| 99 * @this {WebInspector.Action} |
| 100 */ |
| 101 function handleAction(actionDelegate) { |
| 102 var actionId = this._extension.descriptor()['actionId']; |
| 103 var delegate = /** @type {!WebInspector.ActionDelegate} */ (actionDelegate
); |
| 104 return delegate.handleAction(WebInspector.context, actionId); |
| 105 } |
| 106 } |
| 107 |
| 108 /** |
| 109 * @return {string} |
| 110 */ |
| 111 icon() { |
| 112 return this._extension.descriptor()['iconClass'] || ''; |
| 113 } |
| 114 |
| 115 /** |
| 116 * @param {boolean} enabled |
| 117 */ |
| 118 setEnabled(enabled) { |
| 119 if (this._enabled === enabled) |
| 120 return; |
| 121 |
| 122 this._enabled = enabled; |
| 123 this.dispatchEventToListeners(WebInspector.Action.Events.Enabled, enabled); |
| 124 } |
| 125 |
| 126 /** |
| 127 * @return {boolean} |
| 128 */ |
| 129 enabled() { |
| 130 return this._enabled; |
| 131 } |
| 132 |
| 133 /** |
| 134 * @return {string} |
| 135 */ |
| 136 category() { |
| 137 return this._extension.descriptor()['category'] || ''; |
| 138 } |
| 139 |
| 140 /** |
| 141 * @return {string} |
| 142 */ |
| 143 tags() { |
| 144 return this._extension.descriptor()['tags'] || ''; |
| 145 } |
| 146 |
| 147 /** |
| 148 * @return {string} |
| 149 */ |
| 150 title() { |
| 151 var title = this._extension.title(); |
| 152 var options = this._extension.descriptor()['options']; |
| 153 if (options) { |
| 154 for (var pair of options) { |
| 155 if (pair['value'] !== this._toggled) |
| 156 title = pair['title']; |
| 157 } |
| 158 } |
| 159 return title; |
| 160 } |
| 161 |
| 162 /** |
| 163 * @return {boolean} |
| 164 */ |
| 165 toggled() { |
| 166 return this._toggled; |
| 167 } |
| 168 |
| 169 /** |
| 170 * @param {boolean} toggled |
| 171 */ |
| 172 setToggled(toggled) { |
| 173 if (this._toggled === toggled) |
| 174 return; |
| 175 |
| 176 this._toggled = toggled; |
| 177 this.dispatchEventToListeners(WebInspector.Action.Events.Toggled, toggled); |
| 178 } |
| 88 }; | 179 }; |
| 89 | 180 |
| 90 /** @enum {symbol} */ | 181 /** @enum {symbol} */ |
| 91 WebInspector.Action.Events = { | 182 WebInspector.Action.Events = { |
| 92 Enabled: Symbol("Enabled"), | 183 Enabled: Symbol('Enabled'), |
| 93 Toggled: Symbol("Toggled") | 184 Toggled: Symbol('Toggled') |
| 94 }; | |
| 95 | |
| 96 WebInspector.Action.prototype = { | |
| 97 /** | |
| 98 * @return {string} | |
| 99 */ | |
| 100 id: function() | |
| 101 { | |
| 102 return this._extension.descriptor()["actionId"]; | |
| 103 }, | |
| 104 | |
| 105 /** | |
| 106 * @return {!Promise.<boolean>} | |
| 107 */ | |
| 108 execute: function() | |
| 109 { | |
| 110 return this._extension.instance().then(handleAction.bind(this)); | |
| 111 | |
| 112 /** | |
| 113 * @param {!Object} actionDelegate | |
| 114 * @return {boolean} | |
| 115 * @this {WebInspector.Action} | |
| 116 */ | |
| 117 function handleAction(actionDelegate) | |
| 118 { | |
| 119 var actionId = this._extension.descriptor()["actionId"]; | |
| 120 var delegate = /** @type {!WebInspector.ActionDelegate} */(actionDel
egate); | |
| 121 return delegate.handleAction(WebInspector.context, actionId); | |
| 122 } | |
| 123 }, | |
| 124 | |
| 125 /** | |
| 126 * @return {string} | |
| 127 */ | |
| 128 icon: function() | |
| 129 { | |
| 130 return this._extension.descriptor()["iconClass"] || ""; | |
| 131 }, | |
| 132 | |
| 133 /** | |
| 134 * @param {boolean} enabled | |
| 135 */ | |
| 136 setEnabled: function(enabled) | |
| 137 { | |
| 138 if (this._enabled === enabled) | |
| 139 return; | |
| 140 | |
| 141 this._enabled = enabled; | |
| 142 this.dispatchEventToListeners(WebInspector.Action.Events.Enabled, enable
d); | |
| 143 }, | |
| 144 | |
| 145 /** | |
| 146 * @return {boolean} | |
| 147 */ | |
| 148 enabled: function() | |
| 149 { | |
| 150 return this._enabled; | |
| 151 }, | |
| 152 | |
| 153 /** | |
| 154 * @return {string} | |
| 155 */ | |
| 156 category: function() | |
| 157 { | |
| 158 return this._extension.descriptor()["category"] || ""; | |
| 159 }, | |
| 160 | |
| 161 /** | |
| 162 * @return {string} | |
| 163 */ | |
| 164 tags: function() | |
| 165 { | |
| 166 return this._extension.descriptor()["tags"] || ""; | |
| 167 }, | |
| 168 | |
| 169 /** | |
| 170 * @return {string} | |
| 171 */ | |
| 172 title: function() | |
| 173 { | |
| 174 var title = this._extension.title(); | |
| 175 var options = this._extension.descriptor()["options"]; | |
| 176 if (options) { | |
| 177 for (var pair of options) { | |
| 178 if (pair["value"] !== this._toggled) | |
| 179 title = pair["title"]; | |
| 180 } | |
| 181 } | |
| 182 return title; | |
| 183 }, | |
| 184 | |
| 185 /** | |
| 186 * @return {boolean} | |
| 187 */ | |
| 188 toggled: function() | |
| 189 { | |
| 190 return this._toggled; | |
| 191 }, | |
| 192 | |
| 193 /** | |
| 194 * @param {boolean} toggled | |
| 195 */ | |
| 196 setToggled: function(toggled) | |
| 197 { | |
| 198 if (this._toggled === toggled) | |
| 199 return; | |
| 200 | |
| 201 this._toggled = toggled; | |
| 202 this.dispatchEventToListeners(WebInspector.Action.Events.Toggled, toggle
d); | |
| 203 }, | |
| 204 | |
| 205 __proto__: WebInspector.Object.prototype | |
| 206 }; | 185 }; |
| 207 | 186 |
| 208 /** | 187 /** |
| 209 * @interface | 188 * @interface |
| 210 */ | 189 */ |
| 211 WebInspector.ActionDelegate = function() | 190 WebInspector.ActionDelegate = function() {}; |
| 212 { | |
| 213 }; | |
| 214 | 191 |
| 215 WebInspector.ActionDelegate.prototype = { | 192 WebInspector.ActionDelegate.prototype = { |
| 216 /** | 193 /** |
| 217 * @param {!WebInspector.Context} context | 194 * @param {!WebInspector.Context} context |
| 218 * @param {string} actionId | 195 * @param {string} actionId |
| 219 * @return {boolean} | 196 * @return {boolean} |
| 220 */ | 197 */ |
| 221 handleAction: function(context, actionId) {} | 198 handleAction: function(context, actionId) {} |
| 222 }; | 199 }; |
| 223 | 200 |
| 224 /** @type {!WebInspector.ActionRegistry} */ | 201 /** @type {!WebInspector.ActionRegistry} */ |
| 225 WebInspector.actionRegistry; | 202 WebInspector.actionRegistry; |
| OLD | NEW |