1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
app.controller('SpaceMicrogridController', function( |
4
|
|
|
$scope, |
5
|
|
|
$window, |
6
|
|
|
$translate, |
7
|
|
|
SpaceService, |
8
|
|
|
MicrogridService, |
9
|
|
|
SpaceMicrogridService, |
10
|
|
|
toaster, SweetAlert) { |
11
|
|
|
|
12
|
|
|
$scope.spaces = []; |
13
|
|
|
$scope.currentSpaceID = 1; |
14
|
|
|
$scope.microgrids = []; |
15
|
|
|
$scope.spacemicrogrids = []; |
16
|
|
|
$scope.cur_user = JSON.parse($window.localStorage.getItem("myems_admin_ui_current_user")); |
17
|
|
|
|
18
|
|
|
$scope.getAllSpaces = function() { |
19
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
20
|
|
|
SpaceService.getAllSpaces(headers, function (response) { |
21
|
|
|
if (angular.isDefined(response.status) && response.status === 200) { |
22
|
|
|
$scope.spaces = response.data; |
23
|
|
|
} else { |
24
|
|
|
$scope.spaces = []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
var treedata = {'core': {'data': [], "multiple" : false }, "plugins" : [ "wholerow" ]}; |
28
|
|
|
for (var i = 0; i < $scope.spaces.length; i++) { |
29
|
|
|
var node = { |
30
|
|
|
"id": $scope.spaces[i].id.toString(), |
31
|
|
|
"parent": $scope.spaces[i].id == 1 ? '#' : $scope.spaces[i].parent_space.id.toString(), |
32
|
|
|
"text": $scope.spaces[i].name, |
33
|
|
|
"state": $scope.spaces[i].id == 1 ? { 'opened': true, 'selected': false } : undefined |
34
|
|
|
}; |
35
|
|
|
treedata['core']['data'].push(node); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
angular.element(spacetreewithmicrogrid).jstree(treedata); |
39
|
|
|
|
40
|
|
|
angular.element(spacetreewithmicrogrid).on("changed.jstree", function (e, data) { |
41
|
|
|
$scope.currentSpaceID = parseInt(data.selected[0]); |
42
|
|
|
$scope.getMicrogridsBySpaceID($scope.currentSpaceID); |
43
|
|
|
}); |
44
|
|
|
}); |
45
|
|
|
}; |
46
|
|
|
|
47
|
|
|
$scope.getMicrogridsBySpaceID = function(id) { |
48
|
|
|
$scope.spacemicrogrids = []; |
49
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
50
|
|
|
SpaceMicrogridService.getMicrogridsBySpaceID(id, headers, function (response) { |
51
|
|
|
if (angular.isDefined(response.status) && response.status === 200) { |
52
|
|
|
$scope.spacemicrogrids = $scope.spacemicrogrids.concat(response.data); |
53
|
|
|
} else { |
54
|
|
|
$scope.spacemicrogrids = []; |
55
|
|
|
} |
56
|
|
|
}); |
57
|
|
|
}; |
58
|
|
|
|
59
|
|
|
$scope.getAllMicrogrids = function() { |
60
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
61
|
|
|
MicrogridService.getAllMicrogrids(headers, function (response) { |
62
|
|
|
if (angular.isDefined(response.status) && response.status === 200) { |
63
|
|
|
$scope.microgrids = response.data; |
64
|
|
|
} else { |
65
|
|
|
$scope.microgrids = []; |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$scope.pairMicrogrid = function(dragEl, dropEl) { |
71
|
|
|
var microgridid = angular.element('#' + dragEl).scope().microgrid.id; |
72
|
|
|
var spaceid = angular.element(spacetreewithmicrogrid).jstree(true).get_top_selected(); |
73
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
74
|
|
|
SpaceMicrogridService.addPair(spaceid, microgridid, headers, function (response) { |
75
|
|
|
if (angular.isDefined(response.status) && response.status === 201) { |
76
|
|
|
toaster.pop({ |
77
|
|
|
type: "success", |
78
|
|
|
title: $translate.instant("TOASTER.SUCCESS_TITLE"), |
79
|
|
|
body: $translate.instant("TOASTER.BIND_MICROGRID_SUCCESS"), |
80
|
|
|
showCloseButton: true, |
81
|
|
|
}); |
82
|
|
|
$scope.getMicrogridsBySpaceID(spaceid); |
83
|
|
|
} else { |
84
|
|
|
toaster.pop({ |
85
|
|
|
type: "error", |
86
|
|
|
title: $translate.instant(response.data.title), |
87
|
|
|
body: $translate.instant(response.data.description), |
88
|
|
|
showCloseButton: true, |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
}); |
92
|
|
|
}; |
93
|
|
|
|
94
|
|
|
$scope.deleteMicrogridPair = function(dragEl, dropEl) { |
95
|
|
|
if (angular.element('#' + dragEl).hasClass('source')) { |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
var spacemicrogridid = angular.element('#' + dragEl).scope().spacemicrogrid.id; |
99
|
|
|
var spaceid = angular.element(spacetreewithmicrogrid).jstree(true).get_top_selected(); |
100
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
101
|
|
|
SpaceMicrogridService.deletePair(spaceid, spacemicrogridid, headers, function (response) { |
102
|
|
|
if (angular.isDefined(response.status) && response.status === 204) { |
103
|
|
|
toaster.pop({ |
104
|
|
|
type: "success", |
105
|
|
|
title: $translate.instant("TOASTER.SUCCESS_TITLE"), |
106
|
|
|
body: $translate.instant("TOASTER.UNBIND_MICROGRID_SUCCESS"), |
107
|
|
|
showCloseButton: true, |
108
|
|
|
}); |
109
|
|
|
$scope.getMicrogridsBySpaceID(spaceid); |
110
|
|
|
} else { |
111
|
|
|
toaster.pop({ |
112
|
|
|
type: "error", |
113
|
|
|
title: $translate.instant(response.data.title), |
114
|
|
|
body: $translate.instant(response.data.description), |
115
|
|
|
showCloseButton: true, |
116
|
|
|
}); |
117
|
|
|
} |
118
|
|
|
}); |
119
|
|
|
}; |
120
|
|
|
|
121
|
|
|
$scope.refreshSpaceTree = function() { |
122
|
|
|
let headers = { "User-UUID": $scope.cur_user.uuid, "Token": $scope.cur_user.token }; |
123
|
|
|
SpaceService.getAllSpaces(headers, function (response) { |
124
|
|
|
if (angular.isDefined(response.status) && response.status === 200) { |
125
|
|
|
$scope.spaces = response.data; |
126
|
|
|
} else { |
127
|
|
|
$scope.spaces = []; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
var treedata = {'core': {'data': [], "multiple" : false }, "plugins" : [ "wholerow" ]}; |
131
|
|
|
for (var i = 0; i < $scope.spaces.length; i++) { |
132
|
|
|
var node = { |
133
|
|
|
"id": $scope.spaces[i].id.toString(), |
134
|
|
|
"parent": $scope.spaces[i].id == 1 ? '#' : $scope.spaces[i].parent_space.id.toString(), |
135
|
|
|
"text": $scope.spaces[i].name, |
136
|
|
|
"state": $scope.spaces[i].id == 1 ? { 'opened': true, 'selected': false } : undefined |
137
|
|
|
}; |
138
|
|
|
treedata['core']['data'].push(node); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
angular.element(spacetreewithmicrogrid).jstree(true).settings.core.data = treedata['core']['data']; |
142
|
|
|
angular.element(spacetreewithmicrogrid).jstree(true).refresh(); |
143
|
|
|
}); |
144
|
|
|
}; |
145
|
|
|
|
146
|
|
|
$scope.$on('handleBroadcastSpaceChanged', function(event) { |
147
|
|
|
$scope.spacemicrogrids = []; |
148
|
|
|
$scope.refreshSpaceTree(); |
149
|
|
|
}); |
150
|
|
|
|
151
|
|
|
// 初始化数据 |
152
|
|
|
$scope.getAllSpaces(); |
153
|
|
|
$scope.getAllMicrogrids(); |
154
|
|
|
}); |