blob: 6eff78a99c8a88a3abb71ac08e723114d615eb2b [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 angular.module('cordUser', [])
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -080021 .controller('CordUserCtrl', function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig) {
Matteo Scandolodd1e1902016-01-27 14:30:22 -080022
Matteo Scandoloe3aa41b2016-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 Scandolo5d568612016-01-26 11:02:16 -080028
Matteo Scandoloe3aa41b2016-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 Scandolo680cd922016-01-26 17:21:39 -080038 }
39 });
Matteo Scandolo680cd922016-01-26 17:21:39 -080040 }
Matteo Scandolo5d568612016-01-26 11:02:16 -080041
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -080042 $scope.users = res.users;
43 })
44 .catch(function () {
45 $log.error('Problem with resource', SubscriberUsers);
46 });
Matteo Scandolo5d568612016-01-26 11:02:16 -080047
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -080048 $scope.updateLevel = function(user){
49 // TODO save this data and show a confirmation to the user
50 user.$save()
51 .then(function(){
52 console.log('saved');
53 })
54 .catch(function(e){
55 throw new Error(e);
56 });
57 };
Matteo Scandolo5d568612016-01-26 11:02:16 -080058
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -080059 $scope.showRatings = function () {
60 $scope.ratingsShown = !$scope.ratingsShown;
61 };
Matteo Scandolo680cd922016-01-26 17:21:39 -080062
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -080063 $log.debug('Cord User Ctrl has been created.');
64 })
65 .directive('ratingsPanel', function ($log) {
Matteo Scandolo680cd922016-01-26 17:21:39 -080066 return {
67 templateUrl: 'app/view/user/ratingPanel.html',
68 link: function (scope, elem, attrs) {
69 function fillSubMap(order, bool) {
70 var result = {};
71 $.each(order, function (index, cat) {
72 result[cat] = bool;
73 });
74 return result;
75 }
76 function processSubMap(prhbSites) {
77 var result = {};
78 $.each(prhbSites, function (index, cat) {
79 result[cat] = true;
80 });
81 return result;
82 }
83
84 function preprocess(data, order) {
85 return {
86 ALL: fillSubMap(order, false),
87 G: processSubMap(data.G),
88 PG: processSubMap(data.PG),
89 PG_13: processSubMap(data.PG_13),
90 R: processSubMap(data.R),
91 NONE: fillSubMap(order, true)
Matteo Scandolo5d568612016-01-26 11:02:16 -080092 };
Matteo Scandolo680cd922016-01-26 17:21:39 -080093 }
94
95 $.getJSON('/app/data/pc_cats.json', function (data) {
96 scope.level_order = data.level_order;
97 scope.category_order = data.category_order;
98 scope.prohibitedSites = preprocess(
99 data.prohibited, data.category_order
100 );
101 scope.$apply();
102 });
103 }
104 };
Matteo Scandoloe3aa41b2016-01-27 14:42:42 -0800105 });
Matteo Scandolo5d568612016-01-26 11:02:16 -0800106}());