blob: ecdf9701d1b9b9428fb99d010c263f0705ff9a34 [file] [log] [blame]
Matteo Scandolo4eb4ccb2016-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 Scandolo61bea662016-01-26 17:21:39 -080018 'use strict';
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080019
Matteo Scandolo61bea662016-01-26 17:21:39 -080020 var bundleUrlSuffix = '/rs/bundle',
21 userUrlSuffix = '/rs/users',
22 family = 'family',
23 url_filter = 'url_filter';
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080024
Matteo Scandolo61bea662016-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 Scandoloc6036502016-01-26 15:17:10 -080030
Matteo Scandolo61bea662016-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 Scandolo4eb4ccb2016-01-26 11:02:16 -080040
Matteo Scandolo61bea662016-01-26 17:21:39 -080041 // === Get data functions ---
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080042
Matteo Scandolo61bea662016-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 Scandolo4eb4ccb2016-01-26 11:02:16 -080052 }
Matteo Scandolo61bea662016-01-26 17:21:39 -080053 });
54 }
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080055
Matteo Scandolo61bea662016-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 Scandolo4eb4ccb2016-01-26 11:02:16 -080057
Matteo Scandolo61bea662016-01-26 17:21:39 -080058 // add an icon to the user
59 res.users.map(function(user){
60 user['icon_id'] = 'mom';
Matteo Scandolo61bea662016-01-26 17:21:39 -080061 return user;
62 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080063
Matteo Scandolo61bea662016-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 Scandolo4eb4ccb2016-01-26 11:02:16 -080076
Matteo Scandolo39c65a52016-01-27 12:00:18 -080077 $scope.updateLevel = function(user){
78 console.log(user);
79 user.$save()
80 .then(function(){
81 console.log('saved');
82 })
83 .catch(function(e){
84 throw new Error(e);
85 });
86 };
87
88 //function getUsers(url) {
89 // var UserData, userResource;
90 // UserData = $resource(url);
91 // userResource = UserData.get({},
92 // // success
93 // function () {
94 // $scope.users = userResource.users;
95 // if ($.isEmptyObject($scope.shared.userActivity)) {
96 // $scope.users.forEach(function (user) {
97 // var date = randomDate(new Date(2015, 0, 1),
98 // new Date());
99 //
100 // $scope.shared.userActivity[user.id] =
101 // $filter('date')(date, 'mediumTime');
102 // });
103 // }
104 // },
105 // // error
106 // function () {
107 // $log.error('Problem with resource', userResource);
108 // }
109 // );
110 //}
111 //
112 //getUsers($scope.shared.url + userUrlSuffix);
113
Matteo Scandolo61bea662016-01-26 17:21:39 -0800114 // === Form functions ---
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800115
Matteo Scandolo61bea662016-01-26 17:21:39 -0800116 function levelUrl(id, level) {
117 return $scope.shared.url +
118 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
119 }
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800120
Matteo Scandolo39c65a52016-01-27 12:00:18 -0800121 // NOTE This will trigger one request for each user to update url_filter level
Matteo Scandolo61bea662016-01-26 17:21:39 -0800122 $scope.applyChanges = function (changeLevels) {
123 var requests = [];
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800124
Matteo Scandolo61bea662016-01-26 17:21:39 -0800125 if ($scope.users) {
126 $.each($scope.users, function (index, user) {
127 var id = user.id,
128 level = user.profile.url_filter.level;
129 if ($scope.newLevels[id] !== level) {
130 requests.push(levelUrl(id, $scope.newLevels[id]));
131 }
132 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800133
Matteo Scandolo61bea662016-01-26 17:21:39 -0800134 $.each(requests, function (index, req) {
135 getUsers(req);
136 });
137 }
138 changeLevels.$setPristine();
139 $scope.showCheck = true;
140 $timeout(function () {
141 $scope.showCheck = false;
142 }, 3000);
143 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800144
Matteo Scandolo61bea662016-01-26 17:21:39 -0800145 $scope.cancelChanges = function (changeLevels) {
146 if ($scope.users) {
147 $.each($scope.users, function (index, user) {
148 $scope.newLevels[user.id] = user.profile.url_filter.level;
149 });
150 }
151 changeLevels.$setPristine();
152 $scope.showCheck = false;
153 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800154
Matteo Scandolo61bea662016-01-26 17:21:39 -0800155 $scope.showRatings = function () {
156 $scope.ratingsShown = !$scope.ratingsShown;
157 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800158
Matteo Scandolo61bea662016-01-26 17:21:39 -0800159 $log.debug('Cord User Ctrl has been created.');
160 }])
161
162 .directive('ratingsPanel', ['$log', function ($log) {
163 return {
164 templateUrl: 'app/view/user/ratingPanel.html',
165 link: function (scope, elem, attrs) {
166 function fillSubMap(order, bool) {
167 var result = {};
168 $.each(order, function (index, cat) {
169 result[cat] = bool;
170 });
171 return result;
172 }
173 function processSubMap(prhbSites) {
174 var result = {};
175 $.each(prhbSites, function (index, cat) {
176 result[cat] = true;
177 });
178 return result;
179 }
180
181 function preprocess(data, order) {
182 return {
183 ALL: fillSubMap(order, false),
184 G: processSubMap(data.G),
185 PG: processSubMap(data.PG),
186 PG_13: processSubMap(data.PG_13),
187 R: processSubMap(data.R),
188 NONE: fillSubMap(order, true)
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800189 };
Matteo Scandolo61bea662016-01-26 17:21:39 -0800190 }
191
192 $.getJSON('/app/data/pc_cats.json', function (data) {
193 scope.level_order = data.level_order;
194 scope.category_order = data.category_order;
195 scope.prohibitedSites = preprocess(
196 data.prohibited, data.category_order
197 );
198 scope.$apply();
199 });
200 }
201 };
202 }]);
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800203
204}());