StatusBarButton.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (C) 2009 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.Object}
  33. * @param {!Element} element
  34. */
  35. WebInspector.StatusBarItem = function(element)
  36. {
  37. this.element = element;
  38. this._enabled = true;
  39. }
  40. WebInspector.StatusBarItem.prototype = {
  41. /**
  42. * @param {boolean} value
  43. */
  44. setEnabled: function(value)
  45. {
  46. if (this._enabled === value)
  47. return;
  48. this._enabled = value;
  49. this._applyEnabledState();
  50. },
  51. /**
  52. * @protected
  53. */
  54. _applyEnabledState: function()
  55. {
  56. this.element.disabled = !this._enabled;
  57. },
  58. __proto__: WebInspector.Object.prototype
  59. }
  60. /**
  61. * @constructor
  62. * @extends {WebInspector.StatusBarItem}
  63. * @param {string} title
  64. * @param {string} className
  65. * @param {number=} states
  66. */
  67. WebInspector.StatusBarButton = function(title, className, states)
  68. {
  69. WebInspector.StatusBarItem.call(this, document.createElement("button"));
  70. this.element.className = className + " status-bar-item";
  71. this.element.addEventListener("click", this._clicked.bind(this), false);
  72. this.glyph = document.createElement("div");
  73. this.glyph.className = "glyph";
  74. this.element.appendChild(this.glyph);
  75. this.glyphShadow = document.createElement("div");
  76. this.glyphShadow.className = "glyph shadow";
  77. this.element.appendChild(this.glyphShadow);
  78. this.states = states;
  79. if (!states)
  80. this.states = 2;
  81. if (states == 2)
  82. this._state = false;
  83. else
  84. this._state = 0;
  85. this.title = title;
  86. this.className = className;
  87. this._visible = true;
  88. }
  89. WebInspector.StatusBarButton.prototype = {
  90. _clicked: function()
  91. {
  92. this.dispatchEventToListeners("click");
  93. if (this._showOptionsTimer)
  94. clearTimeout(this._showOptionsTimer);
  95. },
  96. /**
  97. * @return {boolean}
  98. */
  99. enabled: function()
  100. {
  101. return this._enabled;
  102. },
  103. get title()
  104. {
  105. return this._title;
  106. },
  107. set title(x)
  108. {
  109. if (this._title === x)
  110. return;
  111. this._title = x;
  112. this.element.title = x;
  113. },
  114. get state()
  115. {
  116. return this._state;
  117. },
  118. set state(x)
  119. {
  120. if (this._state === x)
  121. return;
  122. if (this.states === 2)
  123. this.element.enableStyleClass("toggled-on", x);
  124. else {
  125. this.element.removeStyleClass("toggled-" + this._state);
  126. if (x !== 0)
  127. this.element.addStyleClass("toggled-" + x);
  128. }
  129. this._state = x;
  130. },
  131. get toggled()
  132. {
  133. if (this.states !== 2)
  134. throw("Only used toggled when there are 2 states, otherwise, use state");
  135. return this.state;
  136. },
  137. set toggled(x)
  138. {
  139. if (this.states !== 2)
  140. throw("Only used toggled when there are 2 states, otherwise, use state");
  141. this.state = x;
  142. },
  143. get visible()
  144. {
  145. return this._visible;
  146. },
  147. set visible(x)
  148. {
  149. if (this._visible === x)
  150. return;
  151. this.element.enableStyleClass("hidden", !x);
  152. this._visible = x;
  153. },
  154. /**
  155. * @param {function():Array.<WebInspector.StatusBarButton>} buttonsProvider
  156. */
  157. makeLongClickEnabled: function(buttonsProvider)
  158. {
  159. this.longClickGlyph = document.createElement("div");
  160. this.longClickGlyph.className = "fill long-click-glyph";
  161. this.element.appendChild(this.longClickGlyph);
  162. this.longClickGlyphShadow = document.createElement("div");
  163. this.longClickGlyphShadow.className = "fill long-click-glyph shadow";
  164. this.element.appendChild(this.longClickGlyphShadow);
  165. this.element.addEventListener("mousedown", mouseDown.bind(this), false);
  166. this.element.addEventListener("mouseout", mouseUp.bind(this), false);
  167. this.element.addEventListener("mouseup", mouseUp.bind(this), false);
  168. function mouseDown(e)
  169. {
  170. if (e.which !== 1)
  171. return;
  172. this._showOptionsTimer = setTimeout(this._showOptions.bind(this, buttonsProvider), 200);
  173. }
  174. function mouseUp(e)
  175. {
  176. if (e.which !== 1)
  177. return;
  178. if (this._showOptionsTimer)
  179. clearTimeout(this._showOptionsTimer);
  180. }
  181. },
  182. /**
  183. * @param {function():Array.<WebInspector.StatusBarButton>} buttonsProvider
  184. */
  185. _showOptions: function(buttonsProvider)
  186. {
  187. var buttons = buttonsProvider();
  188. var mainButtonClone = new WebInspector.StatusBarButton(this.title, this.className, this.states);
  189. mainButtonClone.addEventListener("click", this._clicked, this);
  190. mainButtonClone.state = this.state;
  191. buttons.push(mainButtonClone);
  192. var mouseUpListener = mouseUp.bind(this);
  193. document.documentElement.addEventListener("mouseup", mouseUpListener, false);
  194. var optionsGlassPane = new WebInspector.GlassPane();
  195. var optionsBarElement = optionsGlassPane.element.createChild("div", "alternate-status-bar-buttons-bar");
  196. const buttonHeight = 24;
  197. optionsBarElement.style.height = (buttonHeight * buttons.length) + "px";
  198. optionsBarElement.style.left = (this.element.offsetLeft + 1) + "px";
  199. var boundMouseOver = mouseOver.bind(this);
  200. var boundMouseOut = mouseOut.bind(this);
  201. for (var i = 0; i < buttons.length; ++i) {
  202. buttons[i].element.addEventListener("mousemove", boundMouseOver, false);
  203. buttons[i].element.addEventListener("mouseout", boundMouseOut, false);
  204. optionsBarElement.appendChild(buttons[i].element);
  205. }
  206. buttons[buttons.length - 1].element.addStyleClass("emulate-active");
  207. function mouseOver(e)
  208. {
  209. if (e.which !== 1)
  210. return;
  211. var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
  212. buttonElement.addStyleClass("emulate-active");
  213. }
  214. function mouseOut(e)
  215. {
  216. if (e.which !== 1)
  217. return;
  218. var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
  219. buttonElement.removeStyleClass("emulate-active");
  220. }
  221. function mouseUp(e)
  222. {
  223. if (e.which !== 1)
  224. return;
  225. optionsGlassPane.dispose();
  226. document.documentElement.removeEventListener("mouseup", mouseUpListener, false);
  227. for (var i = 0; i < buttons.length; ++i) {
  228. if (buttons[i].element.hasStyleClass("emulate-active"))
  229. buttons[i]._clicked();
  230. }
  231. }
  232. },
  233. __proto__: WebInspector.StatusBarItem.prototype
  234. }
  235. /**
  236. * @constructor
  237. * @extends {WebInspector.StatusBarItem}
  238. * @param {?function(Event)} changeHandler
  239. * @param {string=} className
  240. */
  241. WebInspector.StatusBarComboBox = function(changeHandler, className)
  242. {
  243. WebInspector.StatusBarItem.call(this, document.createElement("span"));
  244. this.element.className = "status-bar-select-container";
  245. this._selectElement = this.element.createChild("select", "status-bar-item");
  246. if (changeHandler)
  247. this._selectElement.addEventListener("change", changeHandler, false);
  248. if (className)
  249. this._selectElement.addStyleClass(className);
  250. }
  251. WebInspector.StatusBarComboBox.prototype = {
  252. /**
  253. * @return {number}
  254. */
  255. size: function()
  256. {
  257. return this._selectElement.childElementCount;
  258. },
  259. /**
  260. * @param {!Element} option
  261. */
  262. addOption: function(option)
  263. {
  264. this._selectElement.appendChild(option);
  265. },
  266. /**
  267. * @param {string} label
  268. * @param {string=} title
  269. * @param {string=} value
  270. * @return {!Element}
  271. */
  272. createOption: function(label, title, value)
  273. {
  274. var option = this._selectElement.createChild("option");
  275. option.text = label;
  276. if (title)
  277. option.title = title;
  278. if (typeof value !== "undefined")
  279. option.value = value;
  280. return option;
  281. },
  282. /**
  283. * @override
  284. */
  285. _applyEnabledState: function()
  286. {
  287. this._selectElement.disabled = !this._enabled;
  288. },
  289. /**
  290. * @param {!Element} option
  291. */
  292. removeOption: function(option)
  293. {
  294. this._selectElement.removeChild(option);
  295. },
  296. removeOptions: function()
  297. {
  298. this._selectElement.removeChildren();
  299. },
  300. /**
  301. * @return {?Element}
  302. */
  303. selectedOption: function()
  304. {
  305. if (this._selectElement.selectedIndex >= 0)
  306. return this._selectElement[this._selectElement.selectedIndex];
  307. return null;
  308. },
  309. /**
  310. * @param {Element} option
  311. */
  312. select: function(option)
  313. {
  314. this._selectElement.selectedIndex = Array.prototype.indexOf.call(this._selectElement, option);
  315. },
  316. __proto__: WebInspector.StatusBarItem.prototype
  317. }