browser/devtools/shared/widgets/SimpleListWidget.jsm

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ft=javascript ts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6 "use strict";
michael@0 7
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
michael@0 12
michael@0 13 this.EXPORTED_SYMBOLS = ["SimpleListWidget"];
michael@0 14
michael@0 15 /**
michael@0 16 * A very simple vertical list view.
michael@0 17 *
michael@0 18 * Note: this widget should be used in tandem with the WidgetMethods in
michael@0 19 * ViewHelpers.jsm.
michael@0 20 *
michael@0 21 * @param nsIDOMNode aNode
michael@0 22 * The element associated with the widget.
michael@0 23 */
michael@0 24 function SimpleListWidget(aNode) {
michael@0 25 this.document = aNode.ownerDocument;
michael@0 26 this.window = this.document.defaultView;
michael@0 27 this._parent = aNode;
michael@0 28
michael@0 29 // Create an internal list container.
michael@0 30 this._list = this.document.createElement("scrollbox");
michael@0 31 this._list.className = "simple-list-widget-container theme-body";
michael@0 32 this._list.setAttribute("flex", "1");
michael@0 33 this._list.setAttribute("orient", "vertical");
michael@0 34 this._parent.appendChild(this._list);
michael@0 35
michael@0 36 // Delegate some of the associated node's methods to satisfy the interface
michael@0 37 // required by WidgetMethods instances.
michael@0 38 ViewHelpers.delegateWidgetAttributeMethods(this, aNode);
michael@0 39 ViewHelpers.delegateWidgetEventMethods(this, aNode);
michael@0 40 }
michael@0 41
michael@0 42 SimpleListWidget.prototype = {
michael@0 43 /**
michael@0 44 * Inserts an item in this container at the specified index.
michael@0 45 *
michael@0 46 * @param number aIndex
michael@0 47 * The position in the container intended for this item.
michael@0 48 * @param nsIDOMNode aContents
michael@0 49 * The node displayed in the container.
michael@0 50 * @return nsIDOMNode
michael@0 51 * The element associated with the displayed item.
michael@0 52 */
michael@0 53 insertItemAt: function(aIndex, aContents) {
michael@0 54 aContents.classList.add("simple-list-widget-item");
michael@0 55
michael@0 56 let list = this._list;
michael@0 57 return list.insertBefore(aContents, list.childNodes[aIndex]);
michael@0 58 },
michael@0 59
michael@0 60 /**
michael@0 61 * Returns the child node in this container situated at the specified index.
michael@0 62 *
michael@0 63 * @param number aIndex
michael@0 64 * The position in the container intended for this item.
michael@0 65 * @return nsIDOMNode
michael@0 66 * The element associated with the displayed item.
michael@0 67 */
michael@0 68 getItemAtIndex: function(aIndex) {
michael@0 69 return this._list.childNodes[aIndex];
michael@0 70 },
michael@0 71
michael@0 72 /**
michael@0 73 * Immediately removes the specified child node from this container.
michael@0 74 *
michael@0 75 * @param nsIDOMNode aChild
michael@0 76 * The element associated with the displayed item.
michael@0 77 */
michael@0 78 removeChild: function(aChild) {
michael@0 79 this._list.removeChild(aChild);
michael@0 80
michael@0 81 if (this._selectedItem == aChild) {
michael@0 82 this._selectedItem = null;
michael@0 83 }
michael@0 84 },
michael@0 85
michael@0 86 /**
michael@0 87 * Removes all of the child nodes from this container.
michael@0 88 */
michael@0 89 removeAllItems: function() {
michael@0 90 let list = this._list;
michael@0 91 let parent = this._parent;
michael@0 92
michael@0 93 while (list.hasChildNodes()) {
michael@0 94 list.firstChild.remove();
michael@0 95 }
michael@0 96
michael@0 97 parent.scrollTop = 0;
michael@0 98 parent.scrollLeft = 0;
michael@0 99 this._selectedItem = null;
michael@0 100 },
michael@0 101
michael@0 102 /**
michael@0 103 * Gets the currently selected child node in this container.
michael@0 104 * @return nsIDOMNode
michael@0 105 */
michael@0 106 get selectedItem() {
michael@0 107 return this._selectedItem;
michael@0 108 },
michael@0 109
michael@0 110 /**
michael@0 111 * Sets the currently selected child node in this container.
michael@0 112 * @param nsIDOMNode aChild
michael@0 113 */
michael@0 114 set selectedItem(aChild) {
michael@0 115 let childNodes = this._list.childNodes;
michael@0 116
michael@0 117 if (!aChild) {
michael@0 118 this._selectedItem = null;
michael@0 119 }
michael@0 120 for (let node of childNodes) {
michael@0 121 if (node == aChild) {
michael@0 122 node.classList.add("selected");
michael@0 123 this._selectedItem = node;
michael@0 124 } else {
michael@0 125 node.classList.remove("selected");
michael@0 126 }
michael@0 127 }
michael@0 128 },
michael@0 129
michael@0 130 /**
michael@0 131 * Adds a new attribute or changes an existing attribute on this container.
michael@0 132 *
michael@0 133 * @param string aName
michael@0 134 * The name of the attribute.
michael@0 135 * @param string aValue
michael@0 136 * The desired attribute value.
michael@0 137 */
michael@0 138 setAttribute: function(aName, aValue) {
michael@0 139 this._parent.setAttribute(aName, aValue);
michael@0 140
michael@0 141 if (aName == "emptyText") {
michael@0 142 this._textWhenEmpty = aValue;
michael@0 143 } else if (aName == "headerText") {
michael@0 144 this._textAsHeader = aValue;
michael@0 145 }
michael@0 146 },
michael@0 147
michael@0 148 /**
michael@0 149 * Removes an attribute on this container.
michael@0 150 *
michael@0 151 * @param string aName
michael@0 152 * The name of the attribute.
michael@0 153 */
michael@0 154 removeAttribute: function(aName) {
michael@0 155 this._parent.removeAttribute(aName);
michael@0 156
michael@0 157 if (aName == "emptyText") {
michael@0 158 this._removeEmptyText();
michael@0 159 }
michael@0 160 },
michael@0 161
michael@0 162 /**
michael@0 163 * Ensures the specified element is visible.
michael@0 164 *
michael@0 165 * @param nsIDOMNode aElement
michael@0 166 * The element to make visible.
michael@0 167 */
michael@0 168 ensureElementIsVisible: function(aElement) {
michael@0 169 if (!aElement) {
michael@0 170 return;
michael@0 171 }
michael@0 172
michael@0 173 // Ensure the element is visible but not scrolled horizontally.
michael@0 174 let boxObject = this._list.boxObject.QueryInterface(Ci.nsIScrollBoxObject);
michael@0 175 boxObject.ensureElementIsVisible(aElement);
michael@0 176 boxObject.scrollBy(-this._list.clientWidth, 0);
michael@0 177 },
michael@0 178
michael@0 179 /**
michael@0 180 * Sets the text displayed permanently in this container as a header.
michael@0 181 * @param string aValue
michael@0 182 */
michael@0 183 set _textAsHeader(aValue) {
michael@0 184 if (this._headerTextNode) {
michael@0 185 this._headerTextNode.setAttribute("value", aValue);
michael@0 186 }
michael@0 187 this._headerTextValue = aValue;
michael@0 188 this._showHeaderText();
michael@0 189 },
michael@0 190
michael@0 191 /**
michael@0 192 * Sets the text displayed in this container when empty.
michael@0 193 * @param string aValue
michael@0 194 */
michael@0 195 set _textWhenEmpty(aValue) {
michael@0 196 if (this._emptyTextNode) {
michael@0 197 this._emptyTextNode.setAttribute("value", aValue);
michael@0 198 }
michael@0 199 this._emptyTextValue = aValue;
michael@0 200 this._showEmptyText();
michael@0 201 },
michael@0 202
michael@0 203 /**
michael@0 204 * Creates and appends a label displayed as this container's header.
michael@0 205 */
michael@0 206 _showHeaderText: function() {
michael@0 207 if (this._headerTextNode || !this._headerTextValue) {
michael@0 208 return;
michael@0 209 }
michael@0 210 let label = this.document.createElement("label");
michael@0 211 label.className = "plain simple-list-widget-perma-text";
michael@0 212 label.setAttribute("value", this._headerTextValue);
michael@0 213
michael@0 214 this._parent.insertBefore(label, this._list);
michael@0 215 this._headerTextNode = label;
michael@0 216 },
michael@0 217
michael@0 218 /**
michael@0 219 * Creates and appends a label signaling that this container is empty.
michael@0 220 */
michael@0 221 _showEmptyText: function() {
michael@0 222 if (this._emptyTextNode || !this._emptyTextValue) {
michael@0 223 return;
michael@0 224 }
michael@0 225 let label = this.document.createElement("label");
michael@0 226 label.className = "plain simple-list-widget-empty-text";
michael@0 227 label.setAttribute("value", this._emptyTextValue);
michael@0 228
michael@0 229 this._parent.appendChild(label);
michael@0 230 this._emptyTextNode = label;
michael@0 231 },
michael@0 232
michael@0 233 /**
michael@0 234 * Removes the label signaling that this container is empty.
michael@0 235 */
michael@0 236 _removeEmptyText: function() {
michael@0 237 if (!this._emptyTextNode) {
michael@0 238 return;
michael@0 239 }
michael@0 240 this._parent.removeChild(this._emptyTextNode);
michael@0 241 this._emptyTextNode = null;
michael@0 242 },
michael@0 243
michael@0 244 window: null,
michael@0 245 document: null,
michael@0 246 _parent: null,
michael@0 247 _list: null,
michael@0 248 _selectedItem: null,
michael@0 249 _headerTextNode: null,
michael@0 250 _headerTextValue: "",
michael@0 251 _emptyTextNode: null,
michael@0 252 _emptyTextValue: ""
michael@0 253 };

mercurial