2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 * @extends {WebInspector.Object}
34 * @param {string} elementType
36 WebInspector.StatusBarItem = function(elementType)
38 this.element = document.createElement(elementType);
43 WebInspector.StatusBarItem.prototype = {
45 * @param {boolean} value
47 setEnabled: function(value)
49 if (this._enabled === value)
51 this._enabled = value;
52 this.applyEnabledState();
58 applyEnabledState: function()
60 this.element.disabled = !this._enabled;
70 if (this._visible === x)
72 this.element.classList.toggle("hidden", !x);
76 __proto__: WebInspector.Object.prototype
81 * @extends {WebInspector.StatusBarItem}
82 * @param {!Array.<string>} counters
83 * @param {string=} className
85 WebInspector.StatusBarCounter = function(counters, className)
87 WebInspector.StatusBarItem.call(this, "div");
88 this.element.className = "status-bar-item status-bar-counter hidden";
90 this.element.classList.add(className);
91 this.element.addEventListener("click", this._clicked.bind(this), false);
92 /** @type {!Array.<!{element: !Element, counter: string, value: number, title: string}>} */
94 for (var i = 0; i < counters.length; ++i) {
95 var element = this.element.createChild("span", "status-bar-counter-item");
96 element.createChild("div", counters[i]);
97 element.createChild("span");
98 this._counters.push({counter: counters[i], element: element, value: 0, title: ""});
103 WebInspector.StatusBarCounter.prototype = {
105 * @param {string} counter
106 * @param {number} value
107 * @param {string} title
109 setCounter: function(counter, value, title)
111 for (var i = 0; i < this._counters.length; ++i) {
112 if (this._counters[i].counter === counter) {
113 this._counters[i].value = value;
114 this._counters[i].title = title;
125 for (var i = 0; i < this._counters.length; ++i) {
126 var counter = this._counters[i];
127 var value = counter.value;
128 if (!counter.value) {
129 counter.element.classList.add("hidden");
132 counter.element.classList.remove("hidden");
133 counter.element.classList.toggle("status-bar-counter-item-first", !total);
134 counter.element.querySelector("span").textContent = value;
139 title += counter.title;
142 this.element.classList.toggle("hidden", !total);
143 this.element.title = title;
147 * @param {!Event} event
149 _clicked: function(event)
151 this.dispatchEventToListeners("click");
154 __proto__: WebInspector.StatusBarItem.prototype
159 * @extends {WebInspector.StatusBarItem}
160 * @param {string} text
161 * @param {string=} className
163 WebInspector.StatusBarText = function(text, className)
165 WebInspector.StatusBarItem.call(this, "span");
166 this.element.className = "status-bar-item status-bar-text";
168 this.element.classList.add(className);
169 this.element.textContent = text;
172 WebInspector.StatusBarText.prototype = {
174 * @param {string} text
176 setText: function(text)
178 this.element.textContent = text;
181 __proto__: WebInspector.StatusBarItem.prototype
186 * @extends {WebInspector.StatusBarItem}
187 * @param {string=} placeholder
188 * @param {number=} width
190 WebInspector.StatusBarInput = function(placeholder, width)
192 WebInspector.StatusBarItem.call(this, "input");
193 this.element.className = "status-bar-item";
194 this.element.addEventListener("input", this._onChangeCallback.bind(this), false);
196 this.element.style.width = width + "px";
198 this.element.setAttribute("placeholder", placeholder);
202 WebInspector.StatusBarInput.Event = {
203 TextChanged: "TextChanged"
206 WebInspector.StatusBarInput.prototype = {
208 * @param {string} value
210 setValue: function(value)
213 this.element.value = value;
221 return this.element.value;
224 _onChangeCallback: function()
226 this.dispatchEventToListeners(WebInspector.StatusBarInput.Event.TextChanged, this.element.value);
229 __proto__: WebInspector.StatusBarItem.prototype
234 * @extends {WebInspector.StatusBarItem}
235 * @param {string} title
236 * @param {string} className
237 * @param {number=} states
239 WebInspector.StatusBarButton = function(title, className, states)
241 WebInspector.StatusBarItem.call(this, "button");
242 this.element.className = className + " status-bar-item";
243 this.element.addEventListener("click", this._clicked.bind(this), false);
245 this.glyph = this.element.createChild("div", "glyph");
246 this.glyphShadow = this.element.createChild("div", "glyph shadow");
248 this.states = states;
258 this.className = className;
261 WebInspector.StatusBarButton.prototype = {
264 this.dispatchEventToListeners("click");
265 if (this._longClickInterval) {
266 clearInterval(this._longClickInterval);
267 delete this._longClickInterval;
274 applyEnabledState: function()
276 this.element.disabled = !this._enabled;
277 if (this._longClickInterval) {
278 clearInterval(this._longClickInterval);
279 delete this._longClickInterval;
288 return this._enabled;
298 if (this._title === x)
301 this.element.title = x;
311 if (this._state === x)
314 if (this.states === 2) {
315 this.element.classList.toggle("toggled-on", x);
317 this.element.classList.remove("toggled-" + this._state);
319 this.element.classList.add("toggled-" + x);
326 if (this.states !== 2)
327 throw("Only used toggled when there are 2 states, otherwise, use state");
333 if (this.states !== 2)
334 throw("Only used toggled when there are 2 states, otherwise, use state");
338 makeLongClickEnabled: function()
340 var boundMouseDown = mouseDown.bind(this);
341 var boundMouseUp = mouseUp.bind(this);
343 this.element.addEventListener("mousedown", boundMouseDown, false);
344 this.element.addEventListener("mouseout", boundMouseUp, false);
345 this.element.addEventListener("mouseup", boundMouseUp, false);
349 this._longClickData = { mouseUp: boundMouseUp, mouseDown: boundMouseDown };
353 * @this {WebInspector.StatusBarButton}
355 function mouseDown(e)
360 this._longClickInterval = setInterval(longClicked.bind(this), 200);
365 * @this {WebInspector.StatusBarButton}
371 if (this._longClickInterval) {
372 clearInterval(this._longClickInterval);
373 delete this._longClickInterval;
378 * @this {WebInspector.StatusBarButton}
380 function longClicked()
383 this.dispatchEventToListeners(longClicks === 1 ? "longClickDown" : "longClickPress");
387 unmakeLongClickEnabled: function()
389 if (!this._longClickData)
391 this.element.removeEventListener("mousedown", this._longClickData.mouseDown, false);
392 this.element.removeEventListener("mouseout", this._longClickData.mouseUp, false);
393 this.element.removeEventListener("mouseup", this._longClickData.mouseUp, false);
394 delete this._longClickData;
398 * @param {?function():!Array.<!WebInspector.StatusBarButton>} buttonsProvider
400 setLongClickOptionsEnabled: function(buttonsProvider)
402 if (buttonsProvider) {
403 if (!this._longClickOptionsData) {
404 this.makeLongClickEnabled();
406 this.longClickGlyph = this.element.createChild("div", "fill long-click-glyph");
407 this.longClickGlyphShadow = this.element.createChild("div", "fill long-click-glyph shadow");
409 var longClickDownListener = this._showOptions.bind(this);
410 this.addEventListener("longClickDown", longClickDownListener, this);
412 this._longClickOptionsData = {
413 glyphElement: this.longClickGlyph,
414 glyphShadowElement: this.longClickGlyphShadow,
415 longClickDownListener: longClickDownListener
418 this._longClickOptionsData.buttonsProvider = buttonsProvider;
420 if (!this._longClickOptionsData)
422 this.element.removeChild(this._longClickOptionsData.glyphElement);
423 this.element.removeChild(this._longClickOptionsData.glyphShadowElement);
425 this.removeEventListener("longClickDown", this._longClickOptionsData.longClickDownListener, this);
426 delete this._longClickOptionsData;
428 this.unmakeLongClickEnabled();
432 _showOptions: function()
434 var buttons = this._longClickOptionsData.buttonsProvider();
435 var mainButtonClone = new WebInspector.StatusBarButton(this.title, this.className, this.states);
436 mainButtonClone.addEventListener("click", this._clicked, this);
437 mainButtonClone.state = this.state;
438 buttons.push(mainButtonClone);
440 document.documentElement.addEventListener("mouseup", mouseUp, false);
442 var optionsGlassPane = new WebInspector.GlassPane();
443 var optionsBarElement = optionsGlassPane.element.createChild("div", "alternate-status-bar-buttons-bar");
444 const buttonHeight = 23;
446 var hostButtonPosition = this.element.totalOffset();
448 var topNotBottom = hostButtonPosition.top + buttonHeight * buttons.length < document.documentElement.offsetHeight;
451 buttons = buttons.reverse();
453 optionsBarElement.style.height = (buttonHeight * buttons.length) + "px";
455 optionsBarElement.style.top = (hostButtonPosition.top + 1) + "px";
457 optionsBarElement.style.top = (hostButtonPosition.top - (buttonHeight * (buttons.length - 1))) + "px";
458 optionsBarElement.style.left = (hostButtonPosition.left + 1) + "px";
460 for (var i = 0; i < buttons.length; ++i) {
461 buttons[i].element.addEventListener("mousemove", mouseOver, false);
462 buttons[i].element.addEventListener("mouseout", mouseOut, false);
463 optionsBarElement.appendChild(buttons[i].element);
465 var hostButtonIndex = topNotBottom ? 0 : buttons.length - 1;
466 buttons[hostButtonIndex].element.classList.add("emulate-active");
468 function mouseOver(e)
472 var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
473 buttonElement.classList.add("emulate-active");
480 var buttonElement = e.target.enclosingNodeOrSelfWithClass("status-bar-item");
481 buttonElement.classList.remove("emulate-active");
488 optionsGlassPane.dispose();
489 document.documentElement.removeEventListener("mouseup", mouseUp, false);
491 for (var i = 0; i < buttons.length; ++i) {
492 if (buttons[i].element.classList.contains("emulate-active")) {
493 buttons[i].element.classList.remove("emulate-active");
494 buttons[i]._clicked();
501 __proto__: WebInspector.StatusBarItem.prototype
507 WebInspector.StatusBarItem.Provider = function()
511 WebInspector.StatusBarItem.Provider.prototype = {
513 * @return {?WebInspector.StatusBarItem}
520 * @extends {WebInspector.StatusBarItem}
521 * @param {?function(!Event)} changeHandler
522 * @param {string=} className
524 WebInspector.StatusBarComboBox = function(changeHandler, className)
526 WebInspector.StatusBarItem.call(this, "span");
527 this.element.className = "status-bar-select-container";
529 this._selectElement = this.element.createChild("select", "status-bar-item");
530 this.element.createChild("div", "status-bar-select-arrow");
532 this._selectElement.addEventListener("change", changeHandler, false);
534 this._selectElement.classList.add(className);
537 WebInspector.StatusBarComboBox.prototype = {
541 selectElement: function()
543 return this._selectElement;
551 return this._selectElement.childElementCount;
555 * @param {!Element} option
557 addOption: function(option)
559 this._selectElement.appendChild(option);
563 * @param {string} label
564 * @param {string=} title
565 * @param {string=} value
568 createOption: function(label, title, value)
570 var option = this._selectElement.createChild("option");
573 option.title = title;
574 if (typeof value !== "undefined")
575 option.value = value;
582 applyEnabledState: function()
584 this._selectElement.disabled = !this._enabled;
588 * @param {!Element} option
590 removeOption: function(option)
592 this._selectElement.removeChild(option);
595 removeOptions: function()
597 this._selectElement.removeChildren();
603 selectedOption: function()
605 if (this._selectElement.selectedIndex >= 0)
606 return this._selectElement[this._selectElement.selectedIndex];
611 * @param {!Element} option
613 select: function(option)
615 this._selectElement.selectedIndex = Array.prototype.indexOf.call(/** @type {?} */ (this._selectElement), option);
619 * @param {number} index
621 setSelectedIndex: function(index)
623 this._selectElement.selectedIndex = index;
629 selectedIndex: function()
631 return this._selectElement.selectedIndex;
634 __proto__: WebInspector.StatusBarItem.prototype
639 * @extends {WebInspector.StatusBarItem}
640 * @param {string} title
642 WebInspector.StatusBarCheckbox = function(title)
644 WebInspector.StatusBarItem.call(this, "label");
645 this.element.classList.add("status-bar-item", "checkbox");
646 this.inputElement = this.element.createChild("input");
647 this.inputElement.type = "checkbox";
648 this.element.createTextChild(title);
651 WebInspector.StatusBarCheckbox.prototype = {
657 return this.inputElement.checked;
660 __proto__: WebInspector.StatusBarItem.prototype
665 * @extends {WebInspector.StatusBarButton}
666 * @param {string} className
667 * @param {!Array.<string>} states
668 * @param {!Array.<string>} titles
669 * @param {string} initialState
670 * @param {!WebInspector.Setting} currentStateSetting
671 * @param {!WebInspector.Setting} lastStateSetting
672 * @param {?function(string)} stateChangedCallback
674 WebInspector.StatusBarStatesSettingButton = function(className, states, titles, initialState, currentStateSetting, lastStateSetting, stateChangedCallback)
676 WebInspector.StatusBarButton.call(this, "", className, states.length);
678 var onClickBound = this._onClick.bind(this);
679 this.addEventListener("click", onClickBound, this);
681 this._states = states;
683 for (var index = 0; index < states.length; index++) {
684 var button = new WebInspector.StatusBarButton(titles[index], className, states.length);
685 button.state = this._states[index];
686 button.addEventListener("click", onClickBound, this);
687 this._buttons.push(button);
690 this._currentStateSetting = currentStateSetting;
691 this._lastStateSetting = lastStateSetting;
692 this._stateChangedCallback = stateChangedCallback;
693 this.setLongClickOptionsEnabled(this._createOptions.bind(this));
695 this._currentState = null;
696 this.toggleState(initialState);
699 WebInspector.StatusBarStatesSettingButton.prototype = {
701 * @param {!WebInspector.Event} e
703 _onClick: function(e)
705 this.toggleState(e.target.state);
709 * @param {string} state
711 toggleState: function(state)
713 if (this._currentState === state)
716 if (this._currentState)
717 this._lastStateSetting.set(this._currentState);
718 this._currentState = state;
719 this._currentStateSetting.set(this._currentState);
721 if (this._stateChangedCallback)
722 this._stateChangedCallback(state);
724 var defaultState = this._defaultState();
725 this.state = defaultState;
726 this.title = this._buttons[this._states.indexOf(defaultState)].title;
732 _defaultState: function()
734 var lastState = this._lastStateSetting.get();
735 if (lastState && this._states.indexOf(lastState) >= 0 && lastState != this._currentState)
737 if (this._states.length > 1 && this._currentState === this._states[0])
738 return this._states[1];
739 return this._states[0];
743 * @return {!Array.<!WebInspector.StatusBarButton>}
745 _createOptions: function()
748 for (var index = 0; index < this._states.length; index++) {
749 if (this._states[index] !== this.state && this._states[index] !== this._currentState)
750 options.push(this._buttons[index]);
755 __proto__: WebInspector.StatusBarButton.prototype