Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +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 | */ |
| 17 | function renderStatusLine() { |
| 18 | $("#plugin_content").append( "<div class='fullwidth'><div class='statusline'/></div>" ); |
| 19 | } |
| 20 | |
| 21 | function renderView( /* Array of String */ columns, /* Array of String */ buttons ) { |
| 22 | renderStatusLine(); |
| 23 | renderButtons(buttons); |
| 24 | var txt = "<div class='table'><table id='plugin_table' class='tablelayout'><thead><tr>"; |
| 25 | for ( var name in columns ) { |
| 26 | txt = txt + "<th class='col_" + columns[name] + "'>" + columns[name] + "</th>"; |
| 27 | } |
| 28 | txt = txt + "</tr></thead><tbody></tbody></table></div>"; |
| 29 | $("#plugin_content").append( txt ); |
| 30 | renderButtons(buttons); |
| 31 | renderStatusLine(); |
| 32 | } |
| 33 | |
| 34 | function renderButtons( buttons ) { |
| 35 | $("#plugin_content").append( "<form method='post' enctype='multipart/form-data'><div class='fullwidth'><div class='buttons'>" + |
| 36 | buttons + "</div></div></form>" ); |
| 37 | } |
| 38 | |
| 39 | function renderData( eventData ) { |
| 40 | $(".statusline").empty().append(eventData.status); |
| 41 | $("#plugin_table > tbody > tr").remove(); |
| 42 | for ( var idx in eventData.data ) { |
| 43 | entry( eventData.data[idx] ); |
| 44 | } |
| 45 | $("#plugin_table").trigger("update"); |
| 46 | if ( drawDetails ) { |
| 47 | renderDetails(eventData); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function entry( /* Object */ dataEntry ) { |
| 52 | var trElement = tr( null, { id: "entry" + dataEntry.id } ); |
| 53 | entryInternal( trElement, dataEntry ); |
| 54 | $("#plugin_table > tbody").append(trElement); |
| 55 | } |
| 56 | |
| 57 | function actionButton( /* Element */ parent, /* string */ id, /* Obj */ action, /* string */ pid ) { |
| 58 | var enabled = action.enabled; |
| 59 | var op = action.link; |
| 60 | var opLabel = action.name; |
| 61 | var img = action.image; |
| 62 | |
| 63 | var arg = id; |
Carsten Ziegeler | 83ff044 | 2009-02-10 13:08:50 +0000 | [diff] [blame] | 64 | if ( op == "configure" ) { |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 65 | arg = pid |
| 66 | } |
| 67 | var input = createElement( "input", null, { |
| 68 | type: 'image', |
| 69 | title: opLabel, |
| 70 | alt: opLabel, |
| 71 | src: imgRoot + '/component_' + img + '.png', |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 72 | style: {"margin-left": "10px"} |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 73 | }); |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 74 | $(input).click(function() {changeDataEntryState(arg, op)}); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 75 | |
| 76 | if (!enabled) { |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 77 | $(input).attr("disabled", true); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 78 | } |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 79 | parent.appendChild( input ); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | function entryInternal( /* Element */ parent, /* Object */ dataEntry ) { |
| 83 | var id = dataEntry.id; |
| 84 | var name = dataEntry.name; |
| 85 | var state = dataEntry.state; |
| 86 | |
| 87 | var inputElement = createElement("img", "rightButton", { |
| 88 | src: appRoot + "/res/imgs/arrow_right.png", |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 89 | style: {"border": "none"}, |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 90 | id: 'img' + id, |
| 91 | title: "Details", |
| 92 | alt: "Details", |
| 93 | width: 14, |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 94 | height: 14 |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 95 | }); |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 96 | $(inputElement).click(function() {showDetails(id)}); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 97 | var titleElement; |
| 98 | if ( drawDetails ) { |
| 99 | titleElement = text(name); |
| 100 | } else { |
| 101 | titleElement = createElement ("a", null, { |
| 102 | href: window.location.pathname + "/" + id |
| 103 | }); |
| 104 | titleElement.appendChild(text(name)); |
| 105 | } |
| 106 | |
| 107 | parent.appendChild( td( null, null, [ text( id ) ] ) ); |
| 108 | parent.appendChild( td( null, null, [ inputElement, text(" "), titleElement ] ) ); |
| 109 | parent.appendChild( td( null, null, [ text( state ) ] ) ); |
| 110 | var actionsTd = td( null, null ); |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 111 | var div = createElement("div", null, { |
| 112 | style: { "text-align" : "left"} |
| 113 | }); |
| 114 | actionsTd.appendChild(div); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 115 | |
| 116 | for ( var a in dataEntry.actions ) { |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 117 | actionButton( div, id, dataEntry.actions[a], dataEntry.pid ); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 118 | } |
| 119 | parent.appendChild( actionsTd ); |
| 120 | } |
| 121 | |
| 122 | function changeDataEntryState(/* long */ id, /* String */ action) { |
Carsten Ziegeler | 83ff044 | 2009-02-10 13:08:50 +0000 | [diff] [blame] | 123 | if ( action == "configure") { |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 124 | window.location = appRoot + "/configMgr/" + id; |
| 125 | return; |
| 126 | } |
| 127 | $.post(pluginRoot + "/" + id, {"action":action}, function(data) { |
| 128 | renderData(data); |
| 129 | }, "json"); |
| 130 | } |
| 131 | |
| 132 | function showDetails( id ) { |
| 133 | $.get(pluginRoot + "/" + id + ".json", null, function(data) { |
| 134 | renderDetails(data); |
| 135 | }, "json"); |
| 136 | } |
| 137 | |
| 138 | function loadData() { |
| 139 | $.get(pluginRoot + "/.json", null, function(data) { |
| 140 | renderData(data); |
| 141 | }, "json"); |
| 142 | } |
| 143 | |
| 144 | function hideDetails( id ) { |
| 145 | $("#img" + id).each(function() { |
| 146 | $("#pluginInlineDetails").remove(); |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 147 | $(this).attr("src", appRoot + "/res/imgs/arrow_right.png"); |
| 148 | $(this).attr("title", "Details"); |
| 149 | $(this).attr("alt", "Details"); |
| 150 | $(this).unbind('click').click(function() {showDetails(id)}); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 151 | }); |
| 152 | } |
| 153 | |
| 154 | function renderDetails( data ) { |
| 155 | data = data.data[0]; |
Carsten Ziegeler | d721c96 | 2009-02-16 16:42:07 +0000 | [diff] [blame] | 156 | $("#pluginInlineDetails").remove(); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 157 | $("#entry" + data.id + " > td").eq(1).append("<div id='pluginInlineDetails'/>"); |
| 158 | $("#img" + data.id).each(function() { |
| 159 | if ( drawDetails ) { |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 160 | $(this).attr("src", appRoot + "/res/imgs/arrow_left.png"); |
| 161 | $(this).attr("title", "Back"); |
| 162 | $(this).attr("alt", "Back"); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 163 | var ref = window.location.pathname; |
| 164 | ref = ref.substring(0, ref.lastIndexOf('/')); |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 165 | $(this).unbind('click').click(function() {window.location = ref;}); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 166 | } else { |
Carsten Ziegeler | 0b83c01 | 2009-03-05 16:14:55 +0000 | [diff] [blame] | 167 | $(this).attr("src", appRoot + "/res/imgs/arrow_down.png"); |
| 168 | $(this).attr("title", "Hide Details"); |
| 169 | $(this).attr("alt", "Hide Details"); |
| 170 | $(this).unbind('click').click(function() {hideDetails(data.id)}); |
Carsten Ziegeler | 83fbe44 | 2009-02-09 20:48:31 +0000 | [diff] [blame] | 171 | } |
| 172 | }); |
| 173 | $("#pluginInlineDetails").append("<table border='0'><tbody></tbody></table>"); |
| 174 | var details = data.props; |
| 175 | for (var idx in details) { |
| 176 | var prop = details[idx]; |
| 177 | |
| 178 | var txt = "<tr><td class='aligntop' noWrap='true' style='border:0px none'>" + prop.key + "</td><td class='aligntop' style='border:0px none'>"; |
| 179 | if (prop.value) { |
| 180 | if ( $.isArray(prop.value) ) { |
| 181 | var i = 0; |
| 182 | for(var pi in prop.value) { |
| 183 | var value = prop.value[pi]; |
| 184 | if (i > 0) { txt = txt + "<br/>"; } |
| 185 | var span; |
| 186 | if (value.substring(0, 2) == "!!") { |
| 187 | txt = txt + "<span style='color: red;'>" + value + "</span>"; |
| 188 | } else { |
| 189 | txt = txt + value; |
| 190 | } |
| 191 | i++; |
| 192 | } |
| 193 | } else { |
| 194 | txt = txt + prop.value; |
| 195 | } |
| 196 | } else { |
| 197 | txt = txt + "\u00a0"; |
| 198 | } |
| 199 | txt = txt + "</td></tr>"; |
| 200 | $("#pluginInlineDetails > table > tbody").append(txt); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | function renderComponents(data) { |
| 205 | $(document).ready(function(){ |
| 206 | renderView( ["Id", "Name", "Status", "Actions"], |
| 207 | "<div class='button'><button class='reloadButton' type='button' name='reload'>Reload</button></div>"); |
| 208 | renderData(data); |
| 209 | |
| 210 | $(".reloadButton").click(loadData); |
| 211 | |
| 212 | var extractMethod = function(node) { |
| 213 | var link = node.getElementsByTagName("a"); |
| 214 | if ( link && link.length == 1 ) { |
| 215 | return link[0].innerHTML; |
| 216 | } |
| 217 | return node.innerHTML; |
| 218 | }; |
| 219 | $("#plugin_table").tablesorter({ |
| 220 | headers: { |
| 221 | 0: { sorter:"digit"}, |
| 222 | 3: { sorter: false } |
| 223 | }, |
| 224 | sortList: [[1,0]], |
| 225 | textExtraction:extractMethod |
| 226 | }); |
| 227 | }); |
| 228 | } |
| 229 | |