123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- goog.provide('goog.ui.Menu');
- goog.provide('goog.ui.Menu.EventType');
- goog.require('goog.dom.TagName');
- goog.require('goog.math.Coordinate');
- goog.require('goog.string');
- goog.require('goog.style');
- goog.require('goog.ui.Component.EventType');
- goog.require('goog.ui.Component.State');
- goog.require('goog.ui.Container');
- goog.require('goog.ui.Container.Orientation');
- goog.require('goog.ui.MenuHeader');
- goog.require('goog.ui.MenuItem');
- goog.require('goog.ui.MenuRenderer');
- goog.require('goog.ui.MenuSeparator');
- goog.ui.Menu = function(opt_domHelper, opt_renderer) {
- goog.ui.Container.call(
- this, goog.ui.Container.Orientation.VERTICAL,
- opt_renderer || goog.ui.MenuRenderer.getInstance(), opt_domHelper);
-
-
-
- this.setFocusable(false);
- };
- goog.inherits(goog.ui.Menu, goog.ui.Container);
- goog.tagUnsealableClass(goog.ui.Menu);
- goog.ui.Menu.EventType = {
-
- BEFORE_SHOW: goog.ui.Component.EventType.BEFORE_SHOW,
-
- SHOW: goog.ui.Component.EventType.SHOW,
-
- BEFORE_HIDE: goog.ui.Component.EventType.HIDE,
-
- HIDE: goog.ui.Component.EventType.HIDE
- };
- goog.ui.Menu.CSS_CLASS = goog.ui.MenuRenderer.CSS_CLASS;
- goog.ui.Menu.prototype.openingCoords;
- goog.ui.Menu.prototype.allowAutoFocus_ = true;
- goog.ui.Menu.prototype.allowHighlightDisabled_ = false;
- goog.ui.Menu.prototype.getCssClass = function() {
- return this.getRenderer().getCssClass();
- };
- goog.ui.Menu.prototype.containsElement = function(element) {
- if (this.getRenderer().containsElement(this, element)) {
- return true;
- }
- for (var i = 0, count = this.getChildCount(); i < count; i++) {
- var child = this.getChildAt(i);
- if (typeof child.containsElement == 'function' &&
- child.containsElement(element)) {
- return true;
- }
- }
- return false;
- };
- goog.ui.Menu.prototype.addItem = function(item) {
- this.addChild(item, true);
- };
- goog.ui.Menu.prototype.addItemAt = function(item, n) {
- this.addChildAt(item, n, true);
- };
- goog.ui.Menu.prototype.removeItem = function(item) {
- var removedChild = this.removeChild(item, true);
- if (removedChild) {
- removedChild.dispose();
- }
- };
- goog.ui.Menu.prototype.removeItemAt = function(n) {
- var removedChild = this.removeChildAt(n, true);
- if (removedChild) {
- removedChild.dispose();
- }
- };
- goog.ui.Menu.prototype.getItemAt = function(n) {
- return (this.getChildAt(n));
- };
- goog.ui.Menu.prototype.getItemCount = function() {
- return this.getChildCount();
- };
- goog.ui.Menu.prototype.getItems = function() {
-
-
- var children = [];
- this.forEachChild(function(child) { children.push(child); });
- return children;
- };
- goog.ui.Menu.prototype.setPosition = function(x, opt_y) {
-
-
- var visible = this.isVisible();
- if (!visible) {
- goog.style.setElementShown(this.getElement(), true);
- }
- goog.style.setPageOffset(this.getElement(), x, opt_y);
- if (!visible) {
- goog.style.setElementShown(this.getElement(), false);
- }
- };
- goog.ui.Menu.prototype.getPosition = function() {
- return this.isVisible() ? goog.style.getPageOffset(this.getElement()) : null;
- };
- goog.ui.Menu.prototype.setAllowAutoFocus = function(allow) {
- this.allowAutoFocus_ = allow;
- if (allow) {
- this.setFocusable(true);
- }
- };
- goog.ui.Menu.prototype.getAllowAutoFocus = function() {
- return this.allowAutoFocus_;
- };
- goog.ui.Menu.prototype.setAllowHighlightDisabled = function(allow) {
- this.allowHighlightDisabled_ = allow;
- };
- goog.ui.Menu.prototype.getAllowHighlightDisabled = function() {
- return this.allowHighlightDisabled_;
- };
- goog.ui.Menu.prototype.setVisible = function(show, opt_force, opt_e) {
- var visibilityChanged =
- goog.ui.Menu.superClass_.setVisible.call(this, show, opt_force);
- if (visibilityChanged && show && this.isInDocument() &&
- this.allowAutoFocus_) {
- this.getKeyEventTarget().focus();
- }
- if (show && opt_e && goog.isNumber(opt_e.clientX)) {
- this.openingCoords = new goog.math.Coordinate(opt_e.clientX, opt_e.clientY);
- } else {
- this.openingCoords = null;
- }
- return visibilityChanged;
- };
- goog.ui.Menu.prototype.handleEnterItem = function(e) {
- if (this.allowAutoFocus_) {
- this.getKeyEventTarget().focus();
- }
- return goog.ui.Menu.superClass_.handleEnterItem.call(this, e);
- };
- goog.ui.Menu.prototype.highlightNextPrefix = function(charStr) {
- var re = new RegExp('^' + goog.string.regExpEscape(charStr), 'i');
- return this.highlightHelper(function(index, max) {
-
- var start = index < 0 ? 0 : index;
- var wrapped = false;
-
-
-
-
-
- do {
- ++index;
- if (index == max) {
- index = 0;
- wrapped = true;
- }
- var name = this.getChildAt(index).getCaption();
- if (name && name.match(re)) {
- return index;
- }
- } while (!wrapped || index != start);
- return this.getHighlightedIndex();
- }, this.getHighlightedIndex());
- };
- goog.ui.Menu.prototype.canHighlightItem = function(item) {
- return (this.allowHighlightDisabled_ || item.isEnabled()) &&
- item.isVisible() && item.isSupportedState(goog.ui.Component.State.HOVER);
- };
- goog.ui.Menu.prototype.decorateInternal = function(element) {
- this.decorateContent(element);
- goog.ui.Menu.superClass_.decorateInternal.call(this, element);
- };
- goog.ui.Menu.prototype.handleKeyEventInternal = function(e) {
- var handled = goog.ui.Menu.base(this, 'handleKeyEventInternal', e);
- if (!handled) {
-
-
- this.forEachChild(function(menuItem) {
- if (!handled && menuItem.getMnemonic &&
- menuItem.getMnemonic() == e.keyCode) {
- if (this.isEnabled()) {
- this.setHighlighted(menuItem);
- }
-
-
- handled = menuItem.handleKeyEvent(e);
- }
- }, this);
- }
- return handled;
- };
- goog.ui.Menu.prototype.setHighlightedIndex = function(index) {
- goog.ui.Menu.base(this, 'setHighlightedIndex', index);
-
-
- var child = this.getChildAt(index);
- if (child) {
- goog.style.scrollIntoContainerView(child.getElement(), this.getElement());
- }
- };
- goog.ui.Menu.prototype.decorateContent = function(element) {
- var renderer = this.getRenderer();
- var contentElements = this.getDomHelper().getElementsByTagNameAndClass(
- goog.dom.TagName.DIV, goog.getCssName(renderer.getCssClass(), 'content'),
- element);
-
-
-
- var length = contentElements.length;
- for (var i = 0; i < length; i++) {
- renderer.decorateChildren(this, contentElements[i]);
- }
- };
|