blob: 3687e4072655834ac0ca5e276dea023ed372a274 [file] [log] [blame]
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +00001/*
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 */
19if ( $.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 Meschberger2aa46b52010-02-19 20:01:28 +000025 css : ["odd ui-state-default", "even ui-state-default"]
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +000026 };
27}
28
29/* initializes static widgets */
30function 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 Meschbergera6fe36c2010-02-18 08:12:37 +000063 var tables = $('table.tablesorter:not(.noauto)');
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +000064 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 Meschbergerf15dc7a2010-02-21 16:45:55 +000073 $(document).ajaxError( function(event, req) {
Felix Meschbergeracb61282010-03-13 14:23:50 +000074 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 Meschbergere8fdd8a2010-02-17 08:04:11 +000077 });
78
79 initStaticWidgets();
80});
81
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000082/* A helper function, used together with tablesorter, when the cells contains mixed text and links. As example:
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000083 elem.tablesorter({
Felix Meschberger775025e2010-02-18 15:29:39 +000084 textExtraction: mixedLinksExtraction
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000085 });
86*/
87function 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*/
95String.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 Meschbergere8fdd8a2010-02-17 08:04:11 +0000102/* replacement for confirm() method, needs 'action' parameter to work.
103 * if action is not set - then default confirm() method is used. */
Felix Meschbergera6fe36c2010-02-18 08:12:37 +0000104function Xconfirm(/* String */text, /* Callback function */action, /* String */title) {
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000105 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 Meschbergera6fe36c2010-02-18 08:12:37 +0000123function Xalert(/* String */text, /* String */title) {
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000124 if (title === undefined) title = "";
125
126 Xdialog(text).dialog({
127 modal: true,
128 title: title,
Felix Meschbergerf15dc7a2010-02-21 16:45:55 +0000129 width: '70%',
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000130 buttons: {
131 "Ok": function() {
132 $(this).dialog('close');
133 }
134 }
135 });
136 return false;
137}
138/* a helper function used by Xconfirm & Xalert */
139function 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}