blob: bc75afd4cda50f947c71d31fe4e10c70bf54f84a [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 angular.module('cordUser', [])
26 .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', '$filter', 'SubscriberUsers', 'cordConfig',
27 function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig) {
Matteo Scandolodd1e1902016-01-27 14:30:22 -080028
Matteo Scandolo680cd922016-01-26 17:21:39 -080029 $scope.page.curr = 'user';
30 $scope.isFamily = false;
31 $scope.newLevels = {};
32 $scope.showCheck = false;
33 $scope.ratingsShown = false;
Matteo Scandolo5d568612016-01-26 11:02:16 -080034
Matteo Scandolo680cd922016-01-26 17:21:39 -080035 // === Get data functions ---
Matteo Scandolo5d568612016-01-26 11:02:16 -080036
Matteo Scandolo680cd922016-01-26 17:21:39 -080037 // NOTE subscriberId should be retrieved by login
38 SubscriberUsers.query({subscriberId: 1}).$promise
39 .then(function(res){
Matteo Scandolodd1e1902016-01-27 14:30:22 -080040 $scope.isFamily = cordConfig.bundles[cordConfig.activeBundle].id === 'family';
Matteo Scandolo680cd922016-01-26 17:21:39 -080041 // if bundle is family search for url_filter level
42 if ($scope.isFamily) {
Matteo Scandolodd1e1902016-01-27 14:30:22 -080043 angular.forEach(cordConfig.bundles[cordConfig.activeBundle].functions, function(fn){
Matteo Scandolo680cd922016-01-26 17:21:39 -080044 if(fn.id === 'url_filter'){
45 $scope.levels = fn.params.levels;
Matteo Scandolo5d568612016-01-26 11:02:16 -080046 }
Matteo Scandolo680cd922016-01-26 17:21:39 -080047 });
48 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080049
Matteo Scandolo680cd922016-01-26 17:21:39 -080050 $scope.users = res.users;
51 })
52 .catch(function () {
Matteo Scandolodd1e1902016-01-27 14:30:22 -080053 $log.error('Problem with resource', SubscriberUsers);
Matteo Scandolo680cd922016-01-26 17:21:39 -080054 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080055
Matteo Scandolobc566852016-01-27 12:00:18 -080056 $scope.updateLevel = function(user){
Matteo Scandolobc566852016-01-27 12:00:18 -080057 user.$save()
58 .then(function(){
59 console.log('saved');
60 })
61 .catch(function(e){
62 throw new Error(e);
63 });
64 };
65
66 //function getUsers(url) {
67 // var UserData, userResource;
68 // UserData = $resource(url);
69 // userResource = UserData.get({},
70 // // success
71 // function () {
72 // $scope.users = userResource.users;
73 // if ($.isEmptyObject($scope.shared.userActivity)) {
74 // $scope.users.forEach(function (user) {
75 // var date = randomDate(new Date(2015, 0, 1),
76 // new Date());
77 //
78 // $scope.shared.userActivity[user.id] =
79 // $filter('date')(date, 'mediumTime');
80 // });
81 // }
82 // },
83 // // error
84 // function () {
85 // $log.error('Problem with resource', userResource);
86 // }
87 // );
88 //}
89 //
90 //getUsers($scope.shared.url + userUrlSuffix);
91
Matteo Scandolo680cd922016-01-26 17:21:39 -080092 // === Form functions ---
Matteo Scandolo5d568612016-01-26 11:02:16 -080093
Matteo Scandolo680cd922016-01-26 17:21:39 -080094 function levelUrl(id, level) {
95 return $scope.shared.url +
96 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
97 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080098
Matteo Scandolobc566852016-01-27 12:00:18 -080099 // NOTE This will trigger one request for each user to update url_filter level
Matteo Scandolo680cd922016-01-26 17:21:39 -0800100 $scope.applyChanges = function (changeLevels) {
101 var requests = [];
Matteo Scandolo5d568612016-01-26 11:02:16 -0800102
Matteo Scandolo680cd922016-01-26 17:21:39 -0800103 if ($scope.users) {
104 $.each($scope.users, function (index, user) {
105 var id = user.id,
106 level = user.profile.url_filter.level;
107 if ($scope.newLevels[id] !== level) {
108 requests.push(levelUrl(id, $scope.newLevels[id]));
109 }
110 });
Matteo Scandolo5d568612016-01-26 11:02:16 -0800111
Matteo Scandolo680cd922016-01-26 17:21:39 -0800112 $.each(requests, function (index, req) {
113 getUsers(req);
114 });
115 }
116 changeLevels.$setPristine();
117 $scope.showCheck = true;
118 $timeout(function () {
119 $scope.showCheck = false;
120 }, 3000);
121 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800122
Matteo Scandolo680cd922016-01-26 17:21:39 -0800123 $scope.cancelChanges = function (changeLevels) {
124 if ($scope.users) {
125 $.each($scope.users, function (index, user) {
126 $scope.newLevels[user.id] = user.profile.url_filter.level;
127 });
128 }
129 changeLevels.$setPristine();
130 $scope.showCheck = false;
131 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800132
Matteo Scandolo680cd922016-01-26 17:21:39 -0800133 $scope.showRatings = function () {
134 $scope.ratingsShown = !$scope.ratingsShown;
135 };
Matteo Scandolo5d568612016-01-26 11:02:16 -0800136
Matteo Scandolo680cd922016-01-26 17:21:39 -0800137 $log.debug('Cord User Ctrl has been created.');
138 }])
139
140 .directive('ratingsPanel', ['$log', function ($log) {
141 return {
142 templateUrl: 'app/view/user/ratingPanel.html',
143 link: function (scope, elem, attrs) {
144 function fillSubMap(order, bool) {
145 var result = {};
146 $.each(order, function (index, cat) {
147 result[cat] = bool;
148 });
149 return result;
150 }
151 function processSubMap(prhbSites) {
152 var result = {};
153 $.each(prhbSites, function (index, cat) {
154 result[cat] = true;
155 });
156 return result;
157 }
158
159 function preprocess(data, order) {
160 return {
161 ALL: fillSubMap(order, false),
162 G: processSubMap(data.G),
163 PG: processSubMap(data.PG),
164 PG_13: processSubMap(data.PG_13),
165 R: processSubMap(data.R),
166 NONE: fillSubMap(order, true)
Matteo Scandolo5d568612016-01-26 11:02:16 -0800167 };
Matteo Scandolo680cd922016-01-26 17:21:39 -0800168 }
169
170 $.getJSON('/app/data/pc_cats.json', function (data) {
171 scope.level_order = data.level_order;
172 scope.category_order = data.category_order;
173 scope.prohibitedSites = preprocess(
174 data.prohibited, data.category_order
175 );
176 scope.$apply();
177 });
178 }
179 };
180 }]);
Matteo Scandolo5d568612016-01-26 11:02:16 -0800181
182}());