1/* 2 * !!!!!!!!! NENI MOZNO NAHRADIT NOVOU VERZI !!!!!!!!!!!!!!!!! 3 * 4 * vlastni upravy na radcich 443 - 451 5 * 6 */ 7 8/* 9 * jQuery Form Plugin 10 * version: 2.28 (10-MAY-2009) 11 * @requires jQuery v1.2.2 or later 12 * 13 * Examples and documentation at: http://malsup.com/jquery/form/ 14 * Dual licensed under the MIT and GPL licenses: 15 * http://www.opensource.org/licenses/mit-license.php 16 * http://www.gnu.org/licenses/gpl.html 17 */ 18;(function($) { 19 var honzaDebug = false; 20/* 21 Usage Note: 22 ----------- 23 Do not use both ajaxSubmit and ajaxForm on the same form. These 24 functions are intended to be exclusive. Use ajaxSubmit if you want 25 to bind your own submit handler to the form. For example, 26 27 $(document).ready(function() { 28 $('#myForm').bind('submit', function() { 29 $(this).ajaxSubmit({ 30 target: '#output' 31 }); 32 return false; // <-- important! 33 }); 34 }); 35 36 Use ajaxForm when you want the plugin to manage all the event binding 37 for you. For example, 38 39 $(document).ready(function() { 40 $('#myForm').ajaxForm({ 41 target: '#output' 42 }); 43 }); 44 45 When using ajaxForm, the ajaxSubmit function will be invoked for you 46 at the appropriate time. 47*/ 48 49/** 50 * ajaxSubmit() provides a mechanism for immediately submitting 51 * an HTML form using AJAX. 52 */ 53$.fn.ajaxSubmit = function(options) { 54 // fast fail if nothing selected (http://dev.jquery.com/ticket/2752) 55 if (!this.length) { 56 log('ajaxSubmit: skipping submit process - no element selected'); 57 return this; 58 } 59 60 if (typeof options == 'function') 61 options = { success: options }; 62 63 var url = $.trim(this.attr('action')); 64 if (url) { 65 // clean url (don't include hash vaue) 66 url = (url.match(/^([^#]+)/)||[])[1]; 67 } 68 url = url || window.location.href || '' 69 70 options = $.extend({ 71 url: url, 72 type: this.attr('method') || 'GET' 73 }, options || {}); 74 75 // hook for manipulating the form data before it is extracted; 76 // convenient for use with rich editors like tinyMCE or FCKEditor 77 var veto = {}; 78 this.trigger('form-pre-serialize', [this, options, veto]); 79 if (veto.veto) { 80 log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); 81 return this; 82 } 83 84 // provide opportunity to alter form data before it is serialized 85 if (options.beforeSerialize && options.beforeSerialize(this, options) === false) { 86 log('ajaxSubmit: submit aborted via beforeSerialize callback'); 87 return this; 88 } 89 90 var a = this.formToArray(options.semantic); 91 if (options.data) { 92 options.extraData = options.data; 93 for (var n in options.data) { 94 if(options.data[n] instanceof Array) { 95 for (var k in options.data[n]) 96 a.push( { name: n, value: options.data[n][k] } ); 97 } 98 else 99 a.push( { name: n, value: options.data[n] } ); 100 } 101 } 102 103 // give pre-submit callback an opportunity to abort the submit 104 if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) { 105 log('ajaxSubmit: submit aborted via beforeSubmit callback'); 106 return this; 107 } 108 109 // fire vetoable 'validate' event 110 this.trigger('form-submit-validate', [a, this, options, veto]); 111 if (veto.veto) { 112 log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); 113 return this; 114 } 115 116 var q = $.param(a); 117 118 if (options.type.toUpperCase() == 'GET') { 119 options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; 120 options.data = null; // data is null for 'get' 121 } 122 else 123 options.data = q; // data is the query string for 'post' 124 125 var $form = this, callbacks = []; 126 if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); 127 if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); 128 129 // perform a load on the target only if dataType is not provided 130 if (!options.dataType && options.target) { 131 var oldSuccess = options.success || function(){}; 132 callbacks.push(function(data) { 133 $(options.target).html(data).each(oldSuccess, arguments); 134 }); 135 } 136 else if (options.success) 137 callbacks.push(options.success); 138 139 options.success = function(data, status) { 140 for (var i=0, max=callbacks.length; i < max; i++) 141 callbacks[i].apply(options, [data, status, $form]); 142 }; 143 144 // are there files to upload? 145 var files = $('input:file', this).fieldValue(); 146 var found = false;
147 for (var j=0; j < files.length; j++) 148 if (files[j]) 149 found = true; 150 151 var multipart = false; 152 /* 153 var mp = 'multipart/form-data'; 154 multipart = ($form.attr('enctype') == mp || $form.attr('encoding') == mp); 155 */ 156 157 // options.iframe allows user to force iframe mode 158 if (options.iframe || found || multipart) { 159 // hack to fix Safari hang (thanks to Tim Molendijk for this) 160 // see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d 161 if (options.closeKeepAlive) 162 { 163 $.get(options.closeKeepAlive, fileUpload); 164 } 165 else 166 { 167 fileUpload(); 168 } // end if 169 } 170 else 171 { 172 $.ajax(options); 173 } // end if 174 175 // fire 'notify' event 176 this.trigger('form-submit-notify', [this, options]); 177 return this; 178 179 180 // private function for handling file uploads (hat tip to YAHOO!) 181 function fileUpload() { 182 var form = $form[0]; 183 184 if ($(':input[name=submit]', form).length) { 185 alert('Error: Form elements must not be named "submit".'); 186 return; 187 } 188 189 var opts = $.extend({}, $.ajaxSettings, options); 190 var s = $.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); 191 192 var id = 'jqFormIO' + (new Date().getTime()); 193 var $io = $('<iframe id="' + id + '" name="' + id + '" src="about:blank" />'); 194 var io = $io[0]; 195 196 $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); 197 198 var xhr = { // mock object 199 aborted: 0, 200 responseText: null, 201 responseXML: null, 202 status: 0, 203 statusText: 'n/a', 204 getAllResponseHeaders: function() {}, 205 getResponseHeader: function() {}, 206 setRequestHeader: function() {}, 207 abort: function() { 208 this.aborted = 1; 209 $io.attr('src','about:blank'); // abort op in progress 210 } 211 }; 212 213 var g = opts.global; 214 // trigger ajax global events so that activity/block indicators work like normal 215 if (g && ! $.active++) $.event.trigger("ajaxStart"); 216 if (g) $.event.trigger("ajaxSend", [xhr, opts]); 217 218 if (s.beforeSend && s.beforeSend(xhr, s) === false) 219 { 220 s.global && $.active--; 221 return; 222 } 223 224 if (xhr.aborted) 225 return; 226 227 var cbInvoked = 0; 228 var timedOut = 0; 229 230 // add submitting element to data if we know it 231 var sub = form.clk; 232 if (sub) { 233 var n = sub.name; 234 if (n && !sub.disabled) { 235 options.extraData = options.extraData || {}; 236 options.extraData[n] = sub.value; 237 if (sub.type == "image") { 238 options.extraData[name+'.x'] = form.clk_x; 239 options.extraData[name+'.y'] = form.clk_y; 240 } 241 } 242 } 243 244 // take a breath so that pending repaints get some cpu time before the upload starts 245 setTimeout(function() { 246 // make sure form attrs are set 247 var t = $form.attr('target'), a = $form.attr('action'); 248 249 // update form attrs in IE friendly way 250 form.setAttribute('target',id); 251 if (form.getAttribute('method') != 'POST') 252 form.setAttribute('method', 'POST'); 253 if (form.getAttribute('action') != opts.url) 254 form.setAttribute('action', opts.url); 255 256 // ie borks in some cases when setting encoding 257 if (! options.skipEncodingOverride) { 258 $form.attr({ 259 encoding: 'multipart/form-data', 260 enctype: 'multipart/form-data' 261 }); 262 } 263 264 // support timout 265 if (opts.timeout) 266 setTimeout(function() { timedOut = true; cb(); }, opts.timeout); 267 268 // add "extra" data to form if provided in options 269 var extraInputs = []; 270 try { 271 if (options.extraData) 272 for (var n in options.extraData) 273 extraInputs.push( 274 $('<input type="hidden" name="'+n+'" value="'+options.extraData[n]+'" />') 275 .appendTo(form)[0]); 276 277 // add iframe to doc and submit the form 278 $io.appendTo('body'); 279 io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); 280 form.submit(); 281 } 282 finally { 283 // reset attrs and remove "extra" input elements 284 form.setAttribute('action',a); 285 t ? form.setAttribute('target', t) : $form.removeAttr('target'); 286 $(extraInputs).remove(); 287 } 288 }, 10); 289 290 var nullCheckFlag = 0; 291 292 function cb() { 293 if (cbInvoked++) return; 294 295 io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); 296 297 var ok = true; 298 try { 299 if (timedOut) throw 'timeout'; 300 // extract the server response from the iframe 301 var data, doc; 302 303 doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; 304 305 if ((doc.body == null || doc.body.innerHTML == '') && !nullCheckFlag) { 306 // in some browsers (cough, Opera 9.2.x) the iframe DOM is not always traversable when 307 // the onload callback fires, so we give them a 2nd chance 308 nullCheckFlag = 1; 309 cbInvoked--; 310 setTimeout(cb, 100); 311 return; 312 } 313 314 xhr.responseText = doc.body ? doc.body.innerHTML : null; 315 xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; 316 xhr.getResponseHeader = function(header){ 317 var headers = {'content-type': opts.dataType}; 318 return headers[header]; 319 }; 320 321 // HONZA TEXTAREA DEBUG 322 if (honzaDebug && window.console && window.console.log) window.console.log(xhr.responseText); 323 324 if (opts.dataType == 'json' || opts.dataType == 'script') { 325 var ta = doc.getElementsByTagName('textarea')[0]; 326 xhr.responseText = ta ? ta.value : xhr.responseText; 327 } 328 else if (opts.dataType == 'xml' && !xhr.responseXML && xhr.responseText != null) { 329 xhr.responseXML = toXml(xhr.responseText); 330 } 331 332 data = httpData(xhr, opts.dataType); 333 } 334 catch(e){ 335 ok = false;
336 if (honzaDebug && window.console && window.console.log) window.console.log(e); 337 //$.handleError(opts, xhr, 'error', e); // metoda odebrana v jQuery 1.5 338 } 339 340 // ordering of these callbacks/triggers is odd, but that's how $.ajax does it 341 if (ok) { 342 opts.success(data, 'success'); 343 if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); 344 } 345 if (g) $.event.trigger("ajaxComplete", [xhr, opts]); 346 if (g && ! --$.active) $.event.trigger("ajaxStop"); 347 if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); 348 349 // clean up 350 setTimeout(function() { 351 $io.remove(); 352 xhr.responseXML = null; 353 }, 100); 354 }; 355 356 function toXml(s, doc) { 357 if (window.ActiveXObject) { 358 doc = new ActiveXObject('Microsoft.XMLDOM'); 359 doc.async = 'false'; 360 doc.loadXML(s); 361 } 362 else 363 doc = (new DOMParser()).parseFromString(s, 'text/xml'); 364 return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; 365 }; 366 367 var parseJSON = $.parseJSON || function(s) { 368 return window['eval']('(' + s + ')'); 369 }; 370 371 function httpData( xhr, type, s ) { // mostly lifted from jq1.4.4 372 var ct = xhr.getResponseHeader('content-type') || '', 373 xml = type === 'xml' || !type && ct.indexOf('xml') >= 0, 374 data = xml ? xhr.responseXML : xhr.responseText; 375 376 if (xml && data.documentElement.nodeName === 'parsererror') { 377 $.error && $.error('parsererror'); 378 } 379 if (s && s.dataFilter) { 380 data = s.dataFilter(data, type); 381 } 382 if (typeof data === 'string') { 383 if (type === 'json' || !type && ct.indexOf('json') >= 0) { 384 data = parseJSON(data); 385 } else if (type === "script" || !type && ct.indexOf("javascript") >= 0) { 386 $.globalEval(data); 387 } 388 } 389 return data; 390 }; 391 }; 392}; 393 394/** 395 * ajaxForm() provides a mechanism for fully automating form submission. 396 * 397 * The advantages of using this method instead of ajaxSubmit() are: 398 * 399 * 1: This method will include coordinates for <input type="image" /> elements (if the element 400 * is used to submit the form). 401 * 2. This method will include the submit element's name/value data (for the element that was 402 * used to submit the form). 403 * 3. This method binds the submit() method to the form for you. 404 * 405 * The options argument for ajaxForm works exactly as it does for ajaxSubmit. ajaxForm merely 406 * passes the options argument along after properly binding events for submit elements and 407 * the form itself. 408 */ 409 410$.fn.ajaxForm = function(options) { 411 return this.ajaxFormUnbind().bind('submit.form-plugin',function() { 412 $(this).ajaxSubmit(options); 413 return false; 414 }).each(function() { 415 // store options in hash 416 $(":submit,input:image", this).bind('click.form-plugin',function(e) { 417 var form = this.form; 418 form.clk = this; 419 if (this.type == 'image') { 420 if (e.offsetX != undefined) { 421 form.clk_x = e.offsetX; 422 form.clk_y = e.offsetY; 423 } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin 424 var offset = $(this).offset(); 425 form.clk_x = e.pageX - offset.left; 426 form.clk_y = e.pageY - offset.top; 427 } else { 428 form.clk_x = e.pageX - this.offsetLeft; 429 form.clk_y = e.pageY - this.offsetTop; 430 } 431 } 432 // clear form vars 433 setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 10); 434 }); 435 }); 436}; 437 438// ajaxFormUnbind unbinds the event handlers that were bound by ajaxForm 439$.fn.ajaxFormUnbind = function() { 440 this.unbind('submit.form-plugin'); 441 return this.each(function() { 442 $(":submit,input:image", this).unbind('click.form-plugin'); 443 }); 444 445}; 446 447/** 448 * formToArray() gathers form element data into an array of objects that can 449 * be passed to any of the following ajax functions: $.get, $.post, or load. 450 * Each object in the array has both a 'name' and 'value' property. An example of 451 * an array for a simple login form might be: 452 * 453 * [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] 454 * 455 * It is this array that is passed to pre-submit callback functions provided to the 456 * ajaxSubmit() and ajaxForm() methods. 457 */ 458$.fn.formToArray = function(semantic) { 459 var a = []; 460 if (this.length == 0) return a; 461 462 var form = this[0]; 463 var els = semantic ? form.getElementsByTagName('*') : form.elements; 464 if (!els) return a; 465 for(var i=0, max=els.length; i < max; i++) { 466 var el = els[i]; 467 var n = el.name; 468 if (!n) continue; 469 470 if (semantic && form.clk && el.type == "image") { 471 // handle image inputs on the fly when semantic == true 472 if(!el.disabled && form.clk == el) { 473 a.push({name: n, value: $(el).val()}); 474 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); 475 } 476 continue; 477 } 478 479 var v = $.fieldValue(el, true); 480 481 // FIX-jd 26.10.2010 pro ruskou mutaci prevadet hodnoty utf na entity 482 // 20101119ov pokud v nenà null, tak jedu dál 483 if (v && $(el).attr('type') != 'hidden' && el.name !='robot_trap' && typeof v == 'string') { 484 // po prechodu na utf uz neni potreba v = v.unicodeToNumEntity(); 485 } 486 487 488 // handle hidden fields 489 if (!$(el).is(':visible') && $(el).attr('type')!='hidden') { 490 //v = '####hidden####'; 491 } 492 493 // handle disabled fields 494 if ($(el).attr('disabled') && $(el).attr('type')!='hidden') { 495 v = '####disabled####'; 496 } 497 498 if (v && v.constructor == Array) { 499 for(var j=0, jmax=v.length; j < jmax; j++) 500 a.push({name: n, value: v[j]}); 501 } 502 else if (v !== null && typeof v != 'undefined') 503 a.push({name: n, value: v}); 504 } 505 506 if (!semantic && form.clk) { 507 // input type=='image' are not found in elements array! handle it here 508 var $input = $(form.clk), input = $input[0], n = input.name; 509 if (n && !input.disabled && input.type == 'image') { 510 a.push({name: n, value: $input.val()}); 511 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); 512 } 513 } 514 return a; 515}; 516 517/** 518 * Serializes form data into a 'submittable' string. This method will return a string 519 * in the format: name1=value1&name2=value2 520 */
521$.fn.formSerialize = function(semantic) { 522 //hand off to jQuery.param for proper encoding 523 return $.param(this.formToArray(semantic)); 524}; 525 526/** 527 * Serializes all field elements in the jQuery object into a query string. 528 * This method will return a string in the format: name1=value1&name2=value2 529 */ 530$.fn.fieldSerialize = function(successful) { 531 var a = []; 532 this.each(function() { 533 var n = this.name; 534 if (!n) return; 535 var v = $.fieldValue(this, successful); 536 if (v && v.constructor == Array) { 537 for (var i=0,max=v.length; i < max; i++) 538 a.push({name: n, value: v[i]}); 539 } 540 else if (v !== null && typeof v != 'undefined') 541 a.push({name: this.name, value: v}); 542 }); 543 544 //hand off to jQuery.param for proper encoding 545 return $.param(a); 546}; 547 548/** 549 * Returns the value(s) of the element in the matched set. For example, consider the following form: 550 * 551 * <form><fieldset> 552 * <input name="A" type="text" /> 553 * <input name="A" type="text" /> 554 * <input name="B" type="checkbox" value="B1" /> 555 * <input name="B" type="checkbox" value="B2"/> 556 * <input name="C" type="radio" value="C1" /> 557 * <input name="C" type="radio" value="C2" /> 558 * </fieldset></form> 559 * 560 * var v = $(':text').fieldValue(); 561 * // if no values are entered into the text inputs 562 * v == ['',''] 563 * // if values entered into the text inputs are 'foo' and 'bar' 564 * v == ['foo','bar'] 565 * 566 * var v = $(':checkbox').fieldValue(); 567 * // if neither checkbox is checked 568 * v === undefined 569 * // if both checkboxes are checked 570 * v == ['B1', 'B2'] 571 * 572 * var v = $(':radio').fieldValue(); 573 * // if neither radio is checked 574 * v === undefined 575 * // if first radio is checked 576 * v == ['C1'] 577 * 578 * The successful argument controls whether or not the field element must be 'successful' 579 * (per http://www.w3.org/TR/html4/interact/forms.html#successful-controls). 580 * The default value of the successful argument is true. If this value is false the value(s) 581 * for each element is returned. 582 * 583 * Note: This method *always* returns an array. If no valid value can be determined the 584 * array will be empty, otherwise it will contain one or more values. 585 */ 586$.fn.fieldValue = function(successful) { 587 588 for (var val=[], i=0, max=this.length; i < max; i++) { 589 var el = this[i]; 590 var v = $.fieldValue(el, successful); 591 if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) 592 continue; 593 v.constructor == Array ? $.merge(val, v) : val.push(v); 594 } 595 return val; 596}; 597 598/** 599 * Returns the value of the field element. 600 */ 601$.fieldValue = function(el, successful) { 602 603 var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); 604 if (typeof successful == 'undefined') successful = true; 605 606 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || 607 (t == 'checkbox' || t == 'radio') && !el.checked || 608 (t == 'submit' || t == 'image') && el.form && el.form.clk != el || 609 tag == 'select' && el.selectedIndex == -1)) 610 return null; 611 612 if (tag == 'select') { 613 var index = el.selectedIndex; 614 if (index < 0) return null; 615 var a = [], ops = el.options; 616 var one = (t == 'select-one'); 617 var max = (one ? index+1 : ops.length); 618 for(var i=(one ? index : 0); i < max; i++) { 619 var op = ops[i]; 620 if (op.selected) { 621 var v = op.value; 622 if (!v) // extra pain for IE... 623 v = (op.attributes && op.attributes['value'] && !(op.attributes['value'].specified)) ? op.text : op.value; 624 if (one) return v; 625 a.push(v); 626 } 627 } 628 return a; 629 } 630 631 return el.value; 632}; 633 634/** 635 * Clears the form data. Takes the following actions on the form's input fields: 636 * - input text fields will have their 'value' property set to the empty string 637 * - select elements will have their 'selectedIndex' property set to -1 638 * - checkbox and radio inputs will have their 'checked' property set to false 639 * - inputs of type submit, button, reset, and hidden will *not* be effected 640 * - button elements will *not* be effected 641 */ 642$.fn.clearForm = function() { 643 return this.each(function() { 644 $('input,select,textarea', this).clearFields(); 645 }); 646}; 647 648/** 649 * Clears the selected form elements. 650 */ 651$.fn.clearFields = $.fn.clearInputs = function() { 652 return this.each(function() { 653 var t = this.type, tag = this.tagName.toLowerCase(); 654 if (t == 'text' || t == 'password' || tag == 'textarea') 655 this.value = ''; 656 else if (t == 'checkbox' || t == 'radio') 657 this.checked = false;
658 else if (tag == 'select') 659 this.selectedIndex = -1; 660 }); 661}; 662 663/** 664 * Resets the form data. Causes all form elements to be reset to their original value. 665 */ 666$.fn.resetForm = function() { 667 return this.each(function() { 668 // guard against an input with the name of 'reset' 669 // note that IE reports the reset function as an 'object' 670 if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) 671 this.reset(); 672 }); 673}; 674 675/** 676 * Enables or disables any matching elements. 677 */ 678$.fn.enable = function(b) { 679 if (b == undefined) b = true; 680 return this.each(function() { 681 this.disabled = !b; 682 }); 683}; 684 685/** 686 * Checks/unchecks any matching checkboxes or radio buttons and 687 * selects/deselects and matching option elements. 688 */ 689$.fn.selected = function(select) { 690 if (select == undefined) select = true; 691 return this.each(function() { 692 var t = this.type; 693 if (t == 'checkbox' || t == 'radio') 694 this.checked = select; 695 else if (this.tagName.toLowerCase() == 'option') { 696 var $sel = $(this).parent('select'); 697 if (select && $sel[0] && $sel[0].type == 'select-one') { 698 // deselect all other options 699 $sel.find('option').selected(false); 700 } 701 this.selected = select; 702 } 703 }); 704}; 705 706// helper fn for console logging 707// set $.fn.ajaxSubmit.debug to true to enable debug logging 708function log() { 709 if ($.fn.ajaxSubmit.debug && window.console && window.console.log) 710 window.console.log('[jquery.form] ' + Array.prototype.join.call(arguments,'')); 711}; 712 713})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.