Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(panel): don't throw exceptions when the groupName is a string
Browse files Browse the repository at this point in the history
- the APIs take a string or Array, we need to handle that w/o exceptions
- fix invalid JSDoc/Closure syntax
- remove duplicate header comments
  • Loading branch information
Splaktar committed Jul 15, 2020
1 parent 7157b3b commit 4178459
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions src/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ angular
* @description
* Creates a panel with the specified options.
*
* @param config {!Object=} Specific configuration object that may contain the
* @param {!Object=} config Specific configuration object that may contain the
* following properties:
*
* - `id` - `{string=}`: An ID to track the panel by. When an ID is provided,
Expand Down Expand Up @@ -348,25 +348,6 @@ angular
* @returns {!MdPanelAnimation} panelAnimation
*/

/**
* @ngdoc method
* @name $mdPanel#newPanelGroup
* @description
* Creates a panel group and adds it to a tracked list of panel groups.
*
* @param {string} groupName Name of the group to create.
* @param {!Object=} config Specific configuration object that may contain the
* following properties:
*
* - `maxOpen` - `{number=}`: The maximum number of panels that are allowed to
* be open within a defined panel group.
*
* @returns {!Object<string,
* {panels: !Array<!MdPanelRef>,
* openPanels: !Array<!MdPanelRef>,
* maxOpen: number}>} panelGroup
*/

/**
* @ngdoc method
* @name $mdPanel#setGroupMaxOpen
Expand Down Expand Up @@ -1018,6 +999,16 @@ function $getProvider() {
];
}

/**
* @param {string|[]} value
* @returns {[]} the input string wrapped in an Array or the original Array
*/
function coerceToArray(value) {
if (angular.isString(value)) {
value = [value];
}
return value;
}

/*****************************************************************************
* MdPanel Service *
Expand Down Expand Up @@ -1171,9 +1162,7 @@ MdPanelService.prototype.create = function(preset, config) {

// Add the panel to each of its requested groups.
if (this._config.groupName) {
if (angular.isString(this._config.groupName)) {
this._config.groupName = [this._config.groupName];
}
this._config.groupName = coerceToArray(this._config.groupName);
angular.forEach(this._config.groupName, function(group) {
panelRef.addToGroup(group);
});
Expand Down Expand Up @@ -1236,28 +1225,27 @@ MdPanelService.prototype.newPanelAnimation = function() {


/**
* @ngdoc method
* @name $mdPanel#newPanelGroup
* @description
* Creates a panel group and adds it to a tracked list of panel groups.
* @param groupName {string} Name of the group to create.
* @param config {!Object=} Specific configuration object that may contain the
* following properties:
* @param {string} groupName Name of the group to create.
* @param {{maxOpen: number}=} config Configuration object that may contain the following
* properties:
*
* - `maxOpen` - `{number=}`: The maximum number of panels that are allowed
* open within a defined panel group.
* - `maxOpen`: The maximum number of panels that are allowed open within a defined panel group.
*
* @returns {!Object<string,
* {panels: !Array<!MdPanelRef>,
* openPanels: !Array<!MdPanelRef>,
* maxOpen: number}>} panelGroup
* @returns {!{panels: !Array<!MdPanelRef>, openPanels: !Array<!MdPanelRef>, maxOpen: number}}
* the new panel group
*/
MdPanelService.prototype.newPanelGroup = function(groupName, config) {
if (!this._groups[groupName]) {
config = config || {};
var group = {
this._groups[groupName] = {
panels: [],
openPanels: [],
maxOpen: config.maxOpen > 0 ? config.maxOpen : Infinity
};
this._groups[groupName] = group;
}
return this._groups[groupName];
};
Expand Down Expand Up @@ -1301,7 +1289,10 @@ MdPanelService.prototype._openCountExceedsMaxOpen = function(groupName) {
* @private
*/
MdPanelService.prototype._closeFirstOpenedPanel = function(groupName) {
this._groups[groupName].openPanels[0].close();
var group = this._groups[groupName];
if (group && group.openPanels.length) {
group.openPanels[0].close();
}
};


Expand Down Expand Up @@ -1483,6 +1474,7 @@ MdPanelRef.prototype.open = function() {
var show = self._simpleBind(self.show, self);
var checkGroupMaxOpen = function() {
if (self.config.groupName) {
self.config.groupName = coerceToArray(self.config.groupName);
angular.forEach(self.config.groupName, function(group) {
if (self._$mdPanel._openCountExceedsMaxOpen(group)) {
self._$mdPanel._closeFirstOpenedPanel(group);
Expand Down Expand Up @@ -1621,6 +1613,7 @@ MdPanelRef.prototype.detach = function() {
MdPanelRef.prototype.destroy = function() {
var self = this;
if (this.config.groupName) {
this.config.groupName = coerceToArray(this.config.groupName);
angular.forEach(this.config.groupName, function(group) {
self.removeFromGroup(group);
});
Expand Down Expand Up @@ -1662,8 +1655,12 @@ MdPanelRef.prototype.show = function() {
var onOpenComplete = self.config['onOpenComplete'] || angular.noop;
var addToGroupOpen = function() {
if (self.config.groupName) {
self.config.groupName = coerceToArray(self.config.groupName);
angular.forEach(self.config.groupName, function(group) {
self._$mdPanel._groups[group].openPanels.push(self);
group = self._$mdPanel._groups[group];
if (group) {
group.openPanels.push(self);
}
});
}
};
Expand Down Expand Up @@ -1706,6 +1703,7 @@ MdPanelRef.prototype.hide = function() {
var removeFromGroupOpen = function() {
if (self.config.groupName) {
var index;
self.config.groupName = coerceToArray(self.config.groupName);
angular.forEach(self.config.groupName, function(group) {
group = self._$mdPanel._groups[group];
index = group.openPanels.indexOf(self);
Expand Down Expand Up @@ -1734,7 +1732,6 @@ MdPanelRef.prototype.hide = function() {
});
};


/**
* Add a class to the panel. DO NOT use this to hide/show the panel.
* @deprecated
Expand Down

0 comments on commit 4178459

Please sign in to comment.