"use strict"; (function () { 'use strict'; /** * @ngdoc object * @name locationModule.locationModel * * @description * This service holds data model with utility methods for location module * It also caches all requested data during user session, to prevent multiple requests for data retrieved * previously * * Additional information on Location instances relation: * On the top of the hierarchy stand Location objects, which generally are offices of the company. * Each Location can have floors information, contained in floormap objects. These objects contain * floor description and reference to floor plan image, which will be used to create tiles for custom google map. * Floormaps can be filled with assets. So last in this hierarchy is Asset object, which can have relation to a Floormap object. */ angular.module('locationModule') .factory('locationModel', ['locationService', 'googleMapService', '$q', function (locationService, googleMapService, $q) { var locationModel = { locationsCache: null, floormapsCache: null, poiByLocationCache: {}, tilesCache: {}, typesCache: {}, typeRequestPromise: $q.when(1), allPOICache: null }; /** * @ngdoc function * @name locationModule.locationModel#getLocationsList * @methodOf locationModule.locationModel * * @description * Retrieves all locations with floormaps related to it. * @returns {HttpPromise} Future object */ locationModel.getLocationsList = function () { if (_.isEmpty(locationModel.locationsCache)) { return locationService.getLocationsBulk().then(function (data) { locationModel.locationsCache = data.locations; locationModel.floormapsCache = _.keyBy(data.floormaps, 'id'); }); } return $q.when(1); }; /** * @ngdoc function * @name locationModule.locationModel#filterLocations * @methodOf locationModule.locationModel * * @param {String} searchText Expression to search for a match on location name or address * @description * Filters locations that match specified search text, with name or address * @returns {HttpPromise} Future object */ locationModel.filterLocations = function (searchText) { return locationModel.getLocationsList() .then(function () { var filteredLocations = _.filter(locationModel.locationsCache, function (location) { var reg = new RegExp(searchText, 'gi'), nameMatch = location.name ? (!!location.name.match(reg)) : false, addressMatch = location.address ? (!!location.address.match(reg)) : false; return (nameMatch || addressMatch); }); return filteredLocations; }); }; /** * @ngdoc function * @name locationModule.locationModel#getLocationByPoiId * @methodOf locationModule.locationModel * @param {String} poiId Asset id used to retrieve, its parent Location data * * @description * Retrieves location that is related to an asset with id provided * @returns {HttpPromise} Future object */ locationModel.getLocationByPoiId = function (poiId) { return locationService.getPOIdetails(poiId, ['locationId']) .then(function (poi) { if (poi) { return locationService.getLocationById(poi.locationId) .then(function (location) { return location; }); } else { return $q.when(null); } }); }; /*locationModel.getAllPOIlist = function(){ if(locationModel.allPOICache && !_.isEmpty(locationModel.allPOICache)){ return $q.when(1); } return locationService.getAllPOIlist().then(function(poiList){ locationModel.allPOICache = poiList; }) };*/ /** * @ngdoc function * @name locationModule.locationModel#getPOItypes * @methodOf locationModule.locationModel * * @description * Retrieves all available asset types, which will be used to distinguish asset * type information(e.g. whether asset is a printer or a Conference room). * @returns {HttpPromise} Future object */ locationModel.getPOItypes = function () { if (!_.isEmpty(locationModel.typesCache)) { return $q.when(1); } locationModel.typeRequestPromise = locationService.getPOItypes() .then(function (types) { locationModel.typesCache = types; }); return locationModel.typeRequestPromise; }; /** * @ngdoc function * @name locationModule.locationModel#getPOItypes * @methodOf locationModule.locationModel * * @description * Retrieves Asset details by its id * @param {String} id Asset unique id to retrieve data for * @returns {HttpPromise} Future object */ locationModel.getPOIdetails = function (id) { locationModel.getPOItypes(); return locationService.getPOIdetails(id).then(function (poi) { return fillPOItypeDetails(poi); }); }; /** * @ngdoc function * @name locationModule.locationModel#getPOIbyLocation * @methodOf locationModule.locationModel * * @description * Retrieves All assets data and their parent floormaps basic info, by location id. * @param {String} id Asset unique id to retrieve data for * @returns {HttpPromise} Future object */ locationModel.getPOIbyLocation = function (locationId) { /*if(locationModel.poiByLocationCache[locationId]){ return $q.when(1); }*/ var typesPromise = locationModel.getPOItypes(); var poiPromise = locationService.getPOIbyLocation(locationId); return $q.all([poiPromise, typesPromise]) .then(function (allResp) { var poiList = allResp[0]; locationModel.poiByLocationCache[locationId] = []; _.each(poiList, function (poi) { poi.floormap = locationModel.floormapsCache ? locationModel.floormapsCache[poi.floorMapId] : {}; poi.type = locationModel.typesCache[poi.assetTypeId]; locationModel.poiByLocationCache[locationId].push(poi); }); }); }; /** * @ngdoc function * @name locationModule.locationModel#filterLocationPOI * @methodOf locationModule.locationModel * * @description * Searches assets, related to specific location, to match search text on name, type or parent floormap's name * @param {String} locationId Location unique id * @param {String} searchText Text to search for a match on asset name or type or asset's parent floormap name * @returns {HttpPromise} Future object */ locationModel.filterLocationPOI = function (locationId, searchText) { return locationModel.getPOIbyLocation(locationId) .then(function () { var reg = new RegExp(searchText, 'gi'), filteredPOI = _.filter(locationModel.poiByLocationCache[locationId], function (poi) { var nameMatch = poi.name ? (!!poi.name.match(reg)) : false, typeMatch = poi.type ? (!!poi.type.name.match(reg)) : false, floorMatch = poi.floormap ? (!!poi.floormap.name.match(reg)) : false; return (nameMatch || typeMatch || floorMatch); }); return filteredPOI; }); }; /*locationModel.searchPOI = function(searchText,typeFilter) { return locationModel.getAllPOIlist().then(function(){ var filteredPOI = typeFilter ? _.filter(locationModel.allPOICache, {assetTypeId: typeFilter}) : locationModel.allPOICache; if(filteredPOI && searchText){ var reg = new RegExp(searchText, 'gi'); filteredPOI = _.filter(filteredPOI, function(poi){ var nameMatch = poi.name ? (!!poi.name.match(reg)) : false, typeMatch = typeFilter ? true : (!!poi.type.name.match(reg)), floorMatch = poi.floormap.name ? (!!poi.floormap.name.match(reg)) : false, locationMatch = poi.location.name ? (!!poi.location.name.match(reg)) : false; return (nameMatch || typeMatch || floorMatch || locationMatch); }); } return filteredPOI; }); };*/ /** * @ngdoc function * @name locationModule.locationModel#getMapTiles * @methodOf locationModule.locationModel * * @description * Retrieves map tiles, used to generate floor map view, by specified child asset details * @param {Object} poi Asset data object, used to retrieve parent floormap id * @returns {HttpPromise} Future object */ locationModel.getMapTiles = function (poi) { if (locationModel.tilesCache[poi.id]) { return $q.when(locationModel.tilesCache[poi.id]); } return locationService.getMapTiles(poi.floorMapId) .then(function (allTiles) { locationModel.tilesCache[poi.id] = allTiles; }); }; /** * @ngdoc function * @name locationModule.locationModel#initLocationMap * @methodOf locationModule.locationModel * * @description * Initiates custom google map on specific DOM, with custom tiles used and viewport * limited to floormap image bounds on the lowest level allowed. As a result user gets * well known interface of google maps, but it is showing floor plan and available * assets on this floor. * @param {Node} mapDOM Node to use for google map initialization * @param {Object} poi Asset data object, including tiles data of the floormap to render * @returns {HttpPromise} Future object */ locationModel.initLocationMap = function (mapNode, poi) { var settings = { mapContainer: mapNode, tiles: poi.tiles }; return googleMapService.initFloorMap(settings) .then(function (mapNode) { return mapNode; }); }; /** * @ngdoc function * @name locationModule.locationModel#addLocationMarker * @methodOf locationModule.locationModel * @requires myitsmApp.googleMapService * @see myitsmApp.googleMapService#panAssetOnMap * @param {Object.} mapData Custom google map instance, on which asset should be marked * @param {Object} poi Asset details object, which should be marked on the map * @returns {Object} Asset object, which will include asset marker information */ locationModel.addLocationMarker = function (mapData, poi) { return googleMapService.panAssetOnMap(mapData.floormap, poi); }; /** * @ngdoc function * @name locationModule.locationModel#fillPOItypeDetails * @methodOf locationModule.locationModel * @param {Object} poi Fills asset type property of an asset data object with asset type data, by assetTypeId property of asset data * @returns {Object} Asset object with type property filled. */ function fillPOItypeDetails(poi) { if (locationModel.typesCache[poi.assetTypeId]) { poi.type = locationModel.typesCache[poi.assetTypeId]; } else { locationModel.typeRequestPromise.then(function () { poi.type = locationModel.typesCache[poi.assetTypeId]; }); } return poi; } return locationModel; } ]); })();