blob: 76bf16aa7c96a30225fe86950474d100471d25ec [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 Coled189e282015-06-05 10:00:22 -070020 var bundleUrlSuffix = '/rs/bundle',
21 userUrlSuffix = '/rs/users',
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070022 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 Cole60553c82015-06-05 08:50:17 -070029 $scope.page.curr = 'user';
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070030 $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 Coled189e282015-06-05 10:00:22 -070036 BundleData = $resource($scope.shared.url + bundleUrlSuffix);
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070037 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
Bri Prebilic Coled189e282015-06-05 10:00:22 -070073 getUsers($scope.shared.url + userUrlSuffix);
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070074
75 // === Form functions ---
76
77 function levelUrl(id, level) {
Bri Prebilic Coled189e282015-06-05 10:00:22 -070078 return $scope.shared.url +
79 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070080 }
81
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070082 $scope.applyChanges = function (changeLevels) {
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070083 var requests = [];
84
85 if ($scope.users) {
86 $.each($scope.users, function (index, user) {
87 var id = user.id,
88 level = user.profile.url_filter.level;
89 if ($scope.newLevels[id] !== level) {
90 requests.push(levelUrl(id, $scope.newLevels[id]));
91 }
92 });
93
94 $.each(requests, function (index, req) {
95 getUsers(req);
96 });
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070097 }
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070098 changeLevels.$setPristine();
99 $scope.showCheck = true;
100 $timeout(function () {
101 $scope.showCheck = false;
102 }, 3000);
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700103 };
104
105 $scope.cancelChanges = function (changeLevels) {
106 if ($scope.users) {
107 $.each($scope.users, function (index, user) {
108 $scope.newLevels[user.id] = user.profile.url_filter.level;
109 });
110 }
111 changeLevels.$setPristine();
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -0700112 $scope.showCheck = false;
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700113 };
Bri Prebilic Cole3c3361c2015-05-19 12:07:29 -0700114
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700115 $log.debug('Cord User Ctrl has been created.');
116 }]);
Bri Prebilic Cole8db16402015-05-18 13:50:31 -0700117
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700118}());