-
Notifications
You must be signed in to change notification settings - Fork 3
/
logwatcher.html
executable file
·199 lines (182 loc) · 7.27 KB
/
logwatcher.html
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
<html>
<head>
<link rel="stylesheet" href="bootstrap.min.css">
<script src="../lib/jquery-1.6.3.min.js" type="text/javascript" language="javascript" charset="utf-8"></script>
<script src="../lib/knockout-2.0.0.js" type="text/javascript" language="javascript" charset="utf-8"></script>
<script type="text/javascript" language="javascript" charset="utf-8">
</script>
<style type="text/css" media="screen">
/* <![CDATA[ */
.time {
font-size:0.8em;
display:inline-block;
overflow:hidden;
white-space:nowrap;
}
.time:hover {
display:inline-block;
width:auto;
}
body {
padding-top:60px;
}
/* ]]> */
</style>
<title>LogWatcher</title>
<script type="text/javascript">
// <![CDATA[
$(function () {
var ws_uri = "ws://localhost:9000";
filewatcher = {};
filewatcher.connection = function() {
if ("WebSocket" in window) {
filewatcher.webSocket = new WebSocket(ws_uri);
} else {
// Firefox 7/8 currently prefixes the WebSocket object
filewatcher.webSocket = new MozWebSocket(ws_uri);
}
console.log("connection trial");
filewatcher.webSocket.onmessage = function (e) {
data = JSON.parse(e.data)
console.log(data)
if (data["type"] == "msg") {
filewatcher.vM.addNewLine(data["line"], data["file"])
} else console.log(data["msg"])
}
filewatcher.webSocket.onopen = function () {
console.log("connected");
$("#connection_status").text("connected");
$("#before_connection").hide();
$("#after_connection").show();
clearInterval(filewatcher.timer);
};
filewatcher.webSocket.onclose = function () {
console.log("disconnected");
$("#connection_status").text("disconnected");
$("#after_connection").hide();
$("#before_connection").show();
clearInterval(filewatcher.timer)
filewatcher.timer = setInterval("filewatcher.connection()", 1000);
};
}
filewatcher.connection();
window.onbeforeunload = function() {
filewatcher.webSocket.onclose = function() {}
filewatcher.webSocket.close();
};
var NewLine = function(newline) {
this.newline = newline;
this._timestamp = new Date();
this.timestamp = function() {
return this._timestamp;
}
}
var File = function (name, fp) {
this.name = name;
this.fp = fp
this.isWatched = ko.observable(true);
this.newlines = ko.observableArray();
this.watched = function () {
this.isWatched(true);
msg = JSON.stringify({
"watch": this.fp
});
filewatcher.webSocket.send(msg);
}.bind(this)
this.unwatched = function () {
this.isWatched(false);
msg = JSON.stringify({
"unwatch": this.fp
})
filewatcher.webSocket.send(msg);
}.bind(this)
this.toggleWatch = function () {
if (this.isWatched())
this.unwatched();
else this.watched();
return true;
}
}
filewatcher.files = []
filewatcher.vM = {
files: ko.observableArray(),
filesByPath : {},
fileToAddName : ko.observable(""),
fileToAddFilepath : ko.observable(""),
addFile : function() {
if (this.fileToAddFilepath() != "" && this.fileToAddName() != "") {
var f = new File(this.fileToAddName(), this.fileToAddFilepath());
this.filesByPath[this.fileToAddFilepath()] = f;
this.files.push(f); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
this.fileToAddName(""); // Clears the text box, because it's bound to the "itemToAdd" observable
this.fileToAddFilepath(""); // Clears the text box, because it's bound to the "itemToAdd" observable
}
},
addNewLine : function(newline, filepath) {
this.filesByPath[filepath].newlines.push(NewLine(newline));
}
};
$.each(filewatcher.files, function (i, file) {
filewatcher.vM.files.push(new File(file[0], file[1]));
});
ko.applyBindings(filewatcher.vM); // This makes Knockout get to work
});
// ]]>
</script>
</head>
<body>
<div class="container">
<div class="topbar">
<div class="topbar-inner">
<div class="container">
<a class="brand">LogWatcher</a>
<div id="connection_status">
</div>
</div>
</div>
</div>
<div id="before_connection">
<p>The connection to the server could not be established.</p>
</div>
<div id="after_connection">
<div data-bind="visible: files().length == 0">
<p>
There is no file to watch yet. You can had a file with the form below.
</p>
</div>
<form data-bind="submit: addFile">
Name : <input data-bind='value: fileToAddName, valueUpdate: "afterkeydown"' placeholder="MyBeautifulFile"/>
Filepath : <input data-bind='value: fileToAddFilepath, valueUpdate: "afterkeydown"' placeholder="/path/to/my/file.txt or ./file.txt"/>
<button class="btn" type="submit" data-bind="enable: ( fileToAddName().length && fileToAddFilepath().length )">Add</button>
</form>
<table id="files" class="zebra-striped bordered-table" data-bind="visible: files().length > 0">
<tr>
<th>Filename</th>
<th>Watch</th>
<th>Filepath</th>
</tr>
<tbody data-bind="foreach: files">
<tr>
<td><span data-bind="text: name" class="name"></span></td>
<td><input type="checkbox" data-bind="checked: isWatched(), click: toggleWatch"/></td>
<td><span data-bind="text: fp" class="help-inline"></span></td>
</tr>
</tbody>
</table>
<div data-bind="foreach: files">
<div class="well" data-bind="visible: isWatched(), attr: { id: 'file_' + fp.replace('.','') } ">
<h2><span data-bind="text: name"></span></h2>
<div class="log_content">
<div data-bind="foreach: newlines">
<div>
<span class="time" data-bind="text: timestamp()"></span>
<span class="modif" data-bind="text: newline"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>