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