|
26 | 26 | * @module material.components.colors |
27 | 27 | * |
28 | 28 | * @description |
29 | | - * With only defining themes, one couldn't get non AngularJS Material elements colored with Material colors, |
30 | | - * `$mdColors` service is used by the md-color directive to convert the 1..n color expressions to RGBA values and will apply |
31 | | - * those values to element as CSS property values. |
| 29 | + * With only defining themes, one couldn't get non AngularJS Material elements colored with |
| 30 | + * Material colors, `$mdColors` service is used by the md-color directive to convert the |
| 31 | + * 1..n color expressions to RGBA values and will apply those values to element as CSS property |
| 32 | + * values. |
32 | 33 | * |
33 | 34 | * @usage |
34 | 35 | * <hljs lang="js"> |
|
61 | 62 | * Gets a color json object, keys are css properties and values are string of the wanted color |
62 | 63 | * Then calculate the rgba() values based on the theme color parts |
63 | 64 | * |
64 | | - * @param {DOMElement} element the element to apply the styles on. |
65 | | - * @param {object} colorExpression json object, keys are css properties and values are string of the wanted color, |
66 | | - * for example: `{color: 'red-A200-0.3'}`. |
| 65 | + * @param {angular.element} element the element to apply the styles on. |
| 66 | + * @param {Object} colorExpression json object, keys are css properties and values are string of |
| 67 | + * the wanted color, for example: `{color: 'red-A200-0.3'}`. |
67 | 68 | * |
68 | 69 | * @usage |
69 | 70 | * <hljs lang="js"> |
|
86 | 87 | } catch (e) { |
87 | 88 | $log.error(e.message); |
88 | 89 | } |
89 | | - |
90 | 90 | } |
91 | 91 |
|
92 | 92 | /** |
|
116 | 116 |
|
117 | 117 | /** |
118 | 118 | * Return the parsed color |
119 | | - * @param color hashmap of color definitions |
120 | | - * @param contrast whether use contrast color for foreground |
121 | | - * @returns rgba color string |
| 119 | + * @param {{hue: *, theme: any, palette: *, opacity: (*|string|number)}} color hash map of color |
| 120 | + * definitions |
| 121 | + * @param {boolean=} contrast whether use contrast color for foreground. Defaults to false. |
| 122 | + * @returns {string} rgba color string |
122 | 123 | */ |
123 | 124 | function parseColor(color, contrast) { |
124 | 125 | contrast = contrast || false; |
|
134 | 135 | /** |
135 | 136 | * Convert the color expression into an object with scope-interpolated values |
136 | 137 | * Then calculate the rgba() values based on the theme color parts |
137 | | - * |
138 | | - * @results Hashmap of CSS properties with associated `rgba( )` string vales |
139 | | - * |
140 | | - * |
| 138 | + * @param {Object} themeColors json object, keys are css properties and values are string of |
| 139 | + * the wanted color, for example: `{color: 'red-A200-0.3'}`. |
| 140 | + * @return {Object} Hashmap of CSS properties with associated `rgba()` string values |
141 | 141 | */ |
142 | 142 | function interpolateColors(themeColors) { |
143 | 143 | var rgbColors = {}; |
|
159 | 159 |
|
160 | 160 | /** |
161 | 161 | * Check if expression has defined theme |
162 | | - * e.g. |
163 | | - * 'myTheme-primary' => true |
164 | | - * 'red-800' => false |
| 162 | + * For instance: |
| 163 | + * 'myTheme-primary' => true |
| 164 | + * 'red-800' => false |
| 165 | + * @param {string} expression color expression like 'red-800', 'red-A200-0.3', |
| 166 | + * 'myTheme-primary', or 'myTheme-primary-400' |
| 167 | + * @return {boolean} true if the expression has a theme part, false otherwise. |
165 | 168 | */ |
166 | 169 | function hasTheme(expression) { |
167 | 170 | return angular.isDefined($mdTheming.THEMES[expression.split('-')[0]]); |
168 | 171 | } |
169 | 172 |
|
170 | 173 | /** |
171 | 174 | * For the evaluated expression, extract the color parts into a hash map |
| 175 | + * @param {string} expression color expression like 'red-800', 'red-A200-0.3', |
| 176 | + * 'myTheme-primary', or 'myTheme-primary-400' |
| 177 | + * @returns {{hue: *, theme: any, palette: *, opacity: (*|string|number)}} |
172 | 178 | */ |
173 | 179 | function extractColorOptions(expression) { |
174 | 180 | var parts = expression.split('-'); |
|
185 | 191 |
|
186 | 192 | /** |
187 | 193 | * Calculate the theme palette name |
| 194 | + * @param {Array} parts |
| 195 | + * @param {string} theme name |
| 196 | + * @return {string} |
188 | 197 | */ |
189 | 198 | function extractPalette(parts, theme) { |
190 | 199 | // If the next section is one of the palettes we assume it's a two word palette |
|
199 | 208 | // If the palette is not in the palette list it's one of primary/accent/warn/background |
200 | 209 | var scheme = $mdTheming.THEMES[theme].colors[palette]; |
201 | 210 | if (!scheme) { |
202 | | - throw new Error($mdUtil.supplant('mdColors: couldn\'t find \'{palette}\' in the palettes.', {palette: palette})); |
| 211 | + throw new Error($mdUtil.supplant( |
| 212 | + 'mdColors: couldn\'t find \'{palette}\' in the palettes.', |
| 213 | + {palette: palette})); |
203 | 214 | } |
204 | 215 | palette = scheme.name; |
205 | 216 | } |
206 | 217 |
|
207 | 218 | return palette; |
208 | 219 | } |
209 | 220 |
|
| 221 | + /** |
| 222 | + * @param {Array} parts |
| 223 | + * @param {string} theme name |
| 224 | + * @return {*} |
| 225 | + */ |
210 | 226 | function extractHue(parts, theme) { |
211 | 227 | var themeColors = $mdTheming.THEMES[theme].colors; |
212 | 228 |
|
213 | 229 | if (parts[1] === 'hue') { |
214 | 230 | var hueNumber = parseInt(parts.splice(2, 1)[0], 10); |
215 | 231 |
|
216 | 232 | if (hueNumber < 1 || hueNumber > 3) { |
217 | | - throw new Error($mdUtil.supplant('mdColors: \'hue-{hueNumber}\' is not a valid hue, can be only \'hue-1\', \'hue-2\' and \'hue-3\'', {hueNumber: hueNumber})); |
| 233 | + throw new Error($mdUtil.supplant( |
| 234 | + 'mdColors: \'hue-{hueNumber}\' is not a valid hue, can be only \'hue-1\', \'hue-2\' and \'hue-3\'', |
| 235 | + {hueNumber: hueNumber})); |
218 | 236 | } |
219 | 237 | parts[1] = 'hue-' + hueNumber; |
220 | 238 |
|
221 | 239 | if (!(parts[0] in themeColors)) { |
222 | | - throw new Error($mdUtil.supplant('mdColors: \'hue-x\' can only be used with [{availableThemes}], but was used with \'{usedTheme}\'', { |
| 240 | + throw new Error($mdUtil.supplant( |
| 241 | + 'mdColors: \'hue-x\' can only be used with [{availableThemes}], but was used with \'{usedTheme}\'', |
| 242 | + { |
223 | 243 | availableThemes: Object.keys(themeColors).join(', '), |
224 | 244 | usedTheme: parts[0] |
225 | 245 | })); |
|
261 | 281 | * </div> |
262 | 282 | * </hljs> |
263 | 283 | * |
264 | | - * `mdColors` directive will automatically watch for changes in the expression if it recognizes an interpolation |
265 | | - * expression or a function. For performance options, you can use `::` prefix to the `md-colors` expression |
266 | | - * to indicate a one-time data binding. |
| 284 | + * `mdColors` directive will automatically watch for changes in the expression if it recognizes |
| 285 | + * an interpolation expression or a function. For performance options, you can use `::` prefix to |
| 286 | + * the `md-colors` expression to indicate a one-time data binding. |
| 287 | + * |
267 | 288 | * <hljs lang="html"> |
268 | 289 | * <md-card md-colors="::{background: '{{theme}}-primary-700'}"> |
269 | 290 | * </md-card> |
270 | 291 | * </hljs> |
271 | | - * |
272 | 292 | */ |
273 | 293 | function MdColorsDirective($mdColors, $mdUtil, $log, $parse) { |
274 | 294 | return { |
|
282 | 302 |
|
283 | 303 | var lastColors = {}; |
284 | 304 |
|
| 305 | + /** |
| 306 | + * @param {string=} theme |
| 307 | + * @return {Object} colors found in the specified theme |
| 308 | + */ |
285 | 309 | var parseColors = function (theme) { |
286 | 310 | if (typeof theme !== 'string') { |
287 | 311 | theme = ''; |
|
298 | 322 | var colors = $parse(attrs.mdColors)(scope); |
299 | 323 |
|
300 | 324 | /** |
301 | | - * If mdTheme is defined up the DOM tree |
302 | | - * we add mdTheme theme to colors who doesn't specified a theme |
| 325 | + * If mdTheme is defined higher up the DOM tree, |
| 326 | + * we add mdTheme's theme to the colors which don't specify a theme. |
303 | 327 | * |
304 | | - * # example |
| 328 | + * @example |
305 | 329 | * <hljs lang="html"> |
306 | 330 | * <div md-theme="myTheme"> |
307 | 331 | * <div md-colors="{background: 'primary-600'}"> |
|
310 | 334 | * </div> |
311 | 335 | * </hljs> |
312 | 336 | * |
313 | | - * 'primary-600' will be 'myTheme-primary-600', |
314 | | - * but 'mySecondTheme-accent-200' will stay the same cause it has a theme prefix |
| 337 | + * 'primary-600' will be changed to 'myTheme-primary-600', |
| 338 | + * but 'mySecondTheme-accent-200' will not be changed since it has a theme defined. |
315 | 339 | */ |
316 | 340 | if (mdThemeController) { |
317 | 341 | Object.keys(colors).forEach(function (prop) { |
|
327 | 351 | return colors; |
328 | 352 | }; |
329 | 353 |
|
| 354 | + /** |
| 355 | + * @param {Object} colors |
| 356 | + */ |
330 | 357 | var cleanElement = function (colors) { |
331 | 358 | if (!angular.equals(colors, lastColors)) { |
332 | 359 | var keys = Object.keys(lastColors); |
|
344 | 371 | }; |
345 | 372 |
|
346 | 373 | /** |
347 | | - * Registering for mgTheme changes and asking mdTheme controller run our callback whenever a theme changes |
| 374 | + * Registering for mgTheme changes and asking mdTheme controller run our callback whenever |
| 375 | + * a theme changes. |
348 | 376 | */ |
349 | 377 | var unregisterChanges = angular.noop; |
350 | 378 |
|
|
375 | 403 |
|
376 | 404 | }; |
377 | 405 |
|
| 406 | + /** |
| 407 | + * @return {boolean} |
| 408 | + */ |
378 | 409 | function shouldColorsWatch() { |
379 | 410 | // Simulate 1x binding and mark mdColorsWatch == false |
380 | 411 | var rawColorExpression = tAttrs.mdColors; |
|
391 | 422 | } |
392 | 423 | } |
393 | 424 | }; |
394 | | - |
395 | 425 | } |
396 | | - |
397 | | - |
398 | 426 | })(); |
0 commit comments