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 |
Felix Meschberger | f15dc7a | 2010-02-21 16:45:55 +0000 | [diff] [blame] | 73 | $(document).ajaxError( function(event, req) { |
Felix Meschberger | acb6128 | 2010-03-13 14:23:50 +0000 | [diff] [blame] | 74 | var text = req.responseXML ? x = req.responseXML : |
| 75 | (req.responseText ? x = req.responseText : req.statusText); |
| 76 | Xalert('The request failed: <br/><pre>' + text + '</pre>', 'AJAX Error'); |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 77 | }); |
| 78 | |
| 79 | initStaticWidgets(); |
| 80 | }); |
| 81 | |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 82 | /* 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] | 83 | elem.tablesorter({ |
Felix Meschberger | 775025e | 2010-02-18 15:29:39 +0000 | [diff] [blame] | 84 | textExtraction: mixedLinksExtraction |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 85 | }); |
| 86 | */ |
| 87 | function mixedLinksExtraction(node) { |
| 88 | var l = node.getElementsByTagName('a'); |
| 89 | return l && l.length > 0 ? l[0].innerHTML : node.innerHTML; |
| 90 | }; |
| 91 | |
| 92 | /* Java-like MessageFormat method. Usage: |
| 93 | 'hello {0}'.msgFormat('world') |
| 94 | */ |
| 95 | String.prototype.msgFormat = function(/* variable arguments*/) { |
| 96 | var i=0; var s=this; |
| 97 | while(i<arguments.length) s=s.replace('{'+i+'}',arguments[i++]); |
| 98 | return s; |
| 99 | } |
| 100 | |
| 101 | |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 102 | /* replacement for confirm() method, needs 'action' parameter to work. |
| 103 | * if action is not set - then default confirm() method is used. */ |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 104 | function Xconfirm(/* String */text, /* Callback function */action, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 105 | if (!$.isFunction(action)) return confirm(text); |
| 106 | if (title === undefined) title = ""; |
| 107 | |
| 108 | Xdialog(text).dialog({ |
| 109 | modal: true, |
| 110 | title: title, |
| 111 | buttons: { |
| 112 | "Yes": function() { |
| 113 | $(this).dialog('close'); |
| 114 | action(); |
| 115 | }, |
| 116 | "No": function() { |
| 117 | $(this).dialog('close'); |
| 118 | } |
| 119 | } |
| 120 | }); |
| 121 | return false; |
| 122 | } |
Felix Meschberger | a6fe36c | 2010-02-18 08:12:37 +0000 | [diff] [blame] | 123 | function Xalert(/* String */text, /* String */title) { |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 124 | if (title === undefined) title = ""; |
| 125 | |
| 126 | Xdialog(text).dialog({ |
| 127 | modal: true, |
| 128 | title: title, |
Felix Meschberger | f15dc7a | 2010-02-21 16:45:55 +0000 | [diff] [blame] | 129 | width: '70%', |
Felix Meschberger | e8fdd8a | 2010-02-17 08:04:11 +0000 | [diff] [blame] | 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 | } |