|
1 /* |
|
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/ |
|
6 */ |
|
7 |
|
8 YUI.add('widget-stdmod', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Provides standard module support for Widgets through an extension. |
|
12 * |
|
13 * @module widget-stdmod |
|
14 */ |
|
15 var L = Y.Lang, |
|
16 Node = Y.Node, |
|
17 UA = Y.UA, |
|
18 Widget = Y.Widget, |
|
19 |
|
20 EMPTY = "", |
|
21 HD = "hd", |
|
22 BD = "bd", |
|
23 FT = "ft", |
|
24 HEADER = "header", |
|
25 BODY = "body", |
|
26 FOOTER = "footer", |
|
27 FILL_HEIGHT = "fillHeight", |
|
28 STDMOD = "stdmod", |
|
29 |
|
30 NODE_SUFFIX = "Node", |
|
31 CONTENT_SUFFIX = "Content", |
|
32 |
|
33 FIRST_CHILD = "firstChild", |
|
34 CHILD_NODES = "childNodes", |
|
35 OWNER_DOCUMENT = "ownerDocument", |
|
36 |
|
37 CONTENT_BOX = "contentBox", |
|
38 |
|
39 HEIGHT = "height", |
|
40 OFFSET_HEIGHT = "offsetHeight", |
|
41 AUTO = "auto", |
|
42 |
|
43 HeaderChange = "headerContentChange", |
|
44 BodyChange = "bodyContentChange", |
|
45 FooterChange = "footerContentChange", |
|
46 FillHeightChange = "fillHeightChange", |
|
47 HeightChange = "heightChange", |
|
48 ContentUpdate = "contentUpdate", |
|
49 |
|
50 RENDERUI = "renderUI", |
|
51 BINDUI = "bindUI", |
|
52 SYNCUI = "syncUI", |
|
53 |
|
54 APPLY_PARSED_CONFIG = "_applyParsedConfig", |
|
55 |
|
56 UI = Y.Widget.UI_SRC; |
|
57 |
|
58 /** |
|
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> |
|
61 * method. |
|
62 * <p> |
|
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. |
|
65 * </p> |
|
66 * @class WidgetStdMod |
|
67 * @param {Object} The user configuration object |
|
68 */ |
|
69 function StdMod(config) { |
|
70 |
|
71 this._stdModNode = this.get(CONTENT_BOX); |
|
72 |
|
73 Y.before(this._renderUIStdMod, this, RENDERUI); |
|
74 Y.before(this._bindUIStdMod, this, BINDUI); |
|
75 Y.before(this._syncUIStdMod, this, SYNCUI); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Constant used to refer the the standard module header, in methods which expect a section specifier |
|
80 * |
|
81 * @property HEADER |
|
82 * @static |
|
83 * @type String |
|
84 */ |
|
85 StdMod.HEADER = HEADER; |
|
86 |
|
87 /** |
|
88 * Constant used to refer the the standard module body, in methods which expect a section specifier |
|
89 * |
|
90 * @property BODY |
|
91 * @static |
|
92 * @type String |
|
93 */ |
|
94 StdMod.BODY = BODY; |
|
95 |
|
96 /** |
|
97 * Constant used to refer the the standard module footer, in methods which expect a section specifier |
|
98 * |
|
99 * @property FOOTER |
|
100 * @static |
|
101 * @type String |
|
102 */ |
|
103 StdMod.FOOTER = FOOTER; |
|
104 |
|
105 /** |
|
106 * Constant used to specify insertion position, when adding content to sections of the standard module in |
|
107 * methods which expect a "where" argument. |
|
108 * <p> |
|
109 * Inserts new content <em>before</em> the sections existing content. |
|
110 * </p> |
|
111 * @property AFTER |
|
112 * @static |
|
113 * @type String |
|
114 */ |
|
115 StdMod.AFTER = "after"; |
|
116 |
|
117 /** |
|
118 * Constant used to specify insertion position, when adding content to sections of the standard module in |
|
119 * methods which expect a "where" argument. |
|
120 * <p> |
|
121 * Inserts new content <em>before</em> the sections existing content. |
|
122 * </p> |
|
123 * @property BEFORE |
|
124 * @static |
|
125 * @type String |
|
126 */ |
|
127 StdMod.BEFORE = "before"; |
|
128 /** |
|
129 * Constant used to specify insertion position, when adding content to sections of the standard module in |
|
130 * methods which expect a "where" argument. |
|
131 * <p> |
|
132 * <em>Replaces</em> the sections existing content, with new content. |
|
133 * </p> |
|
134 * @property REPLACE |
|
135 * @static |
|
136 * @type String |
|
137 */ |
|
138 StdMod.REPLACE = "replace"; |
|
139 |
|
140 var STD_HEADER = StdMod.HEADER, |
|
141 STD_BODY = StdMod.BODY, |
|
142 STD_FOOTER = StdMod.FOOTER, |
|
143 |
|
144 HEADER_CONTENT = STD_HEADER + CONTENT_SUFFIX, |
|
145 FOOTER_CONTENT = STD_FOOTER + CONTENT_SUFFIX, |
|
146 BODY_CONTENT = STD_BODY + CONTENT_SUFFIX; |
|
147 |
|
148 /** |
|
149 * Static property used to define the default attribute |
|
150 * configuration introduced by WidgetStdMod. |
|
151 * |
|
152 * @property ATTRS |
|
153 * @type Object |
|
154 * @static |
|
155 */ |
|
156 StdMod.ATTRS = { |
|
157 |
|
158 /** |
|
159 * @attribute headerContent |
|
160 * @type HTML |
|
161 * @default undefined |
|
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. |
|
164 */ |
|
165 headerContent: { |
|
166 value:null |
|
167 }, |
|
168 |
|
169 /** |
|
170 * @attribute footerContent |
|
171 * @type HTML |
|
172 * @default undefined |
|
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. |
|
175 */ |
|
176 footerContent: { |
|
177 value:null |
|
178 }, |
|
179 |
|
180 /** |
|
181 * @attribute bodyContent |
|
182 * @type HTML |
|
183 * @default undefined |
|
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. |
|
186 */ |
|
187 bodyContent: { |
|
188 value:null |
|
189 }, |
|
190 |
|
191 /** |
|
192 * @attribute fillHeight |
|
193 * @type {String} |
|
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 |
|
197 * their content. |
|
198 */ |
|
199 fillHeight: { |
|
200 value: StdMod.BODY, |
|
201 validator: function(val) { |
|
202 return this._validateFillHeight(val); |
|
203 } |
|
204 } |
|
205 }; |
|
206 |
|
207 /** |
|
208 * The HTML parsing rules for the WidgetStdMod class. |
|
209 * |
|
210 * @property HTML_PARSER |
|
211 * @static |
|
212 * @type Object |
|
213 */ |
|
214 StdMod.HTML_PARSER = { |
|
215 headerContent: function(contentBox) { |
|
216 return this._parseStdModHTML(STD_HEADER); |
|
217 }, |
|
218 |
|
219 bodyContent: function(contentBox) { |
|
220 return this._parseStdModHTML(STD_BODY); |
|
221 }, |
|
222 |
|
223 footerContent : function(contentBox) { |
|
224 return this._parseStdModHTML(STD_FOOTER); |
|
225 } |
|
226 }; |
|
227 |
|
228 /** |
|
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) |
|
232 * |
|
233 * @property SECTION_CLASS_NAMES |
|
234 * @static |
|
235 * @type Object |
|
236 */ |
|
237 StdMod.SECTION_CLASS_NAMES = { |
|
238 header: Widget.getClassName(HD), |
|
239 body: Widget.getClassName(BD), |
|
240 footer: Widget.getClassName(FT) |
|
241 }; |
|
242 |
|
243 /** |
|
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. |
|
246 * e.g. |
|
247 * <pre> |
|
248 * { |
|
249 * header : '<div class="yui-widget-hd"></div>', |
|
250 * body : '<div class="yui-widget-bd"></div>', |
|
251 * footer : '<div class="yui-widget-ft"></div>' |
|
252 * } |
|
253 * </pre> |
|
254 * @property TEMPLATES |
|
255 * @type Object |
|
256 * @static |
|
257 */ |
|
258 StdMod.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>' |
|
262 }; |
|
263 |
|
264 StdMod.prototype = { |
|
265 |
|
266 /** |
|
267 * Synchronizes the UI to match the Widgets standard module state. |
|
268 * <p> |
|
269 * This method is invoked after syncUI is invoked for the Widget class |
|
270 * using YUI's aop infrastructure. |
|
271 * </p> |
|
272 * @method _syncUIStdMod |
|
273 * @protected |
|
274 */ |
|
275 _syncUIStdMod : function() { |
|
276 var stdModParsed = this._stdModParsed; |
|
277 |
|
278 if (!stdModParsed || !stdModParsed[HEADER_CONTENT]) { |
|
279 this._uiSetStdMod(STD_HEADER, this.get(HEADER_CONTENT)); |
|
280 } |
|
281 |
|
282 if (!stdModParsed || !stdModParsed[BODY_CONTENT]) { |
|
283 this._uiSetStdMod(STD_BODY, this.get(BODY_CONTENT)); |
|
284 } |
|
285 |
|
286 if (!stdModParsed || !stdModParsed[FOOTER_CONTENT]) { |
|
287 this._uiSetStdMod(STD_FOOTER, this.get(FOOTER_CONTENT)); |
|
288 } |
|
289 |
|
290 this._uiSetFillHeight(this.get(FILL_HEIGHT)); |
|
291 }, |
|
292 |
|
293 /** |
|
294 * Creates/Initializes the DOM for standard module support. |
|
295 * <p> |
|
296 * This method is invoked after renderUI is invoked for the Widget class |
|
297 * using YUI's aop infrastructure. |
|
298 * </p> |
|
299 * @method _renderUIStdMod |
|
300 * @protected |
|
301 */ |
|
302 _renderUIStdMod : function() { |
|
303 this._stdModNode.addClass(Widget.getClassName(STDMOD)); |
|
304 this._renderStdModSections(); |
|
305 |
|
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); |
|
311 }, |
|
312 |
|
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); } |
|
317 }, |
|
318 |
|
319 /** |
|
320 * Binds event listeners responsible for updating the UI state in response to |
|
321 * Widget standard module related state changes. |
|
322 * <p> |
|
323 * This method is invoked after bindUI is invoked for the Widget class |
|
324 * using YUI's aop infrastructure. |
|
325 * </p> |
|
326 * @method _bindUIStdMod |
|
327 * @protected |
|
328 */ |
|
329 _bindUIStdMod : function() { |
|
330 // this.after(HeaderChange, this._afterHeaderChange); |
|
331 // this.after(BodyChange, this._afterBodyChange); |
|
332 // this.after(FooterChange, this._afterFooterChange); |
|
333 |
|
334 this.after(FillHeightChange, this._afterFillHeightChange); |
|
335 this.after(HeightChange, this._fillHeight); |
|
336 this.after(ContentUpdate, this._fillHeight); |
|
337 }, |
|
338 |
|
339 /** |
|
340 * Default attribute change listener for the headerContent attribute, responsible |
|
341 * for updating the UI, in response to attribute changes. |
|
342 * |
|
343 * @method _afterHeaderChange |
|
344 * @protected |
|
345 * @param {EventFacade} e The event facade for the attribute change |
|
346 */ |
|
347 _afterHeaderChange : function(e) { |
|
348 if (e.src !== UI) { |
|
349 this._uiSetStdMod(STD_HEADER, e.newVal, e.stdModPosition); |
|
350 } |
|
351 }, |
|
352 |
|
353 /** |
|
354 * Default attribute change listener for the bodyContent attribute, responsible |
|
355 * for updating the UI, in response to attribute changes. |
|
356 * |
|
357 * @method _afterBodyChange |
|
358 * @protected |
|
359 * @param {EventFacade} e The event facade for the attribute change |
|
360 */ |
|
361 _afterBodyChange : function(e) { |
|
362 if (e.src !== UI) { |
|
363 this._uiSetStdMod(STD_BODY, e.newVal, e.stdModPosition); |
|
364 } |
|
365 }, |
|
366 |
|
367 /** |
|
368 * Default attribute change listener for the footerContent attribute, responsible |
|
369 * for updating the UI, in response to attribute changes. |
|
370 * |
|
371 * @method _afterFooterChange |
|
372 * @protected |
|
373 * @param {EventFacade} e The event facade for the attribute change |
|
374 */ |
|
375 _afterFooterChange : function(e) { |
|
376 if (e.src !== UI) { |
|
377 this._uiSetStdMod(STD_FOOTER, e.newVal, e.stdModPosition); |
|
378 } |
|
379 }, |
|
380 |
|
381 /** |
|
382 * Default attribute change listener for the fillHeight attribute, responsible |
|
383 * for updating the UI, in response to attribute changes. |
|
384 * |
|
385 * @method _afterFillHeightChange |
|
386 * @protected |
|
387 * @param {EventFacade} e The event facade for the attribute change |
|
388 */ |
|
389 _afterFillHeightChange: function (e) { |
|
390 this._uiSetFillHeight(e.newVal); |
|
391 }, |
|
392 |
|
393 /** |
|
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. |
|
397 * |
|
398 * @method _validateFillHeight |
|
399 * @protected |
|
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 |
|
402 */ |
|
403 _validateFillHeight : function(val) { |
|
404 return !val || val == StdMod.BODY || val == StdMod.HEADER || val == StdMod.FOOTER; |
|
405 }, |
|
406 |
|
407 /** |
|
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 |
|
410 * on the Widget. |
|
411 * |
|
412 * @method _uiSetFillHeight |
|
413 * @protected |
|
414 * @param {String} fillSection A valid section specifier - one of WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER |
|
415 */ |
|
416 _uiSetFillHeight : function(fillSection) { |
|
417 var fillNode = this.getStdModNode(fillSection); |
|
418 var currNode = this._currFillNode; |
|
419 |
|
420 if (currNode && fillNode !== currNode){ |
|
421 currNode.setStyle(HEIGHT, EMPTY); |
|
422 } |
|
423 |
|
424 if (fillNode) { |
|
425 this._currFillNode = fillNode; |
|
426 } |
|
427 |
|
428 this._fillHeight(); |
|
429 }, |
|
430 |
|
431 /** |
|
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"). |
|
435 * |
|
436 * @method _fillHeight |
|
437 * @private |
|
438 */ |
|
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); |
|
444 } |
|
445 } |
|
446 }, |
|
447 |
|
448 /** |
|
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. |
|
452 * |
|
453 * @method _uiSetStdMod |
|
454 * @protected |
|
455 * |
|
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. |
|
460 */ |
|
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); |
|
465 |
|
466 this._addStdModContent(node, content, where); |
|
467 |
|
468 this.set(section + CONTENT_SUFFIX, this._getStdModContent(section), {src:UI}); |
|
469 } else { |
|
470 this._eraseStdMod(section); |
|
471 } |
|
472 this.fire(ContentUpdate); |
|
473 }, |
|
474 |
|
475 /** |
|
476 * Creates the DOM node for the given section, and inserts it into the correct location in the contentBox. |
|
477 * |
|
478 * @method _renderStdMod |
|
479 * @protected |
|
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 |
|
482 */ |
|
483 _renderStdMod : function(section) { |
|
484 |
|
485 var contentBox = this.get(CONTENT_BOX), |
|
486 sectionNode = this._findStdModSection(section); |
|
487 |
|
488 if (!sectionNode) { |
|
489 sectionNode = this._getStdModTemplate(section); |
|
490 } |
|
491 |
|
492 this._insertStdModSection(contentBox, section, sectionNode); |
|
493 |
|
494 this[section + NODE_SUFFIX] = sectionNode; |
|
495 return this[section + NODE_SUFFIX]; |
|
496 }, |
|
497 |
|
498 /** |
|
499 * Removes the DOM node for the given section. |
|
500 * |
|
501 * @method _eraseStdMod |
|
502 * @protected |
|
503 * @param {String} section The section to remove. Either WidgetStdMod.HEADER, WidgetStdMod.BODY or WidgetStdMod.FOOTER. |
|
504 */ |
|
505 _eraseStdMod : function(section) { |
|
506 var sectionNode = this.getStdModNode(section); |
|
507 if (sectionNode) { |
|
508 sectionNode.remove(true); |
|
509 delete this[section + NODE_SUFFIX]; |
|
510 } |
|
511 }, |
|
512 |
|
513 /** |
|
514 * Helper method to insert the Node for the given section into the correct location in the contentBox. |
|
515 * |
|
516 * @method _insertStdModSection |
|
517 * @private |
|
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. |
|
521 */ |
|
522 _insertStdModSection : function(contentBox, section, sectionNode) { |
|
523 var fc = contentBox.get(FIRST_CHILD); |
|
524 |
|
525 if (section === STD_FOOTER || !fc) { |
|
526 contentBox.appendChild(sectionNode); |
|
527 } else { |
|
528 if (section === STD_HEADER) { |
|
529 contentBox.insertBefore(sectionNode, fc); |
|
530 } else { |
|
531 var footer = this[STD_FOOTER + NODE_SUFFIX]; |
|
532 if (footer) { |
|
533 contentBox.insertBefore(sectionNode, footer); |
|
534 } else { |
|
535 contentBox.appendChild(sectionNode); |
|
536 } |
|
537 } |
|
538 } |
|
539 }, |
|
540 |
|
541 /** |
|
542 * Gets a new Node reference for the given standard module section, by cloning |
|
543 * the stored template node. |
|
544 * |
|
545 * @method _getStdModTemplate |
|
546 * @protected |
|
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 |
|
549 */ |
|
550 _getStdModTemplate : function(section) { |
|
551 return Node.create(StdMod.TEMPLATES[section], this._stdModNode.get(OWNER_DOCUMENT)); |
|
552 }, |
|
553 |
|
554 /** |
|
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. |
|
558 * |
|
559 * @method _addStdModContent |
|
560 * @private |
|
561 * |
|
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. |
|
566 */ |
|
567 _addStdModContent : function(node, children, where) { |
|
568 |
|
569 // StdMod where to Node where |
|
570 switch (where) { |
|
571 case StdMod.BEFORE: // 0 is before fistChild |
|
572 where = 0; |
|
573 break; |
|
574 case StdMod.AFTER: // undefined is appendChild |
|
575 where = undefined; |
|
576 break; |
|
577 default: // replace is replace, not specified is replace |
|
578 where = StdMod.REPLACE; |
|
579 } |
|
580 |
|
581 node.insert(children, where); |
|
582 }, |
|
583 |
|
584 /** |
|
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. |
|
587 * |
|
588 * @method _getPreciseHeight |
|
589 * @private |
|
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. |
|
592 */ |
|
593 _getPreciseHeight : function(node) { |
|
594 var height = (node) ? node.get(OFFSET_HEIGHT) : 0, |
|
595 getBCR = "getBoundingClientRect"; |
|
596 |
|
597 if (node && node.hasMethod(getBCR)) { |
|
598 var preciseRegion = node.invoke(getBCR); |
|
599 if (preciseRegion) { |
|
600 height = preciseRegion.bottom - preciseRegion.top; |
|
601 } |
|
602 } |
|
603 |
|
604 return height; |
|
605 }, |
|
606 |
|
607 /** |
|
608 * Helper method to to find the rendered node for the given section, |
|
609 * if it exists. |
|
610 * |
|
611 * @method _findStdModSection |
|
612 * @private |
|
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. |
|
615 */ |
|
616 _findStdModSection: function(section) { |
|
617 return this.get(CONTENT_BOX).one("> ." + StdMod.SECTION_CLASS_NAMES[section]); |
|
618 }, |
|
619 |
|
620 /** |
|
621 * Utility method, used by WidgetStdMods HTML_PARSER implementation |
|
622 * to extract data for each section from markup. |
|
623 * |
|
624 * @method _parseStdModHTML |
|
625 * @private |
|
626 * @param {String} section |
|
627 * @return {String} Inner HTML string with the contents of the section |
|
628 */ |
|
629 _parseStdModHTML : function(section) { |
|
630 |
|
631 var node = this._findStdModSection(section); |
|
632 |
|
633 if (node) { |
|
634 if (!this._stdModParsed) { |
|
635 this._stdModParsed = {}; |
|
636 Y.before(this._applyStdModParsedConfig, this, APPLY_PARSED_CONFIG); |
|
637 } |
|
638 this._stdModParsed[section + CONTENT_SUFFIX] = 1; |
|
639 |
|
640 return node.get("innerHTML"); |
|
641 } |
|
642 |
|
643 return null; |
|
644 }, |
|
645 |
|
646 /** |
|
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 |
|
652 * |
|
653 * @method _applyStdModParsedConfig |
|
654 * @private |
|
655 */ |
|
656 _applyStdModParsedConfig : function(node, cfg, parsedCfg) { |
|
657 var parsed = this._stdModParsed; |
|
658 if (parsed) { |
|
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); |
|
662 } |
|
663 }, |
|
664 |
|
665 /** |
|
666 * Retrieves the child nodes (content) of a standard module section |
|
667 * |
|
668 * @method _getStdModContent |
|
669 * @private |
|
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. |
|
672 */ |
|
673 _getStdModContent : function(section) { |
|
674 return (this[section + NODE_SUFFIX]) ? this[section + NODE_SUFFIX].get(CHILD_NODES) : null; |
|
675 }, |
|
676 |
|
677 /** |
|
678 * Updates the body section of the standard module with the content provided (either an HTML string, or node reference). |
|
679 * <p> |
|
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. |
|
682 * </p> |
|
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. |
|
688 */ |
|
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); |
|
693 }, |
|
694 |
|
695 /** |
|
696 Returns the node reference for the specified `section`. |
|
697 |
|
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 |
|
701 already exist. |
|
702 |
|
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. |
|
710 **/ |
|
711 getStdModNode : function(section, forceCreate) { |
|
712 var node = this[section + NODE_SUFFIX] || null; |
|
713 |
|
714 if (!node && forceCreate) { |
|
715 node = this._renderStdMod(section); |
|
716 } |
|
717 |
|
718 return node; |
|
719 }, |
|
720 |
|
721 /** |
|
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. |
|
728 * |
|
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> |
|
732 * |
|
733 * @method fillHeight |
|
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 |
|
736 * to the widget. |
|
737 */ |
|
738 fillHeight : function(node) { |
|
739 if (node) { |
|
740 var contentBox = this.get(CONTENT_BOX), |
|
741 stdModNodes = [this.headerNode, this.bodyNode, this.footerNode], |
|
742 stdModNode, |
|
743 cbContentHeight, |
|
744 filled = 0, |
|
745 remaining = 0, |
|
746 |
|
747 validNode = false; |
|
748 |
|
749 for (var i = 0, l = stdModNodes.length; i < l; i++) { |
|
750 stdModNode = stdModNodes[i]; |
|
751 if (stdModNode) { |
|
752 if (stdModNode !== node) { |
|
753 filled += this._getPreciseHeight(stdModNode); |
|
754 } else { |
|
755 validNode = true; |
|
756 } |
|
757 } |
|
758 } |
|
759 |
|
760 if (validNode) { |
|
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); |
|
764 } |
|
765 |
|
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); |
|
771 |
|
772 if (L.isNumber(cbContentHeight)) { |
|
773 remaining = cbContentHeight - filled; |
|
774 if (remaining >= 0) { |
|
775 node.set(OFFSET_HEIGHT, remaining); |
|
776 } |
|
777 } |
|
778 } |
|
779 } |
|
780 } |
|
781 }; |
|
782 |
|
783 Y.WidgetStdMod = StdMod; |
|
784 |
|
785 |
|
786 }, '3.10.3', {"requires": ["base-build", "widget"]}); |