Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +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 | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 17 | function renderStatusLine() { |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 18 | document.write( "<div class='fullwidth'>"); |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 19 | document.write( "<div class='statusline'></div>" ); |
| 20 | document.write( "</div>" ); |
| 21 | } |
| 22 | |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 23 | function renderView( /* Array of String */ columns, /* Array of String */ buttons ) { |
| 24 | renderStatusLine(); |
| 25 | renderButtons(buttons); |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 26 | document.write( "<div class='table'>"); |
| 27 | document.write( "<table id='events' class='tablelayout'>" ); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 28 | |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 29 | document.write( "<thead><tr>" ); |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 30 | for ( var name in columns ) { |
| 31 | document.write( "<th>" + columns[name] + "</th>" ); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 32 | } |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 33 | document.write( "</tr></thead><tbody>" ); |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 34 | document.write( "</tbody></table>" ); |
| 35 | document.write( "</div>"); |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 36 | renderButtons(buttons); |
| 37 | renderStatusLine(); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 40 | function renderButtons( buttons ) { |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 41 | document.write( "<div class='fullwidth'>"); |
| 42 | document.write( "<div class='buttons'>" ); |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 43 | for( var b in buttons ) { |
| 44 | document.write( "<div class='button'>"); |
| 45 | document.write(buttons[b]); |
| 46 | document.write( "</div>"); |
| 47 | } |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 48 | document.write( "</div>" ); |
| 49 | document.write( "</div>" ); |
| 50 | } |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 51 | |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 52 | function renderData( eventData ) { |
| 53 | $(".statusline").empty().append(eventData.status); |
| 54 | $("#events > tbody > tr").remove(); |
| 55 | for ( var idx in eventData.data ) { |
| 56 | entry( eventData.data[idx] ); |
| 57 | } |
Carsten Ziegeler | b8b8f31 | 2008-12-16 21:39:53 +0000 | [diff] [blame] | 58 | $("#events").trigger("update"); |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | function entry( /* Object */ dataEntry ) { |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 62 | var trElement = tr( null, { id: "entry" + dataEntry.id } ); |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 63 | entryInternal( trElement, dataEntry ); |
| 64 | $("#events > tbody").append(trElement); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 68 | function entryInternal( /* Element */ parent, /* Object */ dataEntry ) { |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 69 | var id = dataEntry.id; |
| 70 | var topic = dataEntry.topic; |
| 71 | var properties = dataEntry.properties; |
| 72 | |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 73 | parent.appendChild( td( null, null, [ text( printDate(dataEntry.received) ) ] ) ); |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 74 | parent.appendChild( td( null, null, [ text( topic ) ] ) ); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 75 | |
Carsten Ziegeler | 0e1e3cb | 2008-10-27 16:30:38 +0000 | [diff] [blame] | 76 | var propE; |
| 77 | if ( dataEntry.info ) { |
| 78 | propE = text(dataEntry.info); |
| 79 | } else { |
| 80 | var tableE = createElement("table"); |
| 81 | var bodyE = createElement("tbody"); |
| 82 | tableE.appendChild(bodyE); |
| 83 | |
| 84 | for( var p in dataEntry.properties ) { |
Carsten Ziegeler | 220e791 | 2008-12-22 11:31:23 +0000 | [diff] [blame] | 85 | var c1 = td(null, null, [text(p)]); |
| 86 | c1.setAttribute("style", "border:0px none;"); |
| 87 | var c2 = td(null, null, [text(dataEntry.properties[p])]); |
| 88 | c2.setAttribute("style", "border:0px none;"); |
| 89 | bodyE.appendChild(tr(null, null, [ c1, c2 ])); |
Carsten Ziegeler | 0e1e3cb | 2008-10-27 16:30:38 +0000 | [diff] [blame] | 90 | } |
| 91 | propE = tableE; |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Carsten Ziegeler | 0e1e3cb | 2008-10-27 16:30:38 +0000 | [diff] [blame] | 94 | parent.appendChild( td( null, null, [propE] ) ); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 97 | /* displays a date in the user's local timezone */ |
| 98 | function printDate(time) { |
| 99 | var date = time ? new Date(time) : new Date(); |
| 100 | return date.toLocaleString(); |
| 101 | } |
| 102 | |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 103 | function loadData() { |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 104 | $.get(pluginRoot + "/data.json", null, function(data) { |
| 105 | renderData(data); |
| 106 | }, "json"); |
| 107 | } |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 108 | |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 109 | function renderEvents() { |
Carsten Ziegeler | 5639f2a | 2008-11-12 05:20:15 +0000 | [diff] [blame] | 110 | renderView( ["Received", "Topic", "Properties"], |
| 111 | ["<button id='reloadButton' type='button' name='reload'>Reload</button>", |
| 112 | "<button id='clearButton' type='button' name='clear'>Clear List</button>"]); |
Carsten Ziegeler | f990c95 | 2008-10-27 11:36:46 +0000 | [diff] [blame] | 113 | |
Carsten Ziegeler | d2de88c | 2008-10-26 17:47:51 +0000 | [diff] [blame] | 114 | loadData(); |
| 115 | |
| 116 | $("#events").tablesorter(); |
| 117 | $("#reloadButton").click(loadData); |
| 118 | $("#clearButton").click(function () { |
| 119 | $("#events > tbody > tr").remove(); |
| 120 | $.post(pluginRoot, { "action":"clear" }, function(data) { |
| 121 | renderData(data); |
| 122 | }, "json"); |
| 123 | }); |
Carsten Ziegeler | 616cd810 | 2008-10-23 16:44:07 +0000 | [diff] [blame] | 124 | } |