Carsten Ziegeler | def0aa2 | 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 | */ |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 17 | var eventsTable = false; |
Carsten Ziegeler | def0aa2 | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 18 | |
| 19 | /* displays a date in the user's local timezone */ |
| 20 | function printDate(time) { |
| 21 | var date = time ? new Date(time) : new Date(); |
| 22 | return date.toLocaleString(); |
| 23 | } |
| 24 | |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 25 | function renderData( eventData ) { |
| 26 | $('.statline').html(eventData.status); // FIXME: |
| 27 | |
| 28 | // append table view |
| 29 | eventsBody.empty(); |
| 30 | for ( var i in eventData.data ) entry( eventData.data[i] ); |
| 31 | eventsTable.trigger('update').trigger('applyWidgets'); |
| 32 | |
| 33 | // append timeline view |
| 34 | timeline.empty(); |
| 35 | for ( var i in eventData.data ) entryTimeline( eventData.data[i] ); |
Carsten Ziegeler | def0aa2 | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 38 | |
| 39 | function entryTimeline( /* Object */ dataEntry ) { |
| 40 | var txt = '<div class="event event' + dataEntry.category + '" style="width:' + dataEntry.width + '%">' + |
| 41 | '<b>' + dataEntry.offset + '</b> <b>' + dataEntry.topic + '</b>'; |
| 42 | if ( dataEntry.info ) txt += ' : ' + dataEntry.info; |
| 43 | txt += '</div>'; |
| 44 | timeline.prepend(txt); |
Carsten Ziegeler | 40f56e3 | 2009-09-24 14:33:35 +0000 | [diff] [blame] | 45 | } |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 46 | |
| 47 | function entry( /* Object */ dataEntry ) { |
| 48 | var properties = dataEntry.properties; |
| 49 | |
| 50 | var propE; |
| 51 | if ( dataEntry.info ) { |
| 52 | propE = text(dataEntry.info); |
| 53 | } else { |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 54 | var bodyE = createElement('tbody'); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 55 | for( var p in dataEntry.properties ) { |
| 56 | bodyE.appendChild(tr(null, null, [ |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 57 | td('propName', null, [text(p)]), |
| 58 | td('propVal' , null, [text(dataEntry.properties[p])]) |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 59 | ])); |
| 60 | } |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 61 | propE = createElement('table', 'propTable', null, [ bodyE ]); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | $(tr( null, { id: 'entry' + dataEntry.id }, [ |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 65 | td( 'time', null, [ text( printDate(dataEntry.received) ) ] ), |
| 66 | td( 'topic', null, [ text( dataEntry.topic ) ] ), |
| 67 | td( 'detailes', null, [ propE ] ) |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 68 | ])).appendTo(eventsBody); |
| 69 | } |
| 70 | |
| 71 | var timeline = false; |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 72 | var timelineLegend = false; |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 73 | $(document).ready(function(){ |
| 74 | eventsTable = $('#eventsTable'); |
| 75 | eventsBody = eventsTable.find('tbody'); |
| 76 | timeline = $('#timeline'); |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 77 | timelineLegend = $('#timelineLegend'); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 78 | |
| 79 | $('#clear').click(function () { |
| 80 | $.post(pluginRoot, { 'action':'clear' }, renderData, 'json'); |
Carsten Ziegeler | def0aa2 | 2009-09-24 12:09:34 +0000 | [diff] [blame] | 81 | }); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 82 | $('#switch').click(function() { |
| 83 | var timelineHidden = timeline.hasClass('ui-helper-hidden'); |
| 84 | if (timelineHidden) { |
| 85 | $(this).text(i18n.displayList); |
| 86 | timeline.removeClass('ui-helper-hidden'); |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 87 | timelineLegend.removeClass('ui-helper-hidden'); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 88 | eventsTable.addClass('ui-helper-hidden'); |
| 89 | } else { |
| 90 | $(this).text(i18n.displayTimeline); |
| 91 | timeline.addClass('ui-helper-hidden'); |
Valentin Valchev | bb75c89 | 2010-03-24 07:34:38 +0000 | [diff] [blame] | 92 | timelineLegend.addClass('ui-helper-hidden'); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 93 | eventsTable.removeClass('ui-helper-hidden'); |
| 94 | } |
| 95 | }); |
| 96 | $('#reload').click(function() { |
| 97 | $.get(pluginRoot + '/data.json', null, renderData, 'json'); |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 98 | }).click(); |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 99 | |
| 100 | function sendData(action) { |
| 101 | // check topic |
| 102 | var topic = sendTopic.val(); |
| 103 | var topicOk = topic.match(/^[\w-]+(\/[\w-]+)*$/g) != null; |
| 104 | if (topicOk) { |
| 105 | sendTopic.removeClass('ui-state-error'); |
| 106 | } else { |
Valentin Valchev | d8be730 | 2012-04-06 09:05:42 +0000 | [diff] [blame] | 107 | sendTopic.addClass('ui-state-error'); |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 108 | } |
| 109 | var data = sendProperties.propeditor('serialize'); |
Valentin Valchev | d8be730 | 2012-04-06 09:05:42 +0000 | [diff] [blame] | 110 | if (topicOk && typeof data != 'boolean') { |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 111 | $.post(pluginRoot, |
| 112 | data.concat([ |
| 113 | {name : 'action', value : action}, |
| 114 | {name : 'topic', value : topic} |
| 115 | ]), |
| 116 | renderData, |
| 117 | 'json' |
| 118 | ); |
| 119 | sendDialog.dialog("close"); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* send dialog code */ |
| 124 | var sendButtons = {}; |
| 125 | sendButtons[i18n.close] = function() { |
| 126 | $(this).dialog("close"); |
| 127 | } |
Valentin Valchev | d8be730 | 2012-04-06 09:05:42 +0000 | [diff] [blame] | 128 | sendButtons[i18n.reset] = function() { |
Valentin Valchev | 0b61fb9 | 2012-04-06 09:07:58 +0000 | [diff] [blame] | 129 | sendTopic.val(''); |
Valentin Valchev | d8be730 | 2012-04-06 09:05:42 +0000 | [diff] [blame] | 130 | sendProperties.propeditor('reset'); |
| 131 | } |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 132 | sendButtons[i18n.send] = function() { |
| 133 | sendData('send'); |
| 134 | } |
| 135 | sendButtons[i18n.post] = function() { |
| 136 | sendData('post'); |
| 137 | } |
| 138 | var sendDialog = $('#sendDialog').dialog({ |
| 139 | autoOpen: false, |
| 140 | modal : true, |
| 141 | width : '40%', |
| 142 | buttons : sendButtons, |
| 143 | open : function() { |
Valentin Valchev | d8be730 | 2012-04-06 09:05:42 +0000 | [diff] [blame] | 144 | //sendTopic.val(''); |
| 145 | //sendProperties.propeditor('reset'); |
Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 146 | } |
| 147 | }); |
| 148 | var sendTopic = $('#sendTopic'); |
| 149 | var sendProperties = $('#sendProperties').propeditor({ |
| 150 | add: function(el) { |
| 151 | el.find('select').addClass('dynhover'); |
| 152 | initStaticWidgets(el); |
| 153 | } |
| 154 | }); |
| 155 | $('#sendButton').click(function() { |
| 156 | sendDialog.dialog('open'); |
| 157 | }); |
| 158 | |
Valentin Valchev | 699cc02 | 2010-03-15 14:17:58 +0000 | [diff] [blame] | 159 | }); |