blob: acb39513b1ea6853e5771ec0dace9b9390f49b55 [file] [log] [blame]
Carsten Ziegeler580814b2009-09-24 12:09:34 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000017var view = 0;
18
Carsten Ziegeler580814b2009-09-24 12:09:34 +000019function renderStatusLine() {
20 $("#plugin_content").append( "<div class='fullwidth'><div class='statusline'/></div>" );
21}
22
23function renderView( /* Array of String */ columns, /* String */ buttons ) {
24 renderStatusLine();
25 renderButtons(buttons);
26 var txt = "<div class='table'><table id='plugin_table' class='tablelayout'><thead><tr>";
27 for ( var name in columns ) {
28 txt = txt + "<th class='col_" + columns[name] + "'>" + columns[name] + "</th>";
29 }
30 txt = txt + "</tr></thead><tbody></tbody></table></div>";
31 $("#plugin_content").append( txt );
32 renderButtons(buttons);
33 renderStatusLine();
34}
35
36function renderButtons( buttons ) {
37 $("#plugin_content").append( "<form method='post' enctype='multipart/form-data'><div class='fullwidth'><div class='buttons'>" +
38 buttons + "</div></div></form>" );
39}
40
41function renderData( eventData ) {
42 $(".statusline").empty().append(eventData.status);
43 $("#plugin_table > tbody > tr").remove();
44 for ( var idx in eventData.data ) {
45 entry( eventData.data[idx] );
46 }
47 $("#plugin_table").trigger("update");
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000048 if ( view == 1 ) {
49 $("#timeline").remove();
Carsten Ziegeler1dce1202009-09-25 13:32:32 +000050 $("div.table").append( "<div id='timeline' width='100%'></div>" );
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000051 for ( var idx in eventData.data ) {
52 entryTimeline( eventData.data[idx] );
53 }
54 }
Carsten Ziegeler580814b2009-09-24 12:09:34 +000055}
56
57function entry( /* Object */ dataEntry ) {
58 var trElement = tr( null, { id: "entry" + dataEntry.id } );
59 entryInternal( trElement, dataEntry );
60 $("#plugin_table > tbody").append(trElement);
61}
62
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000063function entryTimeline( /* Object */ dataEntry ) {
Carsten Ziegeler1dce1202009-09-25 13:32:32 +000064 var txt = "<div class='event" + dataEntry.category + "' style='overflow:visible;white-space:nowrap;width:" + dataEntry.width + "%;'>";
65 txt = txt + "<b>" + dataEntry.offset + "</b>&nbsp;<b>" + dataEntry.topic + "</b>";
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000066 if ( dataEntry.info ) {
Carsten Ziegeler1dce1202009-09-25 13:32:32 +000067 txt = txt + "&nbsp;:&nbsp;" + dataEntry.info;
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000068 }
69 txt = txt + "</div>";
Carsten Ziegeler1dce1202009-09-25 13:32:32 +000070 $("#timeline").prepend(txt);
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000071}
Carsten Ziegeler580814b2009-09-24 12:09:34 +000072
73function entryInternal( /* Element */ parent, /* Object */ dataEntry ) {
74 var id = dataEntry.id;
75 var topic = dataEntry.topic;
76 var properties = dataEntry.properties;
Carsten Ziegelerb940bc22009-09-24 13:25:25 +000077
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000078 parent.appendChild( td( null, null, [ text( printDate(dataEntry.received) ) ] ) );
79 parent.appendChild( td( null, null, [ text( topic ) ] ) );
Carsten Ziegeler580814b2009-09-24 12:09:34 +000080
81 var propE;
82 if ( dataEntry.info ) {
83 propE = text(dataEntry.info);
84 } else {
85 var tableE = createElement("table");
86 var bodyE = createElement("tbody");
87 tableE.appendChild(bodyE);
88
89 for( var p in dataEntry.properties ) {
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000090 var c1 = td(null, null, [text(p)]);
Carsten Ziegeler580814b2009-09-24 12:09:34 +000091 $(c1).css("border", "0px none");
92 $(c1).css("padding", "0 4px 0 0");
Carsten Ziegeleraf592a52009-09-24 14:33:35 +000093 var c2 = td(null, null, [text(dataEntry.properties[p])]);
Carsten Ziegeler580814b2009-09-24 12:09:34 +000094 $(c2).css("border", "0px none");
95 $(c2).css("padding", "0 0 0 4px");
96 bodyE.appendChild(tr(null, null, [ c1, c2 ]));
97 }
98 propE = tableE;
99 }
100
Carsten Ziegeleraf592a52009-09-24 14:33:35 +0000101 parent.appendChild( td( null, null, [propE] ) );
Carsten Ziegeler580814b2009-09-24 12:09:34 +0000102}
103
104/* displays a date in the user's local timezone */
105function printDate(time) {
106 var date = time ? new Date(time) : new Date();
107 return date.toLocaleString();
108}
109
110function loadData() {
111 $.get(pluginRoot + "/data.json", null, function(data) {
112 renderData(data);
113 }, "json");
114}
115
Carsten Ziegeleraf592a52009-09-24 14:33:35 +0000116function switchView() {
117 if ( view == 0 ) {
118 view = 1;
119 $("#plugin_table").hide();
120 $(".switchButton").empty();
121 $(".switchButton").append("List");
122 loadData();
123 } else {
124 view = 0;
125 $("#timeline").remove();
126 $("#plugin_table").show();
127 $(".switchButton").empty();
128 $(".switchButton").append("Timeline");
129 }
130}
Carsten Ziegeler580814b2009-09-24 12:09:34 +0000131function renderEvents() {
132 $(document).ready(function(){
133 renderView( ["Received", "Topic", "Properties"],
Carsten Ziegeleraf592a52009-09-24 14:33:35 +0000134 "<button class='switchButton' type='button' name='switch'>Timeline</button>" +
Carsten Ziegeler580814b2009-09-24 12:09:34 +0000135 "<button class='clearButton' type='button' name='clear'>Clear List</button>" +
136 "<button class='reloadButton' type='button' name='reload'>Reload</button>");
137 loadData();
138
139 $("#plugin_table").tablesorter();
140 $(".reloadButton").click(loadData);
Carsten Ziegeleraf592a52009-09-24 14:33:35 +0000141 $(".switchButton").click(switchView);
Carsten Ziegeler580814b2009-09-24 12:09:34 +0000142 $(".clearButton").click(function () {
143 $("#plugin_table > tbody > tr").remove();
144 $.post(pluginRoot, { "action":"clear" }, function(data) {
145 renderData(data);
146 }, "json");
147 });
148 });
149}