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 | function randomDate(start, end) { |
| 26 | return new Date( |
| 27 | start.getTime() + Math.random() * (end.getTime() - start.getTime()) |
| 28 | ); |
| 29 | } |
Matteo Scandolo | 2b54546 | 2016-01-26 15:17:10 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 31 | angular.module('cordUser', []) |
| 32 | .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', '$filter', 'SubscriberUsers', 'cordConfig', |
| 33 | function ($log, $scope, $resource, $timeout, $filter, SubscriberUsers, cordConfig) { |
| 34 | var BundleData, bundleResource; |
| 35 | $scope.page.curr = 'user'; |
| 36 | $scope.isFamily = false; |
| 37 | $scope.newLevels = {}; |
| 38 | $scope.showCheck = false; |
| 39 | $scope.ratingsShown = false; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 41 | // === Get data functions --- |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 42 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 43 | // NOTE subscriberId should be retrieved by login |
| 44 | SubscriberUsers.query({subscriberId: 1}).$promise |
| 45 | .then(function(res){ |
| 46 | $scope.isFamily = cordConfig.bundles[0].id === 'family'; |
| 47 | // if bundle is family search for url_filter level |
| 48 | if ($scope.isFamily) { |
| 49 | angular.forEach(cordConfig.bundles[0].functions, function(fn){ |
| 50 | if(fn.id === 'url_filter'){ |
| 51 | $scope.levels = fn.params.levels; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 52 | } |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 53 | }); |
| 54 | } |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 56 | // NOTE the loops creates data that are not available in xos should we move them in a service? Should we define a small backend to store this infos? |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 58 | // add an icon to the user |
| 59 | res.users.map(function(user){ |
| 60 | user['icon_id'] = 'mom'; |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 61 | return user; |
| 62 | }); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 63 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 64 | // add a random login date to the user |
| 65 | res.users.forEach(function(user){ |
| 66 | if(!angular.isDefined(cordConfig.userActivity[user.id])){ |
| 67 | var date = randomDate(new Date(2015, 0, 1), new Date()); |
| 68 | cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime'); |
| 69 | } |
| 70 | }); |
| 71 | $scope.users = res.users; |
| 72 | }) |
| 73 | .catch(function () { |
| 74 | $log.error('Problem with resource', bundleResource); |
| 75 | }); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 77 | $scope.updateLevel = function(user){ |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 78 | user.$save() |
| 79 | .then(function(){ |
| 80 | console.log('saved'); |
| 81 | }) |
| 82 | .catch(function(e){ |
| 83 | throw new Error(e); |
| 84 | }); |
| 85 | }; |
| 86 | |
| 87 | //function getUsers(url) { |
| 88 | // var UserData, userResource; |
| 89 | // UserData = $resource(url); |
| 90 | // userResource = UserData.get({}, |
| 91 | // // success |
| 92 | // function () { |
| 93 | // $scope.users = userResource.users; |
| 94 | // if ($.isEmptyObject($scope.shared.userActivity)) { |
| 95 | // $scope.users.forEach(function (user) { |
| 96 | // var date = randomDate(new Date(2015, 0, 1), |
| 97 | // new Date()); |
| 98 | // |
| 99 | // $scope.shared.userActivity[user.id] = |
| 100 | // $filter('date')(date, 'mediumTime'); |
| 101 | // }); |
| 102 | // } |
| 103 | // }, |
| 104 | // // error |
| 105 | // function () { |
| 106 | // $log.error('Problem with resource', userResource); |
| 107 | // } |
| 108 | // ); |
| 109 | //} |
| 110 | // |
| 111 | //getUsers($scope.shared.url + userUrlSuffix); |
| 112 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 113 | // === Form functions --- |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 114 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 115 | function levelUrl(id, level) { |
| 116 | return $scope.shared.url + |
| 117 | userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level; |
| 118 | } |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 119 | |
Matteo Scandolo | bc56685 | 2016-01-27 12:00:18 -0800 | [diff] [blame] | 120 | // 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] | 121 | $scope.applyChanges = function (changeLevels) { |
| 122 | var requests = []; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 124 | if ($scope.users) { |
| 125 | $.each($scope.users, function (index, user) { |
| 126 | var id = user.id, |
| 127 | level = user.profile.url_filter.level; |
| 128 | if ($scope.newLevels[id] !== level) { |
| 129 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 130 | } |
| 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 | $.each(requests, function (index, req) { |
| 134 | getUsers(req); |
| 135 | }); |
| 136 | } |
| 137 | changeLevels.$setPristine(); |
| 138 | $scope.showCheck = true; |
| 139 | $timeout(function () { |
| 140 | $scope.showCheck = false; |
| 141 | }, 3000); |
| 142 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 143 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 144 | $scope.cancelChanges = function (changeLevels) { |
| 145 | if ($scope.users) { |
| 146 | $.each($scope.users, function (index, user) { |
| 147 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 148 | }); |
| 149 | } |
| 150 | changeLevels.$setPristine(); |
| 151 | $scope.showCheck = false; |
| 152 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 153 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 154 | $scope.showRatings = function () { |
| 155 | $scope.ratingsShown = !$scope.ratingsShown; |
| 156 | }; |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 157 | |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 158 | $log.debug('Cord User Ctrl has been created.'); |
| 159 | }]) |
| 160 | |
| 161 | .directive('ratingsPanel', ['$log', function ($log) { |
| 162 | return { |
| 163 | templateUrl: 'app/view/user/ratingPanel.html', |
| 164 | link: function (scope, elem, attrs) { |
| 165 | function fillSubMap(order, bool) { |
| 166 | var result = {}; |
| 167 | $.each(order, function (index, cat) { |
| 168 | result[cat] = bool; |
| 169 | }); |
| 170 | return result; |
| 171 | } |
| 172 | function processSubMap(prhbSites) { |
| 173 | var result = {}; |
| 174 | $.each(prhbSites, function (index, cat) { |
| 175 | result[cat] = true; |
| 176 | }); |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | function preprocess(data, order) { |
| 181 | return { |
| 182 | ALL: fillSubMap(order, false), |
| 183 | G: processSubMap(data.G), |
| 184 | PG: processSubMap(data.PG), |
| 185 | PG_13: processSubMap(data.PG_13), |
| 186 | R: processSubMap(data.R), |
| 187 | NONE: fillSubMap(order, true) |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 188 | }; |
Matteo Scandolo | 680cd92 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | $.getJSON('/app/data/pc_cats.json', function (data) { |
| 192 | scope.level_order = data.level_order; |
| 193 | scope.category_order = data.category_order; |
| 194 | scope.prohibitedSites = preprocess( |
| 195 | data.prohibited, data.category_order |
| 196 | ); |
| 197 | scope.$apply(); |
| 198 | }); |
| 199 | } |
| 200 | }; |
| 201 | }]); |
Matteo Scandolo | 5d56861 | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 202 | |
| 203 | }()); |