Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 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 () { |
| 18 | 'use strict'; |
| 19 | |
Bri Prebilic Cole | d189e28 | 2015-06-05 10:00:22 -0700 | [diff] [blame] | 20 | var bundleUrlSuffix = '/rs/bundle', |
| 21 | userUrlSuffix = '/rs/users', |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 22 | family = 'family', |
| 23 | url_filter = 'url_filter'; |
| 24 | |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 25 | angular.module('cordUser', []) |
Bri Prebilic Cole | 5d6153e | 2015-05-28 12:03:21 -0700 | [diff] [blame] | 26 | .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout', |
| 27 | function ($log, $scope, $resource, $timeout) { |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 28 | var BundleData, bundleResource; |
Bri Prebilic Cole | 60553c8 | 2015-06-05 08:50:17 -0700 | [diff] [blame] | 29 | $scope.page.curr = 'user'; |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 30 | $scope.isFamily = false; |
| 31 | $scope.newLevels = {}; |
Bri Prebilic Cole | 5d6153e | 2015-05-28 12:03:21 -0700 | [diff] [blame] | 32 | $scope.showCheck = false; |
Bri Prebilic Cole | ba9d052 | 2015-06-10 16:08:54 -0700 | [diff] [blame] | 33 | $scope.ratingsShown = false; |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 34 | |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 35 | // === Get data functions --- |
| 36 | |
Bri Prebilic Cole | d189e28 | 2015-06-05 10:00:22 -0700 | [diff] [blame] | 37 | BundleData = $resource($scope.shared.url + bundleUrlSuffix); |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 38 | bundleResource = BundleData.get({}, |
| 39 | // success |
| 40 | function () { |
| 41 | var result; |
| 42 | $scope.isFamily = (bundleResource.bundle.id === family); |
| 43 | if ($scope.isFamily) { |
| 44 | result = $.grep( |
| 45 | bundleResource.bundle.functions, |
| 46 | function (elem) { |
| 47 | if (elem.id === url_filter) { return true; } |
| 48 | } |
| 49 | ); |
| 50 | $scope.levels = result[0].params.levels; |
| 51 | } |
| 52 | }, |
| 53 | // error |
| 54 | function () { |
| 55 | $log.error('Problem with resource', bundleResource); |
| 56 | } |
| 57 | ); |
| 58 | |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 59 | function getUsers(url) { |
| 60 | var UserData, userResource; |
| 61 | UserData = $resource(url); |
| 62 | userResource = UserData.get({}, |
| 63 | // success |
| 64 | function () { |
| 65 | $scope.users = userResource.users; |
| 66 | }, |
| 67 | // error |
| 68 | function () { |
| 69 | $log.error('Problem with resource', userResource); |
| 70 | } |
| 71 | ); |
| 72 | } |
| 73 | |
Bri Prebilic Cole | d189e28 | 2015-06-05 10:00:22 -0700 | [diff] [blame] | 74 | getUsers($scope.shared.url + userUrlSuffix); |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 75 | |
| 76 | // === Form functions --- |
| 77 | |
| 78 | function levelUrl(id, level) { |
Bri Prebilic Cole | d189e28 | 2015-06-05 10:00:22 -0700 | [diff] [blame] | 79 | return $scope.shared.url + |
| 80 | userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level; |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Bri Prebilic Cole | 5d6153e | 2015-05-28 12:03:21 -0700 | [diff] [blame] | 83 | $scope.applyChanges = function (changeLevels) { |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 84 | var requests = []; |
| 85 | |
| 86 | if ($scope.users) { |
| 87 | $.each($scope.users, function (index, user) { |
| 88 | var id = user.id, |
| 89 | level = user.profile.url_filter.level; |
| 90 | if ($scope.newLevels[id] !== level) { |
| 91 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | $.each(requests, function (index, req) { |
| 96 | getUsers(req); |
| 97 | }); |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 98 | } |
Bri Prebilic Cole | 5d6153e | 2015-05-28 12:03:21 -0700 | [diff] [blame] | 99 | changeLevels.$setPristine(); |
| 100 | $scope.showCheck = true; |
| 101 | $timeout(function () { |
| 102 | $scope.showCheck = false; |
| 103 | }, 3000); |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | $scope.cancelChanges = function (changeLevels) { |
| 107 | if ($scope.users) { |
| 108 | $.each($scope.users, function (index, user) { |
| 109 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 110 | }); |
| 111 | } |
| 112 | changeLevels.$setPristine(); |
Bri Prebilic Cole | 5d6153e | 2015-05-28 12:03:21 -0700 | [diff] [blame] | 113 | $scope.showCheck = false; |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame] | 114 | }; |
Bri Prebilic Cole | 3c3361c | 2015-05-19 12:07:29 -0700 | [diff] [blame] | 115 | |
Bri Prebilic Cole | ba9d052 | 2015-06-10 16:08:54 -0700 | [diff] [blame] | 116 | $scope.showRatings = function () { |
| 117 | $scope.ratingsShown = !$scope.ratingsShown; |
| 118 | }; |
| 119 | |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 120 | $log.debug('Cord User Ctrl has been created.'); |
Bri Prebilic Cole | ba9d052 | 2015-06-10 16:08:54 -0700 | [diff] [blame] | 121 | }]) |
| 122 | |
| 123 | .directive('ratingsPanel', ['$log', function ($log) { |
| 124 | return { |
| 125 | templateUrl: 'app/view/user/ratingPanel.html', |
| 126 | link: function (scope, elem, attrs) { |
| 127 | function fillSubMap(order, bool) { |
| 128 | var result = {}; |
| 129 | $.each(order, function (index, cat) { |
| 130 | result[cat] = bool; |
| 131 | }); |
| 132 | return result; |
| 133 | } |
| 134 | function processSubMap(prhbSites) { |
| 135 | var result = {}; |
| 136 | $.each(prhbSites, function (index, cat) { |
| 137 | result[cat] = true; |
| 138 | }); |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | function preprocess(data, order) { |
| 143 | return { |
| 144 | ALL: fillSubMap(order, false), |
| 145 | G: processSubMap(data.G), |
| 146 | PG: processSubMap(data.PG), |
| 147 | PG_13: processSubMap(data.PG_13), |
| 148 | R: processSubMap(data.R), |
| 149 | NONE: fillSubMap(order, true) |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | $.getJSON('/app/data/pc_cats.json', function (data) { |
| 154 | scope.level_order = data.level_order; |
| 155 | scope.category_order = data.category_order; |
| 156 | scope.prohibitedSites = preprocess( |
| 157 | data.prohibited, data.category_order |
| 158 | ); |
| 159 | scope.$apply(); |
| 160 | }); |
| 161 | } |
| 162 | }; |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 163 | }]); |
Bri Prebilic Cole | 8db1640 | 2015-05-18 13:50:31 -0700 | [diff] [blame] | 164 | |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 165 | }()); |