blob: d1ab8389e3ca3a1d07e920cdfd83b3a3e1adc1cc [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 Scandolo61f05f52016-01-28 09:41:01 -080041 $scope.users = res;
Matteo Scandoloe0628502016-01-27 14:42:42 -080042 })
43 .catch(function () {
44 $log.error('Problem with resource', SubscriberUsers);
45 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080046
Matteo Scandoloe0628502016-01-27 14:42:42 -080047 $scope.updateLevel = function(user){
48 // TODO save this data and show a confirmation to the user
Matteo Scandolo90621b32016-01-27 16:40:21 -080049 // NOTE subscriberId should be retrieved by login
50 SubscriberUsersUrlFilterLevel.updateUrlFilterLevel(1, user.id, user.level)
Matteo Scandoloe0628502016-01-27 14:42:42 -080051 .then(function(){
Matteo Scandolo90621b32016-01-27 16:40:21 -080052 user.updated = true;
Matteo Scandoloe0628502016-01-27 14:42:42 -080053 })
54 .catch(function(e){
55 throw new Error(e);
56 });
57 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080058
Matteo Scandoloe0628502016-01-27 14:42:42 -080059 $scope.showRatings = function () {
60 $scope.ratingsShown = !$scope.ratingsShown;
61 };
Matteo Scandolo61bea662016-01-26 17:21:39 -080062
Matteo Scandoloe0628502016-01-27 14:42:42 -080063 $log.debug('Cord User Ctrl has been created.');
64 })
Matteo Scandolo90621b32016-01-27 16:40:21 -080065 .directive('userUpdatedTick', function($timeout){
66 return {
67 restric: 'E',
68 scope: {
69 user: '='
70 },
71 template: '<span class="icon-saved" ng-show="saved"></span>',
72 link: function(scope, elem){
73 scope.saved = false;
74 scope.$watch('user.updated', function(val){
75 if(val){
76 scope.saved = true;
77 $timeout(function(){
78 scope.saved = false;
79 }, 3000);
80 }
81 });
82 }
83 }
84 })
Matteo Scandoloe0628502016-01-27 14:42:42 -080085 .directive('ratingsPanel', function ($log) {
Matteo Scandolo61bea662016-01-26 17:21:39 -080086 return {
87 templateUrl: 'app/view/user/ratingPanel.html',
88 link: function (scope, elem, attrs) {
89 function fillSubMap(order, bool) {
90 var result = {};
91 $.each(order, function (index, cat) {
92 result[cat] = bool;
93 });
94 return result;
95 }
96 function processSubMap(prhbSites) {
97 var result = {};
98 $.each(prhbSites, function (index, cat) {
99 result[cat] = true;
100 });
101 return result;
102 }
103
104 function preprocess(data, order) {
105 return {
106 ALL: fillSubMap(order, false),
107 G: processSubMap(data.G),
108 PG: processSubMap(data.PG),
109 PG_13: processSubMap(data.PG_13),
110 R: processSubMap(data.R),
111 NONE: fillSubMap(order, true)
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800112 };
Matteo Scandolo61bea662016-01-26 17:21:39 -0800113 }
114
115 $.getJSON('/app/data/pc_cats.json', function (data) {
116 scope.level_order = data.level_order;
117 scope.category_order = data.category_order;
118 scope.prohibitedSites = preprocess(
119 data.prohibited, data.category_order
120 );
121 scope.$apply();
122 });
123 }
124 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800125 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800126}());