blob: 6b42bc4cf6ac014f44d22af291638b1b7640181f [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 Scandolo67962ea2016-01-28 13:44:07 -080021 .controller('CordUserCtrl', function ($log, $scope, $resource, $timeout, $filter, $cookies, 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 Scandolo3eaeb3e2016-01-28 13:26:50 -080028
29 SubscriberUsers.query({subscriberId: $cookies.get('subscriberId')}).$promise
Matteo Scandoloe0628502016-01-27 14:42:42 -080030 .then(function(res){
31 $scope.isFamily = cordConfig.bundles[cordConfig.activeBundle].id === 'family';
32 // if bundle is family search for url_filter level
33 if ($scope.isFamily) {
34 angular.forEach(cordConfig.bundles[cordConfig.activeBundle].functions, function(fn){
35 if(fn.id === 'url_filter'){
36 $scope.levels = fn.params.levels;
Matteo Scandolo61bea662016-01-26 17:21:39 -080037 }
38 });
Matteo Scandolo61bea662016-01-26 17:21:39 -080039 }
Matteo Scandolo61f05f52016-01-28 09:41:01 -080040 $scope.users = res;
Matteo Scandoloe0628502016-01-27 14:42:42 -080041 })
42 .catch(function () {
43 $log.error('Problem with resource', SubscriberUsers);
44 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080045
Matteo Scandoloe0628502016-01-27 14:42:42 -080046 $scope.updateLevel = function(user){
47 // TODO save this data and show a confirmation to the user
Matteo Scandolo90621b32016-01-27 16:40:21 -080048 // NOTE subscriberId should be retrieved by login
49 SubscriberUsersUrlFilterLevel.updateUrlFilterLevel(1, user.id, user.level)
Matteo Scandoloe0628502016-01-27 14:42:42 -080050 .then(function(){
Matteo Scandolo90621b32016-01-27 16:40:21 -080051 user.updated = true;
Matteo Scandoloe0628502016-01-27 14:42:42 -080052 })
53 .catch(function(e){
54 throw new Error(e);
55 });
56 };
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -080057
Matteo Scandoloe0628502016-01-27 14:42:42 -080058 $scope.showRatings = function () {
59 $scope.ratingsShown = !$scope.ratingsShown;
60 };
Matteo Scandolo61bea662016-01-26 17:21:39 -080061
Matteo Scandoloe0628502016-01-27 14:42:42 -080062 $log.debug('Cord User Ctrl has been created.');
63 })
Matteo Scandolo90621b32016-01-27 16:40:21 -080064 .directive('userUpdatedTick', function($timeout){
65 return {
66 restric: 'E',
67 scope: {
68 user: '='
69 },
Matteo Scandolo413c45d2016-01-29 11:40:41 -080070 template: '<span class="icon-saved animate" ng-show="saved"></span>',
Matteo Scandolo90621b32016-01-27 16:40:21 -080071 link: function(scope, elem){
72 scope.saved = false;
73 scope.$watch('user.updated', function(val){
74 if(val){
75 scope.saved = true;
76 $timeout(function(){
77 scope.saved = false;
78 }, 3000);
79 }
80 });
81 }
82 }
83 })
Matteo Scandoloe0628502016-01-27 14:42:42 -080084 .directive('ratingsPanel', function ($log) {
Matteo Scandolo61bea662016-01-26 17:21:39 -080085 return {
86 templateUrl: 'app/view/user/ratingPanel.html',
87 link: function (scope, elem, attrs) {
88 function fillSubMap(order, bool) {
89 var result = {};
90 $.each(order, function (index, cat) {
91 result[cat] = bool;
92 });
93 return result;
94 }
95 function processSubMap(prhbSites) {
96 var result = {};
97 $.each(prhbSites, function (index, cat) {
98 result[cat] = true;
99 });
100 return result;
101 }
102
103 function preprocess(data, order) {
104 return {
105 ALL: fillSubMap(order, false),
106 G: processSubMap(data.G),
107 PG: processSubMap(data.PG),
108 PG_13: processSubMap(data.PG_13),
109 R: processSubMap(data.R),
110 NONE: fillSubMap(order, true)
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800111 };
Matteo Scandolo61bea662016-01-26 17:21:39 -0800112 }
113
114 $.getJSON('/app/data/pc_cats.json', function (data) {
115 scope.level_order = data.level_order;
116 scope.category_order = data.category_order;
117 scope.prohibitedSites = preprocess(
118 data.prohibited, data.category_order
119 );
120 scope.$apply();
121 });
122 }
123 };
Matteo Scandoloe0628502016-01-27 14:42:42 -0800124 });
Matteo Scandolo4eb4ccb2016-01-26 11:02:16 -0800125}());