blob: 971838c079f0847ceb67a387c4b192c22bf96ff4 [file] [log] [blame]
Matteo Scandolo5d568612016-01-26 11:02:16 -08001/*
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
20 var bundleUrlSuffix = '/rs/bundle',
21 userUrlSuffix = '/rs/users',
22 family = 'family',
23 url_filter = 'url_filter';
24
Matteo Scandolo2b545462016-01-26 15:17:10 -080025 function randomDate(start, end) {
26 return new Date(
27 start.getTime() + Math.random() * (end.getTime() - start.getTime())
28 );
29 }
30
Matteo Scandolo5d568612016-01-26 11:02:16 -080031 angular.module('cordUser', [])
Matteo Scandolo2b545462016-01-26 15:17:10 -080032 .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', '$filter',
33 function ($log, $scope, $resource, $timeout, $filter) {
Matteo Scandolo5d568612016-01-26 11:02:16 -080034 var BundleData, bundleResource;
35 $scope.page.curr = 'user';
36 $scope.isFamily = false;
37 $scope.newLevels = {};
38 $scope.showCheck = false;
39 $scope.ratingsShown = false;
40
41 // === Get data functions ---
42
43 BundleData = $resource($scope.shared.url + bundleUrlSuffix);
44 bundleResource = BundleData.get({},
45 // success
46 function () {
47 var result;
48 $scope.isFamily = (bundleResource.bundle.id === family);
49 if ($scope.isFamily) {
50 result = $.grep(
51 bundleResource.bundle.functions,
52 function (elem) {
53 if (elem.id === url_filter) { return true; }
54 }
55 );
56 $scope.levels = result[0].params.levels;
57 }
58 },
59 // error
60 function () {
61 $log.error('Problem with resource', bundleResource);
62 }
63 );
64
65 function getUsers(url) {
66 var UserData, userResource;
67 UserData = $resource(url);
68 userResource = UserData.get({},
69 // success
70 function () {
71 $scope.users = userResource.users;
Matteo Scandolo2b545462016-01-26 15:17:10 -080072 if ($.isEmptyObject($scope.shared.userActivity)) {
73 $scope.users.forEach(function (user) {
74 var date = randomDate(new Date(2015, 0, 1),
75 new Date());
76
77 $scope.shared.userActivity[user.id] =
78 $filter('date')(date, 'mediumTime');
79 });
80 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080081 },
82 // error
83 function () {
84 $log.error('Problem with resource', userResource);
85 }
86 );
87 }
88
89 getUsers($scope.shared.url + userUrlSuffix);
90
91 // === Form functions ---
92
93 function levelUrl(id, level) {
94 return $scope.shared.url +
95 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
96 }
97
98 $scope.applyChanges = function (changeLevels) {
99 var requests = [];
100
101 if ($scope.users) {
102 $.each($scope.users, function (index, user) {
103 var id = user.id,
104 level = user.profile.url_filter.level;
105 if ($scope.newLevels[id] !== level) {
106 requests.push(levelUrl(id, $scope.newLevels[id]));
107 }
108 });
109
110 $.each(requests, function (index, req) {
111 getUsers(req);
112 });
113 }
114 changeLevels.$setPristine();
115 $scope.showCheck = true;
116 $timeout(function () {
117 $scope.showCheck = false;
118 }, 3000);
119 };
120
121 $scope.cancelChanges = function (changeLevels) {
122 if ($scope.users) {
123 $.each($scope.users, function (index, user) {
124 $scope.newLevels[user.id] = user.profile.url_filter.level;
125 });
126 }
127 changeLevels.$setPristine();
128 $scope.showCheck = false;
129 };
130
131 $scope.showRatings = function () {
132 $scope.ratingsShown = !$scope.ratingsShown;
133 };
134
135 $log.debug('Cord User Ctrl has been created.');
136 }])
137
138 .directive('ratingsPanel', ['$log', function ($log) {
139 return {
140 templateUrl: 'app/view/user/ratingPanel.html',
141 link: function (scope, elem, attrs) {
142 function fillSubMap(order, bool) {
143 var result = {};
144 $.each(order, function (index, cat) {
145 result[cat] = bool;
146 });
147 return result;
148 }
149 function processSubMap(prhbSites) {
150 var result = {};
151 $.each(prhbSites, function (index, cat) {
152 result[cat] = true;
153 });
154 return result;
155 }
156
157 function preprocess(data, order) {
158 return {
159 ALL: fillSubMap(order, false),
160 G: processSubMap(data.G),
161 PG: processSubMap(data.PG),
162 PG_13: processSubMap(data.PG_13),
163 R: processSubMap(data.R),
164 NONE: fillSubMap(order, true)
165 };
166 }
167
168 $.getJSON('/app/data/pc_cats.json', function (data) {
169 scope.level_order = data.level_order;
170 scope.category_order = data.category_order;
171 scope.prohibitedSites = preprocess(
172 data.prohibited, data.category_order
173 );
174 scope.$apply();
175 });
176 }
177 };
178 }]);
179
180}());