123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302 |
- goog.provide('goog.ui.Component');
- goog.provide('goog.ui.Component.Error');
- goog.provide('goog.ui.Component.EventType');
- goog.provide('goog.ui.Component.State');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.NodeType');
- goog.require('goog.dom.TagName');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventTarget');
- goog.require('goog.object');
- goog.require('goog.style');
- goog.require('goog.ui.IdGenerator');
- goog.ui.Component = function(opt_domHelper) {
- goog.events.EventTarget.call(this);
-
- this.dom_ = opt_domHelper || goog.dom.getDomHelper();
-
- this.rightToLeft_ = goog.ui.Component.defaultRightToLeft_;
-
- this.id_ = null;
-
- this.inDocument_ = false;
-
-
- this.element_ = null;
-
- this.googUiComponentHandler_ = void 0;
-
- this.model_ = null;
-
- this.parent_ = null;
-
- this.children_ = null;
-
- this.childIndex_ = null;
-
- this.wasDecorated_ = false;
- };
- goog.inherits(goog.ui.Component, goog.events.EventTarget);
- goog.define('goog.ui.Component.ALLOW_DETACHED_DECORATION', false);
- goog.ui.Component.prototype.idGenerator_ = goog.ui.IdGenerator.getInstance();
- goog.define('goog.ui.Component.DEFAULT_BIDI_DIR', 0);
- goog.ui.Component.defaultRightToLeft_ =
- (goog.ui.Component.DEFAULT_BIDI_DIR == 1) ?
- false :
- (goog.ui.Component.DEFAULT_BIDI_DIR == -1) ? true : null;
- goog.ui.Component.EventType = {
-
- BEFORE_SHOW: 'beforeshow',
-
- SHOW: 'show',
-
- HIDE: 'hide',
-
- DISABLE: 'disable',
-
- ENABLE: 'enable',
-
- HIGHLIGHT: 'highlight',
-
- UNHIGHLIGHT: 'unhighlight',
-
- ACTIVATE: 'activate',
-
- DEACTIVATE: 'deactivate',
-
- SELECT: 'select',
-
- UNSELECT: 'unselect',
-
- CHECK: 'check',
-
- UNCHECK: 'uncheck',
-
- FOCUS: 'focus',
-
- BLUR: 'blur',
-
- OPEN: 'open',
-
- CLOSE: 'close',
-
- ENTER: 'enter',
-
- LEAVE: 'leave',
-
- ACTION: 'action',
-
- CHANGE: 'change'
- };
- goog.ui.Component.Error = {
-
- NOT_SUPPORTED: 'Method not supported',
-
- DECORATE_INVALID: 'Invalid element to decorate',
-
- ALREADY_RENDERED: 'Component already rendered',
-
- PARENT_UNABLE_TO_BE_SET: 'Unable to set parent component',
-
- CHILD_INDEX_OUT_OF_BOUNDS: 'Child component index out of bounds',
-
- NOT_OUR_CHILD: 'Child is not in parent component',
-
- NOT_IN_DOCUMENT: 'Operation not supported while component is not in document',
-
- STATE_INVALID: 'Invalid component state'
- };
- goog.ui.Component.State = {
-
- ALL: 0xFF,
-
- DISABLED: 0x01,
-
- HOVER: 0x02,
-
- ACTIVE: 0x04,
-
- SELECTED: 0x08,
-
- CHECKED: 0x10,
-
- FOCUSED: 0x20,
-
- OPENED: 0x40
- };
- goog.ui.Component.getStateTransitionEvent = function(state, isEntering) {
- switch (state) {
- case goog.ui.Component.State.DISABLED:
- return isEntering ? goog.ui.Component.EventType.DISABLE :
- goog.ui.Component.EventType.ENABLE;
- case goog.ui.Component.State.HOVER:
- return isEntering ? goog.ui.Component.EventType.HIGHLIGHT :
- goog.ui.Component.EventType.UNHIGHLIGHT;
- case goog.ui.Component.State.ACTIVE:
- return isEntering ? goog.ui.Component.EventType.ACTIVATE :
- goog.ui.Component.EventType.DEACTIVATE;
- case goog.ui.Component.State.SELECTED:
- return isEntering ? goog.ui.Component.EventType.SELECT :
- goog.ui.Component.EventType.UNSELECT;
- case goog.ui.Component.State.CHECKED:
- return isEntering ? goog.ui.Component.EventType.CHECK :
- goog.ui.Component.EventType.UNCHECK;
- case goog.ui.Component.State.FOCUSED:
- return isEntering ? goog.ui.Component.EventType.FOCUS :
- goog.ui.Component.EventType.BLUR;
- case goog.ui.Component.State.OPENED:
- return isEntering ? goog.ui.Component.EventType.OPEN :
- goog.ui.Component.EventType.CLOSE;
- default:
-
- }
-
- throw Error(goog.ui.Component.Error.STATE_INVALID);
- };
- goog.ui.Component.setDefaultRightToLeft = function(rightToLeft) {
- goog.ui.Component.defaultRightToLeft_ = rightToLeft;
- };
- goog.ui.Component.prototype.getId = function() {
- return this.id_ || (this.id_ = this.idGenerator_.getNextUniqueId());
- };
- goog.ui.Component.prototype.setId = function(id) {
- if (this.parent_ && this.parent_.childIndex_) {
-
- goog.object.remove(this.parent_.childIndex_, this.id_);
- goog.object.add(this.parent_.childIndex_, id, this);
- }
-
- this.id_ = id;
- };
- goog.ui.Component.prototype.getElement = function() {
- return this.element_;
- };
- goog.ui.Component.prototype.getElementStrict = function() {
- var el = this.element_;
- goog.asserts.assert(
- el, 'Can not call getElementStrict before rendering/decorating.');
- return el;
- };
- goog.ui.Component.prototype.setElementInternal = function(element) {
- this.element_ = element;
- };
- goog.ui.Component.prototype.getElementsByClass = function(className) {
- return this.element_ ?
- this.dom_.getElementsByClass(className, this.element_) :
- [];
- };
- goog.ui.Component.prototype.getElementByClass = function(className) {
- return this.element_ ? this.dom_.getElementByClass(className, this.element_) :
- null;
- };
- goog.ui.Component.prototype.getRequiredElementByClass = function(className) {
- var el = this.getElementByClass(className);
- goog.asserts.assert(
- el, 'Expected element in component with class: %s', className);
- return el;
- };
- goog.ui.Component.prototype.getHandler = function() {
-
-
- var self = (this);
- if (!self.googUiComponentHandler_) {
- self.googUiComponentHandler_ = new goog.events.EventHandler(self);
- }
- return self.googUiComponentHandler_;
- };
- goog.ui.Component.prototype.setParent = function(parent) {
- if (this == parent) {
-
- throw Error(goog.ui.Component.Error.PARENT_UNABLE_TO_BE_SET);
- }
- if (parent && this.parent_ && this.id_ && this.parent_.getChild(this.id_) &&
- this.parent_ != parent) {
-
-
- throw Error(goog.ui.Component.Error.PARENT_UNABLE_TO_BE_SET);
- }
- this.parent_ = parent;
- goog.ui.Component.superClass_.setParentEventTarget.call(this, parent);
- };
- goog.ui.Component.prototype.getParent = function() {
- return this.parent_;
- };
- goog.ui.Component.prototype.setParentEventTarget = function(parent) {
- if (this.parent_ && this.parent_ != parent) {
- throw Error(goog.ui.Component.Error.NOT_SUPPORTED);
- }
- goog.ui.Component.superClass_.setParentEventTarget.call(this, parent);
- };
- goog.ui.Component.prototype.getDomHelper = function() {
- return this.dom_;
- };
- goog.ui.Component.prototype.isInDocument = function() {
- return this.inDocument_;
- };
- goog.ui.Component.prototype.createDom = function() {
- this.element_ = this.dom_.createElement(goog.dom.TagName.DIV);
- };
- goog.ui.Component.prototype.render = function(opt_parentElement) {
- this.render_(opt_parentElement);
- };
- goog.ui.Component.prototype.renderBefore = function(sibling) {
- this.render_( (sibling.parentNode), sibling);
- };
- goog.ui.Component.prototype.render_ = function(
- opt_parentElement, opt_beforeNode) {
- if (this.inDocument_) {
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- }
- if (!this.element_) {
- this.createDom();
- }
- if (opt_parentElement) {
- opt_parentElement.insertBefore(this.element_, opt_beforeNode || null);
- } else {
- this.dom_.getDocument().body.appendChild(this.element_);
- }
-
-
-
-
-
- if (!this.parent_ || this.parent_.isInDocument()) {
- this.enterDocument();
- }
- };
- goog.ui.Component.prototype.decorate = function(element) {
- if (this.inDocument_) {
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- } else if (element && this.canDecorate(element)) {
- this.wasDecorated_ = true;
-
- var doc = goog.dom.getOwnerDocument(element);
- if (!this.dom_ || this.dom_.getDocument() != doc) {
- this.dom_ = goog.dom.getDomHelper(element);
- }
-
- this.decorateInternal(element);
-
- if (!goog.ui.Component.ALLOW_DETACHED_DECORATION ||
- goog.dom.contains(doc, element)) {
- this.enterDocument();
- }
- } else {
- throw Error(goog.ui.Component.Error.DECORATE_INVALID);
- }
- };
- goog.ui.Component.prototype.canDecorate = function(element) {
- return true;
- };
- goog.ui.Component.prototype.wasDecorated = function() {
- return this.wasDecorated_;
- };
- goog.ui.Component.prototype.decorateInternal = function(element) {
- this.element_ = element;
- };
- goog.ui.Component.prototype.enterDocument = function() {
- this.inDocument_ = true;
-
-
-
-
- this.forEachChild(function(child) {
- if (!child.isInDocument() && child.getElement()) {
- child.enterDocument();
- }
- });
- };
- goog.ui.Component.prototype.exitDocument = function() {
-
- this.forEachChild(function(child) {
- if (child.isInDocument()) {
- child.exitDocument();
- }
- });
- if (this.googUiComponentHandler_) {
- this.googUiComponentHandler_.removeAll();
- }
- this.inDocument_ = false;
- };
- goog.ui.Component.prototype.disposeInternal = function() {
- if (this.inDocument_) {
- this.exitDocument();
- }
- if (this.googUiComponentHandler_) {
- this.googUiComponentHandler_.dispose();
- delete this.googUiComponentHandler_;
- }
-
- this.forEachChild(function(child) { child.dispose(); });
-
- if (!this.wasDecorated_ && this.element_) {
- goog.dom.removeNode(this.element_);
- }
- this.children_ = null;
- this.childIndex_ = null;
- this.element_ = null;
- this.model_ = null;
- this.parent_ = null;
- goog.ui.Component.superClass_.disposeInternal.call(this);
- };
- goog.ui.Component.prototype.makeId = function(idFragment) {
- return this.getId() + '.' + idFragment;
- };
- goog.ui.Component.prototype.makeIds = function(object) {
- var ids = {};
- for (var key in object) {
- ids[key] = this.makeId(object[key]);
- }
- return ids;
- };
- goog.ui.Component.prototype.getModel = function() {
- return this.model_;
- };
- goog.ui.Component.prototype.setModel = function(obj) {
- this.model_ = obj;
- };
- goog.ui.Component.prototype.getFragmentFromId = function(id) {
- return id.substring(this.getId().length + 1);
- };
- goog.ui.Component.prototype.getElementByFragment = function(idFragment) {
- if (!this.inDocument_) {
- throw Error(goog.ui.Component.Error.NOT_IN_DOCUMENT);
- }
- return this.dom_.getElement(this.makeId(idFragment));
- };
- goog.ui.Component.prototype.addChild = function(child, opt_render) {
-
-
-
-
-
- this.addChildAt(child, this.getChildCount(), opt_render);
- };
- goog.ui.Component.prototype.addChildAt = function(child, index, opt_render) {
- goog.asserts.assert(!!child, 'Provided element must not be null.');
- if (child.inDocument_ && (opt_render || !this.inDocument_)) {
-
-
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- }
- if (index < 0 || index > this.getChildCount()) {
-
- throw Error(goog.ui.Component.Error.CHILD_INDEX_OUT_OF_BOUNDS);
- }
-
- if (!this.childIndex_ || !this.children_) {
- this.childIndex_ = {};
- this.children_ = [];
- }
-
- if (child.getParent() == this) {
- goog.object.set(this.childIndex_, child.getId(), child);
- goog.array.remove(this.children_, child);
-
-
- } else {
- goog.object.add(this.childIndex_, child.getId(), child);
- }
-
-
- child.setParent(this);
- goog.array.insertAt(this.children_, child, index);
- if (child.inDocument_ && this.inDocument_ && child.getParent() == this) {
-
-
- var contentElement = this.getContentElement();
- var insertBeforeElement = contentElement.childNodes[index] || null;
- if (insertBeforeElement != child.getElement()) {
- contentElement.insertBefore(child.getElement(), insertBeforeElement);
- }
- } else if (opt_render) {
-
-
-
-
-
- if (!this.element_) {
- this.createDom();
- }
-
-
-
- var sibling = this.getChildAt(index + 1);
-
- child.render_(this.getContentElement(), sibling ? sibling.element_ : null);
- } else if (
- this.inDocument_ && !child.inDocument_ && child.element_ &&
- child.element_.parentNode &&
-
-
- child.element_.parentNode.nodeType == goog.dom.NodeType.ELEMENT) {
-
-
-
-
-
-
- child.enterDocument();
- }
- };
- goog.ui.Component.prototype.getContentElement = function() {
- return this.element_;
- };
- goog.ui.Component.prototype.isRightToLeft = function() {
- if (this.rightToLeft_ == null) {
- this.rightToLeft_ = goog.style.isRightToLeft(
- this.inDocument_ ? this.element_ : this.dom_.getDocument().body);
- }
- return this.rightToLeft_;
- };
- goog.ui.Component.prototype.setRightToLeft = function(rightToLeft) {
- if (this.inDocument_) {
- throw Error(goog.ui.Component.Error.ALREADY_RENDERED);
- }
- this.rightToLeft_ = rightToLeft;
- };
- goog.ui.Component.prototype.hasChildren = function() {
- return !!this.children_ && this.children_.length != 0;
- };
- goog.ui.Component.prototype.getChildCount = function() {
- return this.children_ ? this.children_.length : 0;
- };
- goog.ui.Component.prototype.getChildIds = function() {
- var ids = [];
-
-
- this.forEachChild(function(child) {
-
- ids.push(child.getId());
- });
- return ids;
- };
- goog.ui.Component.prototype.getChild = function(id) {
-
- return (this.childIndex_ && id) ?
- (
- goog.object.get(this.childIndex_, id)) ||
- null :
- null;
- };
- goog.ui.Component.prototype.getChildAt = function(index) {
-
- return this.children_ ? this.children_[index] || null : null;
- };
- goog.ui.Component.prototype.forEachChild = function(f, opt_obj) {
- if (this.children_) {
- goog.array.forEach(this.children_, f, opt_obj);
- }
- };
- goog.ui.Component.prototype.indexOfChild = function(child) {
- return (this.children_ && child) ? goog.array.indexOf(this.children_, child) :
- -1;
- };
- goog.ui.Component.prototype.removeChild = function(child, opt_unrender) {
- if (child) {
-
-
- var id = goog.isString(child) ? child : child.getId();
- child = this.getChild(id);
- if (id && child) {
- goog.object.remove(this.childIndex_, id);
- goog.array.remove(this.children_, child);
- if (opt_unrender) {
-
-
- child.exitDocument();
- if (child.element_) {
- goog.dom.removeNode(child.element_);
- }
- }
-
-
- child.setParent(null);
- }
- }
- if (!child) {
- throw Error(goog.ui.Component.Error.NOT_OUR_CHILD);
- }
- return (child);
- };
- goog.ui.Component.prototype.removeChildAt = function(index, opt_unrender) {
-
- return this.removeChild(this.getChildAt(index), opt_unrender);
- };
- goog.ui.Component.prototype.removeChildren = function(opt_unrender) {
- var removedChildren = [];
- while (this.hasChildren()) {
- removedChildren.push(this.removeChildAt(0, opt_unrender));
- }
- return removedChildren;
- };
|