2016-12-01 11:40:28 +02:00
|
|
|
/*
|
2020-01-06 16:52:41 +02:00
|
|
|
* Copyright © 2016-2020 The Thingsboard Authors
|
2016-12-01 11:40:28 +02:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
export default angular.module('thingsboard.api.componentDescriptor', [])
|
|
|
|
|
.factory('componentDescriptorService', ComponentDescriptorService).name;
|
|
|
|
|
|
|
|
|
|
/*@ngInject*/
|
|
|
|
|
function ComponentDescriptorService($http, $q) {
|
|
|
|
|
|
|
|
|
|
var componentsByType = {};
|
|
|
|
|
var componentsByClazz = {};
|
|
|
|
|
|
|
|
|
|
var service = {
|
2018-03-21 17:34:07 +02:00
|
|
|
getComponentDescriptorsByTypes: getComponentDescriptorsByTypes
|
2016-12-01 11:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return service;
|
|
|
|
|
|
2020-03-10 18:45:35 +02:00
|
|
|
function getComponentDescriptorsByTypes(componentTypes, ruleChainType) {
|
2018-03-21 17:34:07 +02:00
|
|
|
var deferred = $q.defer();
|
|
|
|
|
var result = [];
|
2020-03-10 18:45:35 +02:00
|
|
|
if (!componentsByType[ruleChainType]) {
|
|
|
|
|
componentsByType[ruleChainType] = {};
|
|
|
|
|
}
|
2018-03-21 17:34:07 +02:00
|
|
|
for (var i=componentTypes.length-1;i>=0;i--) {
|
|
|
|
|
var componentType = componentTypes[i];
|
2020-03-10 18:45:35 +02:00
|
|
|
if (componentsByType[ruleChainType][componentType]) {
|
|
|
|
|
result = result.concat(componentsByType[ruleChainType][componentType]);
|
2018-03-21 17:34:07 +02:00
|
|
|
componentTypes.splice(i, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!componentTypes.length) {
|
|
|
|
|
deferred.resolve(result);
|
|
|
|
|
} else {
|
2020-06-19 16:46:33 +03:00
|
|
|
var url = '/api/components?componentTypes=' + componentTypes.join(',') + '&ruleChainType=' + ruleChainType;
|
2018-03-21 17:34:07 +02:00
|
|
|
$http.get(url, null).then(function success(response) {
|
|
|
|
|
var components = response.data;
|
|
|
|
|
for (var i = 0; i < components.length; i++) {
|
|
|
|
|
var component = components[i];
|
2020-03-10 18:45:35 +02:00
|
|
|
var componentsList = componentsByType[ruleChainType][component.type];
|
2018-03-21 17:34:07 +02:00
|
|
|
if (!componentsList) {
|
|
|
|
|
componentsList = [];
|
2020-03-10 18:45:35 +02:00
|
|
|
componentsByType[ruleChainType][component.type] = componentsList;
|
2018-03-21 17:34:07 +02:00
|
|
|
}
|
|
|
|
|
componentsList.push(component);
|
|
|
|
|
componentsByClazz[component.clazz] = component;
|
|
|
|
|
}
|
|
|
|
|
result = result.concat(components);
|
|
|
|
|
deferred.resolve(components);
|
|
|
|
|
}, function fail() {
|
|
|
|
|
deferred.reject();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return deferred.promise;
|
|
|
|
|
}
|
2016-12-01 11:40:28 +02:00
|
|
|
}
|