Matteo Scandolo | 5d56861 | 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 | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 18 | 'use strict'; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 19 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 20 | var bundleUrlSuffix = '/rs/bundle', |
| 21 | userUrlSuffix = '/rs/users', |
| 22 | family = 'family', |
| 23 | url_filter = 'url_filter'; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 24 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 25 | angular.module('cordUser', []) |
| 26 | .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', '$filter', 'SubscriberUsers', 'cordConfig', |
| 27 | function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig) { |
Matteo Scandolo | dd1e190 | 2016-01-27 14:30:22 -0800 | [diff] [blame^] | 28 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 29 | $scope.page.curr = 'user'; |
| 30 | $scope.isFamily = false; |
| 31 | $scope.newLevels = {}; |
| 32 | $scope.showCheck = false; |
| 33 | $scope.ratingsShown = false; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 34 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 35 | // === Get data functions --- |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 36 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 37 | // NOTE subscriberId should be retrieved by login |
| 38 | SubscriberUsers.query({subscriberId: 1}).$promise |
| 39 | .then(function(res){ |
Matteo Scandolo | dd1e190 | 2016-01-27 14:30:22 -0800 | [diff] [blame^] | 40 | $scope.isFamily = cordConfig.bundles[cordConfig.activeBundle].id === 'family'; |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 41 | // if bundle is family search for url_filter level |
| 42 | if ($scope.isFamily) { |
Matteo Scandolo | dd1e190 | 2016-01-27 14:30:22 -0800 | [diff] [blame^] | 43 | angular.forEach(cordConfig.bundles[cordConfig.activeBundle].functions, function(fn){ |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 44 | if(fn.id === 'url_filter'){ |
| 45 | $scope.levels = fn.params.levels; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 46 | } |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 47 | }); |
| 48 | } |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 49 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 50 | $scope.users = res.users; |
| 51 | }) |
| 52 | .catch(function () { |
Matteo Scandolo | dd1e190 | 2016-01-27 14:30:22 -0800 | [diff] [blame^] | 53 | $log.error('Problem with resource', SubscriberUsers); |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 54 | }); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 56 | $scope.updateLevel = function(user){ |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 57 | user.$save() |
| 58 | .then(function(){ |
| 59 | console.log('saved'); |
| 60 | }) |
| 61 | .catch(function(e){ |
| 62 | throw new Error(e); |
| 63 | }); |
| 64 | }; |
| 65 | |
| 66 | //function getUsers(url) { |
| 67 | // var UserData, userResource; |
| 68 | // UserData = $resource(url); |
| 69 | // userResource = UserData.get({}, |
| 70 | // // success |
| 71 | // function () { |
| 72 | // $scope.users = userResource.users; |
| 73 | // if ($.isEmptyObject($scope.shared.userActivity)) { |
| 74 | // $scope.users.forEach(function (user) { |
| 75 | // var date = randomDate(new Date(2015, 0, 1), |
| 76 | // new Date()); |
| 77 | // |
| 78 | // $scope.shared.userActivity[user.id] = |
| 79 | // $filter('date')(date, 'mediumTime'); |
| 80 | // }); |
| 81 | // } |
| 82 | // }, |
| 83 | // // error |
| 84 | // function () { |
| 85 | // $log.error('Problem with resource', userResource); |
| 86 | // } |
| 87 | // ); |
| 88 | //} |
| 89 | // |
| 90 | //getUsers($scope.shared.url + userUrlSuffix); |
| 91 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 92 | // === Form functions --- |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 93 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 94 | function levelUrl(id, level) { |
| 95 | return $scope.shared.url + |
| 96 | userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level; |
| 97 | } |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 98 | |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 99 | // NOTE This will trigger one request for each user to update url_filter level |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 100 | $scope.applyChanges = function (changeLevels) { |
| 101 | var requests = []; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 103 | if ($scope.users) { |
| 104 | $.each($scope.users, function (index, user) { |
| 105 | var id = user.id, |
| 106 | level = user.profile.url_filter.level; |
| 107 | if ($scope.newLevels[id] !== level) { |
| 108 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 109 | } |
| 110 | }); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 111 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 112 | $.each(requests, function (index, req) { |
| 113 | getUsers(req); |
| 114 | }); |
| 115 | } |
| 116 | changeLevels.$setPristine(); |
| 117 | $scope.showCheck = true; |
| 118 | $timeout(function () { |
| 119 | $scope.showCheck = false; |
| 120 | }, 3000); |
| 121 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 122 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 123 | $scope.cancelChanges = function (changeLevels) { |
| 124 | if ($scope.users) { |
| 125 | $.each($scope.users, function (index, user) { |
| 126 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 127 | }); |
| 128 | } |
| 129 | changeLevels.$setPristine(); |
| 130 | $scope.showCheck = false; |
| 131 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 132 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 133 | $scope.showRatings = function () { |
| 134 | $scope.ratingsShown = !$scope.ratingsShown; |
| 135 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 136 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 137 | $log.debug('Cord User Ctrl has been created.'); |
| 138 | }]) |
| 139 | |
| 140 | .directive('ratingsPanel', ['$log', function ($log) { |
| 141 | return { |
| 142 | templateUrl: 'app/view/user/ratingPanel.html', |
| 143 | link: function (scope, elem, attrs) { |
| 144 | function fillSubMap(order, bool) { |
| 145 | var result = {}; |
| 146 | $.each(order, function (index, cat) { |
| 147 | result[cat] = bool; |
| 148 | }); |
| 149 | return result; |
| 150 | } |
| 151 | function processSubMap(prhbSites) { |
| 152 | var result = {}; |
| 153 | $.each(prhbSites, function (index, cat) { |
| 154 | result[cat] = true; |
| 155 | }); |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | function preprocess(data, order) { |
| 160 | return { |
| 161 | ALL: fillSubMap(order, false), |
| 162 | G: processSubMap(data.G), |
| 163 | PG: processSubMap(data.PG), |
| 164 | PG_13: processSubMap(data.PG_13), |
| 165 | R: processSubMap(data.R), |
| 166 | NONE: fillSubMap(order, true) |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 167 | }; |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | $.getJSON('/app/data/pc_cats.json', function (data) { |
| 171 | scope.level_order = data.level_order; |
| 172 | scope.category_order = data.category_order; |
| 173 | scope.prohibitedSites = preprocess( |
| 174 | data.prohibited, data.category_order |
| 175 | ); |
| 176 | scope.$apply(); |
| 177 | }); |
| 178 | } |
| 179 | }; |
| 180 | }]); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 181 | |
| 182 | }()); |