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