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