123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- 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');
- goog.ui.DrilldownRow = function(opt_properties, opt_domHelper) {
- goog.ui.Component.call(this, opt_domHelper);
- var properties = opt_properties || {};
-
- 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;
- }
-
- this.html_ = html;
-
- this.expanded_ =
- typeof properties.expanded != 'undefined' ? properties.expanded : true;
-
- this.decoratorFn_ = properties.decorator || goog.ui.DrilldownRow.decorate;
-
- this.displayed_ = true;
- };
- goog.inherits(goog.ui.DrilldownRow, goog.ui.Component);
- goog.ui.DrilldownRow.DrilldownRowProperties;
- 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) {
-
- 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'));
- });
- }
- };
- goog.ui.DrilldownRow.prototype.enterDocument = function() {
- goog.ui.DrilldownRow.superClass_.enterDocument.call(this);
- this.decoratorFn_(this, this.getHandler());
- };
- goog.ui.DrilldownRow.prototype.createDom = function() {
- this.setElementInternal(
- goog.ui.DrilldownRow.createRowNode_(this.html_, this.getDomHelper()));
- };
- goog.ui.DrilldownRow.prototype.canDecorate = function(node) {
- return node.tagName == goog.dom.TagName.TR;
- };
- 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();
- }
- };
- goog.ui.DrilldownRow.prototype.removeChild = function(child) {
- goog.dom.removeNode(child.getElement());
- return goog.ui.DrilldownRow.superClass_.removeChild.call(this, child);
- };
- goog.ui.DrilldownRow.prototype.render = function() {
- if (arguments.length) {
- throw Error('A DrilldownRow cannot be placed under a specific parent.');
- } else {
- var parent = this.getParent();
- if (!parent.isInDocument()) {
- throw Error('Cannot render child of un-rendered 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 = (row.nextSibling);
-
- if (row) {
- this.renderBefore(row);
- } else {
-
-
- var tbody = (parent.getElement().parentNode);
- goog.ui.DrilldownRow.superClass_.render.call(this, tbody);
- }
- }
- };
- goog.ui.DrilldownRow.prototype.findIndex = function() {
- var parent = this.getParent();
- if (!parent) {
- throw Error('Component has no parent');
- }
- return parent.indexOfChild(this);
- };
- goog.ui.DrilldownRow.prototype.isExpanded = function() {
- return this.expanded_;
- };
- 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); });
- }
- }
- };
- goog.ui.DrilldownRow.prototype.getDepth = function() {
- for (var component = this, depth = 0;
- component instanceof goog.ui.DrilldownRow;
- component = component.getParent(), depth++) {
- }
- return depth;
- };
- goog.ui.DrilldownRow.decorate = function(selfObj) {
- var depth = selfObj.getDepth();
- var row = selfObj.getElement();
- goog.asserts.assert(row);
- if (!row.cells) {
- throw 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;'},
-
-
- 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'));
-
- var toggler =
- goog.dom.getElementsByTagName(goog.dom.TagName.DIV, fragment)[0];
- selfObj.getHandler().listen(toggler, 'click', function(event) {
- selfObj.setExpanded(!selfObj.isExpanded());
- });
- };
- 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_);
- });
- };
- 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;
- };
- goog.ui.DrilldownRow.createRowNode_ = function(html, dom) {
-
- 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];
- };
- goog.ui.DrilldownRow.prototype.lastRenderedLeaf_ = function() {
- var leaf = null;
- for (var node = this; node && node.isInDocument();
-
- node = node.getChildAt(node.getChildCount() - 1)) {
- leaf = node;
- }
- return (leaf);
- };
- 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;
- };
|