blob: 69585c777f798524d3b9a41d10c45ddcb584434c [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 = {
Felix Meschberger8a051972010-02-19 20:01:28 +000025 css : ["odd ui-state-default", "even ui-state-default"]
Felix Meschberger45c9caa2010-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 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
Valentin Valchev92950782012-04-04 14:16:43 +000073 $(document).ajaxError( function(xevent, req) {
74 var _t = '';
75 if (req.responseText) {
76 _t = req.responseText;
77 } else if (req.statusText) {
78 _t = req.statusText;
79 } else if (req.responseXML) {
80 _t = req.responseXML;
81 }
82 Xalert('The request failed: <br/><pre>' + _t + '</pre>', 'AJAX Error');
Felix Meschberger45c9caa2010-02-17 08:04:11 +000083 });
84
85 initStaticWidgets();
86});
87
Felix Meschberger2fc6a6e2010-02-18 08:12:37 +000088/* A helper function, used together with tablesorter, when the cells contains mixed text and links. As example:
Felix Meschberger2fc6a6e2010-02-18 08:12:37 +000089 elem.tablesorter({
Felix Meschberger63d692a2010-02-18 15:29:39 +000090 textExtraction: mixedLinksExtraction
Felix Meschberger2fc6a6e2010-02-18 08:12:37 +000091 });
92*/
93function mixedLinksExtraction(node) {
94 var l = node.getElementsByTagName('a');
95 return l && l.length > 0 ? l[0].innerHTML : node.innerHTML;
96};
97
98/* Java-like MessageFormat method. Usage:
99 'hello {0}'.msgFormat('world')
100*/
101String.prototype.msgFormat = function(/* variable arguments*/) {
102 var i=0; var s=this;
103 while(i<arguments.length) s=s.replace('{'+i+'}',arguments[i++]);
104 return s;
105}
106
107
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000108/* replacement for confirm() method, needs 'action' parameter to work.
109 * if action is not set - then default confirm() method is used. */
Felix Meschberger2fc6a6e2010-02-18 08:12:37 +0000110function Xconfirm(/* String */text, /* Callback function */action, /* String */title) {
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000111 if (!$.isFunction(action)) return confirm(text);
112 if (title === undefined) title = "";
113
114 Xdialog(text).dialog({
115 modal: true,
116 title: title,
117 buttons: {
118 "Yes": function() {
119 $(this).dialog('close');
120 action();
121 },
122 "No": function() {
123 $(this).dialog('close');
124 }
125 }
126 });
127 return false;
128}
Felix Meschberger2fc6a6e2010-02-18 08:12:37 +0000129function Xalert(/* String */text, /* String */title) {
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000130 if (title === undefined) title = "";
131
132 Xdialog(text).dialog({
133 modal: true,
134 title: title,
Felix Meschberger26444e22010-02-21 16:45:55 +0000135 width: '70%',
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000136 buttons: {
137 "Ok": function() {
138 $(this).dialog('close');
139 }
140 }
141 });
142 return false;
143}
144/* a helper function used by Xconfirm & Xalert */
145function Xdialog(text) {
146 var dialog = $('#dialog'); // use existing dialog element
147
148 if ( dialog.size() == 0 ) { // doesn't exists
149 var element = document.createElement( 'div' );
150 $('body').append(element);
151 dialog = $(element);
152 }
153
154 // init dialog
Valentin Valchev54d95642014-10-03 08:00:42 +0000155 dialog.html(text); // set text & reset dialog
156 if (dialog.is( ":data( 'dialog' )" )) dialog.dialog('destroy');
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000157 return dialog;
158}
159
160
161/* String */ function wordWrap( /* String */ msg ) {
Valentin Valchev809bccb2012-03-28 06:43:03 +0000162 if (msg) {
163 var userAgent = navigator.userAgent.toLowerCase();
164 var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
165
166 return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg;
167 } else {
168 return '';
169 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000170}
171
172
173/* content of the old ui.js */
174
175/* Element */ function clearChildren( /* Element */ element ) {
176 while (element.firstChild) {
177 element.removeChild(element.firstChild);
178 }
179 return element;
180}
181
182/* String */ function serialize( /* Element */ element ) {
183 var result = "";
184
185 if (element) {
186 if (element.nodeValue) {
187 result = element.nodeValue;
188 } else {
189 result += "<" + element.tagName;
190
191 var attrs = element.attributes;
192 for (var i=0; i < attrs.length; i++) {
193 if (attrs[i].nodeValue) {
194 result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'";
195 }
196 }
197
198 var children = element.childNodes;
199 if (children && children.length) {
200 result += ">";
201
202 for (var i=0; i < children.length; i++) {
203 result += serialize( children[i] );
204 }
205 result += "</" + element.tagName + ">";
206 } else {
207 result += "/>";
208 }
209 }
210 }
211
212 return result;
213}
214
215/* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
216 return createElement( "th", cssClass, attrs, children );
217}
218
219/* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
220 return createElement( "tr", cssClass, attrs, children );
221}
222
223/* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
224 return createElement( "td", cssClass, attrs, children );
225}
226
227/* Element */ function text( /* String */ textValue ) {
228 return document.createTextNode( textValue );
229}
230
231/* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000232 var sb = ["<", name];
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000233
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000234 if (attrs && attrs.name) {
235 sb.push(" name='", attrs.name, "'");
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000236 }
237
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000238 if (attrs && attrs.type) {
239 sb.push(" type='", attrs.type, "'");
240 }
241
242 sb.push(">");
243
244 var el = $(sb.join(""));
245
246 if (cssClass) {
247 el.addClass(cssClass);
248 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000249 if (attrs) {
250 for (var lab in attrs) {
251 if ("style" == lab) {
252 var styles = attrs[lab];
253 for (var styleName in styles) {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000254 el.css(styleName, styles[styleName]);
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000255 }
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000256 } else if ("name" == lab || "type" == lab) {
257 //skip
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000258 } else {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000259 el.attr( lab, attrs[lab] );
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000260 }
261 }
262 }
263
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000264 var element = el.get()[0];
265
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000266 if (children && children.length) {
267 for (var i=0; i < children.length; i++) {
Felix Meschberger37e9d422012-04-19 09:00:15 +0000268 if (children[i]) {
269 element.appendChild( children[i] );
270 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000271 }
272 }
273
274 return element;
275}
276
277/* Element */ function addText( /* Element */ element, /* String */ textValue ) {
278 if (element && textValue) {
279 element.appendChild( text( textValue ) );
280 }
281 return element;
282}
Valentin Valchev508d1802010-08-11 07:25:43 +0000283
Felix Meschberger7efb5462011-12-22 12:53:44 +0000284/**
285 * Sets the name cookie at the appRoot (/system/console by default) path
286 * to last for 20 years.
287 * @param name The name of the cookie
288 * @param value The value for the cookie
289 */
290function setCookie( /* String */name, /* String */value) {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000291 var date = new Date();
292 date.setFullYear(date.getFullYear() + 20);
293 $.cookies.del("felix-webconsole-" + name);
294 $.cookies.set("felix-webconsole-" + name, value, {
295 expiresAt : date,
296 path : appRoot
297 });
Felix Meschberger7efb5462011-12-22 12:53:44 +0000298}
299
300/**
301 * Returns the value of the name cookie or nothing if the cookie does
302 * not exist or is not accessible.
303 * @param name The name of the cookie
304 */
305/* String */ function getCookie(/*String */name) {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000306 return $.cookies.get("felix-webconsole-" + name);
Felix Meschberger7efb5462011-12-22 12:53:44 +0000307}
308
Valentin Valchev508d1802010-08-11 07:25:43 +0000309// language selection element
310var langSelect = false;
311$(document).ready(function() {
312 langSelect = $('#langSelect').hover(
313 function() { $(this).find('.flags').show('blind') },
314 function() { $(this).find('.flags').hide('blind') });
315 langSelect.find('.flags img').click(function() {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000316 setCookie("locale", $(this).attr('alt'));
Valentin Valchev508d1802010-08-11 07:25:43 +0000317 location.reload();
318 });
Felix Meschberger7efb5462011-12-22 12:53:44 +0000319 var locale = getCookie("locale");
Valentin Valchev4f71b262010-09-03 06:00:16 +0000320 if (locale) {
321 if ( !$.datepicker.regional[locale] ) locale = '';
322 $.datepicker.setDefaults($.datepicker.regional[locale]);
323 }
Valentin Valchev508d1802010-08-11 07:25:43 +0000324});