2
YUI 3.10.3 (build 2fb5187)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('widget-stdmod', function (Y, NAME) {
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
85
StdMod.HEADER = HEADER;
88
* Constant used to refer the the standard module body, in methods which expect a section specifier
97
* Constant used to refer the the standard module footer, in methods which expect a section specifier
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.
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.
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.
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.
159
* @attribute headerContent
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
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
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 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 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 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();
306
//This normally goes in bindUI but in order to allow setStdModContent() to work before renderUI
307
//stage, these listeners should be set up at the earliest possible time.
308
this.after(HeaderChange, this._afterHeaderChange);
309
this.after(BodyChange, this._afterBodyChange);
310
this.after(FooterChange, this._afterFooterChange);
313
_renderStdModSections : function() {
314
if (L.isValue(this.get(HEADER_CONTENT))) { this._renderStdMod(STD_HEADER); }
315
if (L.isValue(this.get(BODY_CONTENT))) { this._renderStdMod(STD_BODY); }
316
if (L.isValue(this.get(FOOTER_CONTENT))) { this._renderStdMod(STD_FOOTER); }
320
* Binds event listeners responsible for updating the UI state in response to
321
* Widget standard module related state changes.
323
* This method is invoked after bindUI is invoked for the Widget class
324
* using YUI's aop infrastructure.
326
* @method _bindUIStdMod
329
_bindUIStdMod : function() {
330
// this.after(HeaderChange, this._afterHeaderChange);
331
// this.after(BodyChange, this._afterBodyChange);
332
// this.after(FooterChange, this._afterFooterChange);
334
this.after(FillHeightChange, this._afterFillHeightChange);
335
this.after(HeightChange, this._fillHeight);
336
this.after(ContentUpdate, this._fillHeight);
340
* Default attribute change listener for the headerContent attribute, responsible
341
* for updating the UI, in response to attribute changes.
343
* @method _afterHeaderChange
345
* @param {EventFacade} e The event facade for the attribute change
347
_afterHeaderChange : function(e) {
349
this._uiSetStdMod(STD_HEADER, e.newVal, e.stdModPosition);
354
* Default attribute change listener for the bodyContent attribute, responsible
355
* for updating the UI, in response to attribute changes.
357
* @method _afterBodyChange
359
* @param {EventFacade} e The event facade for the attribute change
361
_afterBodyChange : function(e) {
363
this._uiSetStdMod(STD_BODY, e.newVal, e.stdModPosition);
368
* Default attribute change listener for the footerContent attribute, responsible
369
* for updating the UI, in response to attribute changes.
371
* @method _afterFooterChange
373
* @param {EventFacade} e The event facade for the attribute change
375
_afterFooterChange : function(e) {
377
this._uiSetStdMod(STD_FOOTER, e.newVal, e.stdModPosition);
382
* Default attribute change listener for the fillHeight attribute, responsible
383
* for updating the UI, in response to attribute changes.
385
* @method _afterFillHeightChange
387
* @param {EventFacade} e The event facade for the attribute change
389
_afterFillHeightChange: function (e) {
390
this._uiSetFillHeight(e.newVal);
394
* Default validator for the fillHeight attribute. Verifies that the
395
* value set is a valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER,
396
* or a falsey value if fillHeight is to be disabled.
398
* @method _validateFillHeight
400
* @param {String} val The section which should be setup to fill height, or false/null to disable fillHeight
401
* @return true if valid, false if not
403
_validateFillHeight : function(val) {
404
return !val || val == StdMod.BODY || val == StdMod.HEADER || val == StdMod.FOOTER;
408
* Updates the rendered UI, to resize the provided section so that the standard module fills out
409
* the specified widget height. Note: This method does not check whether or not a height is set
412
* @method _uiSetFillHeight
414
* @param {String} fillSection A valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER
416
_uiSetFillHeight : function(fillSection) {
417
var fillNode = this.getStdModNode(fillSection);
418
var currNode = this._currFillNode;
420
if (currNode && fillNode !== currNode){
421
currNode.setStyle(HEIGHT, EMPTY);
425
this._currFillNode = fillNode;
432
* Updates the rendered UI, to resize the current section specified by the fillHeight attribute, so
433
* that the standard module fills out the Widget height. If a height has not been set on Widget,
434
* the section is not resized (height is set to "auto").
436
* @method _fillHeight
439
_fillHeight : function() {
440
if (this.get(FILL_HEIGHT)) {
441
var height = this.get(HEIGHT);
442
if (height != EMPTY && height != AUTO) {
443
this.fillHeight(this._currFillNode);
449
* Updates the rendered UI, adding the provided content (either an HTML string, or node reference),
450
* to the specified section. The content is either added before, after or replaces existing content
451
* in the section, based on the value of the <code>where</code> argument.
453
* @method _uiSetStdMod
456
* @param {String} section The section to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
457
* @param {String | Node} content The new content (either as an HTML string, or Node reference) to add to the section
458
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
459
* If not provided, the content will replace existing content in the section.
461
_uiSetStdMod : function(section, content, where) {
462
// Using isValue, so that "" is valid content
463
if (L.isValue(content)) {
464
var node = this.getStdModNode(section, true);
466
this._addStdModContent(node, content, where);
468
this.set(section + CONTENT_SUFFIX, this._getStdModContent(section), {src:UI});
470
this._eraseStdMod(section);
472
this.fire(ContentUpdate);
476
* Creates the DOM node for the given section, and inserts it into the correct location in the contentBox.
478
* @method _renderStdMod
480
* @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
481
* @return {Node} A reference to the added section node
483
_renderStdMod : function(section) {
485
var contentBox = this.get(CONTENT_BOX),
486
sectionNode = this._findStdModSection(section);
489
sectionNode = this._getStdModTemplate(section);
492
this._insertStdModSection(contentBox, section, sectionNode);
494
this[section + NODE_SUFFIX] = sectionNode;
495
return this[section + NODE_SUFFIX];
499
* Removes the DOM node for the given section.
501
* @method _eraseStdMod
503
* @param {String} section The section to remove. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
505
_eraseStdMod : function(section) {
506
var sectionNode = this.getStdModNode(section);
508
sectionNode.remove(true);
509
delete this[section + NODE_SUFFIX];
514
* Helper method to insert the Node for the given section into the correct location in the contentBox.
516
* @method _insertStdModSection
518
* @param {Node} contentBox A reference to the Widgets content box.
519
* @param {String} section The section to create/render. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
520
* @param {Node} sectionNode The Node for the section.
522
_insertStdModSection : function(contentBox, section, sectionNode) {
523
var fc = contentBox.get(FIRST_CHILD);
525
if (section === STD_FOOTER || !fc) {
526
contentBox.appendChild(sectionNode);
528
if (section === STD_HEADER) {
529
contentBox.insertBefore(sectionNode, fc);
531
var footer = this[STD_FOOTER + NODE_SUFFIX];
533
contentBox.insertBefore(sectionNode, footer);
535
contentBox.appendChild(sectionNode);
542
* Gets a new Node reference for the given standard module section, by cloning
543
* the stored template node.
545
* @method _getStdModTemplate
547
* @param {String} section The section to create a new node for. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
548
* @return {Node} The new Node instance for the section
550
_getStdModTemplate : function(section) {
551
return Node.create(StdMod.TEMPLATES[section], this._stdModNode.get(OWNER_DOCUMENT));
555
* Helper method to add content to a StdMod section node.
556
* The content is added either before, after or replaces the existing node content
557
* based on the value of the <code>where</code> argument.
559
* @method _addStdModContent
562
* @param {Node} node The section Node to be updated.
563
* @param {Node|NodeList|String} children The new content Node, NodeList or String to be added to section Node provided.
564
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
565
* If not provided, the content will replace existing content in the Node.
567
_addStdModContent : function(node, children, where) {
569
// StdMod where to Node where
571
case StdMod.BEFORE: // 0 is before fistChild
574
case StdMod.AFTER: // undefined is appendChild
577
default: // replace is replace, not specified is replace
578
where = StdMod.REPLACE;
581
node.insert(children, where);
585
* Helper method to obtain the precise height of the node provided, including padding and border.
586
* The height could be a sub-pixel value for certain browsers, such as Firefox 3.
588
* @method _getPreciseHeight
590
* @param {Node} node The node for which the precise height is required.
591
* @return {Number} The height of the Node including borders and padding, possibly a float.
593
_getPreciseHeight : function(node) {
594
var height = (node) ? node.get(OFFSET_HEIGHT) : 0,
595
getBCR = "getBoundingClientRect";
597
if (node && node.hasMethod(getBCR)) {
598
var preciseRegion = node.invoke(getBCR);
600
height = preciseRegion.bottom - preciseRegion.top;
608
* Helper method to to find the rendered node for the given section,
611
* @method _findStdModSection
613
* @param {String} section The section for which the render Node is to be found. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
614
* @return {Node} The rendered node for the given section, or null if not found.
616
_findStdModSection: function(section) {
617
return this.get(CONTENT_BOX).one("> ." + StdMod.SECTION_CLASS_NAMES[section]);
621
* Utility method, used by WidgetStdMods HTML_PARSER implementation
622
* to extract data for each section from markup.
624
* @method _parseStdModHTML
626
* @param {String} section
627
* @return {String} Inner HTML string with the contents of the section
629
_parseStdModHTML : function(section) {
631
var node = this._findStdModSection(section);
634
if (!this._stdModParsed) {
635
this._stdModParsed = {};
636
Y.before(this._applyStdModParsedConfig, this, APPLY_PARSED_CONFIG);
638
this._stdModParsed[section + CONTENT_SUFFIX] = 1;
640
return node.get("innerHTML");
647
* This method is injected before the _applyParsedConfig step in
648
* the application of HTML_PARSER, and sets up the state to
649
* identify whether or not we should remove the current DOM content
650
* or not, based on whether or not the current content attribute value
651
* was extracted from the DOM, or provided by the user configuration
653
* @method _applyStdModParsedConfig
656
_applyStdModParsedConfig : function(node, cfg, parsedCfg) {
657
var parsed = this._stdModParsed;
659
parsed[HEADER_CONTENT] = !(HEADER_CONTENT in cfg) && (HEADER_CONTENT in parsed);
660
parsed[BODY_CONTENT] = !(BODY_CONTENT in cfg) && (BODY_CONTENT in parsed);
661
parsed[FOOTER_CONTENT] = !(FOOTER_CONTENT in cfg) && (FOOTER_CONTENT in parsed);
666
* Retrieves the child nodes (content) of a standard module section
668
* @method _getStdModContent
670
* @param {String} section The standard module section whose child nodes are to be retrieved. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
671
* @return {Node} The child node collection of the standard module section.
673
_getStdModContent : function(section) {
674
return (this[section + NODE_SUFFIX]) ? this[section + NODE_SUFFIX].get(CHILD_NODES) : null;
678
* Updates the body section of the standard module with the content provided (either an HTML string, or node reference).
680
* This method can be used instead of the corresponding section content attribute if you'd like to retain the current content of the section,
681
* and insert content before or after it, by specifying the <code>where</code> argument.
683
* @method setStdModContent
684
* @param {String} section The standard module section whose content is to be updated. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER.
685
* @param {String | Node} content The content to be added, either an HTML string or a Node reference.
686
* @param {String} where Optional. Either WidgetStdMod.AFTER, WidgetStdMod.BEFORE or WidgetStdMod.REPLACE.
687
* If not provided, the content will replace existing content in the section.
689
setStdModContent : function(section, content, where) {
690
//var node = this.getStdModNode(section) || this._renderStdMod(section);
691
this.set(section + CONTENT_SUFFIX, content, {stdModPosition:where});
692
//this._addStdModContent(node, content, where);
696
Returns the node reference for the specified `section`.
698
**Note:** The DOM is not queried for the node reference. The reference
699
stored by the widget instance is returned if it was set. Passing a
700
truthy for `forceCreate` will create the section node if it does not
703
@method getStdModNode
704
@param {String} section The section whose node reference is required.
705
Either `WidgetStdMod.HEADER`, `WidgetStdMod.BODY`, or
706
`WidgetStdMod.FOOTER`.
707
@param {Boolean} forceCreate Whether the section node should be created
708
if it does not already exist.
709
@return {Node} The node reference for the `section`, or null if not set.
711
getStdModNode : function(section, forceCreate) {
712
var node = this[section + NODE_SUFFIX] || null;
714
if (!node && forceCreate) {
715
node = this._renderStdMod(section);
722
* Sets the height on the provided header, body or footer element to
723
* fill out the height of the Widget. It determines the height of the
724
* widgets bounding box, based on it's configured height value, and
725
* sets the height of the provided section to fill out any
726
* space remaining after the other standard module section heights
727
* have been accounted for.
729
* <p><strong>NOTE:</strong> This method is not designed to work if an explicit
730
* height has not been set on the Widget, since for an "auto" height Widget,
731
* the heights of the header/body/footer will drive the height of the Widget.</p>
734
* @param {Node} node The node which should be resized to fill out the height
735
* of the Widget bounding box. Should be a standard module section node which belongs
738
fillHeight : function(node) {
740
var contentBox = this.get(CONTENT_BOX),
741
stdModNodes = [this.headerNode, this.bodyNode, this.footerNode],
749
for (var i = 0, l = stdModNodes.length; i < l; i++) {
750
stdModNode = stdModNodes[i];
752
if (stdModNode !== node) {
753
filled += this._getPreciseHeight(stdModNode);
761
if (UA.ie || UA.opera) {
762
// Need to set height to 0, to allow height to be reduced
763
node.set(OFFSET_HEIGHT, 0);
766
cbContentHeight = contentBox.get(OFFSET_HEIGHT) -
767
parseInt(contentBox.getComputedStyle("paddingTop"), 10) -
768
parseInt(contentBox.getComputedStyle("paddingBottom"), 10) -
769
parseInt(contentBox.getComputedStyle("borderBottomWidth"), 10) -
770
parseInt(contentBox.getComputedStyle("borderTopWidth"), 10);
772
if (L.isNumber(cbContentHeight)) {
773
remaining = cbContentHeight - filled;
774
if (remaining >= 0) {
775
node.set(OFFSET_HEIGHT, remaining);
783
Y.WidgetStdMod = StdMod;
786
}, '3.10.3', {"requires": ["base-build", "widget"]});