3
Copyright 2011 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('widget-stdmod', function(Y) {
10
* Provides standard module support for Widgets through an extension.
12
* @module widget-stdmod
26
FILL_HEIGHT = "fillHeight",
30
CONTENT_SUFFIX = "Content",
32
FIRST_CHILD = "firstChild",
33
CHILD_NODES = "childNodes",
34
OWNER_DOCUMENT = "ownerDocument",
36
CONTENT_BOX = "contentBox",
39
OFFSET_HEIGHT = "offsetHeight",
42
HeaderChange = "headerContentChange",
43
BodyChange = "bodyContentChange",
44
FooterChange = "footerContentChange",
45
FillHeightChange = "fillHeightChange",
46
HeightChange = "heightChange",
47
ContentUpdate = "contentUpdate",
49
RENDERUI = "renderUI",
53
APPLY_PARSED_CONFIG = "_applyParsedConfig",
58
* Widget extension, which can be used to add Standard Module support to the
59
* base Widget class, through the <a href="Base.html#method_build">Base.build</a>
62
* The extension adds header, body and footer sections to the Widget's content box and
63
* provides the corresponding methods and attributes to modify the contents of these sections.
66
* @param {Object} The user configuration object
68
function StdMod(config) {
70
this._stdModNode = this.get(CONTENT_BOX);
72
Y.before(this._renderUIStdMod, this, RENDERUI);
73
Y.before(this._bindUIStdMod, this, BINDUI);
74
Y.before(this._syncUIStdMod, this, SYNCUI);
78
* Constant used to refer the the standard module header, in methods which expect a section specifier
84
StdMod.HEADER = HEADER;
87
* Constant used to refer the the standard module body, in methods which expect a section specifier
96
* Constant used to refer the the standard module footer, in methods which expect a section specifier
102
StdMod.FOOTER = FOOTER;
105
* Constant used to specify insertion position, when adding content to sections of the standard module in
106
* methods which expect a "where" argument.
108
* Inserts new content <em>before</em> the sections existing content.
114
StdMod.AFTER = "after";
117
* Constant used to specify insertion position, when adding content to sections of the standard module in
118
* methods which expect a "where" argument.
120
* Inserts new content <em>before</em> the sections existing content.
126
StdMod.BEFORE = "before";
128
* Constant used to specify insertion position, when adding content to sections of the standard module in
129
* methods which expect a "where" argument.
131
* <em>Replaces</em> the sections existing content, with new content.
137
StdMod.REPLACE = "replace";
139
var STD_HEADER = StdMod.HEADER,
140
STD_BODY = StdMod.BODY,
141
STD_FOOTER = StdMod.FOOTER,
143
HEADER_CONTENT = STD_HEADER + CONTENT_SUFFIX,
144
FOOTER_CONTENT = STD_FOOTER + CONTENT_SUFFIX,
145
BODY_CONTENT = STD_BODY + CONTENT_SUFFIX;
148
* Static property used to define the default attribute
149
* configuration introduced by WidgetStdMod.
158
* @attribute headerContent
159
* @type {String | Node}
161
* @description The content to be added to the header section. This will replace any existing content
162
* in the header. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
169
* @attribute footerContent
170
* @type {String | Node}
172
* @description The content to be added to the footer section. This will replace any existing content
173
* in the footer. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
180
* @attribute bodyContent
181
* @type {String | Node}
183
* @description The content to be added to the body section. This will replace any existing content
184
* in the body. If you want to append, or insert new content, use the <a href="#method_setStdModContent">setStdModContent</a> method.
191
* @attribute fillHeight
193
* @default WidgetStdMod.BODY
194
* @description The section (WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER) which should be resized to fill the height of the standard module, when a
195
* height is set on the Widget. If a height is not set on the widget, then all sections are sized based on
200
validator: function(val) {
201
return this._validateFillHeight(val);
207
* The HTML parsing rules for the WidgetStdMod class.
209
* @property HTML_PARSER
213
StdMod.HTML_PARSER = {
214
headerContent: function(contentBox) {
215
return this._parseStdModHTML(STD_HEADER);
218
bodyContent: function(contentBox) {
219
return this._parseStdModHTML(STD_BODY);
222
footerContent : function(contentBox) {
223
return this._parseStdModHTML(STD_FOOTER);
228
* Static hash of default class names used for the header,
229
* body and footer sections of the standard module, keyed by
230
* the section identifier (WidgetStdMod.STD_HEADER, WidgetStdMod.STD_BODY, WidgetStdMod.STD_FOOTER)
232
* @property SECTION_CLASS_NAMES
236
StdMod.SECTION_CLASS_NAMES = {
237
header: Widget.getClassName(HD),
238
body: Widget.getClassName(BD),
239
footer: Widget.getClassName(FT)
243
* The template HTML strings for each of the standard module sections. Section entries are keyed by the section constants,
244
* WidgetStdMod.HEADER, WidgetStdMod.BODY, WidgetStdMod.FOOTER, and contain the HTML to be added for each section.
248
* header : '<div class="yui-widget-hd"></div>',
249
* body : '<div class="yui-widget-bd"></div>',
250
* footer : '<div class="yui-widget-ft"></div>'
253
* @property TEMPLATES
258
header : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_HEADER] + '"></div>',
259
body : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_BODY] + '"></div>',
260
footer : '<div class="' + StdMod.SECTION_CLASS_NAMES[STD_FOOTER] + '"></div>'
266
* Synchronizes the UI to match the Widgets standard module state.
268
* This method is invoked after syncUI is invoked for the Widget class
269
* using YUI's aop infrastructure.
271
* @method _syncUIStdMod
274
_syncUIStdMod : function() {
275
var stdModParsed = this._stdModParsed;
277
if (!stdModParsed || !stdModParsed[HEADER_CONTENT]) {
278
this._uiSetStdMod(STD_HEADER, this.get(HEADER_CONTENT));
281
if (!stdModParsed || !stdModParsed[BODY_CONTENT]) {
282
this._uiSetStdMod(STD_BODY, this.get(BODY_CONTENT));
285
if (!stdModParsed || !stdModParsed[FOOTER_CONTENT]) {
286
this._uiSetStdMod(STD_FOOTER, this.get(FOOTER_CONTENT));
289
this._uiSetFillHeight(this.get(FILL_HEIGHT));
293
* Creates/Initializes the DOM for standard module support.
295
* This method is invoked after renderUI is invoked for the Widget class
296
* using YUI's aop infrastructure.
298
* @method _renderUIStdMod
301
_renderUIStdMod : function() {
302
this._stdModNode.addClass(Widget.getClassName(STDMOD));
303
this._renderStdModSections();
305
//This normally goes in bindUI but in order to allow setStdModContent() to work before renderUI
306
//stage, these listeners should be set up at the earliest possible time.
307
this.after(HeaderChange, this._afterHeaderChange);
308
this.after(BodyChange, this._afterBodyChange);
309
this.after(FooterChange, this._afterFooterChange);
312
_renderStdModSections : function() {
313
if (L.isValue(this.get(HEADER_CONTENT))) { this._renderStdMod(STD_HEADER); }
314
if (L.isValue(this.get(BODY_CONTENT))) { this._renderStdMod(STD_BODY); }
315
if (L.isValue(this.get(FOOTER_CONTENT))) { this._renderStdMod(STD_FOOTER); }
321
* Binds event listeners responsible for updating the UI state in response to
322
* Widget standard module related state changes.
324
* This method is invoked after bindUI is invoked for the Widget class
325
* using YUI's aop infrastructure.
327
* @method _bindUIStdMod
330
_bindUIStdMod : function() {
331
// this.after(HeaderChange, this._afterHeaderChange);
332
// this.after(BodyChange, this._afterBodyChange);
333
// this.after(FooterChange, this._afterFooterChange);
335
this.after(FillHeightChange, this._afterFillHeightChange);
336
this.after(HeightChange, this._fillHeight);
337
this.after(ContentUpdate, this._fillHeight);
341
* Default attribute change listener for the headerContent attribute, responsible
342
* for updating the UI, in response to attribute changes.
344
* @method _afterHeaderChange
346
* @param {EventFacade} e The event facade for the attribute change
348
_afterHeaderChange : function(e) {
350
this._uiSetStdMod(STD_HEADER, e.newVal, e.stdModPosition);
355
* Default attribute change listener for the bodyContent attribute, responsible
356
* for updating the UI, in response to attribute changes.
358
* @method _afterBodyChange
360
* @param {EventFacade} e The event facade for the attribute change
362
_afterBodyChange : function(e) {
364
this._uiSetStdMod(STD_BODY, e.newVal, e.stdModPosition);
369
* Default attribute change listener for the footerContent attribute, responsible
370
* for updating the UI, in response to attribute changes.
372
* @method _afterFooterChange
374
* @param {EventFacade} e The event facade for the attribute change
376
_afterFooterChange : function(e) {
378
this._uiSetStdMod(STD_FOOTER, e.newVal, e.stdModPosition);
383
* Default attribute change listener for the fillHeight attribute, responsible
384
* for updating the UI, in response to attribute changes.
386
* @method _afterFillHeightChange
388
* @param {EventFacade} e The event facade for the attribute change
390
_afterFillHeightChange: function (e) {
391
this._uiSetFillHeight(e.newVal);
395
* Default validator for the fillHeight attribute. Verifies that the
396
* value set is a valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER,
397
* or a falsey value if fillHeight is to be disabled.
399
* @method _validateFillHeight
401
* @param {String} val The section which should be setup to fill height, or false/null to disable fillHeight
402
* @return true if valid, false if not
404
_validateFillHeight : function(val) {
405
return !val || val == StdMod.BODY || val == StdMod.HEADER || val == StdMod.FOOTER;
409
* Updates the rendered UI, to resize the provided section so that the standard module fills out
410
* the specified widget height. Note: This method does not check whether or not a height is set
413
* @method _uiSetFillHeight
415
* @param {String} fillSection A valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER
417
_uiSetFillHeight : function(fillSection) {
418
var fillNode = this.getStdModNode(fillSection);
419
var currNode = this._currFillNode;
421
if (currNode && fillNode !== currNode){
422
currNode.setStyle(HEIGHT, EMPTY);
426
this._currFillNode = fillNode;
433
* Updates the rendered UI, to resize the current section specified by the fillHeight attribute, so
434
* that the standard module fills out the Widget height. If a height has not been set on Widget,
435
* the section is not resized (height is set to "auto").
437
* @method _fillHeight
440
_fillHeight : function() {
441
if (this.get(FILL_HEIGHT)) {
442
var height = this.get(HEIGHT);
443
if (height != EMPTY && height != AUTO) {
444
this.fillHeight(this._currFillNode);
450
* Updates the rendered UI, adding the provided content (either an HTML string, or node reference),
451
* to the specified section. The content is either added before, after or replaces existing content
452
* in the section, based on the value of the <code>where</code> argument.
454
* @method _uiSetStdMod
457
* @param {String} section The section to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
458
* @param {String | Node} content The new content (either as an HTML string, or Node reference) to add to the section
459
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
460
* If not provided, the content will replace existing content in the section.
462
_uiSetStdMod : function(section, content, where) {
463
// Using isValue, so that "" is valid content
464
if (L.isValue(content)) {
465
var node = this.getStdModNode(section) || this._renderStdMod(section);
467
this._addStdModContent(node, content, where);
469
this.set(section + CONTENT_SUFFIX, this._getStdModContent(section), {src:UI});
471
this._eraseStdMod(section);
473
this.fire(ContentUpdate);
477
* Creates the DOM node for the given section, and inserts it into the correct location in the contentBox.
479
* @method _renderStdMod
481
* @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
482
* @return {Node} A reference to the added section node
484
_renderStdMod : function(section) {
486
var contentBox = this.get(CONTENT_BOX),
487
sectionNode = this._findStdModSection(section);
490
sectionNode = this._getStdModTemplate(section);
493
this._insertStdModSection(contentBox, section, sectionNode);
495
this[section + NODE_SUFFIX] = sectionNode;
496
return this[section + NODE_SUFFIX];
500
* Removes the DOM node for the given section.
502
* @method _eraseStdMod
504
* @param {String} section The section to remove. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
506
_eraseStdMod : function(section) {
507
var sectionNode = this.getStdModNode(section);
509
sectionNode.remove(true);
510
delete this[section + NODE_SUFFIX];
515
* Helper method to insert the Node for the given section into the correct location in the contentBox.
517
* @method _insertStdModSection
519
* @param {Node} contentBox A reference to the Widgets content box.
520
* @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
521
* @param {Node} sectionNode The Node for the section.
523
_insertStdModSection : function(contentBox, section, sectionNode) {
524
var fc = contentBox.get(FIRST_CHILD);
526
if (section === STD_FOOTER || !fc) {
527
contentBox.appendChild(sectionNode);
529
if (section === STD_HEADER) {
530
contentBox.insertBefore(sectionNode, fc);
532
var footer = this[STD_FOOTER + NODE_SUFFIX];
534
contentBox.insertBefore(sectionNode, footer);
536
contentBox.appendChild(sectionNode);
543
* Gets a new Node reference for the given standard module section, by cloning
544
* the stored template node.
546
* @method _getStdModTemplate
548
* @param {String} section The section to create a new node for. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
549
* @return {Node} The new Node instance for the section
551
_getStdModTemplate : function(section) {
552
return Node.create(StdMod.TEMPLATES[section], this._stdModNode.get(OWNER_DOCUMENT));
556
* Helper method to add content to a StdMod section node.
557
* The content is added either before, after or replaces the existing node content
558
* based on the value of the <code>where</code> argument.
560
* @method _addStdModContent
563
* @param {Node} node The section Node to be updated.
564
* @param {Node|NodeList|String} children The new content Node, NodeList or String to be added to section Node provided.
565
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
566
* If not provided, the content will replace existing content in the Node.
568
_addStdModContent : function(node, children, where) {
570
// StdMod where to Node where
572
case StdMod.BEFORE: // 0 is before fistChild
575
case StdMod.AFTER: // undefined is appendChild
578
default: // replace is replace, not specified is replace
579
where = StdMod.REPLACE;
582
node.insert(children, where);
586
* Helper method to obtain the precise height of the node provided, including padding and border.
587
* The height could be a sub-pixel value for certain browsers, such as Firefox 3.
589
* @method _getPreciseHeight
591
* @param {Node} node The node for which the precise height is required.
592
* @return {Number} The height of the Node including borders and padding, possibly a float.
594
_getPreciseHeight : function(node) {
595
var height = (node) ? node.get(OFFSET_HEIGHT) : 0,
596
getBCR = "getBoundingClientRect";
598
if (node && node.hasMethod(getBCR)) {
599
var preciseRegion = node.invoke(getBCR);
601
height = preciseRegion.bottom - preciseRegion.top;
609
* Helper method to to find the rendered node for the given section,
612
* @method _findStdModSection
614
* @param {String} section The section for which the render Node is to be found. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
615
* @return {Node} The rendered node for the given section, or null if not found.
617
_findStdModSection: function(section) {
618
return this.get(CONTENT_BOX).one("> ." + StdMod.SECTION_CLASS_NAMES[section]);
622
* Utility method, used by WidgetStdMods HTML_PARSER implementation
623
* to extract data for each section from markup.
625
* @method _parseStdModHTML
627
* @param {String} section
628
* @return {String} Inner HTML string with the contents of the section
630
_parseStdModHTML : function(section) {
632
var node = this._findStdModSection(section);
635
if (!this._stdModParsed) {
636
this._stdModParsed = {};
637
Y.before(this._applyStdModParsedConfig, this, APPLY_PARSED_CONFIG);
639
this._stdModParsed[section + CONTENT_SUFFIX] = 1;
641
return node.get("innerHTML");
648
* This method is injected before the _applyParsedConfig step in
649
* the application of HTML_PARSER, and sets up the state to
650
* identify whether or not we should remove the current DOM content
651
* or not, based on whether or not the current content attribute value
652
* was extracted from the DOM, or provided by the user configuration
654
* @method _applyStdModParsedConfig
657
_applyStdModParsedConfig : function(node, cfg, parsedCfg) {
658
var parsed = this._stdModParsed;
660
parsed[HEADER_CONTENT] = !(HEADER_CONTENT in cfg) && (HEADER_CONTENT in parsed);
661
parsed[BODY_CONTENT] = !(BODY_CONTENT in cfg) && (BODY_CONTENT in parsed);
662
parsed[FOOTER_CONTENT] = !(FOOTER_CONTENT in cfg) && (FOOTER_CONTENT in parsed);
667
* Retrieves the child nodes (content) of a standard module section
669
* @method _getStdModContent
671
* @param {String} section The standard module section whose child nodes are to be retrieved. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
672
* @return {Node} The child node collection of the standard module section.
674
_getStdModContent : function(section) {
675
return (this[section + NODE_SUFFIX]) ? this[section + NODE_SUFFIX].get(CHILD_NODES) : null;
679
* Updates the body section of the standard module with the content provided (either an HTML string, or node reference).
681
* This method can be used instead of the corresponding section content attribute if you'd like to retain the current content of the section,
682
* and insert content before or after it, by specifying the <code>where</code> argument.
684
* @method setStdModContent
685
* @param {String} section The standard module section whose content is to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
686
* @param {String | Node} content The content to be added, either an HTML string or a Node reference.
687
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
688
* If not provided, the content will replace existing content in the section.
690
setStdModContent : function(section, content, where) {
691
//var node = this.getStdModNode(section) || this._renderStdMod(section);
692
this.set(section + CONTENT_SUFFIX, content, {stdModPosition:where});
693
//this._addStdModContent(node, content, where);
697
* Returns the node reference for the given section. Note: The DOM is not queried for the node reference. The reference
698
* stored by the widget instance is returned if set.
700
* @method getStdModNode
701
* @param {String} section The section whose node reference is required. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
702
* @return {Node} The node reference for the section, or null if not set.
704
getStdModNode : function(section) {
705
return this[section + NODE_SUFFIX] || null;
709
* Sets the height on the provided header, body or footer element to
710
* fill out the height of the Widget. It determines the height of the
711
* widgets bounding box, based on it's configured height value, and
712
* sets the height of the provided section to fill out any
713
* space remaining after the other standard module section heights
714
* have been accounted for.
716
* <p><strong>NOTE:</strong> This method is not designed to work if an explicit
717
* height has not been set on the Widget, since for an "auto" height Widget,
718
* the heights of the header/body/footer will drive the height of the Widget.</p>
721
* @param {Node} node The node which should be resized to fill out the height
722
* of the Widget bounding box. Should be a standard module section node which belongs
725
fillHeight : function(node) {
727
var contentBox = this.get(CONTENT_BOX),
728
stdModNodes = [this.headerNode, this.bodyNode, this.footerNode],
736
for (var i = 0, l = stdModNodes.length; i < l; i++) {
737
stdModNode = stdModNodes[i];
739
if (stdModNode !== node) {
740
filled += this._getPreciseHeight(stdModNode);
748
if (UA.ie || UA.opera) {
749
// Need to set height to 0, to allow height to be reduced
750
node.set(OFFSET_HEIGHT, 0);
753
cbContentHeight = contentBox.get(OFFSET_HEIGHT) -
754
parseInt(contentBox.getComputedStyle("paddingTop"), 10) -
755
parseInt(contentBox.getComputedStyle("paddingBottom"), 10) -
756
parseInt(contentBox.getComputedStyle("borderBottomWidth"), 10) -
757
parseInt(contentBox.getComputedStyle("borderTopWidth"), 10);
759
if (L.isNumber(cbContentHeight)) {
760
remaining = cbContentHeight - filled;
761
if (remaining >= 0) {
762
node.set(OFFSET_HEIGHT, remaining);
770
Y.WidgetStdMod = StdMod;
773
}, '3.4.1' ,{requires:['base-build', 'widget']});