1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 base.exportTo('ui', function() {
8 * Helper function for creating new element for define.
10 function createElementHelper(tagName, opt_bag) {
11 // Allow passing in ownerDocument to create in a different document.
13 if (opt_bag && opt_bag.ownerDocument)
14 doc = opt_bag.ownerDocument;
16 return doc.createElement(tagName);
20 * Creates the constructor for a UI element class.
24 * var List = base.ui.define('list');
26 * __proto__: HTMLUListElement.prototype,
27 * decorate: function() {
34 * @param {string|Function} tagNameOrFunction The tagName or
35 * function to use for newly created elements. If this is a function it
36 * needs to return a new element when called.
37 * @return {function(Object=):Element} The constructor function which takes
38 * an optional property bag. The function also has a static
39 * {@code decorate} method added to it.
41 function define(tagNameOrFunction) {
42 var createFunction, tagName;
43 if (typeof tagNameOrFunction == 'function') {
44 createFunction = tagNameOrFunction;
47 createFunction = createElementHelper;
48 tagName = tagNameOrFunction;
52 * Creates a new UI element constructor.
53 * @param {Object=} opt_propertyBag Optional bag of properties to set on the
54 * object after created. The property {@code ownerDocument} is special
55 * cased and it allows you to create the element in a different
56 * document than the default.
59 function f(opt_propertyBag) {
60 var el = createFunction(tagName, opt_propertyBag);
62 for (var propertyName in opt_propertyBag) {
63 el[propertyName] = opt_propertyBag[propertyName];
69 * Decorates an element as a UI element class.
70 * @param {!Element} el The element to decorate.
72 f.decorate = function(el) {
73 el.__proto__ = f.prototype;