Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 18 | 'use strict'; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 19 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 20 | angular.module('cordUser', []) |
Matteo Scandolo | 67962ea | 2016-01-28 13:44:07 -0800 | [diff] [blame] | 21 | .controller('CordUserCtrl', function ($log, $scope, $resource, $timeout, $filter, $cookies, SubscriberUsers, cordConfig, SubscriberUsersUrlFilterLevel) { |
Matteo Scandolo | 10fb87c | 2016-01-27 14:30:22 -0800 | [diff] [blame] | 22 | |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 23 | $scope.page.curr = 'user'; |
| 24 | $scope.isFamily = false; |
| 25 | $scope.newLevels = {}; |
| 26 | $scope.showCheck = false; |
| 27 | $scope.ratingsShown = false; |
Matteo Scandolo | 3eaeb3e | 2016-01-28 13:26:50 -0800 | [diff] [blame] | 28 | |
| 29 | SubscriberUsers.query({subscriberId: $cookies.get('subscriberId')}).$promise |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 30 | .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 Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 37 | } |
| 38 | }); |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 39 | } |
Matteo Scandolo | 61f05f5 | 2016-01-28 09:41:01 -0800 | [diff] [blame] | 40 | $scope.users = res; |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 41 | }) |
| 42 | .catch(function () { |
| 43 | $log.error('Problem with resource', SubscriberUsers); |
| 44 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 45 | |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 46 | $scope.updateLevel = function(user){ |
| 47 | // TODO save this data and show a confirmation to the user |
Matteo Scandolo | 90621b3 | 2016-01-27 16:40:21 -0800 | [diff] [blame] | 48 | // NOTE subscriberId should be retrieved by login |
| 49 | SubscriberUsersUrlFilterLevel.updateUrlFilterLevel(1, user.id, user.level) |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 50 | .then(function(){ |
Matteo Scandolo | 90621b3 | 2016-01-27 16:40:21 -0800 | [diff] [blame] | 51 | user.updated = true; |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 52 | }) |
| 53 | .catch(function(e){ |
| 54 | throw new Error(e); |
| 55 | }); |
| 56 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 58 | $scope.showRatings = function () { |
| 59 | $scope.ratingsShown = !$scope.ratingsShown; |
| 60 | }; |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 61 | |
Matteo Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 62 | $log.debug('Cord User Ctrl has been created.'); |
| 63 | }) |
Matteo Scandolo | 90621b3 | 2016-01-27 16:40:21 -0800 | [diff] [blame] | 64 | .directive('userUpdatedTick', function($timeout){ |
| 65 | return { |
| 66 | restric: 'E', |
| 67 | scope: { |
| 68 | user: '=' |
| 69 | }, |
Matteo Scandolo | 413c45d | 2016-01-29 11:40:41 -0800 | [diff] [blame] | 70 | template: '<span class="icon-saved animate" ng-show="saved"></span>', |
Matteo Scandolo | 90621b3 | 2016-01-27 16:40:21 -0800 | [diff] [blame] | 71 | 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 Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 84 | .directive('ratingsPanel', function ($log) { |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 85 | 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 Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 111 | }; |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 112 | } |
| 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 Scandolo | e062850 | 2016-01-27 14:42:42 -0800 | [diff] [blame] | 124 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 125 | }()); |