forked from kilianc/rtail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ejs
277 lines (232 loc) · 7.75 KB
/
app.ejs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* global angular, $, window, io, document, hljs, ansi_up */
'use strict'
/*!
* main js controller for webapp
*/
angular
.module('app', [
'ngAnimate',
'angularMoment',
'LocalForageModule',
'rt.popup',
'ui.router',
'cfp.hotkeys'
])
.config(function ($stateProvider) {
$stateProvider.state('streams', {
url: '/streams/:stream'
})
})
.directive('resizable', function ($localForage) {
return {
restrict: 'A',
link: function ($scope, $element) {
var $window = $(window)
var $body = $('body')
var handler = $element.find('.resize-handler')
var minWidth = $element.width()
handler.on('mousedown', function () {
$body.addClass('resizing')
$window.on('mousemove', onMouseMove)
})
$window.on('mouseup', function () {
$body.removeClass('resizing')
$window.off('mousemove', onMouseMove)
$localForage.setItem('sidebarWidth', $element.width())
})
function onMouseMove(e) {
var width = Math.min(600, Math.max(minWidth, e.clientX))
$element.css('width', width)
}
}
}
})
.controller('MainController', function MainController($scope, $injector) {
var $sce = $injector.get('$sce')
var $localForage = $injector.get('$localForage')
var $rootScope = $injector.get('$rootScope')
var $state = $injector.get('$state')
var $stateParams = $injector.get('$stateParams')
var $streamLines = $('.stream-lines')
var BUFFER_SIZE = 100
var ctrl = this
ctrl.paused = false
ctrl.version = '<%= version %>'
ctrl.lines = []
/*!
* socket stuff
*/
ctrl.socket = io(document.location.origin, { path: document.location.pathname + 'socket.io' })
ctrl.socket.on('connect', function () {
console.info('connected')
ctrl.activeStream && ctrl.socket.emit('select stream', ctrl.activeStream)
})
ctrl.socket.on('disconnect', function (err) {
console.warn('disconnected: %s', err)
})
ctrl.socket.on('streams', function (streams) {
ctrl.streams = streams
$scope.$apply()
})
ctrl.socket.on('backlog', function (lines) {
if (!lines) return
ctrl.lines = lines.map(formatLine)
$scope.$apply()
updateScroll()
console.info('%s: backlog received %d', ctrl.activeStream, lines.length)
})
ctrl.socket.on('line', function (line) {
ctrl.lines.push(formatLine(line))
if (ctrl.lines.length > BUFFER_SIZE) {
ctrl.lines.length >= 100 && ctrl.lines.shift()
}
$scope.$apply()
updateScroll()
})
ctrl.selectStream = function selectStream(stream) {
ctrl.lines = []
ctrl.activeStream = stream
ctrl.socket.emit('select stream', stream)
ctrl.resume()
$localForage.setItem('activeStream', stream)
}
ctrl.isSelected = function isSelected(stream) {
return ctrl.activeStream === stream
}
function updateScroll() {
var where = ctrl.streamDirection ? $streamLines[0].scrollHeight : 0
$streamLines.scrollTop(where)
}
function formatLine(line) {
if (!line.content) {
// handle empty line
line.html = $sce.trustAsHtml('')
} else if ('object' === line.type) {
// for object just format JSON
line.html = hljs.highlight('json', JSON.stringify(line.content, null, ' ')).value
line.html = $sce.trustAsHtml('<pre>' + line.html + '</pre>')
} else {
// for log lines use ansi format
line.html = escapeHtml(line.content)
line.html = ansi_up.ansi_to_html(line.html, { use_classes: true })
line.html = $sce.trustAsHtml(line.html)
}
return line
}
// https://github.com/component/escape-html/blob/master/index.js#L22
function escapeHtml(html) {
return String(html)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
ctrl.activeStreamFilter = function activeStreamFilter(line) {
try {
return (new RegExp(ctrl.activeStreamRegExp)).test(line.content)
} catch (err) {
return true
}
}
ctrl.resume = function resume() {
if (!ctrl.paused) return
ctrl.paused = false
ctrl.socket.emit('select stream', ctrl.activeStream)
$streamLines.on('wheel', ctrl.pause)
}
ctrl.pause = function pause() {
if (ctrl.paused) return
ctrl.paused = true
ctrl.socket.emit('select stream')
$streamLines.off('wheel', ctrl.pause)
$scope.$apply()
}
$streamLines.on('wheel', ctrl.pause)
/*!
* settings and preferences
*/
ctrl.toggleFavorite = function toggleFavorite(stream) {
if (ctrl.favorites[stream]) {
delete ctrl.favorites[stream]
} else {
ctrl.favorites[stream] = true
}
$localForage.setItem('favorites', ctrl.favorites)
}
ctrl.toggleTimestamp = function toggleTimestamp(stream) {
if (ctrl.hiddenTimestamps[stream]) {
delete ctrl.hiddenTimestamps[stream]
} else {
ctrl.hiddenTimestamps[stream] = true
}
$localForage.setItem('hiddenTimestamps', ctrl.hiddenTimestamps)
}
ctrl.setTheme = function setTheme(theme) {
ctrl.theme = theme
$localForage.setItem('theme', theme)
}
ctrl.setFontFamily = function setFontFamily(fontFamily) {
ctrl.fontFamily = fontFamily
$localForage.setItem('fontFamily', fontFamily)
}
ctrl.incFontSize = function incFontSize() {
ctrl.fontSize = Math.min(7, ctrl.fontSize + 1)
$localForage.setItem('fontSize', ctrl.fontSize)
}
ctrl.resetFontSize = function resetFontSize() {
ctrl.fontSize = 4
$localForage.setItem('fontSize', ctrl.fontSize)
}
ctrl.decFontSize = function decFontSize(fontSize) {
ctrl.fontSize = Math.max(1, ctrl.fontSize - 1)
$localForage.setItem('fontSize', fontSize)
}
ctrl.setStreamDirection = function setStreamDirection(streamDirection) {
ctrl.streamDirection = streamDirection
$localForage.setItem('streamDirection', streamDirection)
}
/*!
* load storage in memory and boot
*/
$localForage.getItem('sidebarWidth').then(function (sidebarWidth) {
ctrl.sidebarWidth = sidebarWidth
})
$localForage.getItem('theme').then(function (theme) {
ctrl.theme = theme || 'dark'
})
$localForage.getItem('fontFamily').then(function (fontFamily) {
ctrl.fontFamily = fontFamily || 1
})
$localForage.getItem('fontSize').then(function (fontSize) {
ctrl.fontSize = fontSize || 4
})
$localForage.getItem('favorites').then(function (favorites) {
ctrl.favorites = favorites || {}
})
$localForage.getItem('hiddenTimestamps').then(function (hiddenTimestamps) {
ctrl.hiddenTimestamps = hiddenTimestamps || {}
})
$localForage.getItem('timestampFormat').then(function (timestampFormat) {
ctrl.timestampFormat = timestampFormat || 'MM/DD/YY hh:mm:ss'
})
$localForage.getItem('activeStream').then(function (activeStream) {
if (!activeStream || ('streams' === $state.current.name && $stateParams.stream)) return
console.info('%s: restoring session', activeStream)
$state.go('streams', { stream: activeStream })
})
$localForage.getItem('streamDirection').then(function (streamDirection) {
ctrl.streamDirection = undefined === streamDirection ? true : streamDirection
})
/*!
* respond to url change
*/
$rootScope.$on('$stateChangeStart', function (e, toState, toParams) {
if ('streams' !== toState.name) return
ctrl.selectStream(toParams.stream)
})
/*!
* tell UI we're ready to roll
*/
ctrl.loaded = true
})