blob: 4e8ad6f532c7a379a178e136c40f491ef8807d1f [file] [log] [blame]
Yoshie Muranakae4194ce2019-05-24 14:33:56 -05001window.angular && (function(angular) {
2 'use strict';
3
4 angular.module('app.common.directives').directive('remoteLoggingServer', [
5 'APIUtils',
6 function(APIUtils) {
7 return {
8 'restrict': 'E', 'template': require('./remote-logging-server.html'),
9 'controller': [
10 '$scope', '$uibModal', 'toastService',
11 function($scope, $uibModal, toastService) {
12 const modalActions = {
13 ADD: 0,
14 EDIT: 1,
15 REMOVE: 2,
16 properties: {
17 0: {
18 title: 'Add remote logging server',
19 actionLabel: 'Add',
20 successMessage: 'Connected to remote logging server.',
21 errorMessage: 'Unable to connect to server.'
22 },
23 1: {
24 title: 'Edit remote logging server',
25 actionLabel: 'Save',
26 successMessage: 'Connected to remote logging server.',
27 errorMessage: 'Unable to save remote logging server.'
28 },
29 2: {
30 title: 'Remove remote logging server',
31 actionLabel: 'Remove',
32 successMessage: 'Remote logging server removed.',
33 errorMessage: 'Unable to remove remote logging server.'
34 }
35 }
36 };
37
38 const modalTemplate =
39 require('./remote-logging-server-modal.html');
40
41 $scope.activeModal;
42 $scope.activeModalProps;
43
44 $scope.remoteServer;
45 $scope.remoteServerForm;
46 $scope.loadError = true;
47
48 $scope.initModal = (type) => {
49 if (type === undefined) {
50 return;
51 }
52 $scope.activeModal = type;
53 $scope.activeModalProps = modalActions.properties[type];
54
55 $uibModal
56 .open({
57 template: modalTemplate,
58 windowTopClass: 'uib-modal',
59 scope: $scope,
60 ariaLabelledBy: 'dialog_label'
61 })
62 .result
63 .then((action) => {
64 switch (action) {
65 case modalActions.ADD:
66 addServer();
67 break;
68 case modalActions.EDIT:
69 editServer();
70 break;
71 case modalActions.REMOVE:
72 removeServer();
73 break;
74 default:
75 setFormValues();
76 }
77 })
78 .catch(() => {
79 // reset form when modal overlay clicked
80 // and modal closes
81 setFormValues();
82 })
83 };
84
85 const addServer = () => {
86 $scope.loading = true;
87 APIUtils.setRemoteLoggingServer($scope.remoteServerForm)
88 .then(() => {
89 $scope.loading = false;
90 $scope.remoteServer = {...$scope.remoteServerForm};
91 toastService.success(
92 $scope.activeModalProps.successMessage);
93 })
94 .catch(() => {
95 $scope.loading = false;
96 $scope.remoteServer = undefined;
97 setFormValues();
98 toastService.error(
99 $scope.activeModalProps.errorMessage);
100 })
101 };
102
103 const editServer = () => {
104 $scope.loading = true;
105 APIUtils.updateRemoteLoggingServer($scope.remoteServerForm)
106 .then(() => {
107 $scope.loading = false;
108 $scope.remoteServer = {...$scope.remoteServerForm};
109 toastService.success(
110 $scope.activeModalProps.successMessage);
111 })
112 .catch(() => {
113 $scope.loading = false;
114 setFormValues();
115 toastService.error(
116 $scope.activeModalProps.errorMessage);
117 })
118 };
119
120 const removeServer = () => {
121 $scope.loading = true;
122 APIUtils.disableRemoteLoggingServer()
123 .then(() => {
124 $scope.loading = false;
125 $scope.remoteServer = undefined;
126 setFormValues();
127 toastService.success(
128 $scope.activeModalProps.successMessage);
129 })
130 .catch(() => {
131 $scope.loading = false;
132 toastService.error(
133 $scope.activeModalProps.errorMessage);
134 })
135 };
136
137 const setFormValues = () => {
138 $scope.remoteServerForm = {...$scope.remoteServer};
139 };
140
141 this.$onInit = () => {
142 APIUtils.getRemoteLoggingServer()
143 .then((remoteServer) => {
144 $scope.loadError = false;
145 $scope.remoteServer = remoteServer;
146 setFormValues();
147 })
148 .catch(() => {
149 $scope.loadError = true;
150 })
151 };
152 }
153 ]
154 }
155 }
156 ])
157})(window.angular);