blob: 55539c433d445728827711f0cee381343a06e9b1 [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 ) {
Felix Meschberger5f559c72011-05-30 11:18:14 +0000221 var sb = ["<", name];
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000222
Felix Meschberger5f559c72011-05-30 11:18:14 +0000223 if (attrs && attrs.name) {
224 sb.push(" name='", attrs.name, "'");
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000225 }
226
Felix Meschberger5f559c72011-05-30 11:18:14 +0000227 if (attrs && attrs.type) {
228 sb.push(" type='", attrs.type, "'");
229 }
230
231 sb.push(">");
232
233 var el = $(sb.join(""));
234
235 if (cssClass) {
236 el.addClass(cssClass);
237 }
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000238 if (attrs) {
239 for (var lab in attrs) {
240 if ("style" == lab) {
241 var styles = attrs[lab];
242 for (var styleName in styles) {
Felix Meschberger5f559c72011-05-30 11:18:14 +0000243 el.css(styleName, styles[styleName]);
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000244 }
Felix Meschberger5f559c72011-05-30 11:18:14 +0000245 } else if ("name" == lab || "type" == lab) {
246 //skip
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000247 } else {
Felix Meschberger5f559c72011-05-30 11:18:14 +0000248 el.attr( lab, attrs[lab] );
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000249 }
250 }
251 }
252
Felix Meschberger5f559c72011-05-30 11:18:14 +0000253 var element = el.get()[0];
254
Felix Meschbergere8fdd8a2010-02-17 08:04:11 +0000255 if (children && children.length) {
256 for (var i=0; i < children.length; i++) {
257 element.appendChild( children[i] );
258 }
259 }
260
261 return element;
262}
263
264/* Element */ function addText( /* Element */ element, /* String */ textValue ) {
265 if (element && textValue) {
266 element.appendChild( text( textValue ) );
267 }
268 return element;
269}
Valentin Pavlov Valchevb8a12ff2010-08-11 07:25:43 +0000270
271// language selection element
272var langSelect = false;
273$(document).ready(function() {
274 langSelect = $('#langSelect').hover(
275 function() { $(this).find('.flags').show('blind') },
276 function() { $(this).find('.flags').hide('blind') });
277 langSelect.find('.flags img').click(function() {
278 $.cookies.set('felix.webconsole.locale', $(this).attr('alt'));
279 location.reload();
280 });
Valentin Pavlov Valchev731a1612010-09-03 06:00:16 +0000281 var locale = $.cookies.get('felix.webconsole.locale');
282 if (locale) {
283 if ( !$.datepicker.regional[locale] ) locale = '';
284 $.datepicker.setDefaults($.datepicker.regional[locale]);
285 }
Valentin Pavlov Valchevb8a12ff2010-08-11 07:25:43 +0000286});