Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 1 | /* |
| 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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 17 | var view = 0; |
| 18 | |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 19 | function renderStatusLine() { |
| 20 | $("#plugin_content").append( "<div class='fullwidth'><div class='statusline'/></div>" ); |
| 21 | } |
| 22 | |
| 23 | function 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 | |
| 36 | function 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 | |
| 41 | function 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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 48 | if ( view == 1 ) { |
| 49 | $("#timeline").remove(); |
Carsten Ziegeler | 1dce120 | 2009-09-25 13:32:32 +0000 | [diff] [blame^] | 50 | $("div.table").append( "<div id='timeline' width='100%'></div>" ); |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 51 | for ( var idx in eventData.data ) { |
| 52 | entryTimeline( eventData.data[idx] ); |
| 53 | } |
| 54 | } |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | function 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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 63 | function entryTimeline( /* Object */ dataEntry ) { |
Carsten Ziegeler | 1dce120 | 2009-09-25 13:32:32 +0000 | [diff] [blame^] | 64 | var txt = "<div class='event" + dataEntry.category + "' style='overflow:visible;white-space:nowrap;width:" + dataEntry.width + "%;'>"; |
| 65 | txt = txt + "<b>" + dataEntry.offset + "</b> <b>" + dataEntry.topic + "</b>"; |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 66 | if ( dataEntry.info ) { |
Carsten Ziegeler | 1dce120 | 2009-09-25 13:32:32 +0000 | [diff] [blame^] | 67 | txt = txt + " : " + dataEntry.info; |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 68 | } |
| 69 | txt = txt + "</div>"; |
Carsten Ziegeler | 1dce120 | 2009-09-25 13:32:32 +0000 | [diff] [blame^] | 70 | $("#timeline").prepend(txt); |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 71 | } |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 72 | |
| 73 | function entryInternal( /* Element */ parent, /* Object */ dataEntry ) { |
| 74 | var id = dataEntry.id; |
| 75 | var topic = dataEntry.topic; |
| 76 | var properties = dataEntry.properties; |
Carsten Ziegeler | b940bc2 | 2009-09-24 13:25:25 +0000 | [diff] [blame] | 77 | |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 78 | parent.appendChild( td( null, null, [ text( printDate(dataEntry.received) ) ] ) ); |
| 79 | parent.appendChild( td( null, null, [ text( topic ) ] ) ); |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 80 | |
| 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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 90 | var c1 = td(null, null, [text(p)]); |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 91 | $(c1).css("border", "0px none"); |
| 92 | $(c1).css("padding", "0 4px 0 0"); |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 93 | var c2 = td(null, null, [text(dataEntry.properties[p])]); |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 94 | $(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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 101 | parent.appendChild( td( null, null, [propE] ) ); |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* displays a date in the user's local timezone */ |
| 105 | function printDate(time) { |
| 106 | var date = time ? new Date(time) : new Date(); |
| 107 | return date.toLocaleString(); |
| 108 | } |
| 109 | |
| 110 | function loadData() { |
| 111 | $.get(pluginRoot + "/data.json", null, function(data) { |
| 112 | renderData(data); |
| 113 | }, "json"); |
| 114 | } |
| 115 | |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 116 | function 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 Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 131 | function renderEvents() { |
| 132 | $(document).ready(function(){ |
| 133 | renderView( ["Received", "Topic", "Properties"], |
Carsten Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 134 | "<button class='switchButton' type='button' name='switch'>Timeline</button>" + |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 135 | "<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 Ziegeler | af592a5 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 141 | $(".switchButton").click(switchView); |
Carsten Ziegeler | 580814b | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 142 | $(".clearButton").click(function () { |
| 143 | $("#plugin_table > tbody > tr").remove(); |
| 144 | $.post(pluginRoot, { "action":"clear" }, function(data) { |
| 145 | renderData(data); |
| 146 | }, "json"); |
| 147 | }); |
| 148 | }); |
| 149 | } |