sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
129 lines (107 loc) • 4.09 kB
JavaScript
// 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 A class for representing items in menus.
* @see goog.ui.Menu
*
*
*
*/
goog.provide('goog.ui.MenuItem');
goog.require('goog.ui.Component.State');
goog.require('goog.ui.Control');
goog.require('goog.ui.ControlContent');
goog.require('goog.ui.MenuItemRenderer');
goog.require('goog.ui.registry');
/**
* Class representing an item in a menu.
*
* @param {goog.ui.ControlContent} content Text caption or DOM structure to
* display as the content of the item (use to add icons or styling to
* menus).
* @param {*=} opt_model Data/model associated with the menu item.
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper used for
* document interactions.
* @param {goog.ui.MenuItemRenderer=} opt_renderer Optional renderer.
* @constructor
* @extends {goog.ui.Control}
*/
goog.ui.MenuItem = function(content, opt_model, opt_domHelper, opt_renderer) {
goog.ui.Control.call(this, content, opt_renderer ||
goog.ui.MenuItemRenderer.getInstance(), opt_domHelper);
this.setValue(opt_model);
};
goog.inherits(goog.ui.MenuItem, goog.ui.Control);
// goog.ui.Component and goog.ui.Control implementation.
/**
* Returns the value associated with the menu item. The default implementation
* returns the model object associated with the item (if any), or its caption.
* @return {*} Value associated with the menu item, if any, or its caption.
*/
goog.ui.MenuItem.prototype.getValue = function() {
var model = this.getModel();
return model != null ? model : this.getCaption();
};
/**
* Sets the value associated with the menu item. The default implementation
* stores the value as the model of the menu item.
* @param {*} value Value to be associated with the menu item.
*/
goog.ui.MenuItem.prototype.setValue = function(value) {
this.setModel(value);
};
/**
* Sets the menu item to be selectable or not. Set to true for menu items
* that represent selectable options.
* @param {boolean} selectable Whether the menu item is selectable.
*/
goog.ui.MenuItem.prototype.setSelectable = function(selectable) {
this.setSupportedState(goog.ui.Component.State.SELECTED, selectable);
if (this.isChecked() && !selectable) {
this.setChecked(false);
}
var element = this.getElement();
if (element) {
this.getRenderer().setSelectable(this, element, selectable);
}
};
/**
* Sets the menu item to be checkable or not. Set to true for menu items
* that represent checkable options.
* @param {boolean} checkable Whether the menu item is checkable.
*/
goog.ui.MenuItem.prototype.setCheckable = function(checkable) {
this.setSupportedState(goog.ui.Component.State.CHECKED, checkable);
var element = this.getElement();
if (element) {
this.getRenderer().setCheckable(this, element, checkable);
}
};
/**
* Returns the text caption of the component while ignoring accelerators.
* @return {?string} Text caption of the component (null if none).
*/
goog.ui.MenuItem.prototype.getCaption = function() {
return this.getCaptionInternal(function(element) {
return goog.dom.classes.has(element,
goog.getCssName('goog-menuitem-accel')) ? '' :
goog.dom.getTextContent(element);
});
};
// Register a decorator factory function for goog.ui.MenuItems.
goog.ui.registry.setDecoratorByClassName(goog.ui.MenuItemRenderer.CSS_CLASS,
function() {
// MenuItem defaults to using MenuItemRenderer.
return new goog.ui.MenuItem(null);
});