browser/devtools/shared/widgets/FastListWidget.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 EventEmitter = require("devtools/toolkit/event-emitter");
michael@0 9 const { Cu, Ci } = require("chrome");
michael@0 10 const { ViewHelpers } = Cu.import("resource:///modules/devtools/ViewHelpers.jsm", {});
michael@0 11
michael@0 12 /**
michael@0 13 * A list menu widget that attempts to be very fast.
michael@0 14 *
michael@0 15 * Note: this widget should be used in tandem with the WidgetMethods in
michael@0 16 * ViewHelpers.jsm.
michael@0 17 *
michael@0 18 * @param nsIDOMNode aNode
michael@0 19 * The element associated with the widget.
michael@0 20 */
michael@0 21 const FastListWidget = module.exports = function FastListWidget(aNode) {
michael@0 22 this.document = aNode.ownerDocument;
michael@0 23 this.window = this.document.defaultView;
michael@0 24 this._parent = aNode;
michael@0 25 this._fragment = this.document.createDocumentFragment();
michael@0 26
michael@0 27 // This is a prototype element that each item added to the list clones.
michael@0 28 this._templateElement = this.document.createElement("hbox");
michael@0 29
michael@0 30 // Create an internal scrollbox container.
michael@0 31 this._list = this.document.createElement("scrollbox");
michael@0 32 this._list.className = "fast-list-widget-container theme-body";
michael@0 33 this._list.setAttribute("flex", "1");
michael@0 34 this._list.setAttribute("orient", "vertical");
michael@0 35 this._list.setAttribute("tabindex", "0");
michael@0 36 this._list.addEventListener("keypress", e => this.emit("keyPress", e), false);
michael@0 37 this._list.addEventListener("mousedown", e => this.emit("mousePress", e), false);
michael@0 38 this._parent.appendChild(this._list);
michael@0 39
michael@0 40 this._orderedMenuElementsArray = [];
michael@0 41 this._itemsByElement = new Map();
michael@0 42
michael@0 43 // This widget emits events that can be handled in a MenuContainer.
michael@0 44 EventEmitter.decorate(this);
michael@0 45
michael@0 46 // Delegate some of the associated node's methods to satisfy the interface
michael@0 47 // required by MenuContainer instances.
michael@0 48 ViewHelpers.delegateWidgetAttributeMethods(this, aNode);
michael@0 49 ViewHelpers.delegateWidgetEventMethods(this, aNode);
michael@0 50 }
michael@0 51
michael@0 52 FastListWidget.prototype = {
michael@0 53 /**
michael@0 54 * Inserts an item in this container at the specified index, optionally
michael@0 55 * grouping by name.
michael@0 56 *
michael@0 57 * @param number aIndex
michael@0 58 * The position in the container intended for this item.
michael@0 59 * @param nsIDOMNode aContents
michael@0 60 * The node to be displayed in the container.
michael@0 61 * @param Object aAttachment [optional]
michael@0 62 * Extra data for the user.
michael@0 63 * @return nsIDOMNode
michael@0 64 * The element associated with the displayed item.
michael@0 65 */
michael@0 66 insertItemAt: function(aIndex, aContents, aAttachment={}) {
michael@0 67 let element = this._templateElement.cloneNode();
michael@0 68 element.appendChild(aContents);
michael@0 69
michael@0 70 if (aIndex >= 0) {
michael@0 71 throw new Error("FastListWidget only supports appending items.");
michael@0 72 }
michael@0 73
michael@0 74 this._fragment.appendChild(element);
michael@0 75 this._orderedMenuElementsArray.push(element);
michael@0 76 this._itemsByElement.set(element, this);
michael@0 77
michael@0 78 return element;
michael@0 79 },
michael@0 80
michael@0 81 /**
michael@0 82 * This is a non-standard widget implementation method. When appending items,
michael@0 83 * they are queued in a document fragment. This method appends the document
michael@0 84 * fragment to the dom.
michael@0 85 */
michael@0 86 flush: function() {
michael@0 87 this._list.appendChild(this._fragment);
michael@0 88 },
michael@0 89
michael@0 90 /**
michael@0 91 * Removes all of the child nodes from this container.
michael@0 92 */
michael@0 93 removeAllItems: function() {
michael@0 94 let parent = this._parent;
michael@0 95 let list = this._list;
michael@0 96
michael@0 97 while (list.hasChildNodes()) {
michael@0 98 list.firstChild.remove();
michael@0 99 }
michael@0 100
michael@0 101 this._selectedItem = null;
michael@0 102
michael@0 103 this._orderedMenuElementsArray.length = 0;
michael@0 104 this._itemsByElement.clear();
michael@0 105 },
michael@0 106
michael@0 107 /**
michael@0 108 * Remove the given item.
michael@0 109 */
michael@0 110 removeChild: function(child) {
michael@0 111 throw new Error("Not yet implemented");
michael@0 112 },
michael@0 113
michael@0 114 /**
michael@0 115 * Gets the currently selected child node in this container.
michael@0 116 * @return nsIDOMNode
michael@0 117 */
michael@0 118 get selectedItem() {
michael@0 119 return this._selectedItem;
michael@0 120 },
michael@0 121
michael@0 122 /**
michael@0 123 * Sets the currently selected child node in this container.
michael@0 124 * @param nsIDOMNode child
michael@0 125 */
michael@0 126 set selectedItem(child) {
michael@0 127 let menuArray = this._orderedMenuElementsArray;
michael@0 128
michael@0 129 if (!child) {
michael@0 130 this._selectedItem = null;
michael@0 131 }
michael@0 132 for (let node of menuArray) {
michael@0 133 if (node == child) {
michael@0 134 node.classList.add("selected");
michael@0 135 this._selectedItem = node;
michael@0 136 } else {
michael@0 137 node.classList.remove("selected");
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 this.ensureElementIsVisible(this.selectedItem);
michael@0 142 },
michael@0 143
michael@0 144 /**
michael@0 145 * Returns the child node in this container situated at the specified index.
michael@0 146 *
michael@0 147 * @param number index
michael@0 148 * The position in the container intended for this item.
michael@0 149 * @return nsIDOMNode
michael@0 150 * The element associated with the displayed item.
michael@0 151 */
michael@0 152 getItemAtIndex: function(index) {
michael@0 153 return this._orderedMenuElementsArray[index];
michael@0 154 },
michael@0 155
michael@0 156 /**
michael@0 157 * Adds a new attribute or changes an existing attribute on this container.
michael@0 158 *
michael@0 159 * @param string name
michael@0 160 * The name of the attribute.
michael@0 161 * @param string value
michael@0 162 * The desired attribute value.
michael@0 163 */
michael@0 164 setAttribute: function(name, value) {
michael@0 165 this._parent.setAttribute(name, value);
michael@0 166
michael@0 167 if (name == "emptyText") {
michael@0 168 this._textWhenEmpty = value;
michael@0 169 }
michael@0 170 },
michael@0 171
michael@0 172 /**
michael@0 173 * Removes an attribute on this container.
michael@0 174 *
michael@0 175 * @param string name
michael@0 176 * The name of the attribute.
michael@0 177 */
michael@0 178 removeAttribute: function(name) {
michael@0 179 this._parent.removeAttribute(name);
michael@0 180
michael@0 181 if (name == "emptyText") {
michael@0 182 this._removeEmptyText();
michael@0 183 }
michael@0 184 },
michael@0 185
michael@0 186 /**
michael@0 187 * Ensures the specified element is visible.
michael@0 188 *
michael@0 189 * @param nsIDOMNode element
michael@0 190 * The element to make visible.
michael@0 191 */
michael@0 192 ensureElementIsVisible: function(element) {
michael@0 193 if (!element) {
michael@0 194 return;
michael@0 195 }
michael@0 196
michael@0 197 // Ensure the element is visible but not scrolled horizontally.
michael@0 198 let boxObject = this._list.boxObject.QueryInterface(Ci.nsIScrollBoxObject);
michael@0 199 boxObject.ensureElementIsVisible(element);
michael@0 200 boxObject.scrollBy(-this._list.clientWidth, 0);
michael@0 201 },
michael@0 202
michael@0 203 /**
michael@0 204 * Sets the text displayed in this container when empty.
michael@0 205 * @param string aValue
michael@0 206 */
michael@0 207 set _textWhenEmpty(aValue) {
michael@0 208 if (this._emptyTextNode) {
michael@0 209 this._emptyTextNode.setAttribute("value", aValue);
michael@0 210 }
michael@0 211 this._emptyTextValue = aValue;
michael@0 212 this._showEmptyText();
michael@0 213 },
michael@0 214
michael@0 215 /**
michael@0 216 * Creates and appends a label signaling that this container is empty.
michael@0 217 */
michael@0 218 _showEmptyText: function() {
michael@0 219 if (this._emptyTextNode || !this._emptyTextValue) {
michael@0 220 return;
michael@0 221 }
michael@0 222 let label = this.document.createElement("label");
michael@0 223 label.className = "plain fast-list-widget-empty-text";
michael@0 224 label.setAttribute("value", this._emptyTextValue);
michael@0 225
michael@0 226 this._parent.insertBefore(label, this._list);
michael@0 227 this._emptyTextNode = label;
michael@0 228 },
michael@0 229
michael@0 230 /**
michael@0 231 * Removes the label signaling that this container is empty.
michael@0 232 */
michael@0 233 _removeEmptyText: function() {
michael@0 234 if (!this._emptyTextNode) {
michael@0 235 return;
michael@0 236 }
michael@0 237 this._parent.removeChild(this._emptyTextNode);
michael@0 238 this._emptyTextNode = null;
michael@0 239 },
michael@0 240
michael@0 241 window: null,
michael@0 242 document: null,
michael@0 243 _parent: null,
michael@0 244 _list: null,
michael@0 245 _selectedItem: null,
michael@0 246 _orderedMenuElementsArray: null,
michael@0 247 _itemsByElement: null,
michael@0 248 _emptyTextNode: null,
michael@0 249 _emptyTextValue: ""
michael@0 250 };

mercurial