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 |
| 73 | $(document).ajaxError( function(event, XMLHttpRequest, ajaxOptions, thrownError) { |
| 74 | var pre = '<br/><pre>'; |
| 75 | for (i in event) pre += i + '=' + event[i] + '\n' |
| 76 | pre += '</pre>'; |
Felix Meschberger | 8adee6d | 2010-02-20 17:40:03 +0000 | [diff] [blame^] | 77 | Xalert('The request failed: ' + thrownError + pre, 'AJAX Error'); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 78 | }); |
| 79 | |
| 80 | initStaticWidgets(); |
| 81 | }); |
| 82 | |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 83 | /* 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] | 84 | elem.tablesorter({ |
Felix Meschberger | 775025e | 2010-02-18 15:29:39 +0000 | [diff] [blame] | 85 | textExtraction: mixedLinksExtraction |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 86 | }); |
| 87 | */ |
| 88 | function mixedLinksExtraction(node) { |
| 89 | var l = node.getElementsByTagName('a'); |
| 90 | return l && l.length > 0 ? l[0].innerHTML : node.innerHTML; |
| 91 | }; |
| 92 | |
| 93 | /* Java-like MessageFormat method. Usage: |
| 94 | 'hello {0}'.msgFormat('world') |
| 95 | */ |
| 96 | String.prototype.msgFormat = function(/* variable arguments*/) { |
| 97 | var i=0; var s=this; |
| 98 | while(i<arguments.length) s=s.replace('{'+i+'}',arguments[i++]); |
| 99 | return s; |
| 100 | } |
| 101 | |
| 102 | |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 103 | /* replacement for confirm() method, needs 'action' parameter to work. |
| 104 | * if action is not set - then default confirm() method is used. */ |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 105 | function Xconfirm(/* String */text, /* Callback function */action, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 106 | if (!$.isFunction(action)) return confirm(text); |
| 107 | if (title === undefined) title = ""; |
| 108 | |
| 109 | Xdialog(text).dialog({ |
| 110 | modal: true, |
| 111 | title: title, |
| 112 | buttons: { |
| 113 | "Yes": function() { |
| 114 | $(this).dialog('close'); |
| 115 | action(); |
| 116 | }, |
| 117 | "No": function() { |
| 118 | $(this).dialog('close'); |
| 119 | } |
| 120 | } |
| 121 | }); |
| 122 | return false; |
| 123 | } |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 124 | function Xalert(/* String */text, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 125 | if (title === undefined) title = ""; |
| 126 | |
| 127 | Xdialog(text).dialog({ |
| 128 | modal: true, |
| 129 | title: title, |
| 130 | buttons: { |
| 131 | "Ok": function() { |
| 132 | $(this).dialog('close'); |
| 133 | } |
| 134 | } |
| 135 | }); |
| 136 | return false; |
| 137 | } |
| 138 | /* a helper function used by Xconfirm & Xalert */ |
| 139 | function Xdialog(text) { |
| 140 | var dialog = $('#dialog'); // use existing dialog element |
| 141 | |
| 142 | if ( dialog.size() == 0 ) { // doesn't exists |
| 143 | var element = document.createElement( 'div' ); |
| 144 | $('body').append(element); |
| 145 | dialog = $(element); |
| 146 | } |
| 147 | |
| 148 | // init dialog |
| 149 | dialog.html(text).dialog('destroy'); // set text & reset dialog |
| 150 | return dialog; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /* String */ function wordWrap( /* String */ msg ) { |
| 155 | var userAgent = navigator.userAgent.toLowerCase(); |
| 156 | var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ); |
| 157 | |
| 158 | return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | /* content of the old ui.js */ |
| 163 | |
| 164 | /* Element */ function clearChildren( /* Element */ element ) { |
| 165 | while (element.firstChild) { |
| 166 | element.removeChild(element.firstChild); |
| 167 | } |
| 168 | return element; |
| 169 | } |
| 170 | |
| 171 | /* String */ function serialize( /* Element */ element ) { |
| 172 | var result = ""; |
| 173 | |
| 174 | if (element) { |
| 175 | if (element.nodeValue) { |
| 176 | result = element.nodeValue; |
| 177 | } else { |
| 178 | result += "<" + element.tagName; |
| 179 | |
| 180 | var attrs = element.attributes; |
| 181 | for (var i=0; i < attrs.length; i++) { |
| 182 | if (attrs[i].nodeValue) { |
| 183 | result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'"; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | var children = element.childNodes; |
| 188 | if (children && children.length) { |
| 189 | result += ">"; |
| 190 | |
| 191 | for (var i=0; i < children.length; i++) { |
| 192 | result += serialize( children[i] ); |
| 193 | } |
| 194 | result += "</" + element.tagName + ">"; |
| 195 | } else { |
| 196 | result += "/>"; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | /* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 205 | return createElement( "th", cssClass, attrs, children ); |
| 206 | } |
| 207 | |
| 208 | /* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 209 | return createElement( "tr", cssClass, attrs, children ); |
| 210 | } |
| 211 | |
| 212 | /* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 213 | return createElement( "td", cssClass, attrs, children ); |
| 214 | } |
| 215 | |
| 216 | /* Element */ function text( /* String */ textValue ) { |
| 217 | return document.createTextNode( textValue ); |
| 218 | } |
| 219 | |
| 220 | /* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 221 | var element = document.createElement( name ); |
| 222 | |
| 223 | if (cssClass) { |
| 224 | $(element).addClass(cssClass); |
| 225 | } |
| 226 | |
| 227 | if (attrs) { |
| 228 | for (var lab in attrs) { |
| 229 | if ("style" == lab) { |
| 230 | var styles = attrs[lab]; |
| 231 | for (var styleName in styles) { |
| 232 | $(element).css(styleName, styles[styleName]); |
| 233 | } |
| 234 | } else { |
| 235 | $(element).attr( lab, attrs[lab] ); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | if (children && children.length) { |
| 241 | for (var i=0; i < children.length; i++) { |
| 242 | element.appendChild( children[i] ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return element; |
| 247 | } |
| 248 | |
| 249 | /* Element */ function addText( /* Element */ element, /* String */ textValue ) { |
| 250 | if (element && textValue) { |
| 251 | element.appendChild( text( textValue ) ); |
| 252 | } |
| 253 | return element; |
| 254 | } |