blob: 579cac78fea2b327c4860db2c63e3aa6188daec0 [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 angular.module('cordUser', [])
Matteo Scandolo90621b32016-01-27 16:40:21 -080021 .controller('CordUserCtrl', function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig, SubscriberUsersUrlFilterLevel) {
Matteo Scandolo10fb87c2016-01-27 14:30:22 -080022
Matteo Scandoloe0628502016-01-27 14:42:42 -080023 $scope.page.curr = 'user';
24 $scope.isFamily = false;
25 $scope.newLevels = {};
26 $scope.showCheck = false;
27 $scope.ratingsShown = false;
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080028
Matteo Scandoloe0628502016-01-27 14:42:42 -080029 // NOTE subscriberId should be retrieved by login
30 SubscriberUsers.query({subscriberId: 1}).$promise
31 .then(function(res){
32 $scope.isFamily = cordConfig.bundles[cordConfig.activeBundle].id === 'family';
33 // if bundle is family search for url_filter level
34 if ($scope.isFamily) {
35 angular.forEach(cordConfig.bundles[cordConfig.activeBundle].functions, function(fn){
36 if(fn.id === 'url_filter'){
37 $scope.levels = fn.params.levels;
Matteo Scandolo61bea662016-01-26 17:21:39 -080038 }
39 });
Matteo Scandolo61bea662016-01-26 17:21:39 -080040 }
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080041
Matteo Scandoloe0628502016-01-27 14:42:42 -080042 $scope.users = res.users;
43 })
44 .catch(function () {
45 $log.error('Problem with resource', SubscriberUsers);
46 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080047
Matteo Scandoloe0628502016-01-27 14:42:42 -080048 $scope.updateLevel = function(user){
49 // TODO save this data and show a confirmation to the user
Matteo Scandolo90621b32016-01-27 16:40:21 -080050 // NOTE subscriberId should be retrieved by login
51 SubscriberUsersUrlFilterLevel.updateUrlFilterLevel(1, user.id, user.level)
Matteo Scandoloe0628502016-01-27 14:42:42 -080052 .then(function(){
Matteo Scandolo90621b32016-01-27 16:40:21 -080053 user.updated = true;
Matteo Scandoloe0628502016-01-27 14:42:42 -080054 })
55 .catch(function(e){
56 throw new Error(e);
57 });
58 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080059
Matteo Scandoloe0628502016-01-27 14:42:42 -080060 $scope.showRatings = function () {
61 $scope.ratingsShown = !$scope.ratingsShown;
62 };
Matteo Scandolo61bea662016-01-26 17:21:39 -080063
Matteo Scandoloe0628502016-01-27 14:42:42 -080064 $log.debug('Cord User Ctrl has been created.');
65 })
Matteo Scandolo90621b32016-01-27 16:40:21 -080066 .directive('userUpdatedTick', function($timeout){
67 return {
68 restric: 'E',
69 scope: {
70 user: '='
71 },
72 template: '<span class="icon-saved" ng-show="saved"></span>',
73 link: function(scope, elem){
74 scope.saved = false;
75 scope.$watch('user.updated', function(val){
76 if(val){
77 scope.saved = true;
78 $timeout(function(){
79 scope.saved = false;
80 }, 3000);
81 }
82 });
83 }
84 }
85 })
Matteo Scandoloe0628502016-01-27 14:42:42 -080086 .directive('ratingsPanel', function ($log) {
Matteo Scandolo61bea662016-01-26 17:21:39 -080087 return {
88 templateUrl: 'app/view/user/ratingPanel.html',
89 link: function (scope, elem, attrs) {
90 function fillSubMap(order, bool) {
91 var result = {};
92 $.each(order, function (index, cat) {
93 result[cat] = bool;
94 });
95 return result;
96 }
97 function processSubMap(prhbSites) {
98 var result = {};
99 $.each(prhbSites, function (index, cat) {
100 result[cat] = true;
101 });
102 return result;
103 }
104
105 function preprocess(data, order) {
106 return {
107 ALL: fillSubMap(order, false),
108 G: processSubMap(data.G),
109 PG: processSubMap(data.PG),
110 PG_13: processSubMap(data.PG_13),
111 R: processSubMap(data.R),
112 NONE: fillSubMap(order, true)
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800113 };
Matteo Scandolo61bea662016-01-26 17:21:39 -0800114 }
115
116 $.getJSON('/app/data/pc_cats.json', function (data) {
117 scope.level_order = data.level_order;
118 scope.category_order = data.category_order;
119 scope.prohibitedSites = preprocess(
120 data.prohibited, data.category_order
121 );
122 scope.$apply();
123 });
124 }
125 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800126 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800127}());