123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- goog.provide('goog.ui.SubMenu');
- goog.require('goog.Timer');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.classlist');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.positioning.AnchoredViewportPosition');
- goog.require('goog.positioning.Corner');
- goog.require('goog.style');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.Menu');
- goog.require('goog.ui.MenuItem');
- goog.require('goog.ui.SubMenuRenderer');
- goog.require('goog.ui.registry');
- goog.ui.SubMenu = function(content, opt_model, opt_domHelper, opt_renderer) {
- goog.ui.MenuItem.call(
- this, content, opt_model, opt_domHelper,
- opt_renderer || goog.ui.SubMenuRenderer.getInstance());
- };
- goog.inherits(goog.ui.SubMenu, goog.ui.MenuItem);
- goog.tagUnsealableClass(goog.ui.SubMenu);
- goog.ui.SubMenu.MENU_DELAY_MS = 218;
- goog.ui.SubMenu.prototype.dismissTimer_ = null;
- goog.ui.SubMenu.prototype.showTimer_ = null;
- goog.ui.SubMenu.prototype.menuIsVisible_ = false;
- goog.ui.SubMenu.prototype.subMenu_ = null;
- goog.ui.SubMenu.prototype.externalSubMenu_ = false;
- goog.ui.SubMenu.prototype.alignToEnd_ = true;
- goog.ui.SubMenu.prototype.isPositionAdjustable_ = false;
- goog.ui.SubMenu.prototype.enterDocument = function() {
- goog.ui.SubMenu.superClass_.enterDocument.call(this);
- this.getHandler().listen(
- this.getParent(), goog.ui.Component.EventType.HIDE, this.onParentHidden_);
- if (this.subMenu_) {
- this.setMenuListenersEnabled_(this.subMenu_, true);
- }
- };
- goog.ui.SubMenu.prototype.exitDocument = function() {
- this.getHandler().unlisten(
- this.getParent(), goog.ui.Component.EventType.HIDE, this.onParentHidden_);
- if (this.subMenu_) {
- this.setMenuListenersEnabled_(this.subMenu_, false);
- if (!this.externalSubMenu_) {
- this.subMenu_.exitDocument();
- goog.dom.removeNode(this.subMenu_.getElement());
- }
- }
- goog.ui.SubMenu.superClass_.exitDocument.call(this);
- };
- goog.ui.SubMenu.prototype.disposeInternal = function() {
- if (this.subMenu_ && !this.externalSubMenu_) {
- this.subMenu_.dispose();
- }
- this.subMenu_ = null;
- goog.ui.SubMenu.superClass_.disposeInternal.call(this);
- };
- goog.ui.SubMenu.prototype.setHighlighted = function(highlight, opt_btnPressed) {
- goog.ui.SubMenu.superClass_.setHighlighted.call(this, highlight);
- if (opt_btnPressed) {
- this.getMenu().setMouseButtonPressed(true);
- }
- if (!highlight) {
- if (this.dismissTimer_) {
- goog.Timer.clear(this.dismissTimer_);
- }
- this.dismissTimer_ = goog.Timer.callOnce(
- this.dismissSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
- }
- };
- goog.ui.SubMenu.prototype.showSubMenu = function() {
-
-
- var parent = this.getParent();
- if (parent && parent.getHighlighted() == this) {
- this.setSubMenuVisible_(true);
- this.dismissSiblings_();
- }
- };
- goog.ui.SubMenu.prototype.dismissSubMenu = function() {
-
-
- var subMenu = this.subMenu_;
- if (subMenu && subMenu.getParent() == this) {
- this.setSubMenuVisible_(false);
- subMenu.forEachChild(function(child) {
- if (typeof child.dismissSubMenu == 'function') {
- child.dismissSubMenu();
- }
- });
- }
- };
- goog.ui.SubMenu.prototype.clearTimers = function() {
- if (this.dismissTimer_) {
- goog.Timer.clear(this.dismissTimer_);
- }
- if (this.showTimer_) {
- goog.Timer.clear(this.showTimer_);
- }
- };
- goog.ui.SubMenu.prototype.setVisible = function(visible, opt_force) {
- var visibilityChanged =
- goog.ui.SubMenu.superClass_.setVisible.call(this, visible, opt_force);
-
-
- if (visibilityChanged && !this.isVisible()) {
- this.dismissSubMenu();
- }
- return visibilityChanged;
- };
- goog.ui.SubMenu.prototype.dismissSiblings_ = function() {
- this.getParent().forEachChild(function(child) {
- if (child != this && typeof child.dismissSubMenu == 'function') {
- child.dismissSubMenu();
- child.clearTimers();
- }
- }, this);
- };
- goog.ui.SubMenu.prototype.handleKeyEvent = function(e) {
- var keyCode = e.keyCode;
- var openKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.LEFT :
- goog.events.KeyCodes.RIGHT;
- var closeKeyCode = this.isRightToLeft() ? goog.events.KeyCodes.RIGHT :
- goog.events.KeyCodes.LEFT;
- if (!this.menuIsVisible_) {
-
-
- if (this.isEnabled() &&
- (keyCode == openKeyCode || keyCode == this.getMnemonic())) {
- this.showSubMenu();
- this.getMenu().highlightFirst();
- this.clearTimers();
-
-
- } else {
- return false;
- }
-
-
- } else if (this.getMenu().handleKeyEvent(e)) {
-
-
-
- } else if (keyCode == closeKeyCode) {
- this.dismissSubMenu();
- } else {
-
- return false;
- }
- e.preventDefault();
- return true;
- };
- goog.ui.SubMenu.prototype.onChildEnter_ = function(e) {
- if (this.subMenu_.getParent() == this) {
- this.clearTimers();
- this.getParentEventTarget().setHighlighted(this);
- this.dismissSiblings_();
- }
- };
- goog.ui.SubMenu.prototype.onParentHidden_ = function(e) {
-
- if (e.target == this.getParentEventTarget()) {
-
-
-
- this.dismissSubMenu();
- this.clearTimers();
- }
- };
- goog.ui.SubMenu.prototype.handleMouseOver = function(e) {
- if (this.isEnabled()) {
- this.clearTimers();
- this.showTimer_ = goog.Timer.callOnce(
- this.showSubMenu, goog.ui.SubMenu.MENU_DELAY_MS, this);
- }
- goog.ui.SubMenu.superClass_.handleMouseOver.call(this, e);
- };
- goog.ui.SubMenu.prototype.performActionInternal = function(e) {
- this.clearTimers();
- var shouldHandleClick =
- this.isSupportedState(goog.ui.Component.State.SELECTED) ||
- this.isSupportedState(goog.ui.Component.State.CHECKED);
- if (shouldHandleClick) {
- return goog.ui.SubMenu.superClass_.performActionInternal.call(this, e);
- } else {
- this.showSubMenu();
- return true;
- }
- };
- goog.ui.SubMenu.prototype.setSubMenuVisible_ = function(visible) {
-
-
- if (!visible && this.getMenu()) {
- this.getMenu().setHighlightedIndex(-1);
- }
-
-
- this.dispatchEvent(
- goog.ui.Component.getStateTransitionEvent(
- goog.ui.Component.State.OPENED, visible));
- var subMenu = this.getMenu();
- if (visible != this.menuIsVisible_) {
- goog.dom.classlist.enable(
- goog.asserts.assert(this.getElement()),
- goog.getCssName('goog-submenu-open'), visible);
- }
- if (visible != subMenu.isVisible()) {
- if (visible) {
-
- if (!subMenu.isInDocument()) {
- subMenu.render();
- }
- subMenu.setHighlightedIndex(-1);
- }
- subMenu.setVisible(visible);
-
-
- if (visible) {
- this.positionSubMenu();
- }
- }
- this.menuIsVisible_ = visible;
- };
- goog.ui.SubMenu.prototype.setMenuListenersEnabled_ = function(menu, attach) {
- var handler = this.getHandler();
- var method = attach ? handler.listen : handler.unlisten;
- method.call(
- handler, menu, goog.ui.Component.EventType.ENTER, this.onChildEnter_);
- };
- goog.ui.SubMenu.prototype.setAlignToEnd = function(alignToEnd) {
- if (alignToEnd != this.alignToEnd_) {
- this.alignToEnd_ = alignToEnd;
- if (this.isInDocument()) {
-
- var oldElement = this.getElement();
- this.exitDocument();
- if (oldElement.nextSibling) {
- this.renderBefore( (oldElement.nextSibling));
- } else {
- this.render( (oldElement.parentNode));
- }
- }
- }
- };
- goog.ui.SubMenu.prototype.isAlignedToEnd = function() {
- return this.alignToEnd_;
- };
- goog.ui.SubMenu.prototype.positionSubMenu = function() {
- var position = new goog.positioning.AnchoredViewportPosition(
- this.getElement(),
- this.isAlignedToEnd() ? goog.positioning.Corner.TOP_END :
- goog.positioning.Corner.TOP_START,
- this.isPositionAdjustable_);
-
- var subMenu = this.getMenu();
- var el = subMenu.getElement();
- if (!subMenu.isVisible()) {
- el.style.visibility = 'hidden';
- goog.style.setElementShown(el, true);
- }
- position.reposition(
- el, this.isAlignedToEnd() ? goog.positioning.Corner.TOP_START :
- goog.positioning.Corner.TOP_END);
- if (!subMenu.isVisible()) {
- goog.style.setElementShown(el, false);
- el.style.visibility = 'visible';
- }
- };
- goog.ui.SubMenu.prototype.addItem = function(item) {
- this.getMenu().addChild(item, true);
- };
- goog.ui.SubMenu.prototype.addItemAt = function(item, n) {
- this.getMenu().addChildAt(item, n, true);
- };
- goog.ui.SubMenu.prototype.removeItem = function(item) {
- var child = this.getMenu().removeChild(item, true);
- if (child) {
- child.dispose();
- }
- };
- goog.ui.SubMenu.prototype.removeItemAt = function(n) {
- var child = this.getMenu().removeChildAt(n, true);
- if (child) {
- child.dispose();
- }
- };
- goog.ui.SubMenu.prototype.getItemAt = function(n) {
- return this.getMenu().getChildAt(n);
- };
- goog.ui.SubMenu.prototype.getItemCount = function() {
- return this.getMenu().getChildCount();
- };
- goog.ui.SubMenu.prototype.getItems = function() {
- return this.getMenu().getItems();
- };
- goog.ui.SubMenu.prototype.getMenu = function() {
- if (!this.subMenu_) {
- this.setMenu(
- new goog.ui.Menu(this.getDomHelper()), true);
- } else if (this.externalSubMenu_ && this.subMenu_.getParent() != this) {
-
-
- this.subMenu_.setParent(this);
- }
-
- if (!this.subMenu_.getElement()) {
- this.subMenu_.createDom();
- }
- return this.subMenu_;
- };
- goog.ui.SubMenu.prototype.setMenu = function(menu, opt_internal) {
- var oldMenu = this.subMenu_;
- if (menu != oldMenu) {
- if (oldMenu) {
- this.dismissSubMenu();
- if (this.isInDocument()) {
- this.setMenuListenersEnabled_(oldMenu, false);
- }
- }
- this.subMenu_ = menu;
- this.externalSubMenu_ = !opt_internal;
- if (menu) {
- menu.setParent(this);
-
- menu.setVisible(false, true);
- menu.setAllowAutoFocus(false);
- menu.setFocusable(false);
- if (this.isInDocument()) {
- this.setMenuListenersEnabled_(menu, true);
- }
- }
- }
- };
- goog.ui.SubMenu.prototype.containsElement = function(element) {
- return this.getMenu().containsElement(element);
- };
- goog.ui.SubMenu.prototype.setPositionAdjustable = function(isAdjustable) {
- this.isPositionAdjustable_ = !!isAdjustable;
- };
- goog.ui.SubMenu.prototype.isPositionAdjustable = function() {
- return this.isPositionAdjustable_;
- };
- goog.ui.registry.setDecoratorByClassName(
- goog.getCssName('goog-submenu'),
- function() { return new goog.ui.SubMenu(null); });
|