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

Commit 4178459

Browse files
committed
fix(panel): don't throw exceptions when the groupName is a string
- the APIs take a string or Array, we need to handle that w/o exceptions - fix invalid JSDoc/Closure syntax - remove duplicate header comments
1 parent 7157b3b commit 4178459

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

src/components/panel/panel.js

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ angular
218218
* @description
219219
* Creates a panel with the specified options.
220220
*
221-
* @param config {!Object=} Specific configuration object that may contain the
221+
* @param {!Object=} config Specific configuration object that may contain the
222222
* following properties:
223223
*
224224
* - `id` - `{string=}`: An ID to track the panel by. When an ID is provided,
@@ -348,25 +348,6 @@ angular
348348
* @returns {!MdPanelAnimation} panelAnimation
349349
*/
350350

351-
/**
352-
* @ngdoc method
353-
* @name $mdPanel#newPanelGroup
354-
* @description
355-
* Creates a panel group and adds it to a tracked list of panel groups.
356-
*
357-
* @param {string} groupName Name of the group to create.
358-
* @param {!Object=} config Specific configuration object that may contain the
359-
* following properties:
360-
*
361-
* - `maxOpen` - `{number=}`: The maximum number of panels that are allowed to
362-
* be open within a defined panel group.
363-
*
364-
* @returns {!Object<string,
365-
* {panels: !Array<!MdPanelRef>,
366-
* openPanels: !Array<!MdPanelRef>,
367-
* maxOpen: number}>} panelGroup
368-
*/
369-
370351
/**
371352
* @ngdoc method
372353
* @name $mdPanel#setGroupMaxOpen
@@ -1018,6 +999,16 @@ function $getProvider() {
1018999
];
10191000
}
10201001

1002+
/**
1003+
* @param {string|[]} value
1004+
* @returns {[]} the input string wrapped in an Array or the original Array
1005+
*/
1006+
function coerceToArray(value) {
1007+
if (angular.isString(value)) {
1008+
value = [value];
1009+
}
1010+
return value;
1011+
}
10211012

10221013
/*****************************************************************************
10231014
* MdPanel Service *
@@ -1171,9 +1162,7 @@ MdPanelService.prototype.create = function(preset, config) {
11711162

11721163
// Add the panel to each of its requested groups.
11731164
if (this._config.groupName) {
1174-
if (angular.isString(this._config.groupName)) {
1175-
this._config.groupName = [this._config.groupName];
1176-
}
1165+
this._config.groupName = coerceToArray(this._config.groupName);
11771166
angular.forEach(this._config.groupName, function(group) {
11781167
panelRef.addToGroup(group);
11791168
});
@@ -1236,28 +1225,27 @@ MdPanelService.prototype.newPanelAnimation = function() {
12361225

12371226

12381227
/**
1228+
* @ngdoc method
1229+
* @name $mdPanel#newPanelGroup
1230+
* @description
12391231
* Creates a panel group and adds it to a tracked list of panel groups.
1240-
* @param groupName {string} Name of the group to create.
1241-
* @param config {!Object=} Specific configuration object that may contain the
1242-
* following properties:
1232+
* @param {string} groupName Name of the group to create.
1233+
* @param {{maxOpen: number}=} config Configuration object that may contain the following
1234+
* properties:
12431235
*
1244-
* - `maxOpen` - `{number=}`: The maximum number of panels that are allowed
1245-
* open within a defined panel group.
1236+
* - `maxOpen`: The maximum number of panels that are allowed open within a defined panel group.
12461237
*
1247-
* @returns {!Object<string,
1248-
* {panels: !Array<!MdPanelRef>,
1249-
* openPanels: !Array<!MdPanelRef>,
1250-
* maxOpen: number}>} panelGroup
1238+
* @returns {!{panels: !Array<!MdPanelRef>, openPanels: !Array<!MdPanelRef>, maxOpen: number}}
1239+
* the new panel group
12511240
*/
12521241
MdPanelService.prototype.newPanelGroup = function(groupName, config) {
12531242
if (!this._groups[groupName]) {
12541243
config = config || {};
1255-
var group = {
1244+
this._groups[groupName] = {
12561245
panels: [],
12571246
openPanels: [],
12581247
maxOpen: config.maxOpen > 0 ? config.maxOpen : Infinity
12591248
};
1260-
this._groups[groupName] = group;
12611249
}
12621250
return this._groups[groupName];
12631251
};
@@ -1301,7 +1289,10 @@ MdPanelService.prototype._openCountExceedsMaxOpen = function(groupName) {
13011289
* @private
13021290
*/
13031291
MdPanelService.prototype._closeFirstOpenedPanel = function(groupName) {
1304-
this._groups[groupName].openPanels[0].close();
1292+
var group = this._groups[groupName];
1293+
if (group && group.openPanels.length) {
1294+
group.openPanels[0].close();
1295+
}
13051296
};
13061297

13071298

@@ -1483,6 +1474,7 @@ MdPanelRef.prototype.open = function() {
14831474
var show = self._simpleBind(self.show, self);
14841475
var checkGroupMaxOpen = function() {
14851476
if (self.config.groupName) {
1477+
self.config.groupName = coerceToArray(self.config.groupName);
14861478
angular.forEach(self.config.groupName, function(group) {
14871479
if (self._$mdPanel._openCountExceedsMaxOpen(group)) {
14881480
self._$mdPanel._closeFirstOpenedPanel(group);
@@ -1621,6 +1613,7 @@ MdPanelRef.prototype.detach = function() {
16211613
MdPanelRef.prototype.destroy = function() {
16221614
var self = this;
16231615
if (this.config.groupName) {
1616+
this.config.groupName = coerceToArray(this.config.groupName);
16241617
angular.forEach(this.config.groupName, function(group) {
16251618
self.removeFromGroup(group);
16261619
});
@@ -1662,8 +1655,12 @@ MdPanelRef.prototype.show = function() {
16621655
var onOpenComplete = self.config['onOpenComplete'] || angular.noop;
16631656
var addToGroupOpen = function() {
16641657
if (self.config.groupName) {
1658+
self.config.groupName = coerceToArray(self.config.groupName);
16651659
angular.forEach(self.config.groupName, function(group) {
1666-
self._$mdPanel._groups[group].openPanels.push(self);
1660+
group = self._$mdPanel._groups[group];
1661+
if (group) {
1662+
group.openPanels.push(self);
1663+
}
16671664
});
16681665
}
16691666
};
@@ -1706,6 +1703,7 @@ MdPanelRef.prototype.hide = function() {
17061703
var removeFromGroupOpen = function() {
17071704
if (self.config.groupName) {
17081705
var index;
1706+
self.config.groupName = coerceToArray(self.config.groupName);
17091707
angular.forEach(self.config.groupName, function(group) {
17101708
group = self._$mdPanel._groups[group];
17111709
index = group.openPanels.indexOf(self);
@@ -1734,7 +1732,6 @@ MdPanelRef.prototype.hide = function() {
17341732
});
17351733
};
17361734

1737-
17381735
/**
17391736
* Add a class to the panel. DO NOT use this to hide/show the panel.
17401737
* @deprecated

0 commit comments

Comments
 (0)