Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [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 () { |
| 18 | 'use strict'; |
| 19 | |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 20 | var bundleUrl = 'http://localhost:8080/rs/bundle', |
| 21 | userUrl = 'http://localhost:8080/rs/users', |
| 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 | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 26 | .controller('CordUserCtrl', ['$log', '$scope', '$resource', |
| 27 | function ($log, $scope, $resource) { |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame^] | 28 | var BundleData, bundleResource; |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 29 | $scope.page = 'user'; |
| 30 | $scope.isFamily = false; |
| 31 | $scope.newLevels = {}; |
| 32 | |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame^] | 33 | // === Get data functions --- |
| 34 | |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 35 | BundleData = $resource(bundleUrl); |
| 36 | bundleResource = BundleData.get({}, |
| 37 | // success |
| 38 | function () { |
| 39 | var result; |
| 40 | $scope.isFamily = (bundleResource.bundle.id === family); |
| 41 | if ($scope.isFamily) { |
| 42 | result = $.grep( |
| 43 | bundleResource.bundle.functions, |
| 44 | function (elem) { |
| 45 | if (elem.id === url_filter) { return true; } |
| 46 | } |
| 47 | ); |
| 48 | $scope.levels = result[0].params.levels; |
| 49 | } |
| 50 | }, |
| 51 | // error |
| 52 | function () { |
| 53 | $log.error('Problem with resource', bundleResource); |
| 54 | } |
| 55 | ); |
| 56 | |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame^] | 57 | function getUsers(url) { |
| 58 | var UserData, userResource; |
| 59 | UserData = $resource(url); |
| 60 | userResource = UserData.get({}, |
| 61 | // success |
| 62 | function () { |
| 63 | $scope.users = userResource.users; |
| 64 | }, |
| 65 | // error |
| 66 | function () { |
| 67 | $log.error('Problem with resource', userResource); |
| 68 | } |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | getUsers(userUrl); |
| 73 | |
| 74 | // === Form functions --- |
| 75 | |
| 76 | function levelUrl(id, level) { |
| 77 | return userUrl + '/' + id + '/apply/url_filter/level/' + level; |
| 78 | } |
| 79 | |
| 80 | $scope.applyChanges = function () { |
| 81 | var requests = []; |
| 82 | |
| 83 | if ($scope.users) { |
| 84 | $.each($scope.users, function (index, user) { |
| 85 | var id = user.id, |
| 86 | level = user.profile.url_filter.level; |
| 87 | if ($scope.newLevels[id] !== level) { |
| 88 | requests.push(levelUrl(id, $scope.newLevels[id])); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | $.each(requests, function (index, req) { |
| 93 | getUsers(req); |
| 94 | }); |
Bri Prebilic Cole | e2958f2 | 2015-05-26 17:18:23 -0700 | [diff] [blame] | 95 | } |
Bri Prebilic Cole | d050a4b | 2015-05-27 15:53:30 -0700 | [diff] [blame^] | 96 | }; |
| 97 | |
| 98 | $scope.cancelChanges = function (changeLevels) { |
| 99 | if ($scope.users) { |
| 100 | $.each($scope.users, function (index, user) { |
| 101 | $scope.newLevels[user.id] = user.profile.url_filter.level; |
| 102 | }); |
| 103 | } |
| 104 | changeLevels.$setPristine(); |
| 105 | }; |
Bri Prebilic Cole | 3c3361c | 2015-05-19 12:07:29 -0700 | [diff] [blame] | 106 | |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 107 | $log.debug('Cord User Ctrl has been created.'); |
| 108 | }]); |
Bri Prebilic Cole | 8db1640 | 2015-05-18 13:50:31 -0700 | [diff] [blame] | 109 | |
Bri Prebilic Cole | e031189 | 2015-05-15 11:27:15 -0700 | [diff] [blame] | 110 | }()); |