ExtensionPanel.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.Panel}
  33. * @param {string} id
  34. * @param {string} pageURL
  35. */
  36. WebInspector.ExtensionPanel = function(id, pageURL)
  37. {
  38. WebInspector.Panel.call(this, id);
  39. this.setHideOnDetach();
  40. this._statusBarItems = [];
  41. var extensionView = new WebInspector.ExtensionView(id, pageURL, "extension panel");
  42. extensionView.show(this.element);
  43. this.setDefaultFocusedElement(extensionView.defaultFocusedElement());
  44. }
  45. WebInspector.ExtensionPanel.prototype = {
  46. defaultFocusedElement: function()
  47. {
  48. return WebInspector.View.prototype.defaultFocusedElement.call(this);
  49. },
  50. statusBarItems: function()
  51. {
  52. return this._statusBarItems;
  53. },
  54. /**
  55. * @param {Element} element
  56. */
  57. addStatusBarItem: function(element)
  58. {
  59. this._statusBarItems.push(element);
  60. },
  61. searchCanceled: function(startingNewSearch)
  62. {
  63. WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.CancelSearch);
  64. WebInspector.Panel.prototype.searchCanceled.apply(this, arguments);
  65. },
  66. /**
  67. * @param {string} query
  68. */
  69. performSearch: function(query)
  70. {
  71. WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PerformSearch, query);
  72. WebInspector.Panel.prototype.performSearch.apply(this, arguments);
  73. },
  74. jumpToNextSearchResult: function()
  75. {
  76. WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.NextSearchResult);
  77. WebInspector.Panel.prototype.jumpToNextSearchResult.call(this);
  78. },
  79. jumpToPreviousSearchResult: function()
  80. {
  81. WebInspector.extensionServer.notifySearchAction(this.name, WebInspector.extensionAPI.panels.SearchAction.PreviousSearchResult);
  82. WebInspector.Panel.prototype.jumpToPreviousSearchResult.call(this);
  83. },
  84. __proto__: WebInspector.Panel.prototype
  85. }
  86. /**
  87. * @constructor
  88. * @param {string} id
  89. * @param {string} iconURL
  90. * @param {string=} tooltip
  91. * @param {boolean=} disabled
  92. */
  93. WebInspector.ExtensionButton = function(id, iconURL, tooltip, disabled)
  94. {
  95. this._id = id;
  96. this.element = document.createElement("button");
  97. this.element.className = "status-bar-item extension";
  98. this.element.addEventListener("click", this._onClicked.bind(this), false);
  99. this.update(iconURL, tooltip, disabled);
  100. }
  101. WebInspector.ExtensionButton.prototype = {
  102. /**
  103. * @param {string} iconURL
  104. * @param {string=} tooltip
  105. * @param {boolean=} disabled
  106. */
  107. update: function(iconURL, tooltip, disabled)
  108. {
  109. if (typeof iconURL === "string")
  110. this.element.style.backgroundImage = "url(" + iconURL + ")";
  111. if (typeof tooltip === "string")
  112. this.element.title = tooltip;
  113. if (typeof disabled === "boolean")
  114. this.element.disabled = disabled;
  115. },
  116. _onClicked: function()
  117. {
  118. WebInspector.extensionServer.notifyButtonClicked(this._id);
  119. }
  120. }
  121. /**
  122. * @constructor
  123. * @extends {WebInspector.SidebarPane}
  124. * @param {string} title
  125. * @param {string} id
  126. */
  127. WebInspector.ExtensionSidebarPane = function(title, id)
  128. {
  129. WebInspector.SidebarPane.call(this, title);
  130. this.setHideOnDetach();
  131. this._id = id;
  132. }
  133. WebInspector.ExtensionSidebarPane.prototype = {
  134. /**
  135. * @param {Object} object
  136. * @param {string} title
  137. * @param {function(?string=)} callback
  138. */
  139. setObject: function(object, title, callback)
  140. {
  141. this._createObjectPropertiesView();
  142. this._setObject(WebInspector.RemoteObject.fromLocalObject(object), title, callback);
  143. },
  144. /**
  145. * @param {string} expression
  146. * @param {string} title
  147. * @param {function(?string=)} callback
  148. */
  149. setExpression: function(expression, title, evaluateOptions, securityOrigin, callback)
  150. {
  151. this._createObjectPropertiesView();
  152. return WebInspector.extensionServer.evaluate(expression, true, false, evaluateOptions, securityOrigin, this._onEvaluate.bind(this, title, callback));
  153. },
  154. /**
  155. * @param {string} url
  156. */
  157. setPage: function(url)
  158. {
  159. if (this._objectPropertiesView) {
  160. this._objectPropertiesView.detach();
  161. delete this._objectPropertiesView;
  162. }
  163. if (this._extensionView)
  164. this._extensionView.detach(true);
  165. this._extensionView = new WebInspector.ExtensionView(this._id, url, "extension fill");
  166. this._extensionView.show(this.bodyElement);
  167. if (!this.bodyElement.style.height)
  168. this.setHeight("150px");
  169. },
  170. /**
  171. * @param {string} height
  172. */
  173. setHeight: function(height)
  174. {
  175. this.bodyElement.style.height = height;
  176. },
  177. /**
  178. * @param {string} title
  179. * @param {function(?string=)} callback
  180. * @param {?Protocol.Error} error
  181. * @param {RuntimeAgent.RemoteObject} result
  182. * @param {boolean=} wasThrown
  183. */
  184. _onEvaluate: function(title, callback, error, result, wasThrown)
  185. {
  186. if (error)
  187. callback(error.toString());
  188. else
  189. this._setObject(WebInspector.RemoteObject.fromPayload(result), title, callback);
  190. },
  191. _createObjectPropertiesView: function()
  192. {
  193. if (this._objectPropertiesView)
  194. return;
  195. if (this._extensionView) {
  196. this._extensionView.detach(true);
  197. delete this._extensionView;
  198. }
  199. this._objectPropertiesView = new WebInspector.ExtensionNotifierView(this._id);
  200. this._objectPropertiesView.show(this.bodyElement);
  201. },
  202. /**
  203. * @param {WebInspector.RemoteObject} object
  204. * @param {string} title
  205. * @param {function(?string=)} callback
  206. */
  207. _setObject: function(object, title, callback)
  208. {
  209. // This may only happen if setPage() was called while we were evaluating the expression.
  210. if (!this._objectPropertiesView) {
  211. callback("operation cancelled");
  212. return;
  213. }
  214. this._objectPropertiesView.element.removeChildren();
  215. var section = new WebInspector.ObjectPropertiesSection(object, title);
  216. if (!title)
  217. section.headerElement.addStyleClass("hidden");
  218. section.expanded = true;
  219. section.editable = false;
  220. this._objectPropertiesView.element.appendChild(section.element);
  221. callback();
  222. },
  223. __proto__: WebInspector.SidebarPane.prototype
  224. }