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 | var bundleUrlSuffix = '/rs/bundle', |
| 21 | userUrlSuffix = '/rs/users', |
| 22 | family = 'family', |
| 23 | url_filter = 'url_filter'; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 24 | |
Matteo Scandolo | 61bea66 | 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 | c603650 | 2016-01-26 15:17:10 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | 61bea66 | 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 | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 40 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 41 | // === Get data functions --- |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 42 | |
Matteo Scandolo | 61bea66 | 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 | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 52 | } |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 53 | }); |
| 54 | } |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 55 | |
Matteo Scandolo | 61bea66 | 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 | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 57 | |
Matteo Scandolo | 61bea66 | 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 | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 61 | return user; |
| 62 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 63 | |
Matteo Scandolo | 61bea66 | 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 | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame^] | 77 | $scope.updateLevel = function(user){ |
| 78 | console.log(user); |
| 79 | user.$save() |
| 80 | .then(function(){ |
| 81 | console.log('saved'); |
| 82 | }) |
| 83 | .catch(function(e){ |
| 84 | throw new Error(e); |
| 85 | }); |
| 86 | }; |
| 87 | |
| 88 | //function getUsers(url) { |
| 89 | // var UserData, userResource; |
| 90 | // UserData = $resource(url); |
| 91 | // userResource = UserData.get({}, |
| 92 | // // success |
| 93 | // function () { |
| 94 | // $scope.users = userResource.users; |
| 95 | // if ($.isEmptyObject($scope.shared.userActivity)) { |
| 96 | // $scope.users.forEach(function (user) { |
| 97 | // var date = randomDate(new Date(2015, 0, 1), |
| 98 | // new Date()); |
| 99 | // |
| 100 | // $scope.shared.userActivity[user.id] = |
| 101 | // $filter('date')(date, 'mediumTime'); |
| 102 | // }); |
| 103 | // } |
| 104 | // }, |
| 105 | // // error |
| 106 | // function () { |
| 107 | // $log.error('Problem with resource', userResource); |
| 108 | // } |
| 109 | // ); |
| 110 | //} |
| 111 | // |
| 112 | //getUsers($scope.shared.url + userUrlSuffix); |
| 113 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 114 | // === Form functions --- |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 115 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 116 | function levelUrl(id, level) { |
| 117 | return $scope.shared.url + |
| 118 | userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level; |
| 119 | } |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 120 | |
Matteo Scandolo | 39c65a5 | 2016-01-27 12:00:18 -0800 | [diff] [blame^] | 121 | // NOTE This will trigger one request for each user to update url_filter level |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 122 | $scope.applyChanges = function (changeLevels) { |
| 123 | var requests = []; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 124 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 125 | if ($scope.users) { |
| 126 | $.each($scope.users, function (index, user) { |
| 127 | var id = user.id, |
| 128 | level = user.profile.url_filter.level; |
| 129 | if ($scope.newLevels[id] !== level) { |
| 130 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 131 | } |
| 132 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 133 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 134 | $.each(requests, function (index, req) { |
| 135 | getUsers(req); |
| 136 | }); |
| 137 | } |
| 138 | changeLevels.$setPristine(); |
| 139 | $scope.showCheck = true; |
| 140 | $timeout(function () { |
| 141 | $scope.showCheck = false; |
| 142 | }, 3000); |
| 143 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 144 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 145 | $scope.cancelChanges = function (changeLevels) { |
| 146 | if ($scope.users) { |
| 147 | $.each($scope.users, function (index, user) { |
| 148 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 149 | }); |
| 150 | } |
| 151 | changeLevels.$setPristine(); |
| 152 | $scope.showCheck = false; |
| 153 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 154 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 155 | $scope.showRatings = function () { |
| 156 | $scope.ratingsShown = !$scope.ratingsShown; |
| 157 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 158 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 159 | $log.debug('Cord User Ctrl has been created.'); |
| 160 | }]) |
| 161 | |
| 162 | .directive('ratingsPanel', ['$log', function ($log) { |
| 163 | return { |
| 164 | templateUrl: 'app/view/user/ratingPanel.html', |
| 165 | link: function (scope, elem, attrs) { |
| 166 | function fillSubMap(order, bool) { |
| 167 | var result = {}; |
| 168 | $.each(order, function (index, cat) { |
| 169 | result[cat] = bool; |
| 170 | }); |
| 171 | return result; |
| 172 | } |
| 173 | function processSubMap(prhbSites) { |
| 174 | var result = {}; |
| 175 | $.each(prhbSites, function (index, cat) { |
| 176 | result[cat] = true; |
| 177 | }); |
| 178 | return result; |
| 179 | } |
| 180 | |
| 181 | function preprocess(data, order) { |
| 182 | return { |
| 183 | ALL: fillSubMap(order, false), |
| 184 | G: processSubMap(data.G), |
| 185 | PG: processSubMap(data.PG), |
| 186 | PG_13: processSubMap(data.PG_13), |
| 187 | R: processSubMap(data.R), |
| 188 | NONE: fillSubMap(order, true) |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 189 | }; |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | $.getJSON('/app/data/pc_cats.json', function (data) { |
| 193 | scope.level_order = data.level_order; |
| 194 | scope.category_order = data.category_order; |
| 195 | scope.prohibitedSites = preprocess( |
| 196 | data.prohibited, data.category_order |
| 197 | ); |
| 198 | scope.$apply(); |
| 199 | }); |
| 200 | } |
| 201 | }; |
| 202 | }]); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 203 | |
| 204 | }()); |