Passed
Push — master ( c69397...91fff1 )
by Guangyu
08:31 queued 12s
created

myems-admin/app/controllers/settings/datasource/datarepairfile.controller.js   A

Complexity

Total Complexity 17
Complexity/F 1.42

Size

Lines of Code 128
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 90
mnd 5
bc 5
fnc 12
dl 0
loc 128
rs 10
bpm 0.4166
cpm 1.4166
noi 0
c 0
b 0
f 0
1
'use strict';
2
3
app.controller('DataRepairFileController', function(
4
	$scope, 
5
	$window,
6
	$translate,
7
	$interval, 
8
	DataRepairFileService, 
9
	toaster, 
10
	SweetAlert) {
11
12
	$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user"));
13
14
	$scope.getAllDataRepairFiles = function() {
15
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
16
		DataRepairFileService.getAllDataRepairFiles(headers, function (response) {
17
			if (angular.isDefined(response.status) && response.status === 200) {
18
				$scope.datarepairfiles = response.data;
19
			} else {
20
				$scope.datarepairfiles = [];
21
			}
22
		});
23
24
	};
25
26
	$scope.dzOptions = {
27
		url: getAPI() + 'datarepairfiles',
28
		acceptedFiles: '.xlsx',
29
		dictDefaultMessage: 'Click(or Drop) to add files',
30
		maxFilesize: '100',
31
		headers: { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }
32
	};
33
34
	$scope.dzCallbacks = {
35
		'addedfile': function(file) {
36
			console.info('File added.', file);
37
		},
38
		'success': function(file, xhr) {
39
			toaster.pop({
40
				type: "success",
41
				title: $translate.instant("TOASTER.SUCCESS_TITLE"),
42
				body: $translate.instant("TOASTER.SUCCESS_ADD_BODY", {template: file.name}),
43
				showCloseButton: true,
44
			});
45
			$scope.getAllDataRepairFiles();
46
		},
47
        'error': function (file, xhr) {
48
            toaster.pop({
49
                type: "error",
50
                title: $translate.instant("TOASTER.ERROR_ADD_BODY", {template: file.name}),
51
                body: $translate.instant(xhr),
52
                showCloseButton: true,
53
            });
54
        }
55
    };
56
57
    $scope.restoreDataRepairFile = function (datarepairfile) {
58
		let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
59
        DataRepairFileService.restoreDataRepairFile(datarepairfile, headers, function (response) {
60
            if (angular.isDefined(response.status) && response.status === 200) {
61
                toaster.pop({
62
                    type: "success",
63
                    title: $translate.instant('TOASTER.SUCCESS_TITLE'),
64
                    body: $translate.instant('SETTING.RESTORE_SUCCESS'),
65
                    showCloseButton: true,
66
                });
67
                $scope.getAllDataRepairFiles();
68
            } else {
69
                toaster.pop({
70
                    type: "error",
71
                    title: $translate.instant(response.data.title),
72
                    body: $translate.instant(response.data.description),
73
                    showCloseButton: true,
74
                });
75
            }
76
        });
77
    };
78
79
	$scope.deleteDataRepairFile = function(datarepairfile) {
80
		SweetAlert.swal({
81
			title: $translate.instant("SWEET.TITLE"),
82
			text: $translate.instant("SWEET.TEXT"),
83
			type: "warning",
84
			showCancelButton: true,
85
			confirmButtonColor: "#DD6B55",
86
			confirmButtonText: $translate.instant("SWEET.CONFIRM_BUTTON_TEXT"),
87
			cancelButtonText: $translate.instant("SWEET.CANCEL_BUTTON_TEXT"),
88
			closeOnConfirm: true,
89
			closeOnCancel: true
90
			},
91
			function(isConfirm) {
92
				if (isConfirm) {
93
					let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token };
94
					DataRepairFileService.deleteDataRepairFile(datarepairfile, headers, function (response) {
95
						if (angular.isDefined(response.status) && response.status === 204) {
96
                            toaster.pop({
97
                                type: "success",
98
                                title: $translate.instant("TOASTER.SUCCESS_TITLE"),
99
                                body: $translate.instant("TOASTER.SUCCESS_DELETE_BODY", {template: $translate.instant("SETTING.DATA_REPAIR_FILE")}),
100
                                showCloseButton: true,
101
                            });
102
							$scope.getAllDataRepairFiles();
103
						} else {
104
                            toaster.pop({
105
                                type: "error",
106
                                title: $translate.instant("TOASTER.ERROR_DELETE_BODY", {template: $translate.instant("SETTING.DATA_REPAIR_FILE")}),
107
                                body: $translate.instant(response.data.description),
108
                                showCloseButton: true,
109
                            });
110
						}
111
					});
112
				}
113
			});
114
	};
115
116
	$scope.getAllDataRepairFiles();
117
	$interval.cancel();
118
119
	$scope.$on('$destroy', function() {
120
	     // Make sure that the interval is destroyed too
121
	     if (angular.isDefined($scope.refeshfiles)) {
122
	         $interval.cancel($scope.refeshfiles);
123
	         $scope.refeshfiles = undefined;
124
	     }
125
	});
126
	$scope.refeshfiles=$interval($scope.getAllDataRepairFiles,1000*8);
127
128
});
129