OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright (c) 2014 The Chromium Authors. All rights reserved. | 3 Copyright (c) 2014 The Chromium Authors. All rights reserved. |
4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
5 found in the LICENSE file. | 5 found in the LICENSE file. |
6 --> | 6 --> |
7 <link rel="import" href="/tracing/base/base.html"> | 7 <link rel="import" href="/tracing/base/base.html"> |
8 <script> | 8 <script> |
9 'use strict'; | 9 'use strict'; |
10 | 10 |
11 tr.exportTo('tr.ui.b', function() { | 11 tr.exportTo('tr.ui.b', function() { |
12 | 12 |
13 /** | 13 /** |
14 * Decorates elements as an instance of a class. | 14 * Decorates elements as an instance of a class. |
15 * @param {string|!Element} source The way to find the element(s) to decorate. | 15 * @param {string|!Element} source The way to find the element(s) to decorate. |
16 * If this is a string then {@code querySeletorAll} is used to find the | 16 * If this is a string then {@code querySeletorAll} is used to find the |
17 * elements to decorate. | 17 * elements to decorate. |
18 * @param {!Function} constr The constructor to decorate with. The constr | 18 * @param {!Function} constr The constructor to decorate with. The constr |
19 * needs to have a {@code decorate} function. | 19 * needs to have a {@code decorate} function. |
20 */ | 20 */ |
21 function decorate(source, constr) { | 21 function decorate(source, constr) { |
22 var elements; | 22 var elements; |
23 if (typeof source == 'string') | 23 if (typeof source === 'string') |
24 elements = Polymer.dom(tr.doc).querySelectorAll(source); | 24 elements = Polymer.dom(tr.doc).querySelectorAll(source); |
25 else | 25 else |
26 elements = [source]; | 26 elements = [source]; |
27 | 27 |
28 for (var i = 0, el; el = elements[i]; i++) { | 28 for (var i = 0, el; el = elements[i]; i++) { |
29 if (!(el instanceof constr)) | 29 if (!(el instanceof constr)) |
30 constr.decorate(el); | 30 constr.decorate(el); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 * be also a function created by tr.ui.b.define. | 65 * be also a function created by tr.ui.b.define. |
66 * | 66 * |
67 * @param {string=} opt_tagNS The namespace in which to create the base | 67 * @param {string=} opt_tagNS The namespace in which to create the base |
68 * element. Has no meaning when opt_parentConstructor is passed and must | 68 * element. Has no meaning when opt_parentConstructor is passed and must |
69 * either be undefined or the same namespace as the parent class. | 69 * either be undefined or the same namespace as the parent class. |
70 * | 70 * |
71 * @return {function(Object=):Element} The newly created component | 71 * @return {function(Object=):Element} The newly created component |
72 * constructor. | 72 * constructor. |
73 */ | 73 */ |
74 function define(className, opt_parentConstructor, opt_tagNS) { | 74 function define(className, opt_parentConstructor, opt_tagNS) { |
75 if (typeof className == 'function') { | 75 if (typeof className === 'function') { |
76 throw new Error('Passing functions as className is deprecated. Please ' + | 76 throw new Error('Passing functions as className is deprecated. Please ' + |
77 'use (className, opt_parentConstructor) to subclass'); | 77 'use (className, opt_parentConstructor) to subclass'); |
78 } | 78 } |
79 | 79 |
80 var className = className.toLowerCase(); | 80 var className = className.toLowerCase(); |
81 if (opt_parentConstructor && !opt_parentConstructor.tagName) | 81 if (opt_parentConstructor && !opt_parentConstructor.tagName) |
82 throw new Error('opt_parentConstructor was not ' + | 82 throw new Error('opt_parentConstructor was not ' + |
83 'created by tr.ui.b.define'); | 83 'created by tr.ui.b.define'); |
84 | 84 |
85 // Walk up the parent constructors until we can find the type of tag | 85 // Walk up the parent constructors until we can find the type of tag |
(...skipping 15 matching lines...) Expand all Loading... |
101 | 101 |
102 /** | 102 /** |
103 * Creates a new UI element constructor. | 103 * Creates a new UI element constructor. |
104 * Arguments passed to the constuctor are provided to the decorate method. | 104 * Arguments passed to the constuctor are provided to the decorate method. |
105 * You will need to call the parent elements decorate method from within | 105 * You will need to call the parent elements decorate method from within |
106 * your decorate method and pass any required parameters. | 106 * your decorate method and pass any required parameters. |
107 * @constructor | 107 * @constructor |
108 */ | 108 */ |
109 function f() { | 109 function f() { |
110 if (opt_parentConstructor && | 110 if (opt_parentConstructor && |
111 f.prototype.__proto__ != opt_parentConstructor.prototype) { | 111 f.prototype.__proto__ !== opt_parentConstructor.prototype) { |
112 throw new Error( | 112 throw new Error( |
113 className + ' prototye\'s __proto__ field is messed up. ' + | 113 className + ' prototye\'s __proto__ field is messed up. ' + |
114 'It MUST be the prototype of ' + opt_parentConstructor.tagName); | 114 'It MUST be the prototype of ' + opt_parentConstructor.tagName); |
115 } | 115 } |
116 | 116 |
117 var el; | 117 var el; |
118 if (tagNS === undefined) | 118 if (tagNS === undefined) |
119 el = tr.doc.createElement(tagName); | 119 el = tr.doc.createElement(tagName); |
120 else | 120 else |
121 el = tr.doc.createElementNS(tagNS, tagName); | 121 el = tr.doc.createElementNS(tagNS, tagName); |
(...skipping 19 matching lines...) Expand all Loading... |
141 f.toString = function() { | 141 f.toString = function() { |
142 if (!f.parentConstructor) | 142 if (!f.parentConstructor) |
143 return f.tagName; | 143 return f.tagName; |
144 return f.parentConstructor.toString() + '::' + f.className; | 144 return f.parentConstructor.toString() + '::' + f.className; |
145 }; | 145 }; |
146 | 146 |
147 return f; | 147 return f; |
148 } | 148 } |
149 | 149 |
150 function elementIsChildOf(el, potentialParent) { | 150 function elementIsChildOf(el, potentialParent) { |
151 if (el == potentialParent) | 151 if (el === potentialParent) |
152 return false; | 152 return false; |
153 | 153 |
154 var cur = el; | 154 var cur = el; |
155 while (Polymer.dom(cur).parentNode) { | 155 while (Polymer.dom(cur).parentNode) { |
156 if (cur == potentialParent) | 156 if (cur === potentialParent) |
157 return true; | 157 return true; |
158 cur = Polymer.dom(cur).parentNode; | 158 cur = Polymer.dom(cur).parentNode; |
159 } | 159 } |
160 return false; | 160 return false; |
161 }; | 161 } |
162 | 162 |
163 return { | 163 return { |
164 decorate: decorate, | 164 decorate: decorate, |
165 define: define, | 165 define: define, |
166 elementIsChildOf: elementIsChildOf | 166 elementIsChildOf: elementIsChildOf |
167 }; | 167 }; |
168 }); | 168 }); |
169 </script> | 169 </script> |
OLD | NEW |