Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +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 | |
| 18 | /* init table sorter defaults */ |
| 19 | if ( $.tablesorter ) { |
| 20 | $.tablesorter.defaults.cssAsc = 'headerSortUp ui-state-focus'; |
| 21 | $.tablesorter.defaults.cssDesc = 'headerSortDown ui-state-focus'; |
| 22 | $.tablesorter.defaults.header = 'header ui-widget-header'; |
| 23 | $.tablesorter.defaults.widgets = ['zebra']; |
| 24 | $.tablesorter.defaults.widgetZebra = { |
Felix Meschberger | 2aa46b5 | 2010-02-19 20:01:28 +0000 | [diff] [blame] | 25 | css : ["odd ui-state-default", "even ui-state-default"] |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 26 | }; |
| 27 | } |
| 28 | |
| 29 | /* initializes static widgets */ |
| 30 | function initStaticWidgets(elem) { |
| 31 | // hover states on the static widgets - form elements |
| 32 | var el = elem ? $(elem) : $(document); |
| 33 | el.find('button, input[type!=checkbox], .dynhover').hover( |
| 34 | function() { $(this).addClass('ui-state-hover'); }, |
| 35 | function() { $(this).removeClass('ui-state-hover'); } |
| 36 | ).addClass('ui-state-default ui-corner-all'); |
| 37 | // fix attribute selector in IE |
| 38 | el.find('input[type=text], input[type=password], input[type=file]').addClass('inputText'); |
| 39 | |
| 40 | // make buttones nicer by applying equal width - not working in IE ;( |
| 41 | el.find('button, input[type=submit], input[type=reset], input[type=button]').each(function(i) { |
| 42 | var txt = $(this).text(); |
| 43 | var apply = txt && txt.length > 1; |
| 44 | if (apply) $(this).css('min-width', '8em'); |
| 45 | }); |
| 46 | |
| 47 | // add default table styling - colors and fonts from the theme |
| 48 | el.find('table.nicetable').addClass('ui-widget'); |
| 49 | el.find('table.nicetable th').addClass('ui-widget-header'); |
| 50 | |
| 51 | // add default styling for table sorter |
| 52 | el.find("table.tablesorter tbody").addClass("ui-widget-content"); |
| 53 | |
| 54 | // add theme styling to the status line |
| 55 | el.find('.statline').addClass('ui-state-highlight') |
| 56 | |
| 57 | el.find('table.tablesorter').trigger("update").trigger("applyWidgets"); |
| 58 | } |
| 59 | |
| 60 | /* automatically executed on load */ |
| 61 | $(document).ready(function() { |
| 62 | // init table-sorter tables - only once! |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 63 | var tables = $('table.tablesorter:not(.noauto)'); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 64 | if (tables.size() > 0) tables.tablesorter(); |
| 65 | |
| 66 | // init navigation |
| 67 | $('#technav div.ui-state-default').hover( |
| 68 | function() { $(this).addClass('ui-state-hover'); }, |
| 69 | function() { $(this).removeClass('ui-state-hover'); } |
| 70 | ); |
| 71 | |
| 72 | // register global ajax error handler |
Valentin Pavlov Valchev | 7d47c11 | 2012-04-04 14:16:43 +0000 | [diff] [blame] | 73 | $(document).ajaxError( function(xevent, req) { |
| 74 | var _t = ''; |
| 75 | if (req.responseText) { |
| 76 | _t = req.responseText; |
| 77 | } else if (req.statusText) { |
| 78 | _t = req.statusText; |
| 79 | } else if (req.responseXML) { |
| 80 | _t = req.responseXML; |
| 81 | } |
| 82 | Xalert('The request failed: <br/><pre>' + _t + '</pre>', 'AJAX Error'); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 83 | }); |
| 84 | |
| 85 | initStaticWidgets(); |
| 86 | }); |
| 87 | |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 88 | /* A helper function, used together with tablesorter, when the cells contains mixed text and links. As example: |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 89 | elem.tablesorter({ |
Felix Meschberger | 775025e | 2010-02-18 15:29:39 +0000 | [diff] [blame] | 90 | textExtraction: mixedLinksExtraction |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 91 | }); |
| 92 | */ |
| 93 | function mixedLinksExtraction(node) { |
| 94 | var l = node.getElementsByTagName('a'); |
| 95 | return l && l.length > 0 ? l[0].innerHTML : node.innerHTML; |
| 96 | }; |
| 97 | |
| 98 | /* Java-like MessageFormat method. Usage: |
| 99 | 'hello {0}'.msgFormat('world') |
| 100 | */ |
| 101 | String.prototype.msgFormat = function(/* variable arguments*/) { |
| 102 | var i=0; var s=this; |
| 103 | while(i<arguments.length) s=s.replace('{'+i+'}',arguments[i++]); |
| 104 | return s; |
| 105 | } |
| 106 | |
| 107 | |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 108 | /* replacement for confirm() method, needs 'action' parameter to work. |
| 109 | * if action is not set - then default confirm() method is used. */ |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 110 | function Xconfirm(/* String */text, /* Callback function */action, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 111 | if (!$.isFunction(action)) return confirm(text); |
| 112 | if (title === undefined) title = ""; |
| 113 | |
| 114 | Xdialog(text).dialog({ |
| 115 | modal: true, |
| 116 | title: title, |
| 117 | buttons: { |
| 118 | "Yes": function() { |
| 119 | $(this).dialog('close'); |
| 120 | action(); |
| 121 | }, |
| 122 | "No": function() { |
| 123 | $(this).dialog('close'); |
| 124 | } |
| 125 | } |
| 126 | }); |
| 127 | return false; |
| 128 | } |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 129 | function Xalert(/* String */text, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 130 | if (title === undefined) title = ""; |
| 131 | |
| 132 | Xdialog(text).dialog({ |
| 133 | modal: true, |
| 134 | title: title, |
Felix Meschberger | f15dc7a | 2010-02-21 16:45:55 +0000 | [diff] [blame] | 135 | width: '70%', |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 136 | buttons: { |
| 137 | "Ok": function() { |
| 138 | $(this).dialog('close'); |
| 139 | } |
| 140 | } |
| 141 | }); |
| 142 | return false; |
| 143 | } |
| 144 | /* a helper function used by Xconfirm & Xalert */ |
| 145 | function Xdialog(text) { |
| 146 | var dialog = $('#dialog'); // use existing dialog element |
| 147 | |
| 148 | if ( dialog.size() == 0 ) { // doesn't exists |
| 149 | var element = document.createElement( 'div' ); |
| 150 | $('body').append(element); |
| 151 | dialog = $(element); |
| 152 | } |
| 153 | |
| 154 | // init dialog |
| 155 | dialog.html(text).dialog('destroy'); // set text & reset dialog |
| 156 | return dialog; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* String */ function wordWrap( /* String */ msg ) { |
Valentin Pavlov Valchev | 7e883df | 2012-03-28 06:43:03 +0000 | [diff] [blame] | 161 | if (msg) { |
| 162 | var userAgent = navigator.userAgent.toLowerCase(); |
| 163 | var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ); |
| 164 | |
| 165 | return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg; |
| 166 | } else { |
| 167 | return ''; |
| 168 | } |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | /* content of the old ui.js */ |
| 173 | |
| 174 | /* Element */ function clearChildren( /* Element */ element ) { |
| 175 | while (element.firstChild) { |
| 176 | element.removeChild(element.firstChild); |
| 177 | } |
| 178 | return element; |
| 179 | } |
| 180 | |
| 181 | /* String */ function serialize( /* Element */ element ) { |
| 182 | var result = ""; |
| 183 | |
| 184 | if (element) { |
| 185 | if (element.nodeValue) { |
| 186 | result = element.nodeValue; |
| 187 | } else { |
| 188 | result += "<" + element.tagName; |
| 189 | |
| 190 | var attrs = element.attributes; |
| 191 | for (var i=0; i < attrs.length; i++) { |
| 192 | if (attrs[i].nodeValue) { |
| 193 | result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'"; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | var children = element.childNodes; |
| 198 | if (children && children.length) { |
| 199 | result += ">"; |
| 200 | |
| 201 | for (var i=0; i < children.length; i++) { |
| 202 | result += serialize( children[i] ); |
| 203 | } |
| 204 | result += "</" + element.tagName + ">"; |
| 205 | } else { |
| 206 | result += "/>"; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | /* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 215 | return createElement( "th", cssClass, attrs, children ); |
| 216 | } |
| 217 | |
| 218 | /* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 219 | return createElement( "tr", cssClass, attrs, children ); |
| 220 | } |
| 221 | |
| 222 | /* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 223 | return createElement( "td", cssClass, attrs, children ); |
| 224 | } |
| 225 | |
| 226 | /* Element */ function text( /* String */ textValue ) { |
| 227 | return document.createTextNode( textValue ); |
| 228 | } |
| 229 | |
| 230 | /* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 231 | var sb = ["<", name]; |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 232 | |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 233 | if (attrs && attrs.name) { |
| 234 | sb.push(" name='", attrs.name, "'"); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 237 | if (attrs && attrs.type) { |
| 238 | sb.push(" type='", attrs.type, "'"); |
| 239 | } |
| 240 | |
| 241 | sb.push(">"); |
| 242 | |
| 243 | var el = $(sb.join("")); |
| 244 | |
| 245 | if (cssClass) { |
| 246 | el.addClass(cssClass); |
| 247 | } |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 248 | if (attrs) { |
| 249 | for (var lab in attrs) { |
| 250 | if ("style" == lab) { |
| 251 | var styles = attrs[lab]; |
| 252 | for (var styleName in styles) { |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 253 | el.css(styleName, styles[styleName]); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 254 | } |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 255 | } else if ("name" == lab || "type" == lab) { |
| 256 | //skip |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 257 | } else { |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 258 | el.attr( lab, attrs[lab] ); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Felix Meschberger | 5f559c7 | 2011-05-30 11:18:14 +0000 | [diff] [blame] | 263 | var element = el.get()[0]; |
| 264 | |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 265 | if (children && children.length) { |
| 266 | for (var i=0; i < children.length; i++) { |
Felix Meschberger | 7f1492b | 2012-04-19 09:00:15 +0000 | [diff] [blame^] | 267 | if (children[i]) { |
| 268 | element.appendChild( children[i] ); |
| 269 | } |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | return element; |
| 274 | } |
| 275 | |
| 276 | /* Element */ function addText( /* Element */ element, /* String */ textValue ) { |
| 277 | if (element && textValue) { |
| 278 | element.appendChild( text( textValue ) ); |
| 279 | } |
| 280 | return element; |
| 281 | } |
Valentin Pavlov Valchev | b8a12ff | 2010-08-11 07:25:43 +0000 | [diff] [blame] | 282 | |
Felix Meschberger | a2b7738 | 2011-12-22 12:53:44 +0000 | [diff] [blame] | 283 | /** |
| 284 | * Sets the name cookie at the appRoot (/system/console by default) path |
| 285 | * to last for 20 years. |
| 286 | * @param name The name of the cookie |
| 287 | * @param value The value for the cookie |
| 288 | */ |
| 289 | function setCookie( /* String */name, /* String */value) { |
Valentin Pavlov Valchev | 39e1be3 | 2012-01-26 11:03:41 +0000 | [diff] [blame] | 290 | var date = new Date(); |
| 291 | date.setFullYear(date.getFullYear() + 20); |
| 292 | $.cookies.del("felix-webconsole-" + name); |
| 293 | $.cookies.set("felix-webconsole-" + name, value, { |
| 294 | expiresAt : date, |
| 295 | path : appRoot |
| 296 | }); |
Felix Meschberger | a2b7738 | 2011-12-22 12:53:44 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Returns the value of the name cookie or nothing if the cookie does |
| 301 | * not exist or is not accessible. |
| 302 | * @param name The name of the cookie |
| 303 | */ |
| 304 | /* String */ function getCookie(/*String */name) { |
Valentin Pavlov Valchev | 39e1be3 | 2012-01-26 11:03:41 +0000 | [diff] [blame] | 305 | return $.cookies.get("felix-webconsole-" + name); |
Felix Meschberger | a2b7738 | 2011-12-22 12:53:44 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Valentin Pavlov Valchev | b8a12ff | 2010-08-11 07:25:43 +0000 | [diff] [blame] | 308 | // language selection element |
| 309 | var langSelect = false; |
| 310 | $(document).ready(function() { |
| 311 | langSelect = $('#langSelect').hover( |
| 312 | function() { $(this).find('.flags').show('blind') }, |
| 313 | function() { $(this).find('.flags').hide('blind') }); |
| 314 | langSelect.find('.flags img').click(function() { |
Valentin Pavlov Valchev | 39e1be3 | 2012-01-26 11:03:41 +0000 | [diff] [blame] | 315 | setCookie("locale", $(this).attr('alt')); |
Valentin Pavlov Valchev | b8a12ff | 2010-08-11 07:25:43 +0000 | [diff] [blame] | 316 | location.reload(); |
| 317 | }); |
Felix Meschberger | a2b7738 | 2011-12-22 12:53:44 +0000 | [diff] [blame] | 318 | var locale = getCookie("locale"); |
Valentin Pavlov Valchev | 731a161 | 2010-09-03 06:00:16 +0000 | [diff] [blame] | 319 | if (locale) { |
| 320 | if ( !$.datepicker.regional[locale] ) locale = ''; |
| 321 | $.datepicker.setDefaults($.datepicker.regional[locale]); |
| 322 | } |
Valentin Pavlov Valchev | b8a12ff | 2010-08-11 07:25:43 +0000 | [diff] [blame] | 323 | }); |