2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('widget-stdmod', function(Y) {
11 * Provides standard module support for Widgets through an extension.
13 * @module widget-stdmod
27 FILL_HEIGHT = "fillHeight",
31 CONTENT_SUFFIX = "Content",
33 FIRST_CHILD = "firstChild",
34 CHILD_NODES = "childNodes",
35 OWNER_DOCUMENT = "ownerDocument",
37 CONTENT_BOX = "contentBox",
40 OFFSET_HEIGHT = "offsetHeight",
43 HeaderChange = "headerContentChange",
44 BodyChange = "bodyContentChange",
45 FooterChange = "footerContentChange",
46 FillHeightChange = "fillHeightChange",
47 HeightChange = "heightChange",
48 ContentUpdate = "contentUpdate",
50 RENDERUI = "renderUI",
54 APPLY_PARSED_CONFIG = "_applyParsedConfig",
59 * Widget extension, which can be used to add Standard Module support to the
60 * base Widget class, through the <a href="Base.html#method_build">Base.build</a>
63 * The extension adds header, body and footer sections to the Widget's content box and
64 * provides the corresponding methods and attributes to modify the contents of these sections.
67 * @param {Object} The user configuration object
69 function StdMod(config) {
71 this._stdModNode = this.get(CONTENT_BOX);
73 Y.before(this._renderUIStdMod, this, RENDERUI);
74 Y.before(this._bindUIStdMod, this, BINDUI);
75 Y.before(this._syncUIStdMod, this, SYNCUI);
79 * Constant used to refer the the standard module header, in methods which expect a section specifier
81 * @property WidgetStdMod.HEADER
85 StdMod.HEADER = HEADER;
88 * Constant used to refer the the standard module body, in methods which expect a section specifier
90 * @property WidgetStdMod.BODY
97 * Constant used to refer the the standard module footer, in methods which expect a section specifier
99 * @property WidgetStdMod.FOOTER
103 StdMod.FOOTER = FOOTER;
106 * Constant used to specify insertion position, when adding content to sections of the standard module in
107 * methods which expect a "where" argument.
109 * Inserts new content <em>before</em> the sections existing content.
111 * @property WidgetStdMod.AFTER
115 StdMod.AFTER = "after";
118 * Constant used to specify insertion position, when adding content to sections of the standard module in
119 * methods which expect a "where" argument.
121 * Inserts new content <em>before</em> the sections existing content.
123 * @property WidgetStdMod.BEFORE
127 StdMod.BEFORE = "before";
129 * Constant used to specify insertion position, when adding content to sections of the standard module in
130 * methods which expect a "where" argument.
132 * <em>Replaces</em> the sections existing content, with new content.
134 * @property WidgetStdMod.REPLACE
138 StdMod.REPLACE = "replace";
140 var STD_HEADER = StdMod.HEADER,
141 STD_BODY = StdMod.BODY,
142 STD_FOOTER = StdMod.FOOTER,
144 HEADER_CONTENT = STD_HEADER + CONTENT_SUFFIX,
145 FOOTER_CONTENT = STD_FOOTER + CONTENT_SUFFIX,
146 BODY_CONTENT = STD_BODY + CONTENT_SUFFIX;
149 * Static property used to define the default attribute
150 * configuration introduced by WidgetStdMod.
152 * @property WidgetStdMod.ATTRS
159 * @attribute headerContent
160 * @type {String | Node}
162 * @description The content to be added to the header section. This will replace any existing content
163 * in the header. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
170 * @attribute footerContent
171 * @type {String | Node}
173 * @description The content to be added to the footer section. This will replace any existing content
174 * in the footer. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
181 * @attribute bodyContent
182 * @type {String | Node}
184 * @description The content to be added to the body section. This will replace any existing content
185 * in the body. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
192 * @attribute fillHeight
194 * @default WidgetStdMod.BODY
195 * @description The section (WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER) which should be resized to fill the height of the standard module, when a
196 * height is set on the Widget. If a height is not set on the widget, then all sections are sized based on
201 validator: function(val) {
202 return this._validateFillHeight(val);
208 * The HTML parsing rules for the WidgetStdMod class.
210 * @property WidgetStdMod.HTML_PARSER
214 StdMod.HTML_PARSER = {
215 headerContent: function(contentBox) {
216 return this._parseStdModHTML(STD_HEADER);
219 bodyContent: function(contentBox) {
220 return this._parseStdModHTML(STD_BODY);
223 footerContent : function(contentBox) {
224 return this._parseStdModHTML(STD_FOOTER);
229 * Static hash of default class names used for the header,
230 * body and footer sections of the standard module, keyed by
231 * the section identifier (WidgetStdMod.STD_HEADER, WidgetStdMod.STD_BODY, WidgetStdMod.STD_FOOTER)
233 * @property WidgetStdMod.SECTION_CLASS_NAMES
237 StdMod.SECTION_CLASS_NAMES = {
238 header: Widget.getClassName(HD),
239 body: Widget.getClassName(BD),
240 footer: Widget.getClassName(FT)
244 * The template HTML strings for each of the standard module sections. Section entries are keyed by the section constants,
245 * WidgetStdMod.HEADER, WidgetStdMod.BODY, WidgetStdMod.FOOTER, and contain the HTML to be added for each section.
249 * header : '<div class="yui-widget-hd"></div>',
250 * body : '<div class="yui-widget-bd"></div>',
251 * footer : '<div class="yui-widget-ft"></div>'
254 * @property WidgetStdMod.TEMPLATES
259 header : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_HEADER] + '"></div>',
260 body : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_BODY] + '"></div>',
261 footer : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_FOOTER] + '"></div>'
267 * Synchronizes the UI to match the Widgets standard module state.
269 * This method is invoked after syncUI is invoked for the Widget class
270 * using YUI's aop infrastructure.
272 * @method _syncUIStdMod
275 _syncUIStdMod : function() {
276 var stdModParsed = this._stdModParsed;
278 if (!stdModParsed || !stdModParsed[HEADER_CONTENT]) {
279 this._uiSetStdMod(STD_HEADER, this.get(HEADER_CONTENT));
282 if (!stdModParsed || !stdModParsed[BODY_CONTENT]) {
283 this._uiSetStdMod(STD_BODY, this.get(BODY_CONTENT));
286 if (!stdModParsed || !stdModParsed[FOOTER_CONTENT]) {
287 this._uiSetStdMod(STD_FOOTER, this.get(FOOTER_CONTENT));
290 this._uiSetFillHeight(this.get(FILL_HEIGHT));
294 * Creates/Initializes the DOM for standard module support.
296 * This method is invoked after renderUI is invoked for the Widget class
297 * using YUI's aop infrastructure.
299 * @method _renderUIStdMod
302 _renderUIStdMod : function() {
303 this._stdModNode.addClass(Widget.getClassName(STDMOD));
304 this._renderStdModSections();
307 _renderStdModSections : function() {
308 if (L.isValue(this.get(HEADER_CONTENT))) { this._renderStdMod(STD_HEADER); }
309 if (L.isValue(this.get(BODY_CONTENT))) { this._renderStdMod(STD_BODY); }
310 if (L.isValue(this.get(FOOTER_CONTENT))) { this._renderStdMod(STD_FOOTER); }
314 * Binds event listeners responsible for updating the UI state in response to
315 * Widget standard module related state changes.
317 * This method is invoked after bindUI is invoked for the Widget class
318 * using YUI's aop infrastructure.
320 * @method _bindUIStdMod
323 _bindUIStdMod : function() {
324 this.after(HeaderChange, this._afterHeaderChange);
325 this.after(BodyChange, this._afterBodyChange);
326 this.after(FooterChange, this._afterFooterChange);
328 this.after(FillHeightChange, this._afterFillHeightChange);
329 this.after(HeightChange, this._fillHeight);
330 this.after(ContentUpdate, this._fillHeight);
334 * Default attribute change listener for the headerContent attribute, responsible
335 * for updating the UI, in response to attribute changes.
337 * @method _afterHeaderChange
339 * @param {EventFacade} e The event facade for the attribute change
341 _afterHeaderChange : function(e) {
343 this._uiSetStdMod(STD_HEADER, e.newVal, e.stdModPosition);
348 * Default attribute change listener for the bodyContent attribute, responsible
349 * for updating the UI, in response to attribute changes.
351 * @method _afterBodyChange
353 * @param {EventFacade} e The event facade for the attribute change
355 _afterBodyChange : function(e) {
357 this._uiSetStdMod(STD_BODY, e.newVal, e.stdModPosition);
362 * Default attribute change listener for the footerContent attribute, responsible
363 * for updating the UI, in response to attribute changes.
365 * @method _afterFooterChange
367 * @param {EventFacade} e The event facade for the attribute change
369 _afterFooterChange : function(e) {
371 this._uiSetStdMod(STD_FOOTER, e.newVal, e.stdModPosition);
376 * Default attribute change listener for the fillHeight attribute, responsible
377 * for updating the UI, in response to attribute changes.
379 * @method _afterFillHeightChange
381 * @param {EventFacade} e The event facade for the attribute change
383 _afterFillHeightChange: function (e) {
384 this._uiSetFillHeight(e.newVal);
388 * Default validator for the fillHeight attribute. Verifies that the
389 * value set is a valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER,
390 * or a falsey value if fillHeight is to be disabled.
392 * @method _validateFillHeight
394 * @param {String} val The section which should be setup to fill height, or false/null to disable fillHeight
395 * @return true if valid, false if not
397 _validateFillHeight : function(val) {
398 return !val || val == StdMod.BODY || val == StdMod.HEADER || val == StdMod.FOOTER;
402 * Updates the rendered UI, to resize the provided section so that the standard module fills out
403 * the specified widget height. Note: This method does not check whether or not a height is set
406 * @method _uiSetFillHeight
408 * @param {String} fillSection A valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER
410 _uiSetFillHeight : function(fillSection) {
411 var fillNode = this.getStdModNode(fillSection);
412 var currNode = this._currFillNode;
414 if (currNode && fillNode !== currNode){
415 currNode.setStyle(HEIGHT, EMPTY);
419 this._currFillNode = fillNode;
426 * Updates the rendered UI, to resize the current section specified by the fillHeight attribute, so
427 * that the standard module fills out the Widget height. If a height has not been set on Widget,
428 * the section is not resized (height is set to "auto").
430 * @method _fillHeight
433 _fillHeight : function() {
434 if (this.get(FILL_HEIGHT)) {
435 var height = this.get(HEIGHT);
436 if (height != EMPTY && height != AUTO) {
437 this.fillHeight(this._currFillNode);
443 * Updates the rendered UI, adding the provided content (either an HTML string, or node reference),
444 * to the specified section. The content is either added before, after or replaces existing content
445 * in the section, based on the value of the <code>where</code> argument.
447 * @method _uiSetStdMod
450 * @param {String} section The section to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
451 * @param {String | Node} content The new content (either as an HTML string, or Node reference) to add to the section
452 * @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
453 * If not provided, the content will replace existing content in the section.
455 _uiSetStdMod : function(section, content, where) {
456 // Using isValue, so that "" is valid content
457 if (L.isValue(content)) {
458 var node = this.getStdModNode(section) || this._renderStdMod(section);
460 this._addStdModContent(node, content, where);
462 this.set(section + CONTENT_SUFFIX, this._getStdModContent(section), {src:UI});
464 this._eraseStdMod(section);
466 this.fire(ContentUpdate);
470 * Creates the DOM node for the given section, and inserts it into the correct location in the contentBox.
472 * @method _renderStdMod
474 * @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
475 * @return {Node} A reference to the added section node
477 _renderStdMod : function(section) {
479 var contentBox = this.get(CONTENT_BOX),
480 sectionNode = this._findStdModSection(section);
483 sectionNode = this._getStdModTemplate(section);
486 this._insertStdModSection(contentBox, section, sectionNode);
488 this[section + NODE_SUFFIX] = sectionNode;
489 return this[section + NODE_SUFFIX];
493 * Removes the DOM node for the given section.
495 * @method _eraseStdMod
497 * @param {String} section The section to remove. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
499 _eraseStdMod : function(section) {
500 var sectionNode = this.getStdModNode(section);
502 sectionNode.remove(true);
503 delete this[section + NODE_SUFFIX];
508 * Helper method to insert the Node for the given section into the correct location in the contentBox.
510 * @method _insertStdModSection
512 * @param {Node} contentBox A reference to the Widgets content box.
513 * @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
514 * @param {Node} sectionNode The Node for the section.
516 _insertStdModSection : function(contentBox, section, sectionNode) {
517 var fc = contentBox.get(FIRST_CHILD);
519 if (section === STD_FOOTER || !fc) {
520 contentBox.appendChild(sectionNode);
522 if (section === STD_HEADER) {
523 contentBox.insertBefore(sectionNode, fc);
525 var footer = this[STD_FOOTER + NODE_SUFFIX];
527 contentBox.insertBefore(sectionNode, footer);
529 contentBox.appendChild(sectionNode);
536 * Gets a new Node reference for the given standard module section, by cloning
537 * the stored template node.
539 * @method _getStdModTemplate
541 * @param {String} section The section to create a new node for. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
542 * @return {Node} The new Node instance for the section
544 _getStdModTemplate : function(section) {
545 return Node.create(StdMod.TEMPLATES[section], this._stdModNode.get(OWNER_DOCUMENT));
549 * Helper method to add content to a StdMod section node.
550 * The content is added either before, after or replaces the existing node content
551 * based on the value of the <code>where</code> argument.
553 * @method _addStdModContent
556 * @param {Node} node The section Node to be updated.
557 * @param {Node|NodeList|String} children The new content Node, NodeList or String to be added to section Node provided.
558 * @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
559 * If not provided, the content will replace existing content in the Node.
561 _addStdModContent : function(node, children, where) {
563 // StdMod where to Node where
565 case StdMod.BEFORE: // 0 is before fistChild
568 case StdMod.AFTER: // undefined is appendChild
571 default: // replace is replace, not specified is replace
572 where = StdMod.REPLACE;
575 node.insert(children, where);
579 * Helper method to obtain the precise height of the node provided, including padding and border.
580 * The height could be a sub-pixel value for certain browsers, such as Firefox 3.
582 * @method _getPreciseHeight
584 * @param {Node} node The node for which the precise height is required.
585 * @return {Number} The height of the Node including borders and padding, possibly a float.
587 _getPreciseHeight : function(node) {
588 var height = (node) ? node.get(OFFSET_HEIGHT) : 0,
589 getBCR = "getBoundingClientRect";
591 if (node && node.hasMethod(getBCR)) {
592 var preciseRegion = node.invoke(getBCR);
594 height = preciseRegion.bottom - preciseRegion.top;
602 * Helper method to to find the rendered node for the given section,
605 * @method _findStdModSection
607 * @param {String} section The section for which the render Node is to be found. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
608 * @return {Node} The rendered node for the given section, or null if not found.
610 _findStdModSection: function(section) {
611 return this.get(CONTENT_BOX).one("> ." + StdMod.SECTION_CLASS_NAMES[section]);
615 * Utility method, used by WidgetStdMods HTML_PARSER implementation
616 * to extract data for each section from markup.
618 * @method _parseStdModHTML
620 * @param {String} section
621 * @return {String} Inner HTML string with the contents of the section
623 _parseStdModHTML : function(section) {
625 var node = this._findStdModSection(section);
628 if (!this._stdModParsed) {
629 this._stdModParsed = {};
630 Y.before(this._applyStdModParsedConfig, this, APPLY_PARSED_CONFIG);
632 this._stdModParsed[section + CONTENT_SUFFIX] = 1;
634 return node.get("innerHTML");
641 * This method is injected before the _applyParsedConfig step in
642 * the application of HTML_PARSER, and sets up the state to
643 * identify whether or not we should remove the current DOM content
644 * or not, based on whether or not the current content attribute value
645 * was extracted from the DOM, or provided by the user configuration
647 * @method _applyStdModParsedConfig
650 _applyStdModParsedConfig : function(node, cfg, parsedCfg) {
651 var parsed = this._stdModParsed;
653 parsed[HEADER_CONTENT] = !(HEADER_CONTENT in cfg) && (HEADER_CONTENT in parsed);
654 parsed[BODY_CONTENT] = !(BODY_CONTENT in cfg) && (BODY_CONTENT in parsed);
655 parsed[FOOTER_CONTENT] = !(FOOTER_CONTENT in cfg) && (FOOTER_CONTENT in parsed);
660 * Retrieves the child nodes (content) of a standard module section
662 * @method _getStdModContent
664 * @param {String} section The standard module section whose child nodes are to be retrieved. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
665 * @return {Node} The child node collection of the standard module section.
667 _getStdModContent : function(section) {
668 return (this[section + NODE_SUFFIX]) ? this[section + NODE_SUFFIX].get(CHILD_NODES) : null;
672 * Updates the body section of the standard module with the content provided (either an HTML string, or node reference).
674 * This method can be used instead of the corresponding section content attribute if you'd like to retain the current content of the section,
675 * and insert content before or after it, by specifying the <code>where</code> argument.
677 * @method setStdModContent
678 * @param {String} section The standard module section whose content is to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
679 * @param {String | Node} content The content to be added, either an HTML string or a Node reference.
680 * @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
681 * If not provided, the content will replace existing content in the section.
683 setStdModContent : function(section, content, where) {
684 this.set(section + CONTENT_SUFFIX, content, {stdModPosition:where});
688 * Returns the node reference for the given section. Note: The DOM is not queried for the node reference. The reference
689 * stored by the widget instance is returned if set.
691 * @method getStdModNode
692 * @param {String} section The section whose node reference is required. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
693 * @return {Node} The node reference for the section, or null if not set.
695 getStdModNode : function(section) {
696 return this[section + NODE_SUFFIX] || null;
700 * Sets the height on the provided header, body or footer element to
701 * fill out the height of the Widget. It determines the height of the
702 * widgets bounding box, based on it's configured height value, and
703 * sets the height of the provided section to fill out any
704 * space remaining after the other standard module section heights
705 * have been accounted for.
707 * <p><strong>NOTE:</strong> This method is not designed to work if an explicit
708 * height has not been set on the Widget, since for an "auto" height Widget,
709 * the heights of the header/body/footer will drive the height of the Widget.</p>
712 * @param {Node} node The node which should be resized to fill out the height
713 * of the Widget bounding box. Should be a standard module section node which belongs
716 fillHeight : function(node) {
718 var contentBox = this.get(CONTENT_BOX),
719 stdModNodes = [this.headerNode, this.bodyNode, this.footerNode],
727 for (var i = 0, l = stdModNodes.length; i < l; i++) {
728 stdModNode = stdModNodes[i];
730 if (stdModNode !== node) {
731 filled += this._getPreciseHeight(stdModNode);
739 if (UA.ie || UA.opera) {
740 // Need to set height to 0, to allow height to be reduced
741 node.set(OFFSET_HEIGHT, 0);
744 cbContentHeight = contentBox.get(OFFSET_HEIGHT) -
745 parseInt(contentBox.getComputedStyle("paddingTop"), 10) -
746 parseInt(contentBox.getComputedStyle("paddingBottom"), 10) -
747 parseInt(contentBox.getComputedStyle("borderBottomWidth"), 10) -
748 parseInt(contentBox.getComputedStyle("borderTopWidth"), 10);
750 if (L.isNumber(cbContentHeight)) {
751 remaining = cbContentHeight - filled;
752 if (remaining >= 0) {
753 node.set(OFFSET_HEIGHT, remaining);
761 Y.WidgetStdMod = StdMod;
764 }, '3.3.0' ,{requires:['base-build', 'widget']});