blob: 0cf2ac278c49ff57ad8a871decd518984f3f5383 [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
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
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000083/* A helper function, used together with tablesorter, when the cells contains mixed text and links. As example:
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000084 elem.tablesorter({
Felix Meschberger775025e2010-02-18 15:29:39 +000085 textExtraction: mixedLinksExtraction
Felix Meschbergera6fe36c2010-02-18 08:12:37 +000086 });
87*/
88function 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*/
96String.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 Meschbergere8fdd8a2010-02-17 08:04:11 +0000103/* replacement for confirm() method, needs 'action' parameter to work.
104 * if action is not set - then default confirm() method is used. */
Felix Meschbergera6fe36c2010-02-18 08:12:37 +0000105function Xconfirm(/* String */text, /* Callback function */action, /* String */title) {
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000106 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 Meschbergera6fe36c2010-02-18 08:12:37 +0000124function Xalert(/* String */text, /* String */title) {
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000125 if (!$.isFunction(action)) return alert(text);
126 if (title === undefined) title = "";
127
128 Xdialog(text).dialog({
129 modal: true,
130 title: title,
131 buttons: {
132 "Ok": function() {
133 $(this).dialog('close');
134 }
135 }
136 });
137 return false;
138}
139/* a helper function used by Xconfirm & Xalert */
140function Xdialog(text) {
141 var dialog = $('#dialog'); // use existing dialog element
142
143 if ( dialog.size() == 0 ) { // doesn't exists
144 var element = document.createElement( 'div' );
145 $('body').append(element);
146 dialog = $(element);
147 }
148
149 // init dialog
150 dialog.html(text).dialog('destroy'); // set text & reset dialog
151 return dialog;
152}
153
154
155/* String */ function wordWrap( /* String */ msg ) {
156 var userAgent = navigator.userAgent.toLowerCase();
157 var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
158
159 return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg;
160}
161
162
163/* content of the old ui.js */
164
165/* Element */ function clearChildren( /* Element */ element ) {
166 while (element.firstChild) {
167 element.removeChild(element.firstChild);
168 }
169 return element;
170}
171
172/* String */ function serialize( /* Element */ element ) {
173 var result = "";
174
175 if (element) {
176 if (element.nodeValue) {
177 result = element.nodeValue;
178 } else {
179 result += "<" + element.tagName;
180
181 var attrs = element.attributes;
182 for (var i=0; i < attrs.length; i++) {
183 if (attrs[i].nodeValue) {
184 result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'";
185 }
186 }
187
188 var children = element.childNodes;
189 if (children && children.length) {
190 result += ">";
191
192 for (var i=0; i < children.length; i++) {
193 result += serialize( children[i] );
194 }
195 result += "</" + element.tagName + ">";
196 } else {
197 result += "/>";
198 }
199 }
200 }
201
202 return result;
203}
204
205/* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
206 return createElement( "th", cssClass, attrs, children );
207}
208
209/* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
210 return createElement( "tr", cssClass, attrs, children );
211}
212
213/* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
214 return createElement( "td", cssClass, attrs, children );
215}
216
217/* Element */ function text( /* String */ textValue ) {
218 return document.createTextNode( textValue );
219}
220
221/* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
222 var element = document.createElement( name );
223
224 if (cssClass) {
225 $(element).addClass(cssClass);
226 }
227
228 if (attrs) {
229 for (var lab in attrs) {
230 if ("style" == lab) {
231 var styles = attrs[lab];
232 for (var styleName in styles) {
233 $(element).css(styleName, styles[styleName]);
234 }
235 } else {
236 $(element).attr( lab, attrs[lab] );
237 }
238 }
239 }
240
241 if (children && children.length) {
242 for (var i=0; i < children.length; i++) {
243 element.appendChild( children[i] );
244 }
245 }
246
247 return element;
248}
249
250/* Element */ function addText( /* Element */ element, /* String */ textValue ) {
251 if (element && textValue) {
252 element.appendChild( text( textValue ) );
253 }
254 return element;
255}