blob: 8e97c015b2de51e032fc3c5e0943375e30d50973 [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 Colee2958f22015-05-26 17:18:23 -070026 .controller('CordUserCtrl', ['$log', '$scope', '$resource',
27 function ($log, $scope, $resource) {
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 = {};
32
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070033 // === Get data functions ---
34
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070035 BundleData = $resource(bundleUrl);
36 bundleResource = BundleData.get({},
37 // success
38 function () {
39 var result;
40 $scope.isFamily = (bundleResource.bundle.id === family);
41 if ($scope.isFamily) {
42 result = $.grep(
43 bundleResource.bundle.functions,
44 function (elem) {
45 if (elem.id === url_filter) { return true; }
46 }
47 );
48 $scope.levels = result[0].params.levels;
49 }
50 },
51 // error
52 function () {
53 $log.error('Problem with resource', bundleResource);
54 }
55 );
56
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070057 function getUsers(url) {
58 var UserData, userResource;
59 UserData = $resource(url);
60 userResource = UserData.get({},
61 // success
62 function () {
63 $scope.users = userResource.users;
64 },
65 // error
66 function () {
67 $log.error('Problem with resource', userResource);
68 }
69 );
70 }
71
72 getUsers(userUrl);
73
74 // === Form functions ---
75
76 function levelUrl(id, level) {
77 return userUrl + '/' + id + '/apply/url_filter/level/' + level;
78 }
79
80 $scope.applyChanges = function () {
81 var requests = [];
82
83 if ($scope.users) {
84 $.each($scope.users, function (index, user) {
85 var id = user.id,
86 level = user.profile.url_filter.level;
87 if ($scope.newLevels[id] !== level) {
88 requests.push(levelUrl(id, $scope.newLevels[id]));
89 }
90 });
91
92 $.each(requests, function (index, req) {
93 getUsers(req);
94 });
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070095 }
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070096 };
97
98 $scope.cancelChanges = function (changeLevels) {
99 if ($scope.users) {
100 $.each($scope.users, function (index, user) {
101 $scope.newLevels[user.id] = user.profile.url_filter.level;
102 });
103 }
104 changeLevels.$setPristine();
105 };
Bri Prebilic Cole3c3361c2015-05-19 12:07:29 -0700106
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700107 $log.debug('Cord User Ctrl has been created.');
108 }]);
Bri Prebilic Cole8db16402015-05-18 13:50:31 -0700109
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700110}());