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'; |
| 61 | // NOTE mock data, waiting for #CORD-516 |
| 62 | var levels = ['R', 'PG', 'PG-13']; |
| 63 | user.profile = { |
| 64 | url_filter: { |
| 65 | level: levels[Math.floor(Math.random() * levels.length)] |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 66 | } |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 67 | }; |
| 68 | return user; |
| 69 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 70 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 71 | // add a random login date to the user |
| 72 | res.users.forEach(function(user){ |
| 73 | if(!angular.isDefined(cordConfig.userActivity[user.id])){ |
| 74 | var date = randomDate(new Date(2015, 0, 1), new Date()); |
| 75 | cordConfig.userActivity[user.id] = $filter('date')(date, 'mediumTime'); |
| 76 | } |
| 77 | }); |
| 78 | $scope.users = res.users; |
| 79 | }) |
| 80 | .catch(function () { |
| 81 | $log.error('Problem with resource', bundleResource); |
| 82 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 83 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 84 | // === Form functions --- |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 85 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 86 | function levelUrl(id, level) { |
| 87 | return $scope.shared.url + |
| 88 | userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level; |
| 89 | } |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 90 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 91 | $scope.applyChanges = function (changeLevels) { |
| 92 | var requests = []; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 93 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 94 | if ($scope.users) { |
| 95 | $.each($scope.users, function (index, user) { |
| 96 | var id = user.id, |
| 97 | level = user.profile.url_filter.level; |
| 98 | if ($scope.newLevels[id] !== level) { |
| 99 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 100 | } |
| 101 | }); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 102 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 103 | $.each(requests, function (index, req) { |
| 104 | getUsers(req); |
| 105 | }); |
| 106 | } |
| 107 | changeLevels.$setPristine(); |
| 108 | $scope.showCheck = true; |
| 109 | $timeout(function () { |
| 110 | $scope.showCheck = false; |
| 111 | }, 3000); |
| 112 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 113 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 114 | $scope.cancelChanges = function (changeLevels) { |
| 115 | if ($scope.users) { |
| 116 | $.each($scope.users, function (index, user) { |
| 117 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 118 | }); |
| 119 | } |
| 120 | changeLevels.$setPristine(); |
| 121 | $scope.showCheck = false; |
| 122 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 123 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 124 | $scope.showRatings = function () { |
| 125 | $scope.ratingsShown = !$scope.ratingsShown; |
| 126 | }; |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 127 | |
Matteo Scandolo | 61bea66 | 2016-01-26 17:21:39 -0800 | [diff] [blame] | 128 | $log.debug('Cord User Ctrl has been created.'); |
| 129 | }]) |
| 130 | |
| 131 | .directive('ratingsPanel', ['$log', function ($log) { |
| 132 | return { |
| 133 | templateUrl: 'app/view/user/ratingPanel.html', |
| 134 | link: function (scope, elem, attrs) { |
| 135 | function fillSubMap(order, bool) { |
| 136 | var result = {}; |
| 137 | $.each(order, function (index, cat) { |
| 138 | result[cat] = bool; |
| 139 | }); |
| 140 | return result; |
| 141 | } |
| 142 | function processSubMap(prhbSites) { |
| 143 | var result = {}; |
| 144 | $.each(prhbSites, function (index, cat) { |
| 145 | result[cat] = true; |
| 146 | }); |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | function preprocess(data, order) { |
| 151 | return { |
| 152 | ALL: fillSubMap(order, false), |
| 153 | G: processSubMap(data.G), |
| 154 | PG: processSubMap(data.PG), |
| 155 | PG_13: processSubMap(data.PG_13), |
| 156 | R: processSubMap(data.R), |
| 157 | NONE: fillSubMap(order, true) |
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 | } |
| 160 | |
| 161 | $.getJSON('/app/data/pc_cats.json', function (data) { |
| 162 | scope.level_order = data.level_order; |
| 163 | scope.category_order = data.category_order; |
| 164 | scope.prohibitedSites = preprocess( |
| 165 | data.prohibited, data.category_order |
| 166 | ); |
| 167 | scope.$apply(); |
| 168 | }); |
| 169 | } |
| 170 | }; |
| 171 | }]); |
Matteo Scandolo | 4eb4ccb | 2016-01-26 11:02:16 -0800 | [diff] [blame] | 172 | |
| 173 | }()); |