blob: 76857a5134f533cbf691a2e881de586f9e623ca2 [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
155 dialog.html(text).dialog('destroy'); // set text & reset dialog
156 return dialog;
157}
158
159
160/* String */ function wordWrap( /* String */ msg ) {
Valentin Valchev809bccb2012-03-28 06:43:03 +0000161 if (msg) {
162 var userAgent = navigator.userAgent.toLowerCase();
163 var isMozilla = /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent );
164
165 return isMozilla ? msg.split('').join(String.fromCharCode('8203')) : msg;
166 } else {
167 return '';
168 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000169}
170
171
172/* content of the old ui.js */
173
174/* Element */ function clearChildren( /* Element */ element ) {
175 while (element.firstChild) {
176 element.removeChild(element.firstChild);
177 }
178 return element;
179}
180
181/* String */ function serialize( /* Element */ element ) {
182 var result = "";
183
184 if (element) {
185 if (element.nodeValue) {
186 result = element.nodeValue;
187 } else {
188 result += "<" + element.tagName;
189
190 var attrs = element.attributes;
191 for (var i=0; i < attrs.length; i++) {
192 if (attrs[i].nodeValue) {
193 result += " " + attrs[i].nodeName + "='" + attrs[i].nodeValue + "'";
194 }
195 }
196
197 var children = element.childNodes;
198 if (children && children.length) {
199 result += ">";
200
201 for (var i=0; i < children.length; i++) {
202 result += serialize( children[i] );
203 }
204 result += "</" + element.tagName + ">";
205 } else {
206 result += "/>";
207 }
208 }
209 }
210
211 return result;
212}
213
214/* Element */ function th( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
215 return createElement( "th", cssClass, attrs, children );
216}
217
218/* Element */ function tr( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
219 return createElement( "tr", cssClass, attrs, children );
220}
221
222/* Element */ function td( /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
223 return createElement( "td", cssClass, attrs, children );
224}
225
226/* Element */ function text( /* String */ textValue ) {
227 return document.createTextNode( textValue );
228}
229
230/* Element */ function createElement( /* String */ name, /* String */ cssClass, /* Map */ attrs, /* Element[] */ children ) {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000231 var sb = ["<", name];
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000232
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000233 if (attrs && attrs.name) {
234 sb.push(" name='", attrs.name, "'");
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000235 }
236
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000237 if (attrs && attrs.type) {
238 sb.push(" type='", attrs.type, "'");
239 }
240
241 sb.push(">");
242
243 var el = $(sb.join(""));
244
245 if (cssClass) {
246 el.addClass(cssClass);
247 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000248 if (attrs) {
249 for (var lab in attrs) {
250 if ("style" == lab) {
251 var styles = attrs[lab];
252 for (var styleName in styles) {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000253 el.css(styleName, styles[styleName]);
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000254 }
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000255 } else if ("name" == lab || "type" == lab) {
256 //skip
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000257 } else {
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000258 el.attr( lab, attrs[lab] );
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000259 }
260 }
261 }
262
Felix Meschbergerbd32ca02011-05-30 11:18:14 +0000263 var element = el.get()[0];
264
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000265 if (children && children.length) {
266 for (var i=0; i < children.length; i++) {
Felix Meschberger37e9d422012-04-19 09:00:15 +0000267 if (children[i]) {
268 element.appendChild( children[i] );
269 }
Felix Meschberger45c9caa2010-02-17 08:04:11 +0000270 }
271 }
272
273 return element;
274}
275
276/* Element */ function addText( /* Element */ element, /* String */ textValue ) {
277 if (element && textValue) {
278 element.appendChild( text( textValue ) );
279 }
280 return element;
281}
Valentin Valchev508d1802010-08-11 07:25:43 +0000282
Felix Meschberger7efb5462011-12-22 12:53:44 +0000283/**
284 * Sets the name cookie at the appRoot (/system/console by default) path
285 * to last for 20 years.
286 * @param name The name of the cookie
287 * @param value The value for the cookie
288 */
289function setCookie( /* String */name, /* String */value) {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000290 var date = new Date();
291 date.setFullYear(date.getFullYear() + 20);
292 $.cookies.del("felix-webconsole-" + name);
293 $.cookies.set("felix-webconsole-" + name, value, {
294 expiresAt : date,
295 path : appRoot
296 });
Felix Meschberger7efb5462011-12-22 12:53:44 +0000297}
298
299/**
300 * Returns the value of the name cookie or nothing if the cookie does
301 * not exist or is not accessible.
302 * @param name The name of the cookie
303 */
304/* String */ function getCookie(/*String */name) {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000305 return $.cookies.get("felix-webconsole-" + name);
Felix Meschberger7efb5462011-12-22 12:53:44 +0000306}
307
Valentin Valchev508d1802010-08-11 07:25:43 +0000308// language selection element
309var langSelect = false;
310$(document).ready(function() {
311 langSelect = $('#langSelect').hover(
312 function() { $(this).find('.flags').show('blind') },
313 function() { $(this).find('.flags').hide('blind') });
314 langSelect.find('.flags img').click(function() {
Valentin Valcheve1d49382012-01-26 11:03:41 +0000315 setCookie("locale", $(this).attr('alt'));
Valentin Valchev508d1802010-08-11 07:25:43 +0000316 location.reload();
317 });
Felix Meschberger7efb5462011-12-22 12:53:44 +0000318 var locale = getCookie("locale");
Valentin Valchev4f71b262010-09-03 06:00:16 +0000319 if (locale) {
320 if ( !$.datepicker.regional[locale] ) locale = '';
321 $.datepicker.setDefaults($.datepicker.regional[locale]);
322 }
Valentin Valchev508d1802010-08-11 07:25:43 +0000323});