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