SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/location/location-service.js

226 lines
9.9 KiB
JavaScript

"use strict";
(function () {
'use strict';
/**
* @ngdoc object
* @name locationModule.locationService
*
* @description
* This service performs all server requests related to locationModule and normalizes data returned from server,
* before returning it to locationModel
*
*/
angular.module('locationModule')
.service('locationService', ['$resource', '$q', function ($resource, $q) {
var resource = $resource('/smartit/rest/v2/:type/:action:itemId', {}, {
getLocation: {
method: 'GET',
isArray: true,
params: {
type: 'location'
}
},
getLocationsBulk: {
method: 'GET',
isArray: true,
params: {
type: 'bulk',
action: 'location',
include: 'floormap[id,name,locationId]',
fields: 'id,name,address',
floor_map_count: true,
floor_map_with_images_only: true
}
},
getPOIbyLocation: {
url: '/smartit/rest/search',
method: 'POST',
isArray: true,
transformRequest: function (data) {
return JSON.stringify({
'queryName': 'MYIT_FLOOR_MAP_ASSETS_BY_LOCATION_ID',
'queryParameters': [{ 'name': 'locationId', 'value': data.id }],
'attributes': {
'LocationFloorMapAsset': ['id', 'floorMapId', 'name', 'assetTypeId', 'assetStatus',
'desc', 'xPos', 'yPos', 'tag']
}
});
}
},
getMapTiles: {
method: 'GET',
isArray: true,
params: {
type: 'image_tile'
}
},
getPOItypes: {
url: '/smartit/rest/search',
method: 'POST',
isArray: true
},
getPOIById: {
url: '/smartit/rest/search',
method: 'POST',
isArray: true,
transformRequest: function (data) {
return JSON.stringify({
'queryName': 'MYIT_FLOOR_MAP_ASSET_GET_BY_ID',
'queryParameters': [{ 'name': 'id', 'value': data.id }],
'attributes': {
'LocationFloorMapAsset': (data.fields || ['id', 'floorMapId', 'name', 'assetTypeId', 'assetStatus', 'xPos', 'yPos', 'locationId'])
}
});
},
transformResponse: function (data) {
var resp = data ? JSON.parse(data) : [], assetData = resp[0] ? resp[0].items : [];
return assetData;
}
}
});
/**
* @ngdoc function
* @name locationModule.locationService#getLocationsBulk
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve all locations with floormaps related to it. Performs data normalization for response
* @returns {HttpPromise} Future object
*/
this.getLocationsBulk = function () {
return resource.getLocationsBulk().$promise
.then(function (data) {
var locations = [], locationData = _.find(data, { dataSourceName: 'location' }).items, floormapData = _.find(data, { dataSourceName: 'floormap' }).items;
if (locationData.length) {
locations = _.filter(locationData, function (location) {
return !!location.floorMapCount;
});
}
return { locations: locations, floormaps: floormapData };
});
};
/**
* @ngdoc function
* @name locationModule.locationService#getLocationById
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve location by id
* @param {String} locationId Unique id of the location item
* @returns {HttpPromise} Future object
*/
this.getLocationById = function (locationId) {
return resource.getLocation({ itemId: locationId }, { fields: 'id,name,address' }).$promise
.then(function (data) {
return data[0].items ? data[0].items[0] : {};
});
};
/**
* @ngdoc function
* @name locationModule.locationService#getPOItypes
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve all asset types
* @returns {HttpPromise} Future object
*/
this.getPOItypes = function () {
return resource.getPOItypes({ queryName: 'MYIT_ALL_ASSETS_TYPE_QUERY' }).$promise
.then(function (resp) {
var types = resp[0].items;
if (types) {
types = _.keyBy(types, 'id');
}
return types;
});
};
/**
* @ngdoc function
* @name locationModule.locationService#getPOIbyLocation
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve asset list related to a location by locationId
* @param {String} locationId Unique id of the location item
* @returns {HttpPromise} Future object
*/
this.getPOIbyLocation = function (locationId) {
return resource.getPOIbyLocation({ id: locationId }).$promise
.then(function (data) {
return data[0].items;
});
};
/**
* @ngdoc function
* @name locationModule.locationService#getPOIdetails
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve asset details, by its id. Allows to control fields list, included in server response
* @param {String} id Unique id of the asset to retrieve details for
* @returns {HttpPromise} Future object
*/
this.getPOIdetails = function (id, fields) {
return resource.getPOIById({ id: id, fields: fields }).$promise
.then(function (res) {
return res[0];
});
};
/**
* @ngdoc function
* @name locationModule.locationService#getPOIdetails
* @methodOf locationModule.locationService
*
* @description
* Executes server request to retrieve floormap tiles, to use for custom gmap initialization
* @param {String} id Unique id of the floormap, to retrieve tiles for
* @returns {HttpPromise} Future object
*/
this.getMapTiles = function (id) {
var allPromises = [];
for (var i = 0, l = LocationVO.SCALE_LEVELS.length; i < l; i++) {
var promise = resource.getMapTiles({
scale: LocationVO.SCALE_LEVELS[i],
floor_map_id: id
}).$promise;
allPromises.push(promise);
}
return $q.all(allPromises)
.then(function (allData) {
var floormapTiles = {};
_.each(allData, function (data) {
processTilesData(data, floormapTiles);
});
return floormapTiles;
});
};
/**
* @ngdoc function
* @name locationModule.locationService#processTilesData
* @methodOf locationModule.locationService
*
* @description
* Creates
* @param {Array} data Array of tile items returned from server. Server generally returns array with one item, that hold object inside it,
* where response details is specified. All results should be contained under items property of response details object
* and it should be an array of tile items
* @returns {Object} Normalized data object collection of floormap tiles.
* All tiles are grouped by zoom size and each group will be used only with specific zoom value.
* Each item in group, has key value formed by combining tile column and row values.
* It is done to simplify process of searching corresponding tile for custom google map grid item using
* column and row value of particular item.
*
*/
function processTilesData(data, allTiles) {
var tiles = data[0].items || [];
for (var i = 0, l = tiles.length; i < l; i++) {
var tile = tiles[i], scale = tile.scale;
allTiles[scale] ? angular.noop() : allTiles[scale] = {};
allTiles[scale][tile.column + '_' + tile.row] = tile;
}
return allTiles;
}
}
]);
})();