blob: 4d69269dd105b4bafa529bb6dbf18c5df60f642c [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';
61 // NOTE mock data, waiting for #CORD-516
62 var levels = ['R', 'PG', 'PG-13'];
63 user.profile = {
64 url_filter: {
65 level: levels[Math.floor(Math.random() * levels.length)]
Matteo Scandolo5d568612016-01-26 11:02:16 -080066 }
Matteo Scandolo680cd922016-01-26 17:21:39 -080067 };
68 return user;
69 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080070
Matteo Scandolo680cd922016-01-26 17:21:39 -080071 // add a random login date to the user
72 res.users.forEach(function(user){
73 if(!angular.isDefined(cordConfig.userActivity[user.id])){
74 var date = randomDate(new Date(2015, 0, 1), new Date());
75 cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime');
76 }
77 });
78 $scope.users = res.users;
79 })
80 .catch(function () {
81 $log.error('Problem with resource', bundleResource);
82 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080083
Matteo Scandolo680cd922016-01-26 17:21:39 -080084 // === Form functions ---
Matteo Scandolo5d568612016-01-26 11:02:16 -080085
Matteo Scandolo680cd922016-01-26 17:21:39 -080086 function levelUrl(id, level) {
87 return $scope.shared.url +
88 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
89 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080090
Matteo Scandolo680cd922016-01-26 17:21:39 -080091 $scope.applyChanges = function (changeLevels) {
92 var requests = [];
Matteo Scandolo5d568612016-01-26 11:02:16 -080093
Matteo Scandolo680cd922016-01-26 17:21:39 -080094 if ($scope.users) {
95 $.each($scope.users, function (index, user) {
96 var id = user.id,
97 level = user.profile.url_filter.level;
98 if ($scope.newLevels[id] !== level) {
99 requests.push(levelUrl(id, $scope.newLevels[id]));
100 }
101 });
Matteo Scandolo5d568612016-01-26 11:02:16 -0800102
Matteo Scandolo680cd922016-01-26 17:21:39 -0800103 $.each(requests, function (index, req) {
104 getUsers(req);
105 });
106 }
107 changeLevels.$setPristine();
108 $scope.showCheck = true;
109 $timeout(function () {
110 $scope.showCheck = false;
111 }, 3000);
112 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800113
Matteo Scandolo680cd922016-01-26 17:21:39 -0800114 $scope.cancelChanges = function (changeLevels) {
115 if ($scope.users) {
116 $.each($scope.users, function (index, user) {
117 $scope.newLevels[user.id] = user.profile.url_filter.level;
118 });
119 }
120 changeLevels.$setPristine();
121 $scope.showCheck = false;
122 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800123
Matteo Scandolo680cd922016-01-26 17:21:39 -0800124 $scope.showRatings = function () {
125 $scope.ratingsShown = !$scope.ratingsShown;
126 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800127
Matteo Scandolo680cd922016-01-26 17:21:39 -0800128 $log.debug('Cord User Ctrl has been created.');
129 }])
130
131 .directive('ratingsPanel', ['$log', function ($log) {
132 return {
133 templateUrl: 'app/view/user/ratingPanel.html',
134 link: function (scope, elem, attrs) {
135 function fillSubMap(order, bool) {
136 var result = {};
137 $.each(order, function (index, cat) {
138 result[cat] = bool;
139 });
140 return result;
141 }
142 function processSubMap(prhbSites) {
143 var result = {};
144 $.each(prhbSites, function (index, cat) {
145 result[cat] = true;
146 });
147 return result;
148 }
149
150 function preprocess(data, order) {
151 return {
152 ALL: fillSubMap(order, false),
153 G: processSubMap(data.G),
154 PG: processSubMap(data.PG),
155 PG_13: processSubMap(data.PG_13),
156 R: processSubMap(data.R),
157 NONE: fillSubMap(order, true)
Matteo Scandolo5d568612016-01-26 11:02:16 -0800158 };
Matteo Scandolo680cd922016-01-26 17:21:39 -0800159 }
160
161 $.getJSON('/app/data/pc_cats.json', function (data) {
162 scope.level_order = data.level_order;
163 scope.category_order = data.category_order;
164 scope.prohibitedSites = preprocess(
165 data.prohibited, data.category_order
166 );
167 scope.$apply();
168 });
169 }
170 };
171 }]);
Matteo Scandolo5d568612016-01-26 11:02:16 -0800172
173}());