123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953 |
- goog.provide('goog.ui.ControlRenderer');
- goog.require('goog.a11y.aria');
- goog.require('goog.a11y.aria.Role');
- goog.require('goog.a11y.aria.State');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.classlist');
- goog.require('goog.object');
- goog.require('goog.string');
- goog.require('goog.style');
- goog.require('goog.ui.Component');
- goog.require('goog.ui.ControlContent');
- goog.require('goog.userAgent');
- goog.forwardDeclare('goog.ui.Control');
- goog.ui.ControlRenderer = function() {};
- goog.addSingletonGetter(goog.ui.ControlRenderer);
- goog.tagUnsealableClass(goog.ui.ControlRenderer);
- goog.ui.ControlRenderer.getCustomRenderer = function(ctor, cssClassName) {
- var renderer = new ctor();
-
- renderer.getCssClass = function() { return cssClassName; };
- return renderer;
- };
- goog.ui.ControlRenderer.CSS_CLASS = goog.getCssName('goog-control');
- goog.ui.ControlRenderer.IE6_CLASS_COMBINATIONS = [];
- goog.ui.ControlRenderer.ariaAttributeMap_;
- goog.ui.ControlRenderer.TOGGLE_ARIA_STATE_MAP_ = goog.object.create(
- goog.a11y.aria.Role.BUTTON, goog.a11y.aria.State.PRESSED,
- goog.a11y.aria.Role.CHECKBOX, goog.a11y.aria.State.CHECKED,
- goog.a11y.aria.Role.MENU_ITEM, goog.a11y.aria.State.SELECTED,
- goog.a11y.aria.Role.MENU_ITEM_CHECKBOX, goog.a11y.aria.State.CHECKED,
- goog.a11y.aria.Role.MENU_ITEM_RADIO, goog.a11y.aria.State.CHECKED,
- goog.a11y.aria.Role.RADIO, goog.a11y.aria.State.CHECKED,
- goog.a11y.aria.Role.TAB, goog.a11y.aria.State.SELECTED,
- goog.a11y.aria.Role.TREEITEM, goog.a11y.aria.State.SELECTED);
- goog.ui.ControlRenderer.prototype.getAriaRole = function() {
-
- return undefined;
- };
- goog.ui.ControlRenderer.prototype.createDom = function(control) {
-
- var element = control.getDomHelper().createDom(
- goog.dom.TagName.DIV, this.getClassNames(control).join(' '),
- control.getContent());
- return element;
- };
- goog.ui.ControlRenderer.prototype.getContentElement = function(element) {
- return element;
- };
- goog.ui.ControlRenderer.prototype.enableClassName = function(
- control, className, enable) {
- var element = (
- control.getElement ? control.getElement() : control);
- if (element) {
- var classNames = [className];
-
-
-
- if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) {
- classNames = this.getAppliedCombinedClassNames_(
- goog.dom.classlist.get(element), className);
- classNames.push(className);
- }
- goog.dom.classlist.enableAll(element, classNames, enable);
- }
- };
- goog.ui.ControlRenderer.prototype.enableExtraClassName = function(
- control, className, enable) {
-
-
- this.enableClassName(control, className, enable);
- };
- goog.ui.ControlRenderer.prototype.canDecorate = function(element) {
- return true;
- };
- goog.ui.ControlRenderer.prototype.decorate = function(control, element) {
-
- if (element.id) {
- control.setId(element.id);
- }
-
- var contentElem = this.getContentElement(element);
- if (contentElem && contentElem.firstChild) {
- control.setContentInternal(
- contentElem.firstChild.nextSibling ?
- goog.array.clone(contentElem.childNodes) :
- contentElem.firstChild);
- } else {
- control.setContentInternal(null);
- }
-
-
-
- var state = 0x00;
- var rendererClassName = this.getCssClass();
- var structuralClassName = this.getStructuralCssClass();
- var hasRendererClassName = false;
- var hasStructuralClassName = false;
- var hasCombinedClassName = false;
- var classNames = goog.array.toArray(goog.dom.classlist.get(element));
- goog.array.forEach(classNames, function(className) {
- if (!hasRendererClassName && className == rendererClassName) {
- hasRendererClassName = true;
- if (structuralClassName == rendererClassName) {
- hasStructuralClassName = true;
- }
- } else if (!hasStructuralClassName && className == structuralClassName) {
- hasStructuralClassName = true;
- } else {
- state |= this.getStateFromClass(className);
- }
- if (this.getStateFromClass(className) == goog.ui.Component.State.DISABLED) {
- goog.asserts.assertElement(contentElem);
- if (goog.dom.isFocusableTabIndex(contentElem)) {
- goog.dom.setFocusableTabIndex(contentElem, false);
- }
- }
- }, this);
- control.setStateInternal(state);
-
-
- if (!hasRendererClassName) {
- classNames.push(rendererClassName);
- if (structuralClassName == rendererClassName) {
- hasStructuralClassName = true;
- }
- }
- if (!hasStructuralClassName) {
- classNames.push(structuralClassName);
- }
- var extraClassNames = control.getExtraClassNames();
- if (extraClassNames) {
- classNames.push.apply(classNames, extraClassNames);
- }
-
-
- if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) {
- var combinedClasses = this.getAppliedCombinedClassNames_(classNames);
- if (combinedClasses.length > 0) {
- classNames.push.apply(classNames, combinedClasses);
- hasCombinedClassName = true;
- }
- }
-
- if (!hasRendererClassName || !hasStructuralClassName || extraClassNames ||
- hasCombinedClassName) {
- goog.dom.classlist.set(element, classNames.join(' '));
- }
- return element;
- };
- goog.ui.ControlRenderer.prototype.initializeDom = function(control) {
-
-
-
- if (control.isRightToLeft()) {
- this.setRightToLeft(control.getElement(), true);
- }
-
-
-
-
- if (control.isEnabled()) {
- this.setFocusable(control, control.isVisible());
- }
- };
- goog.ui.ControlRenderer.prototype.setAriaRole = function(
- element, opt_preferredRole) {
- var ariaRole = opt_preferredRole || this.getAriaRole();
- if (ariaRole) {
- goog.asserts.assert(
- element, 'The element passed as a first parameter cannot be null.');
- var currentRole = goog.a11y.aria.getRole(element);
- if (ariaRole == currentRole) {
- return;
- }
- goog.a11y.aria.setRole(element, ariaRole);
- }
- };
- goog.ui.ControlRenderer.prototype.setAriaStates = function(control, element) {
- goog.asserts.assert(control);
- goog.asserts.assert(element);
- var ariaLabel = control.getAriaLabel();
- if (goog.isDefAndNotNull(ariaLabel)) {
- this.setAriaLabel(element, ariaLabel);
- }
- if (!control.isVisible()) {
- goog.a11y.aria.setState(
- element, goog.a11y.aria.State.HIDDEN, !control.isVisible());
- }
- if (!control.isEnabled()) {
- this.updateAriaState(
- element, goog.ui.Component.State.DISABLED, !control.isEnabled());
- }
- if (control.isSupportedState(goog.ui.Component.State.SELECTED)) {
- this.updateAriaState(
- element, goog.ui.Component.State.SELECTED, control.isSelected());
- }
- if (control.isSupportedState(goog.ui.Component.State.CHECKED)) {
- this.updateAriaState(
- element, goog.ui.Component.State.CHECKED, control.isChecked());
- }
- if (control.isSupportedState(goog.ui.Component.State.OPENED)) {
- this.updateAriaState(
- element, goog.ui.Component.State.OPENED, control.isOpen());
- }
- };
- goog.ui.ControlRenderer.prototype.setAriaLabel = function(element, ariaLabel) {
- goog.a11y.aria.setLabel(element, ariaLabel);
- };
- goog.ui.ControlRenderer.prototype.setAllowTextSelection = function(
- element, allow) {
-
-
- goog.style.setUnselectable(
- element, !allow, !goog.userAgent.IE && !goog.userAgent.OPERA);
- };
- goog.ui.ControlRenderer.prototype.setRightToLeft = function(
- element, rightToLeft) {
- this.enableClassName(
- element, goog.getCssName(this.getStructuralCssClass(), 'rtl'),
- rightToLeft);
- };
- goog.ui.ControlRenderer.prototype.isFocusable = function(control) {
- var keyTarget;
- if (control.isSupportedState(goog.ui.Component.State.FOCUSED) &&
- (keyTarget = control.getKeyEventTarget())) {
- return goog.dom.isFocusableTabIndex(keyTarget);
- }
- return false;
- };
- goog.ui.ControlRenderer.prototype.setFocusable = function(control, focusable) {
- var keyTarget;
- if (control.isSupportedState(goog.ui.Component.State.FOCUSED) &&
- (keyTarget = control.getKeyEventTarget())) {
- if (!focusable && control.isFocused()) {
-
- try {
- keyTarget.blur();
- } catch (e) {
-
- }
-
-
-
-
-
-
- if (control.isFocused()) {
- control.handleBlur(null);
- }
- }
-
- if (goog.dom.isFocusableTabIndex(keyTarget) != focusable) {
- goog.dom.setFocusableTabIndex(keyTarget, focusable);
- }
- }
- };
- goog.ui.ControlRenderer.prototype.setVisible = function(element, visible) {
-
-
- goog.style.setElementShown(element, visible);
- if (element) {
- goog.a11y.aria.setState(element, goog.a11y.aria.State.HIDDEN, !visible);
- }
- };
- goog.ui.ControlRenderer.prototype.setState = function(control, state, enable) {
- var element = control.getElement();
- if (element) {
- var className = this.getClassForState(state);
- if (className) {
- this.enableClassName(control, className, enable);
- }
- this.updateAriaState(element, state, enable);
- }
- };
- goog.ui.ControlRenderer.prototype.updateAriaState = function(
- element, state, enable) {
-
- if (!goog.ui.ControlRenderer.ariaAttributeMap_) {
- goog.ui.ControlRenderer.ariaAttributeMap_ = goog.object.create(
- goog.ui.Component.State.DISABLED, goog.a11y.aria.State.DISABLED,
- goog.ui.Component.State.SELECTED, goog.a11y.aria.State.SELECTED,
- goog.ui.Component.State.CHECKED, goog.a11y.aria.State.CHECKED,
- goog.ui.Component.State.OPENED, goog.a11y.aria.State.EXPANDED);
- }
- goog.asserts.assert(
- element, 'The element passed as a first parameter cannot be null.');
- var ariaAttr = goog.ui.ControlRenderer.getAriaStateForAriaRole_(
- element, goog.ui.ControlRenderer.ariaAttributeMap_[state]);
- if (ariaAttr) {
- goog.a11y.aria.setState(element, ariaAttr, enable);
- }
- };
- goog.ui.ControlRenderer.getAriaStateForAriaRole_ = function(element, attr) {
- var role = goog.a11y.aria.getRole(element);
- if (!role) {
- return attr;
- }
- role = (role);
- var matchAttr = goog.ui.ControlRenderer.TOGGLE_ARIA_STATE_MAP_[role] || attr;
- return goog.ui.ControlRenderer.isAriaState_(attr) ? matchAttr : attr;
- };
- goog.ui.ControlRenderer.isAriaState_ = function(attr) {
- return attr == goog.a11y.aria.State.CHECKED ||
- attr == goog.a11y.aria.State.SELECTED;
- };
- goog.ui.ControlRenderer.prototype.setContent = function(element, content) {
- var contentElem = this.getContentElement(element);
- if (contentElem) {
- goog.dom.removeChildren(contentElem);
- if (content) {
- if (goog.isString(content)) {
- goog.dom.setTextContent(contentElem, content);
- } else {
- var childHandler = function(child) {
- if (child) {
- var doc = goog.dom.getOwnerDocument(contentElem);
- contentElem.appendChild(
- goog.isString(child) ? doc.createTextNode(child) : child);
- }
- };
- if (goog.isArray(content)) {
-
- goog.array.forEach(content, childHandler);
- } else if (goog.isArrayLike(content) && !('nodeType' in content)) {
-
-
-
- goog.array.forEach(
- goog.array.clone( (content)),
- childHandler);
- } else {
-
- childHandler(content);
- }
- }
- }
- }
- };
- goog.ui.ControlRenderer.prototype.getKeyEventTarget = function(control) {
- return control.getElement();
- };
- goog.ui.ControlRenderer.prototype.getCssClass = function() {
- return goog.ui.ControlRenderer.CSS_CLASS;
- };
- goog.ui.ControlRenderer.prototype.getIe6ClassCombinations = function() {
- return [];
- };
- goog.ui.ControlRenderer.prototype.getStructuralCssClass = function() {
- return this.getCssClass();
- };
- goog.ui.ControlRenderer.prototype.getClassNames = function(control) {
- var cssClass = this.getCssClass();
-
- var classNames = [cssClass];
-
- var structuralCssClass = this.getStructuralCssClass();
- if (structuralCssClass != cssClass) {
- classNames.push(structuralCssClass);
- }
-
- var classNamesForState = this.getClassNamesForState(control.getState());
- classNames.push.apply(classNames, classNamesForState);
-
- var extraClassNames = control.getExtraClassNames();
- if (extraClassNames) {
- classNames.push.apply(classNames, extraClassNames);
- }
-
- if (goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('7')) {
- classNames.push.apply(
- classNames, this.getAppliedCombinedClassNames_(classNames));
- }
- return classNames;
- };
- goog.ui.ControlRenderer.prototype.getAppliedCombinedClassNames_ = function(
- classes, opt_includedClass) {
- var toAdd = [];
- if (opt_includedClass) {
- classes = goog.array.concat(classes, [opt_includedClass]);
- }
- goog.array.forEach(this.getIe6ClassCombinations(), function(combo) {
- if (goog.array.every(combo, goog.partial(goog.array.contains, classes)) &&
- (!opt_includedClass || goog.array.contains(combo, opt_includedClass))) {
- toAdd.push(combo.join('_'));
- }
- });
- return toAdd;
- };
- goog.ui.ControlRenderer.prototype.getClassNamesForState = function(state) {
- var classNames = [];
- while (state) {
-
-
- var mask = state & -state;
- classNames.push(
- this.getClassForState(
- (mask)));
- state &= ~mask;
- }
- return classNames;
- };
- goog.ui.ControlRenderer.prototype.getClassForState = function(state) {
- if (!this.classByState_) {
- this.createClassByStateMap_();
- }
- return this.classByState_[state];
- };
- goog.ui.ControlRenderer.prototype.getStateFromClass = function(className) {
- if (!this.stateByClass_) {
- this.createStateByClassMap_();
- }
- var state = parseInt(this.stateByClass_[className], 10);
- return (isNaN(state) ? 0x00 : state);
- };
- goog.ui.ControlRenderer.prototype.createClassByStateMap_ = function() {
- var baseClass = this.getStructuralCssClass();
-
-
- var isValidClassName =
- !goog.string.contains(goog.string.normalizeWhitespace(baseClass), ' ');
- goog.asserts.assert(
- isValidClassName,
- 'ControlRenderer has an invalid css class: \'' + baseClass + '\'');
-
- this.classByState_ = goog.object.create(
- goog.ui.Component.State.DISABLED, goog.getCssName(baseClass, 'disabled'),
- goog.ui.Component.State.HOVER, goog.getCssName(baseClass, 'hover'),
- goog.ui.Component.State.ACTIVE, goog.getCssName(baseClass, 'active'),
- goog.ui.Component.State.SELECTED, goog.getCssName(baseClass, 'selected'),
- goog.ui.Component.State.CHECKED, goog.getCssName(baseClass, 'checked'),
- goog.ui.Component.State.FOCUSED, goog.getCssName(baseClass, 'focused'),
- goog.ui.Component.State.OPENED, goog.getCssName(baseClass, 'open'));
- };
- goog.ui.ControlRenderer.prototype.createStateByClassMap_ = function() {
-
- if (!this.classByState_) {
- this.createClassByStateMap_();
- }
-
- this.stateByClass_ = goog.object.transpose(this.classByState_);
- };
|