1 |
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | angular.module('wjm', ['ui.bootstrap', 'ui.calendar', 'angularTreeview', 'ngDialog']).
|
---|
22 |
|
---|
23 | controller('resourceCtrl', function ($rootScope, $scope, uiCalendarConfig, $compile, ngDialog, $timeout) {
|
---|
24 | var vm = $scope;
|
---|
25 |
|
---|
26 | var date = new Date();
|
---|
27 |
|
---|
28 | var d = date.getDate();
|
---|
29 | var m = date.getMonth();
|
---|
30 | var y = date.getFullYear();
|
---|
31 | var hubber = $.connection.calendarHub;
|
---|
32 |
|
---|
33 | vm.currentcal = [];//Reference to current calendar (treeview.currentNode.calendar)
|
---|
34 | $scope.permissionview = false;//Show permissions or not
|
---|
35 | $scope.resaddview = false;//Show add resources or not
|
---|
36 | vm.selectedEventId = -1;//Selected downtime (-1 for none)
|
---|
37 | vm.groups = []; //All resource groups
|
---|
38 | vm.clients = []; //All resources
|
---|
39 | vm.init = function () {
|
---|
40 |
|
---|
41 | var v = document.getElementById("userId").innerHTML;
|
---|
42 | $.connection.hub.qs = { 'userid': v };
|
---|
43 | //Connection string set to identify the unique session ID for the user
|
---|
44 | $.connection.hub.start().done(function () {
|
---|
45 | hubber.server.requestInfo();//initial data request
|
---|
46 | });
|
---|
47 |
|
---|
48 | //Process all initial data needed (heavy load)
|
---|
49 | hubber.client.processData = function (data, users, groups) {
|
---|
50 | vm.data = JSON.parse(data);
|
---|
51 | vm.permUsers = JSON.parse(users);
|
---|
52 | vm.permGroups = JSON.parse(groups);
|
---|
53 | $scope.buildTree();
|
---|
54 |
|
---|
55 | $scope.$apply();
|
---|
56 | };
|
---|
57 |
|
---|
58 | //Saving from current graph is done -> refresh it
|
---|
59 | hubber.client.savingCurrentDone = function () {
|
---|
60 | vm.calendarSaver = false;
|
---|
61 | $scope.clearCurrentCalendar();
|
---|
62 | $scope.$apply();
|
---|
63 | };
|
---|
64 |
|
---|
65 | //Saving all graphs done -> clear everything
|
---|
66 | hubber.client.savingAllDone = function () {
|
---|
67 | vm.calendarSaver = false;
|
---|
68 | clearAllCalendarsFunc();
|
---|
69 | $scope.$apply();
|
---|
70 | };
|
---|
71 |
|
---|
72 | //Dispose toggle refresh for current
|
---|
73 | hubber.client.processDispose = function (disp) {
|
---|
74 | vm.treeview.currentNode.IsDisposable = disp;
|
---|
75 | $scope.calendarDispose = false;
|
---|
76 | $scope.$apply();
|
---|
77 | }
|
---|
78 |
|
---|
79 | //Permissions refresh for specific resource
|
---|
80 | hubber.client.processPermissions = function (id, perm) {
|
---|
81 | var json = JSON.parse(perm);
|
---|
82 |
|
---|
83 | $scope.treeview.currentNode.permissions = json;
|
---|
84 | refreshPermissions();
|
---|
85 | $scope.permissionLoader = false;
|
---|
86 | $scope.$apply();
|
---|
87 | }
|
---|
88 |
|
---|
89 | //Process downtimes for a resource. Data conversion
|
---|
90 | hubber.client.processDowntime = function (id, down) {
|
---|
91 | var json = JSON.parse(down);
|
---|
92 | var arrdown = [];
|
---|
93 | var str = "";
|
---|
94 | for (var i = 0; i < json.length; i++) {
|
---|
95 | if (json[i].DowntimeType === 0) {
|
---|
96 | str = "Unavailable";
|
---|
97 | col = "#006080";
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | str = "Shutdown";
|
---|
101 | col = "#993300";
|
---|
102 | }
|
---|
103 | arrdown.push({
|
---|
104 | id: json[i].Id,
|
---|
105 | title: str,
|
---|
106 | start: new Date(json[i].StartDate),
|
---|
107 | end: new Date(json[i].EndDate),
|
---|
108 | allDay: json[i].AllDayEvent,
|
---|
109 | color: col,
|
---|
110 | rec: {
|
---|
111 | recurrence: json[i].Recurring,
|
---|
112 | recid: json[i].RecurringId,
|
---|
113 | days: [false, false, false, false, false, false, false],
|
---|
114 | start: new Date(json[i].StartDate),
|
---|
115 | end: new Date(json[i].EndDate)
|
---|
116 |
|
---|
117 | },
|
---|
118 | changed: false
|
---|
119 | });
|
---|
120 | }
|
---|
121 | var dat = { 'id': id, 'down': [arrdown] };
|
---|
122 | $scope.treeview.currentNode.calendar = dat;
|
---|
123 |
|
---|
124 | $scope.currentcal = $scope.treeview.currentNode.calendar;
|
---|
125 | $("#resourcecalendar").fullCalendar('refresh');
|
---|
126 | $scope.$apply();
|
---|
127 | }
|
---|
128 |
|
---|
129 | //Initial function to build the resource tree
|
---|
130 | $scope.buildTree = function () {
|
---|
131 | vm.tree = [];
|
---|
132 | vm.temptree = [];
|
---|
133 | vm.top = false;
|
---|
134 | var ungrouped = {
|
---|
135 | children: [],
|
---|
136 | Name: 'Ungrouped'
|
---|
137 | }
|
---|
138 | for (; vm.data.length > 0;) {
|
---|
139 |
|
---|
140 | if (vm.data[0].ParentResourceId == null && vm.data[0].IsDisposable !== undefined) {
|
---|
141 | var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
|
---|
142 | vm.clients.push(curr);
|
---|
143 | ungrouped.children.push(curr);
|
---|
144 | }
|
---|
145 | else if (vm.data[0].ParentResourceId == null) {
|
---|
146 | var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
|
---|
147 | vm.groups.push(curr);
|
---|
148 | vm.tree.push(curr);
|
---|
149 | }
|
---|
150 | else {
|
---|
151 | var curr = $scope.seekChildren(vm.data.splice(0, 1)[0]);
|
---|
152 | if (curr.IsDisposable !== undefined)
|
---|
153 | vm.clients.push(curr);
|
---|
154 | else
|
---|
155 | vm.groups.push(curr);
|
---|
156 | vm.temptree.push(curr);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | vm.tree.push(ungrouped);
|
---|
160 |
|
---|
161 | };//End for hubber init
|
---|
162 |
|
---|
163 | //menu navigation
|
---|
164 | $scope.menuchange = function (i) {
|
---|
165 | switch (i) {
|
---|
166 | case 0:
|
---|
167 | $scope.permissionview = false;
|
---|
168 | $scope.resaddview = false;
|
---|
169 | break;
|
---|
170 | case 1:
|
---|
171 | $scope.permissionview = true;
|
---|
172 | $scope.resaddview = false;
|
---|
173 | break;
|
---|
174 | case 2:
|
---|
175 | $scope.permissionview = false;
|
---|
176 | $scope.resaddview = true;
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | //Seeks for children, used recursively for building the treeview
|
---|
182 | $scope.seekChildren = function (current) {
|
---|
183 | current.calendar = [];
|
---|
184 | current.children = [];
|
---|
185 | current.todelete = [];
|
---|
186 | current.changes = false;
|
---|
187 | current.collapsed = true;
|
---|
188 | for (var t = 0; t < vm.temptree.length;) {
|
---|
189 | if (current.Id == vm.temptree[t].ParentResourceId) {
|
---|
190 | current.children.push(vm.temptree.splice(t, 1)[0]);
|
---|
191 | }
|
---|
192 | else {
|
---|
193 | t++;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | var childc = current.children.length;//Remembers count of children received from temp tree;
|
---|
197 | for (var t = 0; t < vm.data.length;) {
|
---|
198 | if (current.Id == vm.data[t].ParentResourceId) {
|
---|
199 | if (vm.data[t].IsDisposable !== undefined)
|
---|
200 | vm.clients.push(vm.data[t]);
|
---|
201 | else
|
---|
202 | vm.groups.push(vm.data[t]);
|
---|
203 | current.children.push(vm.data.splice(t, 1)[0]);
|
---|
204 | }
|
---|
205 | else {
|
---|
206 | t++;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | for (var t = childc; t < current.children.length; t++) {
|
---|
210 | current.children[t] = $scope.seekChildren(current.children[t]);
|
---|
211 | }
|
---|
212 | return current;
|
---|
213 | }
|
---|
214 |
|
---|
215 | //Change to another resource
|
---|
216 | $scope.$watch("treeview.currentNode", function (newValue, oldValue) {
|
---|
217 | $scope.currentcal = [];
|
---|
218 | $scope.resaddview = false;
|
---|
219 | $(".selected.ng-binding").addClass('loaded');
|
---|
220 | $scope.selectedEventId = -1;
|
---|
221 |
|
---|
222 | //Check if resource already has downtime data loaded
|
---|
223 | if ($scope.treeview.currentNode != null && $scope.treeview.currentNode.Id != undefined) {
|
---|
224 | if ($scope.treeview.currentNode.calendar.length === 0) {
|
---|
225 | //Reach out to server to receive resource data
|
---|
226 | hubber.server.requestPermissions(vm.treeview.currentNode.Id);
|
---|
227 | hubber.server.requestDownTime(vm.treeview.currentNode.Id);
|
---|
228 | vm.permissionLoader = true;
|
---|
229 | }
|
---|
230 | else {
|
---|
231 | $timeout(function () {
|
---|
232 | refreshPermissions();
|
---|
233 | //set previously loaded data
|
---|
234 | $scope.currentcal = $scope.treeview.currentNode.calendar;
|
---|
235 | }, 0);
|
---|
236 |
|
---|
237 | }
|
---|
238 | $scope.refreshAdds();
|
---|
239 | }
|
---|
240 |
|
---|
241 | });
|
---|
242 | }
|
---|
243 |
|
---|
244 | //Resets the add resources menu
|
---|
245 | $scope.refreshAdds = function() {
|
---|
246 | for (var i = 0; i < $scope.clients.length; i++) {
|
---|
247 | $scope.clients[i].add = false;
|
---|
248 | for (var j = 0; j < $scope.treeview.currentNode.children.length; j++) {
|
---|
249 | if ($scope.clients[i].Id === $scope.treeview.currentNode.children[j].Id)
|
---|
250 | $scope.clients[i].add = true;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | //Resets the permissions menu
|
---|
256 | function refreshPermissions() {
|
---|
257 | for (var i = 0; i < $scope.permUsers.length; i++){
|
---|
258 | $scope.permUsers[i].state = false;
|
---|
259 | for (var j = 0; j < $scope.treeview.currentNode.permissions.length; j++) {
|
---|
260 | if($scope.permUsers[i].Id === $scope.treeview.currentNode.permissions[j].GrantedUserId)
|
---|
261 | $scope.permUsers[i].state = true;
|
---|
262 | }
|
---|
263 | }
|
---|
264 | for (var i = 0; i < $scope.permGroups.length; i++) {
|
---|
265 | $scope.permGroups[i].state = false;
|
---|
266 | for (var j = 0; j < $scope.treeview.currentNode.permissions.length; j++) {
|
---|
267 | if ($scope.permGroups[i].Id === $scope.treeview.currentNode.permissions[j].GrantedUserId)
|
---|
268 | $scope.permGroups[i].state = true;
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | //Add resource group show by clearing the tree
|
---|
274 | $scope.clearTreeSelect = function () {
|
---|
275 | if (vm.treeview.currentNode != undefined) {
|
---|
276 | vm.treeview.currentNode.selected = undefined;
|
---|
277 | vm.treeview.currentNode = undefined;
|
---|
278 | vm.selectedEventId = -1;
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | //Reach out to server to toggle disposable for current calendar
|
---|
283 | $scope.toggleDisposable = function () {
|
---|
284 | $scope.calendarDispose = true;
|
---|
285 | hubber.server.toggleDisposable(vm.treeview.currentNode.Id);
|
---|
286 | }
|
---|
287 |
|
---|
288 | //Pushes permissions to server
|
---|
289 | $scope.pushPermissions = function () {
|
---|
290 | var perms = [];
|
---|
291 | $scope.permissionLoader = true;
|
---|
292 | var node = $scope.treeview.currentNode;
|
---|
293 | for (var i = 0; i < $scope.permGroups.length; i++) {
|
---|
294 | if ($scope.permGroups[i].state === true)
|
---|
295 | perms.push($scope.permGroups[i].Id);
|
---|
296 | }
|
---|
297 | for (var i = 0; i < $scope.permUsers.length; i++) {
|
---|
298 | if ($scope.permUsers[i].state === true)
|
---|
299 | perms.push($scope.permUsers[i].Id);
|
---|
300 | }
|
---|
301 | hubber.server.changePermissions(perms, node.Id);
|
---|
302 | }
|
---|
303 |
|
---|
304 | //Collects all data from a single resource to save to the server
|
---|
305 | function collectInfoToSave(node, refresh, last) {
|
---|
306 | var arr = node.calendar.down[0];
|
---|
307 | var toadd = [];
|
---|
308 | var todel = node.todelete;
|
---|
309 | var toupd = [];
|
---|
310 | for (var i = 0; i < arr.length; i++) {
|
---|
311 | if (arr[i].id === '00000000-0000-0000-0000-000000000000') {
|
---|
312 | //NEW DOWNTIME
|
---|
313 | var t = arr[i].allDay.toString();
|
---|
314 | toadd.push([
|
---|
315 | arr[i].title,//status
|
---|
316 | "" + Date.parse(arr[i].start),//start
|
---|
317 | "" + Date.parse(arr[i].end),//end
|
---|
318 | t,// allday
|
---|
319 | node.Id, //resource id
|
---|
320 | arr[i].rec.recurrence.toString(),//true on recur
|
---|
321 | arr[i].rec.recid,//recur id
|
---|
322 | "" + Date.parse(arr[i].rec.start),
|
---|
323 | "" + Date.parse(arr[i].rec.end),
|
---|
324 | arr[i].rec.days.join(",")
|
---|
325 |
|
---|
326 |
|
---|
327 | ]);
|
---|
328 | }
|
---|
329 | else if (arr[i].changed === true) {
|
---|
330 | //EDIT EXISTING DOWNTIME
|
---|
331 | var t = arr[i].allDay.toString();
|
---|
332 | toupd.push([
|
---|
333 | arr[i].id,//id to update
|
---|
334 | arr[i].title,//status
|
---|
335 | "" + Date.parse(arr[i].start),//start
|
---|
336 | "" + Date.parse(arr[i].end),//end
|
---|
337 | t,//allday
|
---|
338 | arr[i].rec.recurrence.toString(),//true on recur
|
---|
339 | arr[i].rec.recid,//recur id
|
---|
340 | "" + Date.parse(arr[i].rec.start),
|
---|
341 | "" + Date.parse(arr[i].rec.end),
|
---|
342 | arr[i].rec.days.join(",")
|
---|
343 |
|
---|
344 | ]);
|
---|
345 | }
|
---|
346 | }
|
---|
347 | vm.sendtoserv = [todel, toadd, toupd];
|
---|
348 | hubber.server.saveCalendar(node.Id, todel, toadd, toupd, refresh, last);
|
---|
349 | }
|
---|
350 |
|
---|
351 | //Save current calendar
|
---|
352 | $scope.saveCurrentCalendar = function () {
|
---|
353 | vm.calendarSaver = true;
|
---|
354 | collectInfoToSave(vm.treeview.currentNode, true, false);//true for refresh, false for save all
|
---|
355 | //true for refresh, false for showing it's only one calendar saved.
|
---|
356 | }
|
---|
357 |
|
---|
358 | //Save all changed calendars
|
---|
359 | $scope.saveAllCalendars = function () {
|
---|
360 |
|
---|
361 | $scope.allSave = [];
|
---|
362 | for (var i = 0; i < $scope.tree.length; i++)
|
---|
363 | createSaveRequests($scope.tree[i]);
|
---|
364 | ngDialog.openConfirm({
|
---|
365 | template:
|
---|
366 | '<p>Are you sure you want to save all changes made to ' + $scope.allSave.length + ' calendars?</p>' +
|
---|
367 | '<div>' +
|
---|
368 | '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
|
---|
369 | '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
|
---|
370 | '</button></div>',
|
---|
371 | plain: true
|
---|
372 | }).then(function (success) {
|
---|
373 | vm.calendarSaver = true;
|
---|
374 |
|
---|
375 | for (var i = 0; i < $scope.allSave.length; i++) {
|
---|
376 | if (i >= $scope.allSave.length - 1)
|
---|
377 | collectInfoToSave($scope.allSave[i], false, true);
|
---|
378 | else
|
---|
379 | collectInfoToSave($scope.allSave[i], false, false);
|
---|
380 | }
|
---|
381 | });
|
---|
382 |
|
---|
383 |
|
---|
384 | }
|
---|
385 |
|
---|
386 | //Checks treeview for edits and builds new array containing these (recurse for children)
|
---|
387 | function createSaveRequests(node) {
|
---|
388 | if (node.changes === true)
|
---|
389 | $scope.allSave.push(node);
|
---|
390 | for (var i = 0; i < node.children.length; i++) {
|
---|
391 | createSaveRequests(node.children[i]);
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | //Clears and refreshes calendar for current resource (delete current changes)
|
---|
396 | $scope.clearCurrentCalendar = function () {
|
---|
397 |
|
---|
398 | $scope.selectedEventId = -1;
|
---|
399 | $scope.treeview.currentNode.calendar = null;
|
---|
400 | $scope.treeview.currentNode.todelete = [];
|
---|
401 | $scope.treeview.currentNode.changes = false;
|
---|
402 | $scope.currentcal = [];
|
---|
403 | hubber.server.requestDownTime(vm.treeview.currentNode.Id);
|
---|
404 | $(".selected.ng-scope").removeClass('changed');
|
---|
405 | $(".selected.ng-binding").removeClass('changed');
|
---|
406 |
|
---|
407 |
|
---|
408 | }
|
---|
409 |
|
---|
410 | //Clears all calendars and refreshes current (Deletes all changes)
|
---|
411 | $scope.clearAllCalendars = function () {
|
---|
412 | ngDialog.openConfirm({
|
---|
413 | template:
|
---|
414 | '<p>Are you sure you want to delete all calendar changes?</p>' +
|
---|
415 | '<div>' +
|
---|
416 | '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
|
---|
417 | '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
|
---|
418 | '</button></div>',
|
---|
419 | plain: true
|
---|
420 | }).then(function (success) {
|
---|
421 | clearAllCalendarsFunc();
|
---|
422 | });
|
---|
423 |
|
---|
424 | }
|
---|
425 |
|
---|
426 | //Function that clears all calendar (separate so dialog is not called when saveAll is finished)
|
---|
427 | function clearAllCalendarsFunc() {
|
---|
428 | $(".ng-binding.changed").removeClass('changed');
|
---|
429 | $(".ng-scope.changed").removeClass('changed');
|
---|
430 | $(".ng-binding.loaded").removeClass('loaded');
|
---|
431 | $(".ng-scope.loaded").removeClass('loaded');
|
---|
432 | $scope.selectedEventId = -1;
|
---|
433 | $scope.treeview.currentNode.calendar = null;
|
---|
434 | $scope.currentcal = [];
|
---|
435 | for (var i = 0; i < $scope.tree.length; i++)
|
---|
436 | clearCalendarsRecurse($scope.tree[i]);
|
---|
437 | hubber.server.requestDownTime(vm.treeview.currentNode.Id);
|
---|
438 | $(".selected.ng-binding").addClass('loaded');
|
---|
439 | $(".selected.ng-scope").addClass('loaded');
|
---|
440 | }
|
---|
441 |
|
---|
442 | //Recurse trough tree view
|
---|
443 | function clearCalendarsRecurse(node) {
|
---|
444 |
|
---|
445 | node.calendar = [];
|
---|
446 | node.todelete = [];
|
---|
447 | node.changes = false;
|
---|
448 | for (var i = 0; i < node.children.length; i++) {
|
---|
449 | clearCalendarsRecurse(node.children[i]);
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | //Delete all downtimes from current resource
|
---|
454 | $scope.deleteAllEvents = function () {
|
---|
455 | vm.calendarDeleter = true;
|
---|
456 | vm.selectedEventId = -1;
|
---|
457 | var node = vm.treeview.currentNode.calendar.down[0];
|
---|
458 | for (var i = 0; i < node.length;) {
|
---|
459 | if (node[i].id != '00000000-0000-0000-0000-000000000000')
|
---|
460 | vm.treeview.currentNode.todelete.push(node[i].id);
|
---|
461 | $scope.setChanged(vm.selectedEventId);
|
---|
462 | node.splice(i, 1);
|
---|
463 | }
|
---|
464 | vm.calendarDeleter = false;
|
---|
465 | ngDialog.open({
|
---|
466 | template:
|
---|
467 | '<p>All events have been deleted. Save the changes to confirm deletion, clearing will restore all the events from the server</p>',
|
---|
468 | plain: true
|
---|
469 | });
|
---|
470 | }
|
---|
471 |
|
---|
472 | //Delete all past events
|
---|
473 | $scope.deleteAllPreviousEvents = function () {
|
---|
474 | vm.calendarDeleter = true;
|
---|
475 | vm.selectedEventId = -1;
|
---|
476 | var node = vm.treeview.currentNode.calendar.down[0];
|
---|
477 | for (var i = 0; i < node.length;) {
|
---|
478 | if (node[i].end < Date.now()) {
|
---|
479 | if (node[i].id != '00000000-0000-0000-0000-000000000000')
|
---|
480 | vm.treeview.currentNode.todelete.push(node[i].id);
|
---|
481 | $scope.setChanged(vm.selectedEventId);
|
---|
482 | node.splice(i, 1);
|
---|
483 | }
|
---|
484 | else
|
---|
485 | i++;
|
---|
486 | }
|
---|
487 | vm.calendarDeleter = false;
|
---|
488 | ngDialog.open({
|
---|
489 | template:
|
---|
490 | '<p>All previous events have been deleted. Save the changes to confirm deletion</p>',
|
---|
491 | plain: true
|
---|
492 | });
|
---|
493 | }
|
---|
494 |
|
---|
495 | //Sets the status of a downtime AND the current resource to changed
|
---|
496 | $scope.setChanged = function (id) {
|
---|
497 | if (id != -1) {
|
---|
498 | if (vm.treeview.currentNode.calendar.down[0][id].title === "Unavailable")
|
---|
499 | vm.treeview.currentNode.calendar.down[0][id].color = '#0099cc';
|
---|
500 | else
|
---|
501 | vm.treeview.currentNode.calendar.down[0][id].color = '#ff5500';
|
---|
502 |
|
---|
503 | vm.treeview.currentNode.calendar.down[0][id].changed = true;
|
---|
504 | }
|
---|
505 | vm.treeview.currentNode.changes = true;
|
---|
506 | $(".selected").addClass('changed');
|
---|
507 | }
|
---|
508 |
|
---|
509 | //Adds event by click on empty space
|
---|
510 | $scope.calendarClick = function (date, jsEvent, view) {
|
---|
511 |
|
---|
512 | var newid = 0;
|
---|
513 | if (vm.treeview.currentNode.calendar.down[0].length != 0)
|
---|
514 | newid = vm.treeview.currentNode.calendar.down[0][(vm.treeview.currentNode.calendar.down[0].length - 1)]._id + 1;
|
---|
515 | var dat = date.toDate();
|
---|
516 | var end = new Date(dat);
|
---|
517 | end.setHours(dat.getHours() + 2);
|
---|
518 | vm.treeview.currentNode.calendar.down[0].push({
|
---|
519 | id: '00000000-0000-0000-0000-000000000000',//Makes it recognizable as new for the server
|
---|
520 | title: 'Unavailable',
|
---|
521 | start: dat,
|
---|
522 | end: end,
|
---|
523 | allDay: date._ambigTime,
|
---|
524 | rec: {
|
---|
525 | recurrence: false,
|
---|
526 | recid: '0',
|
---|
527 | days: [false, false, false, false, false, false, false],
|
---|
528 | start: dat,
|
---|
529 | end: end
|
---|
530 |
|
---|
531 | },
|
---|
532 | _id: (newid)
|
---|
533 | });
|
---|
534 | vm.selectedEventId = vm.treeview.currentNode.calendar.down[0].length - 1;
|
---|
535 | vm.setChanged(vm.selectedEventId);
|
---|
536 | $scope.currentcal = [];
|
---|
537 | $scope.currentcal = vm.treeview.currentNode.calendar;
|
---|
538 |
|
---|
539 |
|
---|
540 | }
|
---|
541 |
|
---|
542 | //Finds array index for specific _id
|
---|
543 | function checkId(id) {
|
---|
544 | for (var i = 0; i < vm.treeview.currentNode.calendar.down[0].length ; i++) {
|
---|
545 | if (vm.treeview.currentNode.calendar.down[0][i]._id === id)
|
---|
546 | return i;
|
---|
547 | }
|
---|
548 | return -1;
|
---|
549 | }
|
---|
550 | //Set selected downtime
|
---|
551 | $scope.eventClick = function (date, jsEvent, view) {
|
---|
552 | vm.selectedEventId = checkId(date._id);
|
---|
553 | };
|
---|
554 | //Sets selected downtime by clicking on the button at bottom on the page, moves to date
|
---|
555 | $scope.eventClickBtn = function (id) {
|
---|
556 | vm.selectedEventId = checkId(id);
|
---|
557 | vm.goToDate();
|
---|
558 | };
|
---|
559 |
|
---|
560 | //Drag and drop downtime
|
---|
561 | $scope.dragandDrop = function (event, delta, revertFunc, jsEvent, ui, view) {
|
---|
562 | vm.selectedEventId = checkId(event._id);
|
---|
563 | vm.setChanged(vm.selectedEventId);
|
---|
564 | if (event.end == null) {
|
---|
565 | event.end = moment(event.start).add(2, 'hours');
|
---|
566 | }
|
---|
567 | if (vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay && event.start._ambigTime == false) {
|
---|
568 | event.allDay = false;
|
---|
569 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay = false;
|
---|
570 | }
|
---|
571 | else if (!vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay && event.start._ambigTime == true) {
|
---|
572 | event.allDay = true;
|
---|
573 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].allDay = true;
|
---|
574 | //vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = null;
|
---|
575 | }
|
---|
576 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].start = new Date(event.start);
|
---|
577 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = new Date(event.end);
|
---|
578 | };
|
---|
579 | //Resize downtime
|
---|
580 | $scope.resizeEvent = function (event, delta, revertFunc, jsEvent, ui, view) {
|
---|
581 | vm.selectedEventId = checkId(event._id);
|
---|
582 | vm.setChanged(vm.selectedEventId);
|
---|
583 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].start = new Date(event.start);
|
---|
584 | vm.treeview.currentNode.calendar.down[0][vm.selectedEventId].end = new Date(event.end);
|
---|
585 | };
|
---|
586 |
|
---|
587 | //Removes a downtime from client (existing downtime gets referenced in todelete
|
---|
588 | $scope.remove = function (index) {
|
---|
589 | vm.selectedEventId = -1;
|
---|
590 | vm.setChanged(vm.selectedEventId);
|
---|
591 | if (vm.treeview.currentNode.calendar.down[0][index].id != "00000000-0000-0000-0000-000000000000") {
|
---|
592 | vm.treeview.currentNode.todelete.push(vm.treeview.currentNode.calendar.down[0][index].id);
|
---|
593 | }
|
---|
594 | vm.treeview.currentNode.calendar.down[0].splice(index, 1);
|
---|
595 |
|
---|
596 |
|
---|
597 | };
|
---|
598 | //Remove trough list view at bottom
|
---|
599 | $scope.removeList = function (id) {
|
---|
600 | var index = checkId(id);
|
---|
601 | vm.selectedEventId = -1;
|
---|
602 | vm.setChanged(vm.selectedEventId);
|
---|
603 | if (vm.treeview.currentNode.calendar.down[0][index].id != "00000000-0000-0000-0000-000000000000") {
|
---|
604 | vm.treeview.currentNode.todelete.push(vm.treeview.currentNode.calendar.down[0][index].id);
|
---|
605 | }
|
---|
606 | vm.treeview.currentNode.calendar.down[0].splice(index, 1);
|
---|
607 |
|
---|
608 |
|
---|
609 | };
|
---|
610 | //Check if start is before end AND start is after now (recursion)
|
---|
611 | $scope.checkDateStartEnd = function () {
|
---|
612 | if ($scope.currentcal.down[0][vm.selectedEventId].rec.start > $scope.currentcal.down[0][vm.selectedEventId].rec.end ||
|
---|
613 | $scope.currentcal.down[0][vm.selectedEventId].rec.end === undefined) {
|
---|
614 |
|
---|
615 | $scope.currentcal.down[0][vm.selectedEventId].rec.end = new Date($scope.currentcal.down[0][vm.selectedEventId].rec.start);
|
---|
616 |
|
---|
617 | }
|
---|
618 | if ($scope.currentcal.down[0][vm.selectedEventId].rec.start < Date.now())
|
---|
619 | $scope.currentcal.down[0][vm.selectedEventId].rec.start = new Date();
|
---|
620 | }
|
---|
621 | //pushes all changes to other existing recurs and creates new downtimes where needed
|
---|
622 | $scope.pushRecurChanges = function (recid) {
|
---|
623 | ngDialog.openConfirm({
|
---|
624 | template:
|
---|
625 | '<p>This will change all existing recurrences and create new recurrences where needed. Are you sure?</p>' +
|
---|
626 | '<div>' +
|
---|
627 | '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
|
---|
628 | '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
|
---|
629 | '</button></div>',
|
---|
630 | plain: true
|
---|
631 | }).then(function (success) {
|
---|
632 |
|
---|
633 | var arr = vm.treeview.currentNode.calendar.down[0];
|
---|
634 | //Deep copy of downtime for referencing
|
---|
635 | var ob = $.extend(true, {}, arr[vm.selectedEventId]);
|
---|
636 |
|
---|
637 | for (var i = 0; i < arr.length;) {//Go trough downtimes and find all recurrences
|
---|
638 | if (arr[i].rec.recid === recid) {
|
---|
639 | if (new Date(arr[i].start) < new Date(ob.rec.start) ||
|
---|
640 | new Date(arr[i].end) > new Date(ob.rec.end) ||
|
---|
641 | !ob.rec.days[new Date(arr[i].start).getDay()]) {
|
---|
642 | //Downtime is out of recurrence boundaries
|
---|
643 | //Before start date OR After end date OR Not on right day of the week
|
---|
644 | vm.remove(i);//NO I++ -> REMOVE SPLICES ARRAY
|
---|
645 | vm.selectedEventId = -1;
|
---|
646 |
|
---|
647 | }
|
---|
648 | else {
|
---|
649 | //Edit downtime when it is within the bounds
|
---|
650 | arr[i].start.setHours(ob.start.getHours(), ob.start.getMinutes());
|
---|
651 | arr[i].end.setHours(ob.end.getHours(), ob.end.getMinutes());
|
---|
652 | arr[i].title = ob.title;
|
---|
653 | arr[i].rec = ob.rec;
|
---|
654 | arr[i].allDay = ob.allDay;
|
---|
655 | vm.setChanged(i);
|
---|
656 | i++;
|
---|
657 | }
|
---|
658 | }
|
---|
659 | else
|
---|
660 | i++;
|
---|
661 | }
|
---|
662 | //init for new downtimes.
|
---|
663 | var start = new Date(ob.rec.start);
|
---|
664 | start.setHours(2, 0, 0, 0);//Beginning of the day + time conversion +02
|
---|
665 | var end = new Date(ob.rec.end);
|
---|
666 | end = new Date(end.setHours(24, 0, 0, 0) + 1000 * 3600 * 2);//End of the day + time conversion +02
|
---|
667 |
|
---|
668 | loop1: //Loop start to end with single day increment
|
---|
669 | for (var d = (start.getTime()) ; d < (end.getTime()) ;) {
|
---|
670 | var tog = ob.rec.days[new Date(d).getDay()];
|
---|
671 | //Check if day of the week is included in recursion
|
---|
672 | if (tog) {
|
---|
673 | loop2://Loop checking existing downtime array to see if day is already filled or not
|
---|
674 | for (var i = 0; i < arr.length; i++) {
|
---|
675 |
|
---|
676 | if (arr[i].rec.recid === recid) {
|
---|
677 | var dend = (d + 1000 * 3600 * 24);
|
---|
678 | if (arr[i].start.getTime() >= d &&
|
---|
679 | arr[i].end.getTime() <= dend) {
|
---|
680 | d += (1000 * 3600 * 24);//add day to loop 1
|
---|
681 | continue loop1;//breaks out of loop2 and skips to next day
|
---|
682 | }
|
---|
683 | }
|
---|
684 | }
|
---|
685 | //Made it here = new event needed: init
|
---|
686 | var ts = new Date(d);
|
---|
687 | ts.setHours(new Date(ob.start).getHours(), new Date(ob.start).getMinutes());
|
---|
688 | var te = new Date(d);
|
---|
689 | te.setHours(new Date(ob.end).getHours(), new Date(ob.end).getMinutes());
|
---|
690 | arr.push({
|
---|
691 | id: '00000000-0000-0000-0000-000000000000',
|
---|
692 | title: ob.title,
|
---|
693 | start: ts,
|
---|
694 | end: te,
|
---|
695 | allDay: ob.allDay,
|
---|
696 | rec: ob.rec
|
---|
697 | });
|
---|
698 | vm.setChanged(arr.length - 1);
|
---|
699 |
|
---|
700 | }
|
---|
701 |
|
---|
702 | d += (1000 * 3600 * 24);// adds one day
|
---|
703 |
|
---|
704 | }
|
---|
705 | });
|
---|
706 | }
|
---|
707 |
|
---|
708 | //Delete all bound recurrences
|
---|
709 | $scope.deleteAllRecurrences = function (recid) {
|
---|
710 | ngDialog.openConfirm({
|
---|
711 | template:
|
---|
712 | '<p>This will delete every found recurrence. Are you sure?</p>' +
|
---|
713 | '<div>' +
|
---|
714 | '<button type="button" class="btn btn-default" ng-click="closeThisDialog(0)">No </button> ' +
|
---|
715 | '<button type="button" class="btn btn-primary" ng-click="confirm(1)">Yes' +
|
---|
716 | '</button></div>',
|
---|
717 | plain: true
|
---|
718 | }).then(function (success) {
|
---|
719 | var arr = vm.treeview.currentNode.calendar.down[0];
|
---|
720 | for (var i = 0; i < arr.length;) {
|
---|
721 | if (arr[i].rec.recid === recid)
|
---|
722 | vm.remove(i);
|
---|
723 | else
|
---|
724 | i++;
|
---|
725 | }
|
---|
726 | });
|
---|
727 | }
|
---|
728 |
|
---|
729 | //Moves calendar to selected date
|
---|
730 | $scope.goToDate = function () {
|
---|
731 | $("#resourcecalendar").fullCalendar('gotoDate', vm.currentcal.down[0][vm.selectedEventId].start);
|
---|
732 | }
|
---|
733 | /* Renders Tooltip */
|
---|
734 | $scope.eventRender = function (event, element, view) {
|
---|
735 | element.attr({
|
---|
736 | 'tooltip': event.title,
|
---|
737 | 'tooltip-append-to-body': true
|
---|
738 | });
|
---|
739 | //$compile(element)($scope);
|
---|
740 | };
|
---|
741 | //Calendar configuration
|
---|
742 | $scope.uiConfig = {
|
---|
743 | calendar: {
|
---|
744 | height: 500,
|
---|
745 | editable: true,
|
---|
746 | defaultView: 'agendaWeek',
|
---|
747 | firstDay: 1,
|
---|
748 | header: {
|
---|
749 | left: 'title',
|
---|
750 | center: 'agendaWeek, agendaDay',
|
---|
751 | right: 'today prev,next'
|
---|
752 | },
|
---|
753 | timezone: 'UTC',
|
---|
754 | timeFormat: 'HH:mm',
|
---|
755 | eventClick: $scope.eventClick,
|
---|
756 | eventDrop: $scope.dragandDrop,
|
---|
757 | eventResize: $scope.resizeEvent,
|
---|
758 | eventRender: $scope.eventRender,
|
---|
759 | dayClick: $scope.calendarClick
|
---|
760 |
|
---|
761 |
|
---|
762 |
|
---|
763 | }
|
---|
764 | };
|
---|
765 |
|
---|
766 |
|
---|
767 |
|
---|
768 |
|
---|
769 |
|
---|
770 | }).filter('disp', function () {
|
---|
771 | return function (input) {//Filter boolean to string
|
---|
772 | return input ? 'Disposable' : 'Not disposable';
|
---|
773 | }
|
---|
774 | }).directive('animateOnChange', function ($timeout) {
|
---|
775 | return function (scope, element, attr) {//Animation on downtime info to show change
|
---|
776 | scope.$watch(attr.animateOnChange, function (nv, ov) {
|
---|
777 |
|
---|
778 | if (nv != ov) {
|
---|
779 | if (scope.currentcal.down[0][scope.selectedEventId].title === "Unavailable") {
|
---|
780 | element.addClass('changed');
|
---|
781 | $timeout(function () {
|
---|
782 | element.removeClass('changed');
|
---|
783 | }, 250);
|
---|
784 | }
|
---|
785 | else {
|
---|
786 | element.addClass('changedshut');
|
---|
787 | $timeout(function () {
|
---|
788 | element.removeClass('changedshut');
|
---|
789 | }, 250);
|
---|
790 | }
|
---|
791 | }
|
---|
792 | });
|
---|
793 | };
|
---|
794 | });
|
---|
795 |
|
---|
796 |
|
---|