bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/annotate/web/static/js/annotate.ts (about) 1 /// <reference path="typings/main/ambient/angular/angular.d.ts" /> 2 /// <reference path="typings/main/ambient/jquery/jquery.d.ts" /> 3 /// <reference path="typings/main/definitions/moment/moment.d.ts" /> 4 /// <reference path="typings/main/ambient/underscore/underscore.d.ts" /> 5 6 declare var moment: any; 7 8 var annotateApp = angular.module('annotateApp', [ 9 'ngRoute', 10 'annotateControllers', 11 'mgcrea.ngStrap', 12 ]); 13 14 var timeFormat = 'YYYY-MM-DDTHH:mm:ssZ'; 15 16 class Annotation { 17 Id: string; 18 Message: string; 19 StartDate: string; // RFC3999 20 EndDate: string; // RFC3999 21 CreationUser: string; 22 Url: string; 23 Source: string; 24 Host: string; 25 Owner: string; 26 Category: string; 27 28 constructor(a?) { 29 a = a || {}; 30 this.Id = a.Id || ""; 31 this.Message = a.Message || ""; 32 this.StartDate = a.StartDate || ""; 33 this.EndDate = a.EndDate || ""; 34 this.CreationUser = a.CreationUser || getUser() || ""; 35 this.Url = a.Url || ""; 36 this.Source = a.Source || "annotate-ui"; 37 this.Host = a.Host || ""; 38 this.Owner = a.Owner || getOwner() || ""; 39 this.Category = a.Category || ""; 40 } 41 setTime() { 42 var now = moment().format(timeFormat) 43 this.StartDate = now; 44 this.EndDate = now; 45 } 46 } 47 48 49 50 // Reference Struct 51 // type Annotation struct { 52 // Id string 53 // Message string 54 // StartDate time.Time 55 // EndDate time.Time 56 // CreationUser string 57 // Url *url.URL `json:",omitempty"` 58 // Source string 59 // Host string 60 // Owner string 61 // Category string 62 // } 63 64 65 annotateApp.config(['$routeProvider', '$locationProvider', '$httpProvider', function($routeProvider, $locationProvider, $httpProvider) { 66 $locationProvider.html5Mode(true); 67 $routeProvider. 68 when('/', { 69 title: 'Create', 70 templateUrl: 'static/partials/create.html', 71 controller: 'CreateCtrl', 72 }). 73 when('/list', { 74 title: 'List', 75 templateUrl: 'static/partials/list.html', 76 controller: 'ListCtrl', 77 }). 78 otherwise({ 79 redirectTo: '/', 80 }); 81 }]); 82 83 annotateApp.run(['$location', '$rootScope', function($location, $rootScope) { 84 // $rootScope.$on('$routeChangeSuccess', function(event, current, previous) { 85 // $rootScope.title = current.$$route.title; 86 // }); 87 }]); 88 89 var annotateControllers = angular.module('annotateControllers', []) 90 91 annotateControllers.controller('AnnotateCtrl', ['$scope', '$route', '$http', '$rootScope', function($scope, $route, $http, $rootScope) { 92 $scope.active = (v) => { 93 if (!$route.current) { 94 return null; 95 } 96 if ($route.current.loadedTemplateUrl == 'partials/' + v + '.html') { 97 return { active: true }; 98 } 99 return null; 100 }; 101 }]); 102 103 annotateControllers.controller('CreateCtrl', ['$scope', '$http', '$routeParams', function($scope, $http, $p) { 104 if ($p.guid) { 105 $http.get('/annotation/' + $p.guid) 106 .success((data) => { 107 $scope.annotation = new Annotation(data); 108 }) 109 .error((error) => { 110 $scope.error = error; 111 }) 112 } else { 113 var a = new Annotation(); 114 a.setTime(); 115 $scope.annotation = a; 116 } 117 $http.get('/annotation/values/Owner') 118 .success((data) => { 119 $scope.owners = data; 120 }) 121 $http.get('/annotation/values/Category') 122 .success((data) => { 123 $scope.categories = data; 124 }) 125 $http.get('/annotation/values/Host') 126 .success((data) => { 127 $scope.hosts = data; 128 }) 129 $scope.switch = () => { 130 var m = moment.parseZone($scope.annotation.StartDate); 131 if (m.zone() == 0) { 132 $scope.annotation.StartDate = moment($scope.annotation.StartDate).local().format(timeFormat); 133 $scope.annotation.EndDate = moment($scope.annotation.EndDate).local().format(timeFormat); 134 } else { 135 $scope.annotation.StartDate = moment($scope.annotation.StartDate).utc().format(timeFormat); 136 $scope.annotation.EndDate = moment($scope.annotation.EndDate).utc().format(timeFormat); 137 } 138 } 139 $scope.submit = () => { 140 var idMissing = $scope.annotation.Id == ""; 141 if (idMissing && $scope.annotation.CreationUser != "") { 142 setUser($scope.annotation.CreationUser); 143 } 144 if (idMissing && $scope.annotation.Owner != "") { 145 setOwner($scope.annotation.Owner); 146 } 147 $http.post('/annotation', $scope.annotation) 148 .success((data) => { 149 $scope.annotation = new Annotation(data); 150 $scope.error = ""; 151 }) 152 .error((error) => { 153 $scope.error = error; 154 }) 155 }; 156 }]); 157 158 annotateControllers.controller('ListCtrl', ['$scope', '$http', function($scope, $http) { 159 $scope.EndDate = moment().format(timeFormat); 160 $scope.StartDate = moment().subtract(1, "hours").format(timeFormat); 161 $scope.url = (url) => { 162 url = url.replace(/.*?:\/\//g, "") 163 if (url.length > 20) { 164 url = url.substring(0, 20 - 3) + "..." 165 } 166 return url; 167 } 168 $scope.active = (a) => { 169 var now = moment(); 170 return moment(a.StartDate).isBefore(now) && moment(a.EndDate).isAfter(now); 171 } 172 $scope.get = () => { 173 var params = "StartDate=" + encodeURIComponent($scope.StartDate) + "&EndDate=" + encodeURIComponent($scope.EndDate); 174 $http.get('/annotation/query?' + params) 175 .success(function(data) { 176 $scope.annotations = data; 177 }) 178 .error(function(error) { 179 $scope.status = 'Unable to fetch annotations: ' + error; 180 }); 181 } 182 $scope.get(); 183 $scope.delete = (id) => { 184 $http.delete('/annotation/' + id) 185 .error((error) => { 186 $scope.status = error; 187 }) 188 .success(() => { 189 // Remove deleted item from scope model 190 $scope.annotations = _.without($scope.annotations, _.findWhere($scope.annotations, { "Id": id })); 191 }) 192 } 193 $scope.switch = () => { 194 var m = moment.parseZone($scope.StartDate); 195 if (m.zone() == 0) { 196 $scope.StartDate = moment($scope.StartDate).local().format(timeFormat); 197 $scope.EndDate = moment($scope.EndDate).local().format(timeFormat); 198 } else { 199 $scope.StartDate = moment($scope.StartDate).utc().format(timeFormat); 200 $scope.EndDate = moment($scope.EndDate).utc().format(timeFormat); 201 } 202 } 203 }]); 204 205 // From http://www.quirksmode.org/js/cookies.html 206 declare function escape(string: string): string; 207 208 declare function unescape(string: string): string; 209 210 interface Date { 211 toGMTString(): string; 212 } 213 214 function createCookie(name, value, days) { 215 var expires; 216 if (days) { 217 var date = new Date(); 218 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 219 expires = "; expires=" + date.toGMTString(); 220 } 221 else { 222 expires = ""; 223 } 224 document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/"; 225 } 226 function readCookie(name) { 227 var nameEQ = escape(name) + "="; 228 var ca = document.cookie.split(';'); 229 for (var i = 0; i < ca.length; i++) { 230 var c = ca[i]; 231 while (c.charAt(0) === ' ') 232 c = c.substring(1, c.length); 233 if (c.indexOf(nameEQ) === 0) 234 return unescape(c.substring(nameEQ.length, c.length)); 235 } 236 return null; 237 } 238 function eraseCookie(name) { 239 createCookie(name, "", -1); 240 } 241 242 function getUser() { 243 return readCookie('action-user'); 244 } 245 function setUser(name) { 246 createCookie('action-user', name, 1000); 247 } 248 249 function getOwner() { 250 return readCookie('action-owner'); 251 } 252 function setOwner(name) { 253 console.log("set-cookie owner") 254 createCookie('action-owner', name, 1000); 255 }