StatusBarButton.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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} text
  64. * @param {string=} className
  65. */
  66. WebInspector.StatusBarText = function(text, className)
  67. {
  68. WebInspector.StatusBarItem.call(this, document.createElement("span"));
  69. this.element.className = "status-bar-item status-bar-text";
  70. if (className)
  71. this.element.addStyleClass(className);
  72. this.element.textContent = text;
  73. }
  74. WebInspector.StatusBarText.prototype = {
  75. /**
  76. * @param {string} text
  77. */
  78. setText: function(text)
  79. {
  80. this.element.textContent = text;
  81. },
  82. __proto__: WebInspector.StatusBarItem.prototype
  83. }
  84. /**
  85. * @constructor
  86. * @extends {WebInspector.StatusBarItem}
  87. * @param {string} title
  88. * @param {string} className
  89. * @param {number=} states
  90. */
  91. WebInspector.StatusBarButton = function(title, className, states)
  92. {
  93. WebInspector.StatusBarItem.call(this, document.createElement("button"));
  94. this.element.className = className + " status-bar-item";
  95. this.element.addEventListener("click", this._clicked.bind(this), false);
  96. this.glyph = document.createElement("div");
  97. this.glyph.className = "glyph";
  98. this.element.appendChild(this.glyph);
  99. this.glyphShadow = document.createElement("div");
  100. this.glyphShadow.className = "glyph shadow";
  101. this.element.appendChild(this.glyphShadow);
  102. this.states = states;
  103. if (!states)
  104. this.states = 2;
  105. if (states == 2)
  106. this._state = false;
  107. else
  108. this._state = 0;
  109. this.title = title;
  110. this.className = className;
  111. this._visible = true;
  112. }
  113. WebInspector.StatusBarButton.prototype = {
  114. _clicked: function()
  115. {
  116. this.dispatchEventToListeners("click");
  117. if (this._longClickInterval) {
  118. clearInterval(this._longClickInterval);
  119. delete this._longClickInterval;
  120. }
  121. },
  122. /**
  123. * @override
  124. */
  125. _applyEnabledState: function()
  126. {
  127. this.element.disabled = !this._enabled;
  128. if (this._longClickInterval) {
  129. clearInterval(this._longClickInterval);
  130. delete this._longClickInterval;
  131. }
  132. },
  133. /**
  134. * @return {boolean}
  135. */
  136. enabled: function()
  137. {
  138. return this._enabled;
  139. },
  140. get title()
  141. {
  142. return this._title;
  143. },
  144. set title(x)
  145. {
  146. if (this._title === x)
  147. return;
  148. this._title = x;
  149. this.element.title = x;
  150. },
  151. get state()
  152. {
  153. return this._state;
  154. },
  155. set state(x)
  156. {
  157. if (this._state === x)
  158. return;
  159. if (this.states === 2)
  160. this.element.enableStyleClass("toggled-on", x);
  161. else {
  162. this.element.removeStyleClass("toggled-" + this._state);
  163. if (x !== 0)
  164. this.element.addStyleClass("toggled-" + x);
  165. }
  166. this._state = x;
  167. },
  168. get toggled()
  169. {
  170. if (this.states !== 2)
  171. throw("Only used toggled when there are 2 states, otherwise, use state");
  172. return this.state;
  173. },
  174. set toggled(x)
  175. {
  176. if (this.states !== 2)
  177. throw("Only used toggled when there are 2 states, otherwise, use state");
  178. this.state = x;
  179. },
  180. get visible()
  181. {
  182. return this._visible;
  183. },
  184. set visible(x)
  185. {
  186. if (this._visible === x)
  187. return;
  188. this.element.enableStyleClass("hidden", !x);
  189. this._visible = x;
  190. },
  191. makeLongClickEnabled: function()
  192. {
  193. var boundMouseDown = mouseDown.bind(this);
  194. var boundMouseUp = mouseUp.bind(this);
  195. this.element.addEventListener("mousedown", boundMouseDown, false);
  196. this.element.addEventListener("mouseout", boundMouseUp, false);
  197. this.element.addEventListener("mouseup", boundMouseUp, false);
  198. var longClicks = 0;
  199. this._longClickData = { mouseUp: boundMouseUp, mouseDown: boundMouseDown };
  200. function mouseDown(e)
  201. {
  202. if (e.which !== 1)
  203. return;
  204. longClicks = 0;
  205. this._longClickInterval = setInterval(longClicked.bind(this), 200);
  206. }
  207. function mouseUp(e)
  208. {
  209. if (e.which !== 1)
  210. return;
  211. if (this._longClickInterval) {
  212. clearInterval(this._longClickInterval);
  213. delete this._longClickInterval;
  214. }
  215. }
  216. function longClicked()
  217. {
  218. ++longClicks;
  219. this.dispatchEventToListeners(longClicks === 1 ? "longClickDown" : "longClickPress");
  220. }
  221. },
  222. unmakeLongClickEnabled: function()
  223. {
  224. if (!this._longClickData)
  225. return;
  226. this.element.removeEventListener("mousedown", this._longClickData.mouseDown, false);
  227. this.element.removeEventListener("mouseout", this._longClickData.mouseUp, false);
  228. this.element.removeEventListener("mouseup", this._longClickData.mouseUp, false);
  229. delete this._longClickData;
  230. },
  231. /**
  232. * @param {?function():Array.<WebInspector.StatusBarButton>} buttonsProvider
  233. */
  234. setLongClickOptionsEnabled: function(buttonsProvider)
  235. {
  236. if (buttonsProvider) {
  237. if (!this._longClickOptionsData) {
  238. this.makeLongClickEnabled();
  239. this.longClickGlyph = document.createElement("div");
  240. this.longClickGlyph.className = "fill long-click-glyph";
  241. this.element.appendChild(this.longClickGlyph);
  242. this.longClickGlyphShadow = document.createElement("div");
  243. this.longClickGlyphShadow.className = "fill long-click-glyph shadow";
  244. this.element.appendChild(this.longClickGlyphShadow);
  245. var longClickDownListener = this._showOptions.bind(this);
  246. this.addEventListener("longClickDown", longClickDownListener, this);
  247. this._longClickOptionsData = {
  248. glyphElement: this.longClickGlyph,
  249. glyphShadowElement: this.longClickGlyphShadow,
  250. longClickDownListener: longClickDownListener
  251. };
  252. }
  253. this._longClickOptionsData.buttonsProvider = buttonsProvider;
  254. } else {
  255. if (!this._longClickOptionsData)
  256. return;
  257. this.element.removeChild(this._longClickOptionsData.glyphElement);
  258. this.element.removeChild(this._longClickOptionsData.glyphShadowElement);
  259. this.removeEventListener("longClickDown", this._longClickOptionsData.longClickDownListener, this);
  260. delete this._longClickOptionsData;
  261. this.unmakeLongClickEnabled();
  262. }
  263. },
  264. _showOptions: function()
  265. {
  266. var buttons = this._longClickOptionsData.buttonsProvider();
  267. var mainButtonClone = new WebInspector.StatusBarButton(this.title, this.className, this.states);
  268. mainButtonClone.addEventListener("click", this._clicked, this);
  269. mainButtonClone.state = this.state;
  270. buttons.push(mainButtonClone);
  271. var mouseUpListener = mouseUp.bind(this);
  272. document.documentElement.addEventListener("mouseup", mouseUpListener, false);
  273. var optionsGlassPane = new WebInspector.GlassPane();
  274. var optionsBarElement = optionsGlassPane.element.createChild("div", "alternate-status-bar-buttons-bar");
  275. const buttonHeight = 23;
  276. var hostButtonPosition = this.element.totalOffset();
  277. var topNotBottom = hostButtonPosition.top < document.documentElement.offsetHeight / 2;
  278. if (topNotBottom)
  279. buttons = buttons.reverse();
  280. optionsBarElement.style.height = (buttonHeight * buttons.length) + "px";
  281. if (topNotBottom)
  282. optionsBarElement.style.top = (hostButtonPosition.top + 1) + "px";
  283. else
  284. optionsBarElement.style.top = (hostButtonPosition.top - (buttonHeight * (buttons.length - 1))) + "px";
  285. optionsBarElement.style.left = (hostButtonPosition.left + 1) + "px";
  286. var boundMouseOver = mouseOver.bind(this);
  287. var boundMouseOut = mouseOut.bind(this);
  288. for (var i = 0; i < buttons.length; ++i) {
  289. buttons[i].element.addEventListener("mousemove", boundMouseOver, false);
  290. buttons[i].element.addEventListener("mouseout", boundMouseOut, false);
  291. optionsBarElement.appendChild(buttons[i].element);
  292. }
  293. var hostButtonIndex = topNotBottom ? 0 : buttons.length - 1;
  294. buttons[hostButtonIndex].element.addStyleClass("emulate-active");
  295. function mouseOver(e)
  296. {
  297. if (e.which !== 1)
  298. return;
  299. var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
  300. buttonElement.addStyleClass("emulate-active");
  301. }
  302. function mouseOut(e)
  303. {
  304. if (e.which !== 1)
  305. return;
  306. var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
  307. buttonElement.removeStyleClass("emulate-active");
  308. }
  309. function mouseUp(e)
  310. {
  311. if (e.which !== 1)
  312. return;
  313. optionsGlassPane.dispose();
  314. document.documentElement.removeEventListener("mouseup", mouseUpListener, false);
  315. for (var i = 0; i < buttons.length; ++i) {
  316. if (buttons[i].element.hasStyleClass("emulate-active")) {
  317. buttons[i].element.removeStyleClass("emulate-active");
  318. buttons[i]._clicked();
  319. break;
  320. }
  321. }
  322. }
  323. },
  324. __proto__: WebInspector.StatusBarItem.prototype
  325. }
  326. /**
  327. * @constructor
  328. * @extends {WebInspector.StatusBarItem}
  329. * @param {?function(Event)} changeHandler
  330. * @param {string=} className
  331. */
  332. WebInspector.StatusBarComboBox = function(changeHandler, className)
  333. {
  334. WebInspector.StatusBarItem.call(this, document.createElement("span"));
  335. this.element.className = "status-bar-select-container";
  336. this._selectElement = this.element.createChild("select", "status-bar-item");
  337. this.element.createChild("div", "status-bar-select-arrow");
  338. if (changeHandler)
  339. this._selectElement.addEventListener("change", changeHandler, false);
  340. if (className)
  341. this._selectElement.addStyleClass(className);
  342. }
  343. WebInspector.StatusBarComboBox.prototype = {
  344. /**
  345. * @return {!Element}
  346. */
  347. selectElement: function()
  348. {
  349. return this._selectElement;
  350. },
  351. /**
  352. * @return {number}
  353. */
  354. size: function()
  355. {
  356. return this._selectElement.childElementCount;
  357. },
  358. /**
  359. * @param {!Element} option
  360. */
  361. addOption: function(option)
  362. {
  363. this._selectElement.appendChild(option);
  364. },
  365. /**
  366. * @param {string} label
  367. * @param {string=} title
  368. * @param {string=} value
  369. * @return {!Element}
  370. */
  371. createOption: function(label, title, value)
  372. {
  373. var option = this._selectElement.createChild("option");
  374. option.text = label;
  375. if (title)
  376. option.title = title;
  377. if (typeof value !== "undefined")
  378. option.value = value;
  379. return option;
  380. },
  381. /**
  382. * @override
  383. */
  384. _applyEnabledState: function()
  385. {
  386. this._selectElement.disabled = !this._enabled;
  387. },
  388. /**
  389. * @param {!Element} option
  390. */
  391. removeOption: function(option)
  392. {
  393. this._selectElement.removeChild(option);
  394. },
  395. removeOptions: function()
  396. {
  397. this._selectElement.removeChildren();
  398. },
  399. /**
  400. * @return {?Element}
  401. */
  402. selectedOption: function()
  403. {
  404. if (this._selectElement.selectedIndex >= 0)
  405. return this._selectElement[this._selectElement.selectedIndex];
  406. return null;
  407. },
  408. /**
  409. * @param {Element} option
  410. */
  411. select: function(option)
  412. {
  413. this._selectElement.selectedIndex = Array.prototype.indexOf.call(this._selectElement, option);
  414. },
  415. /**
  416. * @param {number} index
  417. */
  418. setSelectedIndex: function(index)
  419. {
  420. this._selectElement.selectedIndex = index;
  421. },
  422. /**
  423. * @return {number}
  424. */
  425. selectedIndex: function()
  426. {
  427. return this._selectElement.selectedIndex;
  428. },
  429. __proto__: WebInspector.StatusBarItem.prototype
  430. }