"use strict"; (function () { 'use strict'; angular.module('customWidgetsModule').directive('poiLocation', ['customFieldLinkFunction', 'ticketModel', 'locationModel', 'googleMapService', '$state', '$timeout', 'events', function (customFieldLinkFunction, ticketModel, locationModel, googleMapService, $state, $timeout, events) { return { restrict: 'E', replace: true, scope: { data: '=', isEditable: '=' }, templateUrl: 'views/custom-widgets/poi-location.html', link: function (scope) { customFieldLinkFunction(scope); scope.$watch('data.setValueFlag', function (value) { if (value !== '#$#' && value) { scope.data.setValueFlag = '#$#'; } }); scope.clearField = function (clearAll) { if (clearAll) { scope.viewData.location = null; scope.viewData.poi = null; } else { scope.viewData.poi = null; } scope.data.value = null; }; scope.filterLocationsByCriteria = function (term) { return locationModel.filterLocations(term).then(function (locations) { return locations; }); }; scope.filterPOIbyCriteria = function (term) { scope.state.loadingPOI = true; return locationModel.filterLocationPOI(scope.viewData.location.id, term).then(function (poiList) { return poiList; }).finally(function () { scope.state.loadingPOI = false; }); }; scope.handleLocationChange = function () { scope.viewData.poi = null; scope.data.value = null; }; scope.handlePOIChange = function () { if (scope.viewData.poi && scope.viewData.poi.id) { scope.data.value = { poiId: scope.viewData.poi.id, poiName: scope.viewData.poi.name, flooMapId: scope.viewData.poi.floormap ? scope.viewData.poi.floormap.id : '' }; } }; scope.showPOIMap = function (poi) { if (poi && poi.poiId && googleMapService.isAvailable) { $state.go('location', { mapId: poi.floorMapId, id: poi.poiId }); } }; function handleToggleEditMode() { scope.initialData = _.cloneDeep(scope.data.value); scope.state = { dataIsLoading: true, loadingLocations: false, loadingPOI: false }; scope.viewData = { location: null, poi: scope.initialData ? scope.initialData.poiName : null }; if (scope.initialData && scope.initialData.poiId) { locationModel.getLocationByPoiId(scope.initialData.poiId) .then(function (location) { scope.state.dataIsLoading = false; scope.viewData.location = location; if (!scope.viewData.poi) { var assetsByLocation = locationModel.poiByLocationCache[location.id] || []; var assetData = _.findWhere(assetsByLocation, { id: scope.initialData.poiId }); if (assetData) { $timeout(function () { scope.viewData.poi = { name: assetData.name, id: assetData.id, floormap: { id: assetData.floorMapId } }; }); } } locationModel.getLocationsList(); }); } else { scope.state.dataIsLoading = false; } } function handleDiscardChanges() { scope.data.value = _.cloneDeep(scope.initialData); } handleToggleEditMode(); scope.$on(events.DISCARD_CHANGES, handleDiscardChanges); scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode); } }; }]); }());