blob: bb44fec94ab3435a9b72f603037c1d1303bc915a [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 Coleba9d0522015-06-10 16:08:54 -070033 $scope.ratingsShown = false;
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070034
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070035 // === Get data functions ---
36
Bri Prebilic Coled189e282015-06-05 10:00:22 -070037 BundleData = $resource($scope.shared.url + bundleUrlSuffix);
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070038 bundleResource = BundleData.get({},
39 // success
40 function () {
41 var result;
42 $scope.isFamily = (bundleResource.bundle.id === family);
43 if ($scope.isFamily) {
44 result = $.grep(
45 bundleResource.bundle.functions,
46 function (elem) {
47 if (elem.id === url_filter) { return true; }
48 }
49 );
50 $scope.levels = result[0].params.levels;
51 }
52 },
53 // error
54 function () {
55 $log.error('Problem with resource', bundleResource);
56 }
57 );
58
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070059 function getUsers(url) {
60 var UserData, userResource;
61 UserData = $resource(url);
62 userResource = UserData.get({},
63 // success
64 function () {
65 $scope.users = userResource.users;
66 },
67 // error
68 function () {
69 $log.error('Problem with resource', userResource);
70 }
71 );
72 }
73
Bri Prebilic Coled189e282015-06-05 10:00:22 -070074 getUsers($scope.shared.url + userUrlSuffix);
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070075
76 // === Form functions ---
77
78 function levelUrl(id, level) {
Bri Prebilic Coled189e282015-06-05 10:00:22 -070079 return $scope.shared.url +
80 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070081 }
82
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070083 $scope.applyChanges = function (changeLevels) {
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -070084 var requests = [];
85
86 if ($scope.users) {
87 $.each($scope.users, function (index, user) {
88 var id = user.id,
89 level = user.profile.url_filter.level;
90 if ($scope.newLevels[id] !== level) {
91 requests.push(levelUrl(id, $scope.newLevels[id]));
92 }
93 });
94
95 $.each(requests, function (index, req) {
96 getUsers(req);
97 });
Bri Prebilic Colee2958f22015-05-26 17:18:23 -070098 }
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -070099 changeLevels.$setPristine();
100 $scope.showCheck = true;
101 $timeout(function () {
102 $scope.showCheck = false;
103 }, 3000);
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700104 };
105
106 $scope.cancelChanges = function (changeLevels) {
107 if ($scope.users) {
108 $.each($scope.users, function (index, user) {
109 $scope.newLevels[user.id] = user.profile.url_filter.level;
110 });
111 }
112 changeLevels.$setPristine();
Bri Prebilic Cole5d6153e2015-05-28 12:03:21 -0700113 $scope.showCheck = false;
Bri Prebilic Coled050a4b2015-05-27 15:53:30 -0700114 };
Bri Prebilic Cole3c3361c2015-05-19 12:07:29 -0700115
Bri Prebilic Coleba9d0522015-06-10 16:08:54 -0700116 $scope.showRatings = function () {
117 $scope.ratingsShown = !$scope.ratingsShown;
118 };
119
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700120 $log.debug('Cord User Ctrl has been created.');
Bri Prebilic Coleba9d0522015-06-10 16:08:54 -0700121 }])
122
123 .directive('ratingsPanel', ['$log', function ($log) {
124 return {
125 templateUrl: 'app/view/user/ratingPanel.html',
126 link: function (scope, elem, attrs) {
127 function fillSubMap(order, bool) {
128 var result = {};
129 $.each(order, function (index, cat) {
130 result[cat] = bool;
131 });
132 return result;
133 }
134 function processSubMap(prhbSites) {
135 var result = {};
136 $.each(prhbSites, function (index, cat) {
137 result[cat] = true;
138 });
139 return result;
140 }
141
142 function preprocess(data, order) {
143 return {
144 ALL: fillSubMap(order, false),
145 G: processSubMap(data.G),
146 PG: processSubMap(data.PG),
147 PG_13: processSubMap(data.PG_13),
148 R: processSubMap(data.R),
149 NONE: fillSubMap(order, true)
150 };
151 }
152
153 $.getJSON('/app/data/pc_cats.json', function (data) {
154 scope.level_order = data.level_order;
155 scope.category_order = data.category_order;
156 scope.prohibitedSites = preprocess(
157 data.prohibited, data.category_order
158 );
159 scope.$apply();
160 });
161 }
162 };
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700163 }]);
Bri Prebilic Cole8db16402015-05-18 13:50:31 -0700164
Bri Prebilic Colee0311892015-05-15 11:27:15 -0700165}());