Passed
Push — master ( d4f9b5...ba834d )
by Guangyu
20:25 queued 12s
created

admin/app/controllers/settings/costcenter/costfile.controller.js   A

Complexity

Total Complexity 18
Complexity/F 1.5

Size

Lines of Code 182
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 18
eloc 129
mnd 6
bc 6
fnc 12
dl 0
loc 182
rs 10
bpm 0.5
cpm 1.5
noi 0
c 0
b 0
f 0
1
'use strict';
2
3
app.controller('CostFileController', function (
4
    $scope, 
5
	$window,
6
    $common, 
7
    $translate, 
8
    $uibModal, 
9
    $interval, 
10
    CostFileService, 
11
    toaster, 
12
    SweetAlert) {
13
14
    $scope.cur_user = JSON.parse($window.localStorage.getItem("currentUser"));
15
16
    $scope.getAllCostFiles = function () {
17
        CostFileService.getAllCostFiles(function (error, data) {
18
            if (!error) {
19
                $scope.costfiles = data;
20
            } else {
21
                $scope.costfiles = [];
22
            }
23
        });
24
    };
25
26
    $scope.dzOptions = {
27
        url: getAPI() + 'costfiles',
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
            var popType = 'TOASTER.SUCCESS';
40
            var popTitle = $common.toaster.success_title;
41
            var popBody = $common.toaster.success_add_body.format(file.name);
42
43
            popType = $translate.instant(popType);
44
            popTitle = $translate.instant(popTitle);
45
            popBody = $translate.instant(popBody);
46
47
            toaster.pop({
48
                type: popType,
49
                title: popTitle,
50
                body: popBody,
51
                showCloseButton: true,
52
            });
53
            $scope.getAllCostFiles();
54
        },
55
        'error': function (file, xhr) {
56
            var popType = 'TOASTER.ERROR';
57
            var popTitle = $common.toaster.error_title;
58
            var popBody = $common.toaster.error_add_body.format(file.name);
59
60
            popType = $translate.instant(popType);
61
            popTitle = $translate.instant(popTitle);
62
            popBody = $translate.instant(popBody);
63
64
            toaster.pop({
65
                type: popType,
66
                title: popTitle,
67
                body: popBody,
68
                showCloseButton: true,
69
            });
70
        }
71
    };
72
73
    $scope.restoreCostFile = function (costfile) {
74
        CostFileService.restoreCostFile(costfile, function (error, data) {
75
            if (!error) {
76
                toaster.pop({
77
                    type: $translate.instant('TOASTER.SUCCESS'),
78
                    title: $translate.instant('TOASTER.SUCCESS_TITLE'),
79
                    body: $translate.instant('SETTING.RESTORE_SUCCESS'),
80
                    showCloseButton: true,
81
                });
82
                $scope.getAllCostFiles();
83
            } else {
84
                toaster.pop({
85
                    type: $translate.instant('TOASTER.ERROR'),
86
                    title: $translate.instant(error.title),
87
                    body: $translate.instant(error.description),
88
                    showCloseButton: true,
89
                });
90
            }
91
        });
92
    };
93
94
    $scope.deleteCostFile = function (costfile) {
95
        SweetAlert.swal({
96
                title: $translate.instant($common.sweet.title),
97
                text: $translate.instant($common.sweet.text),
98
                type: "warning",
99
                showCancelButton: true,
100
                confirmButtonColor: "#DD6B55",
101
                confirmButtonText: $translate.instant($common.sweet.confirmButtonText),
102
                cancelButtonText: $translate.instant($common.sweet.cancelButtonText),
103
                closeOnConfirm: true,
104
                closeOnCancel: true
105
            },
106
            function (isConfirm) {
107
                if (isConfirm) {
108
                    CostFileService.deleteCostFile(costfile, function (error, status) {
109
                        if (angular.isDefined(status) && status == 204) {
110
                            var templateName = "SETTING.COST_FILE";
111
                            templateName = $translate.instant(templateName);
112
113
                            var popType = 'TOASTER.SUCCESS';
114
                            var popTitle = $common.toaster.success_title;
115
                            var popBody = $common.toaster.success_delete_body;
116
117
                            popType = $translate.instant(popType);
118
                            popTitle = $translate.instant(popTitle);
119
                            popBody = $translate.instant(popBody, {template: templateName});
120
121
                            toaster.pop({
122
                                type: popType,
123
                                title: popTitle,
124
                                body: popBody,
125
                                showCloseButton: true,
126
                            });
127
128
129
                            $scope.getAllCostFiles();
130
                        } else if (angular.isDefined(status) && status == 400) {
131
                            var popType = 'TOASTER.ERROR';
132
                            var popTitle = error.title;
133
                            var popBody = error.description;
134
135
                            popType = $translate.instant(popType);
136
                            popTitle = $translate.instant(popTitle);
137
                            popBody = $translate.instant(popBody);
138
139
140
                            toaster.pop({
141
                                type: popType,
142
                                title: popTitle,
143
                                body: popBody,
144
                                showCloseButton: true,
145
                            });
146
                        } else {
147
                            var templateName = "TOASTER.COST_FILE";
148
                            templateName = $translate.instant(templateName);
149
150
                            var popType = 'TOASTER.ERROR';
151
                            var popTitle = $common.toaster.error_title;
152
                            var popBody = $common.toaster.error_delete_body;
153
154
                            popType = $translate.instant(popType);
155
                            popTitle = $translate.instant(popTitle);
156
                            popBody = $translate.instant(popBody, {template: templateName});
157
158
                            toaster.pop({
159
                                type: popType,
160
                                title: popTitle,
161
                                body: popBody,
162
                                showCloseButton: true,
163
                            });
164
                        }
165
                    });
166
                }
167
            });
168
    };
169
170
    $scope.getAllCostFiles();
171
    $interval.cancel();
172
173
    $scope.$on('$destroy', function () {
174
        // Make sure that the interval is destroyed too
175
        if (angular.isDefined($scope.refeshfiles)) {
176
            $interval.cancel($scope.refeshfiles);
177
            $scope.refeshfiles = undefined;
178
        }
179
    });
180
    $scope.refeshfiles = $interval($scope.getAllCostFiles, 1000 * 8);
181
182
});