blob: bb44fec94ab3435a9b72f603037c1d1303bc915a [file] [log] [blame]
Matteo Scandolo5d568612016-01-26 11:02:16 -08001/*
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 () {
18 'use strict';
19
20 var bundleUrlSuffix = '/rs/bundle',
21 userUrlSuffix = '/rs/users',
22 family = 'family',
23 url_filter = 'url_filter';
24
25 angular.module('cordUser', [])
26 .controller('CordUserCtrl', ['$log', '$scope', '$resource', '$timeout',
27 function ($log, $scope, $resource, $timeout) {
28 var BundleData, bundleResource;
29 $scope.page.curr = 'user';
30 $scope.isFamily = false;
31 $scope.newLevels = {};
32 $scope.showCheck = false;
33 $scope.ratingsShown = false;
34
35 // === Get data functions ---
36
37 BundleData = $resource($scope.shared.url + bundleUrlSuffix);
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
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
74 getUsers($scope.shared.url + userUrlSuffix);
75
76 // === Form functions ---
77
78 function levelUrl(id, level) {
79 return $scope.shared.url +
80 userUrlSuffix + '/' + id + '/apply/url_filter/level/' + level;
81 }
82
83 $scope.applyChanges = function (changeLevels) {
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 });
98 }
99 changeLevels.$setPristine();
100 $scope.showCheck = true;
101 $timeout(function () {
102 $scope.showCheck = false;
103 }, 3000);
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();
113 $scope.showCheck = false;
114 };
115
116 $scope.showRatings = function () {
117 $scope.ratingsShown = !$scope.ratingsShown;
118 };
119
120 $log.debug('Cord User Ctrl has been created.');
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 };
163 }]);
164
165}());