|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2014, Sebastian Sdorra |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +'use strict'; |
| 26 | + |
| 27 | +angular.module('adfDynamicSample', [ |
| 28 | + 'adf', 'ngRoute', 'structures', |
| 29 | + 'sample.widgets.news', 'sample.widgets.weather', |
| 30 | + 'sample.widgets.linklist', 'sample.widgets.randommsg' |
| 31 | + ]) |
| 32 | + .config(function($routeProvider){ |
| 33 | + $routeProvider |
| 34 | + .when('/boards', { |
| 35 | + templateUrl: 'partials/default.html' |
| 36 | + }) |
| 37 | + .when('/boards/:id', { |
| 38 | + controller: 'dashboardCtrl', |
| 39 | + controllerAs: 'dashboard', |
| 40 | + templateUrl: 'partials/dashboard.html', |
| 41 | + resolve: { |
| 42 | + data: function($route, storeService){ |
| 43 | + return storeService.get($route.current.params.id); |
| 44 | + } |
| 45 | + } |
| 46 | + }) |
| 47 | + .otherwise({ |
| 48 | + redirectTo: '/boards' |
| 49 | + }); |
| 50 | + }) |
| 51 | + .service('storeService', function($http, $q){ |
| 52 | + return { |
| 53 | + getAll: function(){ |
| 54 | + var deferred = $q.defer(); |
| 55 | + $http.get('/v1/store') |
| 56 | + .success(function(data){ |
| 57 | + deferred.resolve(data.dashboards); |
| 58 | + }) |
| 59 | + .error(function(){ |
| 60 | + deferred.reject(); |
| 61 | + }); |
| 62 | + return deferred.promise; |
| 63 | + }, |
| 64 | + get: function(id){ |
| 65 | + var deferred = $q.defer(); |
| 66 | + $http.get('/v1/store/' + id) |
| 67 | + .success(function(data){ |
| 68 | + deferred.resolve(data); |
| 69 | + }) |
| 70 | + .error(function(){ |
| 71 | + deferred.reject(); |
| 72 | + }); |
| 73 | + return deferred.promise; |
| 74 | + }, |
| 75 | + set: function(id, data){ |
| 76 | + var deferred = $q.defer(); |
| 77 | + $http.post('/v1/store/' + id, data) |
| 78 | + .success(function(data){ |
| 79 | + deferred.resolve(); |
| 80 | + }) |
| 81 | + .error(function(){ |
| 82 | + deferred.reject(); |
| 83 | + }); |
| 84 | + return deferred.promise; |
| 85 | + }, |
| 86 | + delete: function(id){ |
| 87 | + var deferred = $q.defer(); |
| 88 | + $http.delete('/v1/store/' + id) |
| 89 | + .success(function(data){ |
| 90 | + deferred.resolve(data); |
| 91 | + }) |
| 92 | + .error(function(){ |
| 93 | + deferred.reject(); |
| 94 | + }); |
| 95 | + return deferred.promise; |
| 96 | + } |
| 97 | + }; |
| 98 | + }) |
| 99 | + .controller('navigationCtrl', function($scope, $q, $location, storeService){ |
| 100 | + var nav = this; |
| 101 | + nav.navCollapsed = true; |
| 102 | + |
| 103 | + this.toggleNav = function(){ |
| 104 | + nav.navCollapsed = ! nav.navCollapsed; |
| 105 | + }; |
| 106 | + |
| 107 | + this.navClass = function(page) { |
| 108 | + var currentRoute = $location.path().substring(1); |
| 109 | + return page === currentRoute || new RegExp(page).test(currentRoute) ? 'active' : ''; |
| 110 | + }; |
| 111 | + |
| 112 | + this.create = function(){ |
| 113 | + var id = '_' + new Date().getTime(); |
| 114 | + var q = storeService.set(id, { |
| 115 | + "title": "New Sample", |
| 116 | + "structure": "4-8", |
| 117 | + "rows": [{ |
| 118 | + "columns": [{ |
| 119 | + "styleClass": "col-md-4", |
| 120 | + "widgets": [] |
| 121 | + },{ |
| 122 | + "styleClass": "col-md-8", |
| 123 | + "widgets": [] |
| 124 | + }] |
| 125 | + }] |
| 126 | + }); |
| 127 | + |
| 128 | + $q.all([q, storeService.getAll()]).then(function(values){ |
| 129 | + nav.items = values[1]; |
| 130 | + }); |
| 131 | + }; |
| 132 | + |
| 133 | + storeService.getAll().then(function(data){ |
| 134 | + nav.items = data; |
| 135 | + }); |
| 136 | + |
| 137 | + $scope.$on('navChanged', function(){ |
| 138 | + storeService.getAll().then(function(data){ |
| 139 | + nav.items = data; |
| 140 | + }); |
| 141 | + }); |
| 142 | + }) |
| 143 | + .controller('dashboardCtrl', function($location, $rootScope, $scope, $routeParams, storeService, data){ |
| 144 | + this.name = $routeParams.id; |
| 145 | + this.model = data; |
| 146 | + |
| 147 | + this.delete = function(id){ |
| 148 | + storeService.delete(id); |
| 149 | + $location.path('/'); |
| 150 | + $rootScope.$broadcast('navChanged'); |
| 151 | + }; |
| 152 | + |
| 153 | + $scope.$on('adfDashboardChanged', function(event, name, model) { |
| 154 | + storeService.set(name, model); |
| 155 | + }); |
| 156 | + }); |
0 commit comments