blob: f7450cbebc566a3a78f490651abb58e39501c59b [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 () {
Matteo Scandolo680cd922016-01-26 17:21:39 -080018 'use strict';
Matteo Scandolo5d568612016-01-26 11:02:16 -080019
Matteo Scandolo680cd922016-01-26 17:21:39 -080020 var bundleUrlSuffix = '/rs/bundle',
21 userUrlSuffix = '/rs/users',
22 family = 'family',
23 url_filter = 'url_filter';
Matteo Scandolo5d568612016-01-26 11:02:16 -080024
Matteo Scandolo680cd922016-01-26 17:21:39 -080025 function randomDate(start, end) {
26 return new Date(
27 start.getTime() + Math.random() * (end.getTime() - start.getTime())
28 );
29 }
Matteo Scandolo2b545462016-01-26 15:17:10 -080030
Matteo Scandolo680cd922016-01-26 17:21:39 -080031 angular.module('cordUser', [])
32 .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', '$filter', 'SubscriberUsers', 'cordConfig',
33 function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig) {
34 var BundleData, bundleResource;
35 $scope.page.curr = 'user';
36 $scope.isFamily = false;
37 $scope.newLevels = {};
38 $scope.showCheck = false;
39 $scope.ratingsShown = false;
Matteo Scandolo5d568612016-01-26 11:02:16 -080040
Matteo Scandolo680cd922016-01-26 17:21:39 -080041 // === Get data functions ---
Matteo Scandolo5d568612016-01-26 11:02:16 -080042
Matteo Scandolo680cd922016-01-26 17:21:39 -080043 // NOTE subscriberId should be retrieved by login
44 SubscriberUsers.query({subscriberId: 1}).$promise
45 .then(function(res){
46 $scope.isFamily = cordConfig.bundles[0].id === 'family';
47 // if bundle is family search for url_filter level
48 if ($scope.isFamily) {
49 angular.forEach(cordConfig.bundles[0].functions, function(fn){
50 if(fn.id === 'url_filter'){
51 $scope.levels = fn.params.levels;
Matteo Scandolo5d568612016-01-26 11:02:16 -080052 }
Matteo Scandolo680cd922016-01-26 17:21:39 -080053 });
54 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080055
Matteo Scandolo680cd922016-01-26 17:21:39 -080056 // NOTE the loops creates data that are not available in xos should we move them in a service? Should we define a small backend to store this infos?
Matteo Scandolo5d568612016-01-26 11:02:16 -080057
Matteo Scandolo680cd922016-01-26 17:21:39 -080058 // add an icon to the user
59 res.users.map(function(user){
60 user['icon_id'] = 'mom';
Matteo Scandolo680cd922016-01-26 17:21:39 -080061 return user;
62 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080063
Matteo Scandolo680cd922016-01-26 17:21:39 -080064 // add a random login date to the user
65 res.users.forEach(function(user){
66 if(!angular.isDefined(cordConfig.userActivity[user.id])){
67 var date = randomDate(new Date(2015, 0, 1), new Date());
68 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
69 }
70 });
71 $scope.users = res.users;
72 })
73 .catch(function () {
74 $log.error('Problem with resource', bundleResource);
75 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080076
Matteo Scandolobc566852016-01-27 12:00:18 -080077 $scope.updateLevel = function(user){
Matteo Scandolobc566852016-01-27 12:00:18 -080078 user.$save()
79 .then(function(){
80 console.log('saved');
81 })
82 .catch(function(e){
83 throw new Error(e);
84 });
85 };
86
87 //function getUsers(url) {
88 // var UserData, userResource;
89 // UserData = $resource(url);
90 // userResource = UserData.get({},
91 // // success
92 // function () {
93 // $scope.users = userResource.users;
94 // if ($.isEmptyObject($scope.shared.userActivity)) {
95 // $scope.users.forEach(function (user) {
96 // var date = randomDate(new Date(2015, 0, 1),
97 // new Date());
98 //
99 // $scope.shared.userActivity[user.id] =
100 // $filter('date')(date, 'mediumTime');
101 // });
102 // }
103 // },
104 // // error
105 // function () {
106 // $log.error('Problem with resource', userResource);
107 // }
108 // );
109 //}
110 //
111 //getUsers($scope.shared.url + userUrlSuffix);
112
Matteo Scandolo680cd922016-01-26 17:21:39 -0800113 // === Form functions ---
Matteo Scandolo5d568612016-01-26 11:02:16 -0800114
Matteo Scandolo680cd922016-01-26 17:21:39 -0800115 function levelUrl(id, level) {
116 return $scope.shared.url +
117 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
118 }
Matteo Scandolo5d568612016-01-26 11:02:16 -0800119
Matteo Scandolobc566852016-01-27 12:00:18 -0800120 // NOTE This will trigger one request for each user to update url_filter level
Matteo Scandolo680cd922016-01-26 17:21:39 -0800121 $scope.applyChanges = function (changeLevels) {
122 var requests = [];
Matteo Scandolo5d568612016-01-26 11:02:16 -0800123
Matteo Scandolo680cd922016-01-26 17:21:39 -0800124 if ($scope.users) {
125 $.each($scope.users, function (index, user) {
126 var id = user.id,
127 level = user.profile.url_filter.level;
128 if ($scope.newLevels[id] !== level) {
129 requests.push(levelUrl(id, $scope.newLevels[id]));
130 }
131 });
Matteo Scandolo5d568612016-01-26 11:02:16 -0800132
Matteo Scandolo680cd922016-01-26 17:21:39 -0800133 $.each(requests, function (index, req) {
134 getUsers(req);
135 });
136 }
137 changeLevels.$setPristine();
138 $scope.showCheck = true;
139 $timeout(function () {
140 $scope.showCheck = false;
141 }, 3000);
142 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800143
Matteo Scandolo680cd922016-01-26 17:21:39 -0800144 $scope.cancelChanges = function (changeLevels) {
145 if ($scope.users) {
146 $.each($scope.users, function (index, user) {
147 $scope.newLevels[user.id] = user.profile.url_filter.level;
148 });
149 }
150 changeLevels.$setPristine();
151 $scope.showCheck = false;
152 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800153
Matteo Scandolo680cd922016-01-26 17:21:39 -0800154 $scope.showRatings = function () {
155 $scope.ratingsShown = !$scope.ratingsShown;
156 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800157
Matteo Scandolo680cd922016-01-26 17:21:39 -0800158 $log.debug('Cord User Ctrl has been created.');
159 }])
160
161 .directive('ratingsPanel', ['$log', function ($log) {
162 return {
163 templateUrl: 'app/view/user/ratingPanel.html',
164 link: function (scope, elem, attrs) {
165 function fillSubMap(order, bool) {
166 var result = {};
167 $.each(order, function (index, cat) {
168 result[cat] = bool;
169 });
170 return result;
171 }
172 function processSubMap(prhbSites) {
173 var result = {};
174 $.each(prhbSites, function (index, cat) {
175 result[cat] = true;
176 });
177 return result;
178 }
179
180 function preprocess(data, order) {
181 return {
182 ALL: fillSubMap(order, false),
183 G: processSubMap(data.G),
184 PG: processSubMap(data.PG),
185 PG_13: processSubMap(data.PG_13),
186 R: processSubMap(data.R),
187 NONE: fillSubMap(order, true)
Matteo Scandolo5d568612016-01-26 11:02:16 -0800188 };
Matteo Scandolo680cd922016-01-26 17:21:39 -0800189 }
190
191 $.getJSON('/app/data/pc_cats.json', function (data) {
192 scope.level_order = data.level_order;
193 scope.category_order = data.category_order;
194 scope.prohibitedSites = preprocess(
195 data.prohibited, data.category_order
196 );
197 scope.$apply();
198 });
199 }
200 };
201 }]);
Matteo Scandolo5d568612016-01-26 11:02:16 -0800202
203}());