192 lines
9.7 KiB
JavaScript
192 lines
9.7 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('myitsmApp')
|
|
.directive('editPoi', ['events', 'locationModel', '$state', '$timeout', function (events, locationModel, $state, $timeout) {
|
|
return {
|
|
restrict: 'E',
|
|
templateUrl: 'views/work-order/edit-poi.html',
|
|
replace: true,
|
|
scope: {
|
|
ticket: '=',
|
|
isDraft: '=',
|
|
editMode: '=mode',
|
|
updateIsHandledByParent: '='
|
|
},
|
|
link: function (scope) {
|
|
scope.clearField = function (clearAll) {
|
|
if (clearAll) {
|
|
scope.viewData.location = null;
|
|
scope.viewData.poi = null;
|
|
}
|
|
else {
|
|
scope.viewData.poi = null;
|
|
}
|
|
scope.state.pendingChanges = true;
|
|
};
|
|
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.ticket.location = {};
|
|
scope.state.pendingChanges = true;
|
|
};
|
|
scope.handlePOIChange = function () {
|
|
scope.state.pendingChanges = true;
|
|
if (scope.viewData.poi && scope.viewData.poi.id) {
|
|
scope.ticket.location.poiId = scope.viewData.poi.id;
|
|
scope.ticket.location.poiName = scope.viewData.poi.name;
|
|
scope.ticket.location.flooMapId = scope.viewData.poi.floormap ? scope.viewData.poi.floormap.id : '';
|
|
}
|
|
};
|
|
},
|
|
controller: ['$scope', 'ticketModel', 'locationModel', 'googleMapService', function ($scope, ticketModel, locationModel, googleMapService) {
|
|
var ticket = $scope.ticket;
|
|
$scope.showPOIMap = function (poi) {
|
|
if (poi && poi.poiId && googleMapService.isAvailable) {
|
|
$state.go('location', { mapId: poi.floorMapId, id: poi.poiId });
|
|
}
|
|
};
|
|
function handleSaveChanges() {
|
|
$scope.save();
|
|
}
|
|
function applyChanges() {
|
|
var poi = $scope.viewData.poi || {};
|
|
if (poi.id) {
|
|
$scope.ticket.location = {
|
|
poiId: poi.id,
|
|
poiName: poi.name,
|
|
floorMapId: poi.floormap ? poi.floormap.id : ''
|
|
};
|
|
}
|
|
else {
|
|
$scope.ticket.location = null;
|
|
}
|
|
$scope.state.pendingChanges = false;
|
|
if (!$scope.updateIsHandledByParent || $scope.isDraft) {
|
|
$scope.$emit(events.SAVE_CHANGES_COMPLETE);
|
|
}
|
|
}
|
|
function handleDiscardChanges() {
|
|
$scope.cancel();
|
|
}
|
|
/**
|
|
* Collect changes
|
|
* @return {Object} change set
|
|
*/
|
|
function collectChanges() {
|
|
var changes = {};
|
|
if ($scope.state.pendingChanges) {
|
|
var initialPoiId = $scope.initialData.poiId;
|
|
var viewDataPoiId = $scope.viewData.poi && $scope.viewData.poi.id || null;
|
|
if (initialPoiId !== viewDataPoiId) {
|
|
var poi = $scope.viewData.poi || {};
|
|
changes.poiId = poi.id || null;
|
|
}
|
|
}
|
|
return changes;
|
|
}
|
|
/**
|
|
* Save changes
|
|
*/
|
|
$scope.save = function () {
|
|
var changes = collectChanges();
|
|
var isDraft = $scope.isDraft;
|
|
var singleMode = !$scope.updateIsHandledByParent;
|
|
$scope.$emit(events.SAVE_CHANGES_REQUEST, changes, singleMode || isDraft);
|
|
if (_.size(changes)) {
|
|
if (isDraft) {
|
|
applyChanges();
|
|
}
|
|
else {
|
|
if (singleMode) {
|
|
updatePOI(changes)
|
|
.then(function () {
|
|
applyChanges();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
else if (singleMode || isDraft) {
|
|
$scope.$emit(events.SAVE_CHANGES_COMPLETE);
|
|
}
|
|
};
|
|
$scope.cancel = function () {
|
|
$scope.ticket.location = $scope.initialData;
|
|
$scope.state.pendingChanges = false;
|
|
};
|
|
var removeWatcher = $scope.$watch('ticket', function (newData) {
|
|
if (newData) {
|
|
handleToggleEditMode();
|
|
removeWatcher();
|
|
}
|
|
});
|
|
function handleToggleEditMode() {
|
|
$scope.initialData = _.clone($scope.ticket.location) || {};
|
|
$scope.state = {
|
|
pendingChanges: false,
|
|
dataIsLoading: true,
|
|
loadingPOI: false
|
|
};
|
|
$scope.viewData = {
|
|
location: null,
|
|
poi: $scope.initialData.poiName
|
|
};
|
|
if ($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 = _.find(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 updatePOI(changes) {
|
|
return ticketModel.update(ticket.id, ticket.type, changes);
|
|
}
|
|
/**
|
|
* Handle save all changes event
|
|
* @param event
|
|
* @param eventData
|
|
*/
|
|
function handleSaveAllChangesComplete() {
|
|
if ($scope.updateIsHandledByParent && !$scope.isDraft) {
|
|
applyChanges();
|
|
}
|
|
}
|
|
$scope.$on(events.SAVE_CHANGES, handleSaveChanges);
|
|
$scope.$on(events.TOGGLE_EDIT_MODE, handleToggleEditMode);
|
|
$scope.$on(events.DISCARD_CHANGES, handleDiscardChanges);
|
|
$scope.$on(events.SAVE_ALL_CHANGES_COMPLETE, handleSaveAllChangesComplete);
|
|
}]
|
|
};
|
|
}]);
|
|
}());
|