~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to src/maasserver/static/js/yui/3.4.1/dom-base/dom-base.js

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-15 18:14:08 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120315181408-zgl94hzo0x4n99an
Tags: 0.1+bzr295+dfsg-0ubuntu2
* debian/patches:
  - 01-fix-database-settings.patch: Update to set PSERV_URL.
  - 02-pserv-config.patch: Set port to 8001.
* debian/maas.postinst: Run maas-import-isos on install.
* debian/control: Depends on rabbitmq-server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
YUI 3.4.1 (build 4118)
3
 
Copyright 2011 Yahoo! Inc. All rights reserved.
4
 
Licensed under the BSD License.
5
 
http://yuilibrary.com/license/
6
 
*/
7
 
YUI.add('dom-base', function(Y) {
8
 
 
9
 
var documentElement = Y.config.doc.documentElement,
10
 
    Y_DOM = Y.DOM,
11
 
    TAG_NAME = 'tagName',
12
 
    OWNER_DOCUMENT = 'ownerDocument',
13
 
    EMPTY_STRING = '',
14
 
    addFeature = Y.Features.add,
15
 
    testFeature = Y.Features.test;
16
 
 
17
 
Y.mix(Y_DOM, {
18
 
    /**
19
 
     * Returns the text content of the HTMLElement. 
20
 
     * @method getText         
21
 
     * @param {HTMLElement} element The html element. 
22
 
     * @return {String} The text content of the element (includes text of any descending elements).
23
 
     */
24
 
    getText: (documentElement.textContent !== undefined) ?
25
 
        function(element) {
26
 
            var ret = '';
27
 
            if (element) {
28
 
                ret = element.textContent;
29
 
            }
30
 
            return ret || '';
31
 
        } : function(element) {
32
 
            var ret = '';
33
 
            if (element) {
34
 
                ret = element.innerText || element.nodeValue; // might be a textNode
35
 
            }
36
 
            return ret || '';
37
 
        },
38
 
 
39
 
    /**
40
 
     * Sets the text content of the HTMLElement. 
41
 
     * @method setText         
42
 
     * @param {HTMLElement} element The html element. 
43
 
     * @param {String} content The content to add. 
44
 
     */
45
 
    setText: (documentElement.textContent !== undefined) ?
46
 
        function(element, content) {
47
 
            if (element) {
48
 
                element.textContent = content;
49
 
            }
50
 
        } : function(element, content) {
51
 
            if ('innerText' in element) {
52
 
                element.innerText = content;
53
 
            } else if ('nodeValue' in element) {
54
 
                element.nodeValue = content;
55
 
            }
56
 
    },
57
 
 
58
 
    CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
59
 
        'for': 'htmlFor',
60
 
        'class': 'className'
61
 
    } : { // w3c
62
 
        'htmlFor': 'for',
63
 
        'className': 'class'
64
 
    },
65
 
 
66
 
    /**
67
 
     * Provides a normalized attribute interface. 
68
 
     * @method setAttribute
69
 
     * @param {HTMLElement} el The target element for the attribute.
70
 
     * @param {String} attr The attribute to set.
71
 
     * @param {String} val The value of the attribute.
72
 
     */
73
 
    setAttribute: function(el, attr, val, ieAttr) {
74
 
        if (el && attr && el.setAttribute) {
75
 
            attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
76
 
            el.setAttribute(attr, val, ieAttr);
77
 
        }
78
 
    },
79
 
 
80
 
 
81
 
    /**
82
 
     * Provides a normalized attribute interface. 
83
 
     * @method getAttibute
84
 
     * @param {HTMLElement} el The target element for the attribute.
85
 
     * @param {String} attr The attribute to get.
86
 
     * @return {String} The current value of the attribute. 
87
 
     */
88
 
    getAttribute: function(el, attr, ieAttr) {
89
 
        ieAttr = (ieAttr !== undefined) ? ieAttr : 2;
90
 
        var ret = '';
91
 
        if (el && attr && el.getAttribute) {
92
 
            attr = Y_DOM.CUSTOM_ATTRIBUTES[attr] || attr;
93
 
            ret = el.getAttribute(attr, ieAttr);
94
 
 
95
 
            if (ret === null) {
96
 
                ret = ''; // per DOM spec
97
 
            }
98
 
        }
99
 
        return ret;
100
 
    },
101
 
 
102
 
    VALUE_SETTERS: {},
103
 
 
104
 
    VALUE_GETTERS: {},
105
 
 
106
 
    getValue: function(node) {
107
 
        var ret = '', // TODO: return null?
108
 
            getter;
109
 
 
110
 
        if (node && node[TAG_NAME]) {
111
 
            getter = Y_DOM.VALUE_GETTERS[node[TAG_NAME].toLowerCase()];
112
 
 
113
 
            if (getter) {
114
 
                ret = getter(node);
115
 
            } else {
116
 
                ret = node.value;
117
 
            }
118
 
        }
119
 
 
120
 
        // workaround for IE8 JSON stringify bug
121
 
        // which converts empty string values to null
122
 
        if (ret === EMPTY_STRING) {
123
 
            ret = EMPTY_STRING; // for real
124
 
        }
125
 
 
126
 
        return (typeof ret === 'string') ? ret : '';
127
 
    },
128
 
 
129
 
    setValue: function(node, val) {
130
 
        var setter;
131
 
 
132
 
        if (node && node[TAG_NAME]) {
133
 
            setter = Y_DOM.VALUE_SETTERS[node[TAG_NAME].toLowerCase()];
134
 
 
135
 
            if (setter) {
136
 
                setter(node, val);
137
 
            } else {
138
 
                node.value = val;
139
 
            }
140
 
        }
141
 
    },
142
 
 
143
 
    creators: {}
144
 
});
145
 
 
146
 
addFeature('value-set', 'select', {
147
 
    test: function() {
148
 
        var node = Y.config.doc.createElement('select');
149
 
        node.innerHTML = '<option>1</option><option>2</option>';
150
 
        node.value = '2';
151
 
        return (node.value && node.value === '2');
152
 
    }
153
 
});
154
 
 
155
 
if (!testFeature('value-set', 'select')) {
156
 
    Y_DOM.VALUE_SETTERS.select = function(node, val) {
157
 
        for (var i = 0, options = node.getElementsByTagName('option'), option;
158
 
                option = options[i++];) {
159
 
            if (Y_DOM.getValue(option) === val) {
160
 
                option.selected = true;
161
 
                //Y_DOM.setAttribute(option, 'selected', 'selected');
162
 
                break;
163
 
            }
164
 
        }
165
 
    }
166
 
}
167
 
 
168
 
Y.mix(Y_DOM.VALUE_GETTERS, {
169
 
    button: function(node) {
170
 
        return (node.attributes && node.attributes.value) ? node.attributes.value.value : '';
171
 
    }
172
 
});
173
 
 
174
 
Y.mix(Y_DOM.VALUE_SETTERS, {
175
 
    // IE: node.value changes the button text, which should be handled via innerHTML
176
 
    button: function(node, val) {
177
 
        var attr = node.attributes.value;
178
 
        if (!attr) {
179
 
            attr = node[OWNER_DOCUMENT].createAttribute('value');
180
 
            node.setAttributeNode(attr);
181
 
        }
182
 
 
183
 
        attr.value = val;
184
 
    }
185
 
});
186
 
 
187
 
 
188
 
Y.mix(Y_DOM.VALUE_GETTERS, {
189
 
    option: function(node) {
190
 
        var attrs = node.attributes;
191
 
        return (attrs.value && attrs.value.specified) ? node.value : node.text;
192
 
    },
193
 
 
194
 
    select: function(node) {
195
 
        var val = node.value,
196
 
            options = node.options;
197
 
 
198
 
        if (options && options.length) {
199
 
            // TODO: implement multipe select
200
 
            if (node.multiple) {
201
 
            } else {
202
 
                val = Y_DOM.getValue(options[node.selectedIndex]);
203
 
            }
204
 
        }
205
 
 
206
 
        return val;
207
 
    }
208
 
});
209
 
var addClass, hasClass, removeClass;
210
 
 
211
 
Y.mix(Y.DOM, {
212
 
    /**
213
 
     * Determines whether a DOM element has the given className.
214
 
     * @method hasClass
215
 
     * @for DOM
216
 
     * @param {HTMLElement} element The DOM element. 
217
 
     * @param {String} className the class name to search for
218
 
     * @return {Boolean} Whether or not the element has the given class. 
219
 
     */
220
 
    hasClass: function(node, className) {
221
 
        var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
222
 
        return re.test(node.className);
223
 
    },
224
 
 
225
 
    /**
226
 
     * Adds a class name to a given DOM element.
227
 
     * @method addClass         
228
 
     * @for DOM
229
 
     * @param {HTMLElement} element The DOM element. 
230
 
     * @param {String} className the class name to add to the class attribute
231
 
     */
232
 
    addClass: function(node, className) {
233
 
        if (!Y.DOM.hasClass(node, className)) { // skip if already present 
234
 
            node.className = Y.Lang.trim([node.className, className].join(' '));
235
 
        }
236
 
    },
237
 
 
238
 
    /**
239
 
     * Removes a class name from a given element.
240
 
     * @method removeClass         
241
 
     * @for DOM
242
 
     * @param {HTMLElement} element The DOM element. 
243
 
     * @param {String} className the class name to remove from the class attribute
244
 
     */
245
 
    removeClass: function(node, className) {
246
 
        if (className && hasClass(node, className)) {
247
 
            node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
248
 
                            className + '(?:\\s+|$)'), ' '));
249
 
 
250
 
            if ( hasClass(node, className) ) { // in case of multiple adjacent
251
 
                removeClass(node, className);
252
 
            }
253
 
        }                 
254
 
    },
255
 
 
256
 
    /**
257
 
     * Replace a class with another class for a given element.
258
 
     * If no oldClassName is present, the newClassName is simply added.
259
 
     * @method replaceClass  
260
 
     * @for DOM
261
 
     * @param {HTMLElement} element The DOM element 
262
 
     * @param {String} oldClassName the class name to be replaced
263
 
     * @param {String} newClassName the class name that will be replacing the old class name
264
 
     */
265
 
    replaceClass: function(node, oldC, newC) {
266
 
        removeClass(node, oldC); // remove first in case oldC === newC
267
 
        addClass(node, newC);
268
 
    },
269
 
 
270
 
    /**
271
 
     * If the className exists on the node it is removed, if it doesn't exist it is added.
272
 
     * @method toggleClass  
273
 
     * @for DOM
274
 
     * @param {HTMLElement} element The DOM element
275
 
     * @param {String} className the class name to be toggled
276
 
     * @param {Boolean} addClass optional boolean to indicate whether class
277
 
     * should be added or removed regardless of current state
278
 
     */
279
 
    toggleClass: function(node, className, force) {
280
 
        var add = (force !== undefined) ? force :
281
 
                !(hasClass(node, className));
282
 
 
283
 
        if (add) {
284
 
            addClass(node, className);
285
 
        } else {
286
 
            removeClass(node, className);
287
 
        }
288
 
    }
289
 
});
290
 
 
291
 
hasClass = Y.DOM.hasClass;
292
 
removeClass = Y.DOM.removeClass;
293
 
addClass = Y.DOM.addClass;
294
 
 
295
 
var re_tag = /<([a-z]+)/i,
296
 
 
297
 
    Y_DOM = Y.DOM,
298
 
 
299
 
    addFeature = Y.Features.add,
300
 
    testFeature = Y.Features.test,
301
 
 
302
 
    creators = {},
303
 
 
304
 
    createFromDIV = function(html, tag) {
305
 
        var div = Y.config.doc.createElement('div'),
306
 
            ret = true;
307
 
 
308
 
        div.innerHTML = html;
309
 
        if (!div.firstChild || div.firstChild.tagName !== tag.toUpperCase()) {
310
 
            ret = false;
311
 
        }
312
 
 
313
 
        return ret;
314
 
    },
315
 
 
316
 
    re_tbody = /(?:\/(?:thead|tfoot|tbody|caption|col|colgroup)>)+\s*<tbody/,
317
 
 
318
 
    TABLE_OPEN = '<table>',
319
 
    TABLE_CLOSE = '</table>';
320
 
 
321
 
Y.mix(Y.DOM, {
322
 
    _fragClones: {},
323
 
 
324
 
    _create: function(html, doc, tag) {
325
 
        tag = tag || 'div';
326
 
 
327
 
        var frag = Y_DOM._fragClones[tag];
328
 
        if (frag) {
329
 
            frag = frag.cloneNode(false);
330
 
        } else {
331
 
            frag = Y_DOM._fragClones[tag] = doc.createElement(tag);
332
 
        }
333
 
        frag.innerHTML = html;
334
 
        return frag;
335
 
    },
336
 
 
337
 
    /**
338
 
     * Creates a new dom node using the provided markup string. 
339
 
     * @method create
340
 
     * @param {String} html The markup used to create the element
341
 
     * @param {HTMLDocument} doc An optional document context 
342
 
     * @return {HTMLElement|DocumentFragment} returns a single HTMLElement 
343
 
     * when creating one node, and a documentFragment when creating
344
 
     * multiple nodes.
345
 
     */
346
 
    create: function(html, doc) {
347
 
        if (typeof html === 'string') {
348
 
            html = Y.Lang.trim(html); // match IE which trims whitespace from innerHTML
349
 
 
350
 
        }
351
 
 
352
 
        doc = doc || Y.config.doc;
353
 
        var m = re_tag.exec(html),
354
 
            create = Y_DOM._create,
355
 
            custom = creators,
356
 
            ret = null,
357
 
            creator,
358
 
            tag, nodes;
359
 
 
360
 
        if (html != undefined) { // not undefined or null
361
 
            if (m && m[1]) {
362
 
                creator = custom[m[1].toLowerCase()];
363
 
                if (typeof creator === 'function') {
364
 
                    create = creator; 
365
 
                } else {
366
 
                    tag = creator;
367
 
                }
368
 
            }
369
 
 
370
 
            nodes = create(html, doc, tag).childNodes;
371
 
 
372
 
            if (nodes.length === 1) { // return single node, breaking parentNode ref from "fragment"
373
 
                ret = nodes[0].parentNode.removeChild(nodes[0]);
374
 
            } else if (nodes[0] && nodes[0].className === 'yui3-big-dummy') { // using dummy node to preserve some attributes (e.g. OPTION not selected)
375
 
                if (nodes.length === 2) {
376
 
                    ret = nodes[0].nextSibling;
377
 
                } else {
378
 
                    nodes[0].parentNode.removeChild(nodes[0]); 
379
 
                     ret = Y_DOM._nl2frag(nodes, doc);
380
 
                }
381
 
            } else { // return multiple nodes as a fragment
382
 
                 ret = Y_DOM._nl2frag(nodes, doc);
383
 
            }
384
 
        }
385
 
 
386
 
        return ret;
387
 
    },
388
 
 
389
 
    _nl2frag: function(nodes, doc) {
390
 
        var ret = null,
391
 
            i, len;
392
 
 
393
 
        if (nodes && (nodes.push || nodes.item) && nodes[0]) {
394
 
            doc = doc || nodes[0].ownerDocument; 
395
 
            ret = doc.createDocumentFragment();
396
 
 
397
 
            if (nodes.item) { // convert live list to static array
398
 
                nodes = Y.Array(nodes, 0, true);
399
 
            }
400
 
 
401
 
            for (i = 0, len = nodes.length; i < len; i++) {
402
 
                ret.appendChild(nodes[i]); 
403
 
            }
404
 
        } // else inline with log for minification
405
 
        return ret;
406
 
    },
407
 
 
408
 
    /**
409
 
     * Inserts content in a node at the given location 
410
 
     * @method addHTML
411
 
     * @param {HTMLElement} node The node to insert into
412
 
     * @param {HTMLElement | Array | HTMLCollection} content The content to be inserted 
413
 
     * @param {HTMLElement} where Where to insert the content
414
 
     * If no "where" is given, content is appended to the node
415
 
     * Possible values for "where"
416
 
     * <dl>
417
 
     * <dt>HTMLElement</dt>
418
 
     * <dd>The element to insert before</dd>
419
 
     * <dt>"replace"</dt>
420
 
     * <dd>Replaces the existing HTML</dd>
421
 
     * <dt>"before"</dt>
422
 
     * <dd>Inserts before the existing HTML</dd>
423
 
     * <dt>"before"</dt>
424
 
     * <dd>Inserts content before the node</dd>
425
 
     * <dt>"after"</dt>
426
 
     * <dd>Inserts content after the node</dd>
427
 
     * </dl>
428
 
     */
429
 
    addHTML: function(node, content, where) {
430
 
        var nodeParent = node.parentNode,
431
 
            i = 0,
432
 
            item,
433
 
            ret = content,
434
 
            newNode;
435
 
            
436
 
 
437
 
        if (content != undefined) { // not null or undefined (maybe 0)
438
 
            if (content.nodeType) { // DOM node, just add it
439
 
                newNode = content;
440
 
            } else if (typeof content == 'string' || typeof content == 'number') {
441
 
                ret = newNode = Y_DOM.create(content);
442
 
            } else if (content[0] && content[0].nodeType) { // array or collection 
443
 
                newNode = Y.config.doc.createDocumentFragment();
444
 
                while ((item = content[i++])) {
445
 
                    newNode.appendChild(item); // append to fragment for insertion
446
 
                }
447
 
            }
448
 
        }
449
 
 
450
 
        if (where) {
451
 
            if (newNode && where.parentNode) { // insert regardless of relationship to node
452
 
                where.parentNode.insertBefore(newNode, where);
453
 
            } else {
454
 
                switch (where) {
455
 
                    case 'replace':
456
 
                        while (node.firstChild) {
457
 
                            node.removeChild(node.firstChild);
458
 
                        }
459
 
                        if (newNode) { // allow empty content to clear node
460
 
                            node.appendChild(newNode);
461
 
                        }
462
 
                        break;
463
 
                    case 'before':
464
 
                        if (newNode) {
465
 
                            nodeParent.insertBefore(newNode, node);
466
 
                        }
467
 
                        break;
468
 
                    case 'after':
469
 
                        if (newNode) {
470
 
                            if (node.nextSibling) { // IE errors if refNode is null
471
 
                                nodeParent.insertBefore(newNode, node.nextSibling);
472
 
                            } else {
473
 
                                nodeParent.appendChild(newNode);
474
 
                            }
475
 
                        }
476
 
                        break;
477
 
                    default:
478
 
                        if (newNode) {
479
 
                            node.appendChild(newNode);
480
 
                        }
481
 
                }
482
 
            }
483
 
        } else if (newNode) {
484
 
            node.appendChild(newNode);
485
 
        }
486
 
 
487
 
        return ret;
488
 
    }
489
 
});
490
 
 
491
 
addFeature('innerhtml', 'table', {
492
 
    test: function() {
493
 
        var node = Y.config.doc.createElement('table');
494
 
        try {
495
 
            node.innerHTML = '<tbody></tbody>';
496
 
        } catch(e) {
497
 
            return false;
498
 
        }
499
 
        return (node.firstChild && node.firstChild.nodeName === 'TBODY');
500
 
    }
501
 
});
502
 
 
503
 
addFeature('innerhtml-div', 'tr', {
504
 
    test: function() {
505
 
        return createFromDIV('<tr></tr>', 'tr');
506
 
    }
507
 
});
508
 
 
509
 
addFeature('innerhtml-div', 'script', {
510
 
    test: function() {
511
 
        return createFromDIV('<script></script>', 'script');
512
 
    }
513
 
});
514
 
 
515
 
if (!testFeature('innerhtml', 'table')) {
516
 
    // TODO: thead/tfoot with nested tbody
517
 
        // IE adds TBODY when creating TABLE elements (which may share this impl)
518
 
    creators.tbody = function(html, doc) {
519
 
        var frag = Y_DOM.create(TABLE_OPEN + html + TABLE_CLOSE, doc),
520
 
            tb = frag.children.tags('tbody')[0];
521
 
 
522
 
        if (frag.children.length > 1 && tb && !re_tbody.test(html)) {
523
 
            tb.parentNode.removeChild(tb); // strip extraneous tbody
524
 
        }
525
 
        return frag;
526
 
    };
527
 
}
528
 
 
529
 
if (!testFeature('innerhtml-div', 'script')) {
530
 
    creators.script = function(html, doc) {
531
 
        var frag = doc.createElement('div');
532
 
 
533
 
        frag.innerHTML = '-' + html;
534
 
        frag.removeChild(frag.firstChild);
535
 
        return frag;
536
 
    }
537
 
 
538
 
    creators.link = creators.style = creators.script;
539
 
}
540
 
 
541
 
if (!testFeature('innerhtml-div', 'tr')) {
542
 
    Y.mix(creators, {
543
 
        option: function(html, doc) {
544
 
            return Y_DOM.create('<select><option class="yui3-big-dummy" selected></option>' + html + '</select>', doc);
545
 
        },
546
 
 
547
 
        tr: function(html, doc) {
548
 
            return Y_DOM.create('<tbody>' + html + '</tbody>', doc);
549
 
        },
550
 
 
551
 
        td: function(html, doc) {
552
 
            return Y_DOM.create('<tr>' + html + '</tr>', doc);
553
 
        }, 
554
 
 
555
 
        col: function(html, doc) {
556
 
            return Y_DOM.create('<colgroup>' + html + '</colgroup>', doc);
557
 
        }, 
558
 
 
559
 
        tbody: 'table'
560
 
    });
561
 
 
562
 
    Y.mix(creators, {
563
 
        legend: 'fieldset',
564
 
        th: creators.td,
565
 
        thead: creators.tbody,
566
 
        tfoot: creators.tbody,
567
 
        caption: creators.tbody,
568
 
        colgroup: creators.tbody,
569
 
        optgroup: creators.option
570
 
    });
571
 
}
572
 
 
573
 
Y_DOM.creators = creators;
574
 
Y.mix(Y.DOM, {
575
 
    /**
576
 
     * Sets the width of the element to the given size, regardless
577
 
     * of box model, border, padding, etc.
578
 
     * @method setWidth
579
 
     * @param {HTMLElement} element The DOM element. 
580
 
     * @param {String|Int} size The pixel height to size to
581
 
     */
582
 
 
583
 
    setWidth: function(node, size) {
584
 
        Y.DOM._setSize(node, 'width', size);
585
 
    },
586
 
 
587
 
    /**
588
 
     * Sets the height of the element to the given size, regardless
589
 
     * of box model, border, padding, etc.
590
 
     * @method setHeight
591
 
     * @param {HTMLElement} element The DOM element. 
592
 
     * @param {String|Int} size The pixel height to size to
593
 
     */
594
 
 
595
 
    setHeight: function(node, size) {
596
 
        Y.DOM._setSize(node, 'height', size);
597
 
    },
598
 
 
599
 
    _setSize: function(node, prop, val) {
600
 
        val = (val > 0) ? val : 0;
601
 
        var size = 0;
602
 
 
603
 
        node.style[prop] = val + 'px';
604
 
        size = (prop === 'height') ? node.offsetHeight : node.offsetWidth;
605
 
 
606
 
        if (size > val) {
607
 
            val = val - (size - val);
608
 
 
609
 
            if (val < 0) {
610
 
                val = 0;
611
 
            }
612
 
 
613
 
            node.style[prop] = val + 'px';
614
 
        }
615
 
    }
616
 
});
617
 
 
618
 
 
619
 
}, '3.4.1' ,{requires:['dom-core']});