1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Tree-like drilldown components for HTML tables.
*
* This component supports expanding and collapsing groups of rows in
* HTML tables. The behavior is like typical Tree widgets, but tables
* need special support to enable the tree behaviors.
*
* Any row or rows in an HTML table can be DrilldownRows. The root
* DrilldownRow nodes are always visible in the table, but the rest show
* or hide as input events expand and collapse their ancestors.
*
* Programming them: Top-level DrilldownRows are made by decorating
* a TR element. Children are made with addChild or addChildAt, and
* are entered into the document by the render() method.
*
* A DrilldownRow can have any number of children. If it has no children
* it can be loaded, not loaded, or with a load in progress.
* Top-level DrilldownRows are always displayed (though setting
* style.display on a containing DOM node could make one be not
* visible to the user). A DrilldownRow can be expanded, or not. A
* DrilldownRow displays if all of its ancestors are expanded.
*
* Set up event handlers and style each row for the application in an
* enterDocument method.
*
* Children normally render into the document lazily, at the first
* moment when all ancestors are expanded.
*
* @see ../demos/drilldownrow.html
*/
// TODO(user): Build support for dynamically loading DrilldownRows,
// probably using automplete as an example to follow.
// TODO(user): Make DrilldownRows accessible through the keyboard.
// The render method is redefined in this class because when addChildAt renders
// the new child it assumes that the child's DOM node will be a child
// of the parent component's DOM node, but all DOM nodes of DrilldownRows
// in the same tree of DrilldownRows are siblings to each other.
//
// Arguments (or lack of arguments) to the render methods in Component
// all determine the place of the new DOM node in the DOM tree, but
// the place of a new DrilldownRow in the DOM needs to be determined by
// its position in the tree of DrilldownRows.
goog.provide('goog.ui.DrilldownRow');
goog.require('goog.asserts');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.dom.safe');
goog.require('goog.html.SafeHtml');
goog.require('goog.string.Unicode');
goog.require('goog.ui.Component');
/**
* Builds a DrilldownRow component, which can overlay a tree
* structure onto sections of an HTML table.
*
* @param {!goog.ui.DrilldownRow.DrilldownRowProperties=} opt_properties
* Optional properties.
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
* @constructor
* @extends {goog.ui.Component}
* @final
*/
goog.ui.DrilldownRow = function(opt_properties, opt_domHelper) {
goog.ui.Component.call(this, opt_domHelper);
var properties = opt_properties || {};
// Initialize instance variables.
var html;
if (!goog.isDefAndNotNull(properties.html)) {
html = goog.html.SafeHtml.EMPTY;
} else {
goog.asserts.assert(properties.html instanceof goog.html.SafeHtml);
html = properties.html;
}
/**
* String of HTML to initialize the DOM structure for the table row.
* Should have the form '<tr attr="etc">Row contents here</tr>'.
* @type {!goog.html.SafeHtml}
* @private
*/
this.html_ = html;
/**
* Controls whether this component's children will show when it shows.
* @type {boolean}
* @private
*/
this.expanded_ =
typeof properties.expanded != 'undefined' ? properties.expanded : true;
/**
* If this component's DOM element is created from a string of
* HTML, this is the function to call when it is entered into the DOM tree.
* @type {Function} args are DrilldownRow and goog.events.EventHandler
* of the DrilldownRow.
* @private
*/
this.decoratorFn_ = properties.decorator || goog.ui.DrilldownRow.decorate;
/**
* Is the DrilldownRow to be displayed? If it is rendered, this mirrors
* the style.display of the DrilldownRow's row.
* @type {boolean}
* @private
*/
this.displayed_ = true;
};
goog.inherits(goog.ui.DrilldownRow, goog.ui.Component);
/**
* Used to define properties for a new DrilldownRow. Properties can contain:
* loaded: initializes the isLoaded property, defaults to true.
* expanded: DrilldownRow expanded or not, default is true.
* html: Relevant and required for DrilldownRows to be added as
* children. Ignored when decorating an existing table row.
* decorator: Function that accepts one DrilldownRow argument, and
* should customize and style the row. The default is to call
* goog.ui.DrilldownRow.decorator.
* @typedef {{
* loaded: (boolean|undefined),
* expanded: (boolean|undefined),
* html: (!goog.html.SafeHtml|undefined),
* decorator: (Function|undefined)
* }}
*/
goog.ui.DrilldownRow.DrilldownRowProperties;
/**
* Example object with properties of the form accepted by the class
* constructor. These are educational and show the compiler that
* these properties can be set so it doesn't emit warnings.
*/
goog.ui.DrilldownRow.sampleProperties = {
html: goog.html.SafeHtml.create(
goog.dom.TagName.TR, {},
goog.html.SafeHtml.concat(
goog.html.SafeHtml.create(goog.dom.TagName.TD, {}, 'Sample'),
goog.html.SafeHtml.create(goog.dom.TagName.TD, {}, 'Sample'))),
loaded: true,
decorator: function(selfObj, handler) {
// When the mouse is hovering, add CSS class goog-drilldown-hover.
goog.ui.DrilldownRow.decorate(selfObj);
var row = selfObj.getElement();
handler.listen(row, 'mouseover', function() {
goog.dom.classlist.add(row, goog.getCssName('goog-drilldown-hover'));
});
handler.listen(row, 'mouseout', function() {
goog.dom.classlist.remove(row, goog.getCssName('goog-drilldown-hover'));
});
}
};
//
// Implementations of Component methods.
//
/**
* The base class method calls its superclass method and this
* drilldown's 'decorator' method as defined in the constructor.
* @override
*/
goog.ui.DrilldownRow.prototype.enterDocument = function() {
goog.ui.DrilldownRow.superClass_.enterDocument.call(this);
this.decoratorFn_(this, this.getHandler());
};
/** @override */
goog.ui.DrilldownRow.prototype.createDom = function() {
this.setElementInternal(
goog.ui.DrilldownRow.createRowNode_(this.html_, this.getDomHelper()));
};
/**
* A top-level DrilldownRow decorates a TR element.
*
* @param {Element} node The element to test for decorability.
* @return {boolean} true iff the node is a TR.
* @override
*/
goog.ui.DrilldownRow.prototype.canDecorate = function(node) {
return node.tagName == goog.dom.TagName.TR;
};
/**
* Child drilldowns are rendered when needed.
*
* @param {goog.ui.Component} child New DrilldownRow child to be added.
* @param {number} index position to be occupied by the child.
* @param {boolean=} opt_render true to force immediate rendering.
* @override
*/
goog.ui.DrilldownRow.prototype.addChildAt = function(child, index, opt_render) {
goog.asserts.assertInstanceof(child, goog.ui.DrilldownRow);
goog.ui.DrilldownRow.superClass_.addChildAt.call(this, child, index, false);
child.setDisplayable_(this.isVisible_() && this.isExpanded());
if (opt_render && !child.isInDocument()) {
child.render();
}
};
/** @override */
goog.ui.DrilldownRow.prototype.removeChild = function(child) {
goog.dom.removeNode(child.getElement());
return goog.ui.DrilldownRow.superClass_.removeChild.call(this, child);
};
/**
* Rendering of DrilldownRow's is on need, do not call this directly
* from application code.
*
* Rendering a DrilldownRow places it according to its position in its
* tree of DrilldownRows. DrilldownRows cannot be placed any other
* way so this method does not use any arguments. This does not call
* the base class method and does not modify any of this
* DrilldownRow's children.
* @override
*/
goog.ui.DrilldownRow.prototype.render = function() {
if (arguments.length) {
throw new Error('A DrilldownRow cannot be placed under a specific parent.');
} else {
var parent = this.getParent();
if (!parent.isInDocument()) {
throw new Error('Cannot render child of un-rendered parent');
}
// The new child's TR node needs to go just after the last TR
// of the part of the parent's subtree that is to the left
// of this. The subtree includes the parent.
goog.asserts.assertInstanceof(parent, goog.ui.DrilldownRow);
var previous = parent.previousRenderedChild_(this);
var row;
if (previous) {
goog.asserts.assertInstanceof(previous, goog.ui.DrilldownRow);
row = previous.lastRenderedLeaf_().getElement();
} else {
row = parent.getElement();
}
row = /** @type {Element} */ (row.nextSibling);
// Render the child row component into the document.
if (row) {
this.renderBefore(row);
} else {
// Render at the end of the parent of this DrilldownRow's
// DOM element.
var tbody = /** @type {Element} */ (parent.getElement().parentNode);
goog.ui.DrilldownRow.superClass_.render.call(this, tbody);
}
}
};
/**
* Finds the numeric index of this child within its parent Component.
* Throws an exception if it has no parent.
*
* @return {number} index of this within the children of the parent Component.
*/
goog.ui.DrilldownRow.prototype.findIndex = function() {
var parent = this.getParent();
if (!parent) {
throw new Error('Component has no parent');
}
return parent.indexOfChild(this);
};
//
// Type-specific operations
//
/**
* Returns the expanded state of the DrilldownRow.
*
* @return {boolean} true iff this is expanded.
*/
goog.ui.DrilldownRow.prototype.isExpanded = function() {
return this.expanded_;
};
/**
* Sets the expanded state of this DrilldownRow: makes all children
* displayable or not displayable corresponding to the expanded state.
*
* @param {boolean} expanded whether this should be expanded or not.
*/
goog.ui.DrilldownRow.prototype.setExpanded = function(expanded) {
if (expanded != this.expanded_) {
this.expanded_ = expanded;
var elem = this.getElement();
goog.asserts.assert(elem);
goog.dom.classlist.toggle(elem, goog.getCssName('goog-drilldown-expanded'));
goog.dom.classlist.toggle(
elem, goog.getCssName('goog-drilldown-collapsed'));
if (this.isVisible_()) {
this.forEachChild(function(child) { child.setDisplayable_(expanded); });
}
}
};
/**
* Returns this DrilldownRow's level in the tree. Top level is 1.
*
* @return {number} depth of this DrilldownRow in its tree of drilldowns.
*/
goog.ui.DrilldownRow.prototype.getDepth = function() {
for (var component = this, depth = 0;
component instanceof goog.ui.DrilldownRow;
component = component.getParent(), depth++) {
}
return depth;
};
/**
* This static function is a default decorator that adds HTML at the
* beginning of the first cell to display indentation and an expander
* image; sets up a click handler on the toggler; initializes a class
* for the row: either goog-drilldown-expanded or
* goog-drilldown-collapsed, depending on the initial state of the
* DrilldownRow; and sets up a click event handler on the toggler
* element.
*
* This creates a DIV with class=toggle. Your application can set up
* CSS style rules something like this:
*
* tr.goog-drilldown-expanded .toggle {
* background-image: url('minus.png');
* }
*
* tr.goog-drilldown-collapsed .toggle {
* background-image: url('plus.png');
* }
*
* These background images show whether the DrilldownRow is expanded.
*
* @param {goog.ui.DrilldownRow} selfObj DrilldownRow to be decorated.
*/
goog.ui.DrilldownRow.decorate = function(selfObj) {
var depth = selfObj.getDepth();
var row = selfObj.getElement();
goog.asserts.assert(row);
if (!row.cells) {
throw new Error('No cells');
}
var cell = row.cells[0];
var dom = selfObj.getDomHelper();
var fragment = dom.createDom(
goog.dom.TagName.DIV, {'style': 'float: left; width: ' + depth + 'em;'},
dom.createDom(
goog.dom.TagName.DIV,
{'class': 'toggle', 'style': 'width: 1em; float: right;'},
// NOTE: NBSP is probably only needed by IE6. This div can probably be
// made contentless.
goog.string.Unicode.NBSP));
cell.insertBefore(fragment, cell.firstChild);
goog.dom.classlist.add(
row, selfObj.isExpanded() ? goog.getCssName('goog-drilldown-expanded') :
goog.getCssName('goog-drilldown-collapsed'));
// Default mouse event handling:
var toggler =
goog.dom.getElementsByTagName(goog.dom.TagName.DIV, fragment)[0];
selfObj.getHandler().listen(toggler, 'click', function(event) {
selfObj.setExpanded(!selfObj.isExpanded());
});
};
//
// Private methods
//
/**
* Turn display of a DrilldownRow on or off. If the DrilldownRow has not
* yet been rendered, this renders it. This propagates the effect
* of the change recursively as needed -- children displaying iff the
* parent is displayed and expanded.
*
* @param {boolean} display state, true iff display is desired.
* @private
*/
goog.ui.DrilldownRow.prototype.setDisplayable_ = function(display) {
if (display && !this.isInDocument()) {
this.render();
}
if (this.displayed_ == display) {
return;
}
this.displayed_ = display;
if (this.isInDocument()) {
this.getElement().style.display = display ? '' : 'none';
}
var selfObj = this;
this.forEachChild(function(child) {
child.setDisplayable_(display && selfObj.expanded_);
});
};
/**
* True iff this and all its DrilldownRow parents are displayable. The
* value is an approximation to actual visibility, since it does not
* look at whether DOM nodes containing the top-level component have
* display: none, visibility: hidden or are otherwise not displayable.
* So this visibility is relative to the top-level component.
*
* @return {boolean} visibility of this relative to its top-level drilldown.
* @private
*/
goog.ui.DrilldownRow.prototype.isVisible_ = function() {
for (var component = this; component instanceof goog.ui.DrilldownRow;
component = component.getParent()) {
if (!component.displayed_) return false;
}
return true;
};
/**
* Create and return a TR element from HTML that looks like
* "<tr> ... </tr>".
*
* @param {!goog.html.SafeHtml} html for one row.
* @param {!goog.dom.DomHelper} dom DOM to hold the Element.
* @return {Element} table row node created from the HTML.
* @private
*/
goog.ui.DrilldownRow.createRowNode_ = function(html, dom) {
// Note: this may be slow.
var tableHtml = goog.html.SafeHtml.create(goog.dom.TagName.TABLE, {}, html);
var div = dom.createElement(goog.dom.TagName.DIV);
goog.dom.safe.setInnerHtml(div, tableHtml);
return div.firstChild.rows[0];
};
/**
* Get the recursively rightmost child that is in the document.
*
* @return {goog.ui.DrilldownRow} rightmost child currently entered in
* the document, potentially this DrilldownRow. If this is in the
* document, result is non-null.
* @private
*/
goog.ui.DrilldownRow.prototype.lastRenderedLeaf_ = function() {
var leaf = null;
for (var node = this; node && node.isInDocument();
// Node will become undefined if parent has no children.
node = node.getChildAt(node.getChildCount() - 1)) {
leaf = node;
}
return /** @type {goog.ui.DrilldownRow} */ (leaf);
};
/**
* Search this node's direct children for the last one that is in the
* document and is before the given child.
* @param {goog.ui.DrilldownRow} child The child to stop the search at.
* @return {goog.ui.Component?} The last child component before the given child
* that is in the document.
* @private
*/
goog.ui.DrilldownRow.prototype.previousRenderedChild_ = function(child) {
for (var i = this.getChildCount() - 1; i >= 0; i--) {
if (this.getChildAt(i) == child) {
for (var j = i - 1; j >= 0; j--) {
var prev = this.getChildAt(j);
if (prev.isInDocument()) {
return prev;
}
}
}
}
return null;
};
|