/
logviewer.directive.coffee
155 lines (134 loc) · 5.88 KB
/
logviewer.directive.coffee
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
# logviewer. This directive uses jquery for simplicity
class Logviewer extends Directive
constructor: ($log, $window, buildbotService, $timeout, $sce) ->
$window = angular.element($window)
directive = ->
self =
num_lines: 0
auto_scroll: true
lines: []
onScroll: (e) ->
$timeout(self.maybeLoadMore, 1)
return null
setHeight: (elm) ->
height = $window.height() - elm.offset().top - 20
elm.css({height: height + "px"})
setNumLines: (num_lines) ->
if num_lines > self.num_lines
for i in [self.num_lines..num_lines - 1]
self.lines.push
content: "."
class: "log_o"
self.num_lines = num_lines
if self.auto_scroll
$timeout(self.autoScroll, 1)
else
$timeout(self.maybeLoadMore, 1)
autoScroll: ->
self.elm.scrollTop(self.raw.scrollHeight)
$timeout(self.maybeLoadMore, 1)
getViewPortLineBoundary: ->
line_height = self.raw.scrollHeight / self.num_lines
firstline = Math.round(self.raw.scrollTop / line_height) - 1
lastline = firstline + Math.round(self.elm.height() / line_height) + 1
if firstline < 0
firstline = 0
if lastline >= self.num_lines
lastline = self.num_lines - 1
firstline:firstline
lastline:lastline
lines_per_page: lastline - firstline
maybeLoadMore: ->
if self.loading
return
if self.scope.log.type != 's'
return
b = self.getViewPortLineBoundary()
if b.lastline > self.num_lines - 3
self.auto_scroll = true
else
self.auto_scroll = false
# if search mode, then we need to load everything
if self.scope.searchText?.length > 0
b.firstline = 0
b.lastline = self.num_lines - 1
lines = self.lines
# tighten refresh boundary to the part that is not loaded
# from top...
while lines[b.firstline].loaded and b.firstline < b.lastline
b.firstline += 1
# and from bottom...
while lines[b.lastline].loaded and b.firstline < b.lastline
b.lastline -= 1
# hurray! everything is already there!
if b.firstline == b.lastline and lines[b.firstline].loaded
return
# load one more page up if the previous line is not loaded
if b.firstline > 0 and not lines[b.firstline - 1].loaded
b.firstline -= b.lines_per_page
if b.firstline < 0
b.firstline = 0
# load one more page down if the previous page is not loaded
if b.lastline < self.num_lines - 1 and not lines[b.lastline + 1].loaded
b.lastline += b.lines_per_page
if b.lastline > self.num_lines - 1
b.lastline = self.num_lines - 1
spec =
offset: b.firstline
limit: b.lastline - b.firstline + 1
self.loading = true
self.log.all('contents').getList(spec).then (content) ->
self.loading = false
content = content[0]
offset = spec.offset
for line in content.content.split("\n")
logclass = "o"
if line.length > 0
logclass = line[0]
line = line[1..]
lines[offset] =
content: line
class: "log_" + logclass
loaded: true
offset += 1
self.maybeLoadMore()
return null
updateLog: ->
self.log.bind(self.scope)
.then (log) ->
if log.type == 's'
self.scope.$watch "log.num_lines", ->
self.setNumLines(log.num_lines)
else
self.log.all('contents').getList().then (content) ->
self.scope.content = $sce.trustAs($sce.HTML, content[0].content)
link: (scope, elm, attr) ->
elm = elm.children("pre")
self.setHeight(elm)
self.elm = elm
self.raw = elm[0]
$window.resize(-> self.setHeight(elm))
elm.bind("scroll", self.onScroll)
scope.lines = self.lines
self.scope = scope
unwatch = scope.$watch "logid", (n, o) ->
if n?
self.logid = n
unwatch()
self.log = buildbotService.one('logs', self.logid)
self.updateLog()
# for the search feature, we need to load everything
scope.$watch "searchText", (n, o) ->
if n? and n?.length > 0
self.maybeLoadMore()
return null
return {
replace: true
transclude: true
restrict: 'E'
scope: {logid:"="}
templateUrl: "views/logviewer.html"
link: (scope, elm, attr) ->
self = directive()
self.link(scope, elm, attr)
}