blob: 3ccfe04f76df1927a1126cc37b70b06181d001ac [file] [log] [blame]
Bri Prebilic Colee0311892015-05-15 11:27:15 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17(function () {
18 'use strict';
19
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070020 var bundleUrl = 'http://localhost:8080/rs/bundle',
21 userUrl = 'http://localhost:8080/rs/users',
22 family = 'family',
23 url_filter = 'url_filter';
24
Bri Prebilic Colee0311892015-05-15 11:27:15 -070025 angular.module('cordUser', [])
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070026 .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout',
27 function ($log, $scope, $resource, $timeout) {
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070028 var BundleData, bundleResource;
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070029 $scope.page = 'user';
30 $scope.isFamily = false;
31 $scope.newLevels = {};
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070032 $scope.showCheck = false;
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070033
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070034 // === Get data functions ---
35
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070036 BundleData = $resource(bundleUrl);
37 bundleResource = BundleData.get({},
38 // success
39 function () {
40 var result;
41 $scope.isFamily = (bundleResource.bundle.id === family);
42 if ($scope.isFamily) {
43 result = $.grep(
44 bundleResource.bundle.functions,
45 function (elem) {
46 if (elem.id === url_filter) { return true; }
47 }
48 );
49 $scope.levels = result[0].params.levels;
50 }
51 },
52 // error
53 function () {
54 $log.error('Problem with resource', bundleResource);
55 }
56 );
57
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070058 function getUsers(url) {
59 var UserData, userResource;
60 UserData = $resource(url);
61 userResource = UserData.get({},
62 // success
63 function () {
64 $scope.users = userResource.users;
65 },
66 // error
67 function () {
68 $log.error('Problem with resource', userResource);
69 }
70 );
71 }
72
73 getUsers(userUrl);
74
75 // === Form functions ---
76
77 function levelUrl(id, level) {
78 return userUrl + '/' + id + '/apply/url_filter/level/' + level;
79 }
80
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070081 $scope.applyChanges = function (changeLevels) {
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070082 var requests = [];
83
84 if ($scope.users) {
85 $.each($scope.users, function (index, user) {
86 var id = user.id,
87 level = user.profile.url_filter.level;
88 if ($scope.newLevels[id] !== level) {
89 requests.push(levelUrl(id, $scope.newLevels[id]));
90 }
91 });
92
93 $.each(requests, function (index, req) {
94 getUsers(req);
95 });
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070096 }
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070097 changeLevels.$setPristine();
98 $scope.showCheck = true;
99 $timeout(function () {
100 $scope.showCheck = false;
101 }, 3000);
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700102 };
103
104 $scope.cancelChanges = function (changeLevels) {
105 if ($scope.users) {
106 $.each($scope.users, function (index, user) {
107 $scope.newLevels[user.id] = user.profile.url_filter.level;
108 });
109 }
110 changeLevels.$setPristine();
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -0700111 $scope.showCheck = false;
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700112 };
Bri Prebilic Cole3c3361c2015-05-19 12:07:29 -0700113
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700114 $log.debug('Cord User Ctrl has been created.');
115 }]);
Bri Prebilic Cole8db16402015-05-18 13:50:31 -0700116
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700117}());