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 = { |
| 25 | css : ["odd", "even ui-state-default"] |
| 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! |
| 63 | var tables = $('table.tablesorter'); |
| 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>'; |
| 77 | XAlert('The request failed: ' + thrownError + pre, 'AJAX Error'); |
| 78 | }); |
| 79 | |
| 80 | initStaticWidgets(); |
| 81 | }); |
| 82 | |
| 83 | /* replacement for confirm() method, needs 'action' parameter to work. |
| 84 | * if action is not set - then default confirm() method is used. */ |
| 85 | function Xconfirm(text, action, title) { |
| 86 | if (!$.isFunction(action)) return confirm(text); |
| 87 | if (title === undefined) title = ""; |
| 88 | |
| 89 | Xdialog(text).dialog({ |
| 90 | modal: true, |
| 91 | title: title, |
| 92 | buttons: { |
| 93 | "Yes": function() { |
| 94 | $(this).dialog('close'); |
| 95 | action(); |
| 96 | }, |
| 97 | "No": function() { |
| 98 | $(this).dialog('close'); |
| 99 | } |
| 100 | } |
| 101 | }); |
| 102 | return false; |
| 103 | } |
| 104 | function Xalert(text, title) { |
| 105 | if (!$.isFunction(action)) return alert(text); |
| 106 | if (title === undefined) title = ""; |
| 107 | |
| 108 | Xdialog(text).dialog({ |
| 109 | modal: true, |
| 110 | title: title, |
| 111 | buttons: { |
| 112 | "Ok": function() { |
| 113 | $(this).dialog('close'); |
| 114 | } |
| 115 | } |
| 116 | }); |
| 117 | return false; |
| 118 | } |
| 119 | /* a helper function used by Xconfirm & Xalert */ |
| 120 | function Xdialog(text) { |
| 121 | var dialog = $('#dialog'); // use existing dialog element |
| 122 | |
| 123 | if ( dialog.size() == 0 ) { // doesn't exists |
| 124 | var element = document.createElement( 'div' ); |
| 125 | $('body').append(element); |
| 126 | dialog = $(element); |
| 127 | } |
| 128 | |
| 129 | // init dialog |
| 130 | dialog.html(text).dialog('destroy'); // set text & reset dialog |
| 131 | return dialog; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | /* String */ function wordWrap( /* String */ msg ) { |
| 136 | var userAgent = navigator.userAgent.toLowerCase(); |
| 137 | var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent ); |
| 138 | |
| 139 | return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* content of the old ui.js */ |
| 144 | |
| 145 | /* Element */ function clearChildren( /* Element */ element ) { |
| 146 | while (element.firstChild) { |
| 147 | element.removeChild(element.firstChild); |
| 148 | } |
| 149 | return element; |
| 150 | } |
| 151 | |
| 152 | /* String */ function serialize( /* Element */ element ) { |
| 153 | var result = ""; |
| 154 | |
| 155 | if (element) { |
| 156 | if (element.nodeValue) { |
| 157 | result = element.nodeValue; |
| 158 | } else { |
| 159 | result += "<" + element.tagName; |
| 160 | |
| 161 | var attrs = element.attributes; |
| 162 | for (var i=0; i < attrs.length; i++) { |
| 163 | if (attrs[i].nodeValue) { |
| 164 | result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'"; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | var children = element.childNodes; |
| 169 | if (children && children.length) { |
| 170 | result += ">"; |
| 171 | |
| 172 | for (var i=0; i < children.length; i++) { |
| 173 | result += serialize( children[i] ); |
| 174 | } |
| 175 | result += "</" + element.tagName + ">"; |
| 176 | } else { |
| 177 | result += "/>"; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return result; |
| 183 | } |
| 184 | |
| 185 | /* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 186 | return createElement( "th", cssClass, attrs, children ); |
| 187 | } |
| 188 | |
| 189 | /* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 190 | return createElement( "tr", cssClass, attrs, children ); |
| 191 | } |
| 192 | |
| 193 | /* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 194 | return createElement( "td", cssClass, attrs, children ); |
| 195 | } |
| 196 | |
| 197 | /* Element */ function text( /* String */ textValue ) { |
| 198 | return document.createTextNode( textValue ); |
| 199 | } |
| 200 | |
| 201 | /* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) { |
| 202 | var element = document.createElement( name ); |
| 203 | |
| 204 | if (cssClass) { |
| 205 | $(element).addClass(cssClass); |
| 206 | } |
| 207 | |
| 208 | if (attrs) { |
| 209 | for (var lab in attrs) { |
| 210 | if ("style" == lab) { |
| 211 | var styles = attrs[lab]; |
| 212 | for (var styleName in styles) { |
| 213 | $(element).css(styleName, styles[styleName]); |
| 214 | } |
| 215 | } else { |
| 216 | $(element).attr( lab, attrs[lab] ); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | if (children && children.length) { |
| 222 | for (var i=0; i < children.length; i++) { |
| 223 | element.appendChild( children[i] ); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return element; |
| 228 | } |
| 229 | |
| 230 | /* Element */ function addText( /* Element */ element, /* String */ textValue ) { |
| 231 | if (element && textValue) { |
| 232 | element.appendChild( text( textValue ) ); |
| 233 | } |
| 234 | return element; |
| 235 | } |