OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 cr.define('cr.ui', function() { | 5 base.exportTo('ui', function() { |
6 | 6 |
7 /** | 7 /** |
8 * Decorates elements as an instance of a class. | 8 * Decorates elements as an instance of a class. |
9 * @param {string|!Element} source The way to find the element(s) to decorate. | 9 * @param {string|!Element} source The way to find the element(s) to decorate. |
10 * If this is a string then {@code querySeletorAll} is used to find the | 10 * If this is a string then {@code querySeletorAll} is used to find the |
11 * elements to decorate. | 11 * elements to decorate. |
12 * @param {!Function} constr The constructor to decorate with. The constr | 12 * @param {!Function} constr The constructor to decorate with. The constr |
13 * needs to have a {@code decorate} function. | 13 * needs to have a {@code decorate} function. |
14 */ | 14 */ |
15 function decorate(source, constr) { | 15 function decorate(source, constr) { |
16 var elements; | 16 var elements; |
17 if (typeof source == 'string') | 17 if (typeof source == 'string') |
18 elements = cr.doc.querySelectorAll(source); | 18 elements = base.doc.querySelectorAll(source); |
19 else | 19 else |
20 elements = [source]; | 20 elements = [source]; |
21 | 21 |
22 for (var i = 0, el; el = elements[i]; i++) { | 22 for (var i = 0, el; el = elements[i]; i++) { |
23 if (!(el instanceof constr)) | 23 if (!(el instanceof constr)) |
24 constr.decorate(el); | 24 constr.decorate(el); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 /** | 28 /** |
29 * Helper function for creating new element for define. | 29 * Helper function for creating new element for define. |
30 */ | 30 */ |
31 function createElementHelper(tagName, opt_bag) { | 31 function createElementHelper(tagName, opt_bag) { |
32 // Allow passing in ownerDocument to create in a different document. | 32 // Allow passing in ownerDocument to create in a different document. |
33 var doc; | 33 var doc; |
34 if (opt_bag && opt_bag.ownerDocument) | 34 if (opt_bag && opt_bag.ownerDocument) |
35 doc = opt_bag.ownerDocument; | 35 doc = opt_bag.ownerDocument; |
36 else | 36 else |
37 doc = cr.doc; | 37 doc = base.doc; |
38 return doc.createElement(tagName); | 38 return doc.createElement(tagName); |
39 } | 39 } |
40 | 40 |
41 /** | 41 /** |
42 * Creates the constructor for a UI element class. | 42 * Creates the constructor for a UI element class. |
43 * | 43 * |
44 * Usage: | 44 * Usage: |
45 * <pre> | 45 * <pre> |
46 * var List = cr.ui.define('list'); | 46 * var List = ui.define('list'); |
47 * List.prototype = { | 47 * List.prototype = { |
48 * __proto__: HTMLUListElement.prototype, | 48 * __proto__: HTMLUListElement.prototype, |
49 * decorate: function() { | 49 * decorate: function() { |
50 * ... | 50 * ... |
51 * }, | 51 * }, |
52 * ... | 52 * ... |
53 * }; | 53 * }; |
54 * </pre> | 54 * </pre> |
55 * | 55 * |
56 * @param {string|Function} tagNameOrFunction The tagName or | 56 * @param {string|Function} tagNameOrFunction The tagName or |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 return f; | 99 return f; |
100 } | 100 } |
101 | 101 |
102 /** | 102 /** |
103 * Input elements do not grow and shrink with their content. This is a simple | 103 * Input elements do not grow and shrink with their content. This is a simple |
104 * (and not very efficient) way of handling shrinking to content with support | 104 * (and not very efficient) way of handling shrinking to content with support |
105 * for min width and limited by the width of the parent element. | 105 * for min width and limited by the width of the parent element. |
106 * @param {HTMLElement} el The element to limit the width for. | 106 * @param {HTMLElement} el The element to limit the width for. |
107 * @param {number} parentEl The parent element that should limit the size. | 107 * @param {number} parentEl The parent element that should limit the size. |
108 * @param {number} min The minimum width. | 108 * @param {number} min The minimum width. |
109 * @param {number} opt_scale Optional scale factor to apply to the width. | |
110 */ | 109 */ |
111 function limitInputWidth(el, parentEl, min, opt_scale) { | 110 function limitInputWidth(el, parentEl, min) { |
112 // Needs a size larger than borders | 111 // Needs a size larger than borders |
113 el.style.width = '10px'; | 112 el.style.width = '10px'; |
114 var doc = el.ownerDocument; | 113 var doc = el.ownerDocument; |
115 var win = doc.defaultView; | 114 var win = doc.defaultView; |
116 var computedStyle = win.getComputedStyle(el); | 115 var computedStyle = win.getComputedStyle(el); |
117 var parentComputedStyle = win.getComputedStyle(parentEl); | 116 var parentComputedStyle = win.getComputedStyle(parentEl); |
118 var rtl = computedStyle.direction == 'rtl'; | 117 var rtl = computedStyle.direction == 'rtl'; |
119 | 118 |
120 // To get the max width we get the width of the treeItem minus the position | 119 // To get the max width we get the width of the treeItem minus the position |
121 // of the input. | 120 // of the input. |
122 var inputRect = el.getBoundingClientRect(); // box-sizing | 121 var inputRect = el.getBoundingClientRect(); // box-sizing |
123 var parentRect = parentEl.getBoundingClientRect(); | 122 var parentRect = parentEl.getBoundingClientRect(); |
124 var startPos = rtl ? parentRect.right - inputRect.right : | 123 var startPos = rtl ? parentRect.right - inputRect.right : |
125 inputRect.left - parentRect.left; | 124 inputRect.left - parentRect.left; |
126 | 125 |
127 // Add up border and padding of the input. | 126 // Add up border and padding of the input. |
128 var inner = parseInt(computedStyle.borderLeftWidth, 10) + | 127 var inner = parseInt(computedStyle.borderLeftWidth, 10) + |
129 parseInt(computedStyle.paddingLeft, 10) + | 128 parseInt(computedStyle.paddingLeft, 10) + |
130 parseInt(computedStyle.paddingRight, 10) + | 129 parseInt(computedStyle.paddingRight, 10) + |
131 parseInt(computedStyle.borderRightWidth, 10); | 130 parseInt(computedStyle.borderRightWidth, 10); |
132 | 131 |
133 // We also need to subtract the padding of parent to prevent it to overflow. | 132 // We also need to subtract the padding of parent to prevent it to overflow. |
134 var parentPadding = rtl ? parseInt(parentComputedStyle.paddingLeft, 10) : | 133 var parentPadding = rtl ? parseInt(parentComputedStyle.paddingLeft, 10) : |
135 parseInt(parentComputedStyle.paddingRight, 10); | 134 parseInt(parentComputedStyle.paddingRight, 10); |
136 | 135 |
137 var max = parentEl.clientWidth - startPos - inner - parentPadding; | 136 var max = parentEl.clientWidth - startPos - inner - parentPadding; |
138 if (opt_scale) | |
139 max *= opt_scale; | |
140 | 137 |
141 function limit() { | 138 function limit() { |
142 if (el.scrollWidth > max) { | 139 if (el.scrollWidth > max) { |
143 el.style.width = max + 'px'; | 140 el.style.width = max + 'px'; |
144 } else { | 141 } else { |
145 el.style.width = 0; | 142 el.style.width = 0; |
146 var sw = el.scrollWidth; | 143 var sw = el.scrollWidth; |
147 if (sw < min) { | 144 if (sw < min) { |
148 el.style.width = min + 'px'; | 145 el.style.width = min + 'px'; |
149 } else { | 146 } else { |
(...skipping 11 matching lines...) Expand all Loading... |
161 * subpixel layout issues, the value is rounded to the nearest integral value. | 158 * subpixel layout issues, the value is rounded to the nearest integral value. |
162 * @param {number} pixels The number of pixels. | 159 * @param {number} pixels The number of pixels. |
163 * @return {string} e.g. '16px'. | 160 * @return {string} e.g. '16px'. |
164 */ | 161 */ |
165 function toCssPx(pixels) { | 162 function toCssPx(pixels) { |
166 if (!window.isFinite(pixels)) | 163 if (!window.isFinite(pixels)) |
167 console.error('Pixel value is not a number: ' + pixels); | 164 console.error('Pixel value is not a number: ' + pixels); |
168 return Math.round(pixels) + 'px'; | 165 return Math.round(pixels) + 'px'; |
169 } | 166 } |
170 | 167 |
| 168 function createSpan(opt_text) { |
| 169 var spanEl = document.createElement('span'); |
| 170 if (opt_text) |
| 171 spanEl.textContent = opt_text; |
| 172 return spanEl; |
| 173 }; |
| 174 |
171 return { | 175 return { |
172 decorate: decorate, | 176 decorate: decorate, |
173 define: define, | 177 define: define, |
174 limitInputWidth: limitInputWidth, | 178 limitInputWidth: limitInputWidth, |
175 toCssPx: toCssPx, | 179 toCssPx: toCssPx, |
| 180 createSpan: createSpan |
176 }; | 181 }; |
177 }); | 182 }); |
OLD | NEW |