Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 5cdb33f

Browse files
committed
refactor(colors): clean up Closure / JSDoc comments and types
Relates to #11673
1 parent ec9aa25 commit 5cdb33f

File tree

2 files changed

+64
-36
lines changed

2 files changed

+64
-36
lines changed

src/components/colors/colors.js

+61-33
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
* @module material.components.colors
2727
*
2828
* @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.
3233
*
3334
* @usage
3435
* <hljs lang="js">
@@ -61,9 +62,9 @@
6162
* Gets a color json object, keys are css properties and values are string of the wanted color
6263
* Then calculate the rgba() values based on the theme color parts
6364
*
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'}`.
6768
*
6869
* @usage
6970
* <hljs lang="js">
@@ -86,7 +87,6 @@
8687
} catch (e) {
8788
$log.error(e.message);
8889
}
89-
9090
}
9191

9292
/**
@@ -116,9 +116,10 @@
116116

117117
/**
118118
* 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
122123
*/
123124
function parseColor(color, contrast) {
124125
contrast = contrast || false;
@@ -134,10 +135,9 @@
134135
/**
135136
* Convert the color expression into an object with scope-interpolated values
136137
* 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
141141
*/
142142
function interpolateColors(themeColors) {
143143
var rgbColors = {};
@@ -159,16 +159,22 @@
159159

160160
/**
161161
* 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.
165168
*/
166169
function hasTheme(expression) {
167170
return angular.isDefined($mdTheming.THEMES[expression.split('-')[0]]);
168171
}
169172

170173
/**
171174
* 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)}}
172178
*/
173179
function extractColorOptions(expression) {
174180
var parts = expression.split('-');
@@ -185,6 +191,9 @@
185191

186192
/**
187193
* Calculate the theme palette name
194+
* @param {Array} parts
195+
* @param {string} theme name
196+
* @return {string}
188197
*/
189198
function extractPalette(parts, theme) {
190199
// If the next section is one of the palettes we assume it's a two word palette
@@ -199,27 +208,38 @@
199208
// If the palette is not in the palette list it's one of primary/accent/warn/background
200209
var scheme = $mdTheming.THEMES[theme].colors[palette];
201210
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}));
203214
}
204215
palette = scheme.name;
205216
}
206217

207218
return palette;
208219
}
209220

221+
/**
222+
* @param {Array} parts
223+
* @param {string} theme name
224+
* @return {*}
225+
*/
210226
function extractHue(parts, theme) {
211227
var themeColors = $mdTheming.THEMES[theme].colors;
212228

213229
if (parts[1] === 'hue') {
214230
var hueNumber = parseInt(parts.splice(2, 1)[0], 10);
215231

216232
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}));
218236
}
219237
parts[1] = 'hue-' + hueNumber;
220238

221239
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+
{
223243
availableThemes: Object.keys(themeColors).join(', '),
224244
usedTheme: parts[0]
225245
}));
@@ -261,14 +281,14 @@
261281
* </div>
262282
* </hljs>
263283
*
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+
*
267288
* <hljs lang="html">
268289
* <md-card md-colors="::{background: '{{theme}}-primary-700'}">
269290
* </md-card>
270291
* </hljs>
271-
*
272292
*/
273293
function MdColorsDirective($mdColors, $mdUtil, $log, $parse) {
274294
return {
@@ -282,6 +302,10 @@
282302

283303
var lastColors = {};
284304

305+
/**
306+
* @param {string=} theme
307+
* @return {Object} colors found in the specified theme
308+
*/
285309
var parseColors = function (theme) {
286310
if (typeof theme !== 'string') {
287311
theme = '';
@@ -298,10 +322,10 @@
298322
var colors = $parse(attrs.mdColors)(scope);
299323

300324
/**
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.
303327
*
304-
* # example
328+
* @example
305329
* <hljs lang="html">
306330
* <div md-theme="myTheme">
307331
* <div md-colors="{background: 'primary-600'}">
@@ -310,8 +334,8 @@
310334
* </div>
311335
* </hljs>
312336
*
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.
315339
*/
316340
if (mdThemeController) {
317341
Object.keys(colors).forEach(function (prop) {
@@ -327,6 +351,9 @@
327351
return colors;
328352
};
329353

354+
/**
355+
* @param {Object} colors
356+
*/
330357
var cleanElement = function (colors) {
331358
if (!angular.equals(colors, lastColors)) {
332359
var keys = Object.keys(lastColors);
@@ -344,7 +371,8 @@
344371
};
345372

346373
/**
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.
348376
*/
349377
var unregisterChanges = angular.noop;
350378

@@ -375,6 +403,9 @@
375403

376404
};
377405

406+
/**
407+
* @return {boolean}
408+
*/
378409
function shouldColorsWatch() {
379410
// Simulate 1x binding and mark mdColorsWatch == false
380411
var rawColorExpression = tAttrs.mdColors;
@@ -391,8 +422,5 @@
391422
}
392423
}
393424
};
394-
395425
}
396-
397-
398426
})();

src/core/util/color.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function ColorUtilFactory() {
3030

3131
/**
3232
* Converts rgba value to hex string
33-
* @param color {string}
33+
* @param {string} color
3434
* @returns {string}
3535
*/
3636
function rgbaToHex(color) {
@@ -46,7 +46,7 @@ function ColorUtilFactory() {
4646

4747
/**
4848
* Converts an RGB color to RGBA
49-
* @param color {string}
49+
* @param {string} color
5050
* @returns {string}
5151
*/
5252
function rgbToRgba (color) {
@@ -55,7 +55,7 @@ function ColorUtilFactory() {
5555

5656
/**
5757
* Converts an RGBA color to RGB
58-
* @param color {string}
58+
* @param {string} color
5959
* @returns {string}
6060
*/
6161
function rgbaToRgb (color) {

0 commit comments

Comments
 (0)