images
3 years ago
Common.js
3 years ago
jquery.alert.js
3 years ago
jquery.calendar.js
3 years ago
jquery.cleditor.css
3 years ago
jquery.cleditor.js
3 years ago
jquery.colorselect.js
3 years ago
jquery.dropdown.js
3 years ago
jquery.form.js
3 years ago
jquery.validate.js
3 years ago
multiview.js
3 years ago
multiview.public.js
3 years ago
repeat.js
3 years ago
rrule.js
3 years ago
underscore.js
3 years ago
widget.admin.js
3 years ago
jquery.form.js
462 lines
| 1 | (function($) { |
| 2 | $.fn.ajaxSubmit = function(options) { |
| 3 | if (!this.length) { |
| 4 | log('ajaxSubmit: skipping submit process - no element selected'); |
| 5 | return this; |
| 6 | } |
| 7 | |
| 8 | if (typeof options == 'function') |
| 9 | options = { success: options }; |
| 10 | |
| 11 | options = $.extend({ |
| 12 | url: this.attr('action') || window.location.toString(), |
| 13 | type: this.attr('method') || 'GET' |
| 14 | }, options || {}); |
| 15 | |
| 16 | var veto = {}; |
| 17 | this.trigger('form-pre-serialize', [this, options, veto]); |
| 18 | if (veto.veto) { |
| 19 | log('ajaxSubmit: submit vetoed via form-pre-serialize trigger'); |
| 20 | return this; |
| 21 | } |
| 22 | |
| 23 | if (options.beforeSerialize && options.beforeSerialize(this, options) === false) { |
| 24 | log('ajaxSubmit: submit aborted via beforeSerialize callback'); |
| 25 | return this; |
| 26 | } |
| 27 | |
| 28 | var a = this.formToArray(options.semantic); |
| 29 | if (options.data) { |
| 30 | options.extraData = options.data; |
| 31 | for (var n in options.data) { |
| 32 | if(options.data[n] instanceof Array) { |
| 33 | for (var k in options.data[n]) |
| 34 | a.push( { name: n, value: options.data[n][k] } ) |
| 35 | } |
| 36 | else |
| 37 | a.push( { name: n, value: options.data[n] } ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) { |
| 42 | log('ajaxSubmit: submit aborted via beforeSubmit callback'); |
| 43 | return this; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | this.trigger('form-submit-validate', [a, this, options, veto]); |
| 48 | if (veto.veto) { |
| 49 | log('ajaxSubmit: submit vetoed via form-submit-validate trigger'); |
| 50 | return this; |
| 51 | } |
| 52 | |
| 53 | var q = $.param(a); |
| 54 | |
| 55 | if (options.type.toUpperCase() == 'GET') { |
| 56 | options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q; |
| 57 | options.data = null; |
| 58 | } |
| 59 | else |
| 60 | options.data = q; |
| 61 | |
| 62 | var $form = this, callbacks = []; |
| 63 | if (options.resetForm) callbacks.push(function() { $form.resetForm(); }); |
| 64 | if (options.clearForm) callbacks.push(function() { $form.clearForm(); }); |
| 65 | |
| 66 | if (!options.dataType && options.target) { |
| 67 | var oldSuccess = options.success || function(){}; |
| 68 | callbacks.push(function(data) { |
| 69 | $(options.target).html(data).each(oldSuccess, arguments); |
| 70 | }); |
| 71 | } |
| 72 | else if (options.success) |
| 73 | callbacks.push(options.success); |
| 74 | |
| 75 | options.success = function(data, status) { |
| 76 | for (var i=0, max=callbacks.length; i < max; i++) |
| 77 | callbacks[i].apply(options, [data, status, $form]); |
| 78 | }; |
| 79 | |
| 80 | var files = $('input:file', this).fieldValue(); |
| 81 | var found = false; |
| 82 | for (var j=0; j < files.length; j++) |
| 83 | if (files[j]) |
| 84 | found = true; |
| 85 | |
| 86 | if (options.iframe || found) { |
| 87 | if ($.browser.safari && options.closeKeepAlive) |
| 88 | $.get(options.closeKeepAlive, fileUpload); |
| 89 | else |
| 90 | fileUpload(); |
| 91 | } |
| 92 | else |
| 93 | $.ajax(options); |
| 94 | this.trigger('form-submit-notify', [this, options]); |
| 95 | return this; |
| 96 | |
| 97 | function fileUpload() { |
| 98 | var form = $form[0]; |
| 99 | |
| 100 | if ($(':input[@name=submit]', form).length) { |
| 101 | alert('Error: Form elements must not be named "submit".'); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | var opts = $.extend({}, $.ajaxSettings, options); |
| 106 | var s = jQuery.extend(true, {}, $.extend(true, {}, $.ajaxSettings), opts); |
| 107 | |
| 108 | var id = 'jqFormIO' + (new Date().getTime()); |
| 109 | var $io = $('<iframe id="' + id + '" name="' + id + '" />'); |
| 110 | var io = $io[0]; |
| 111 | |
| 112 | if ($.browser.msie || $.browser.opera) |
| 113 | io.src = 'javascript:false;document.write("");'; |
| 114 | $io.css({ position: 'absolute', top: '-1000px', left: '-1000px' }); |
| 115 | |
| 116 | var xhr = { |
| 117 | aborted: 0, |
| 118 | responseText: null, |
| 119 | responseXML: null, |
| 120 | status: 0, |
| 121 | statusText: 'n/a', |
| 122 | getAllResponseHeaders: function() {}, |
| 123 | getResponseHeader: function() {}, |
| 124 | setRequestHeader: function() {}, |
| 125 | abort: function() { |
| 126 | this.aborted = 1; |
| 127 | $io.attr('src','about:blank'); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | var g = opts.global; |
| 132 | |
| 133 | if (g && ! $.active++) $.event.trigger("ajaxStart"); |
| 134 | if (g) $.event.trigger("ajaxSend", [xhr, opts]); |
| 135 | |
| 136 | if (s.beforeSend && s.beforeSend(xhr, s) === false) { |
| 137 | s.global && jQuery.active--; |
| 138 | return; |
| 139 | } |
| 140 | if (xhr.aborted) |
| 141 | return; |
| 142 | |
| 143 | var cbInvoked = 0; |
| 144 | var timedOut = 0; |
| 145 | |
| 146 | var sub = form.clk; |
| 147 | if (sub) { |
| 148 | var n = sub.name; |
| 149 | if (n && !sub.disabled) { |
| 150 | options.extraData = options.extraData || {}; |
| 151 | options.extraData[n] = sub.value; |
| 152 | if (sub.type == "image") { |
| 153 | options.extraData[name+'.x'] = form.clk_x; |
| 154 | options.extraData[name+'.y'] = form.clk_y; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | setTimeout(function() { |
| 160 | var t = $form.attr('target'), a = $form.attr('action'); |
| 161 | $form.attr({ |
| 162 | target: id, |
| 163 | method: 'POST', |
| 164 | action: opts.url |
| 165 | }); |
| 166 | |
| 167 | if (! options.skipEncodingOverride) { |
| 168 | $form.attr({ |
| 169 | encoding: 'multipart/form-data', |
| 170 | enctype: 'multipart/form-data' |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | |
| 175 | if (opts.timeout) |
| 176 | setTimeout(function() { timedOut = true; cb(); }, opts.timeout); |
| 177 | |
| 178 | var extraInputs = []; |
| 179 | try { |
| 180 | if (options.extraData) |
| 181 | for (var n in options.extraData) |
| 182 | extraInputs.push( |
| 183 | $('<input type="hidden" name="'+n+'" value="'+options.extraData[n]+'" />') |
| 184 | .appendTo(form)[0]); |
| 185 | |
| 186 | $io.appendTo('body'); |
| 187 | io.attachEvent ? io.attachEvent('onload', cb) : io.addEventListener('load', cb, false); |
| 188 | form.submit(); |
| 189 | } |
| 190 | finally { |
| 191 | $form.attr('action', a); |
| 192 | t ? $form.attr('target', t) : $form.removeAttr('target'); |
| 193 | $(extraInputs).remove(); |
| 194 | } |
| 195 | }, 10); |
| 196 | |
| 197 | function cb() { |
| 198 | if (cbInvoked++) return; |
| 199 | |
| 200 | io.detachEvent ? io.detachEvent('onload', cb) : io.removeEventListener('load', cb, false); |
| 201 | |
| 202 | var operaHack = 0; |
| 203 | var ok = true; |
| 204 | try { |
| 205 | if (timedOut) throw 'timeout'; |
| 206 | |
| 207 | var data, doc; |
| 208 | |
| 209 | doc = io.contentWindow ? io.contentWindow.document : io.contentDocument ? io.contentDocument : io.document; |
| 210 | |
| 211 | if (doc.body == null && !operaHack && $.browser.opera) { |
| 212 | operaHack = 1; |
| 213 | cbInvoked--; |
| 214 | setTimeout(cb, 100); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | xhr.responseText = doc.body ? doc.body.innerHTML : null; |
| 219 | xhr.responseXML = doc.XMLDocument ? doc.XMLDocument : doc; |
| 220 | xhr.getResponseHeader = function(header){ |
| 221 | var headers = {'content-type': opts.dataType}; |
| 222 | return headers[header]; |
| 223 | }; |
| 224 | |
| 225 | if (opts.dataType == 'json' || opts.dataType == 'script') { |
| 226 | var ta = doc.getElementsByTagName('textarea')[0]; |
| 227 | xhr.responseText = ta ? ta.value : xhr.responseText; |
| 228 | } |
| 229 | else if (opts.dataType == 'xml' && !xhr.responseXML && xhr.responseText != null) { |
| 230 | xhr.responseXML = toXml(xhr.responseText); |
| 231 | } |
| 232 | data = $.httpData(xhr, opts.dataType); |
| 233 | } |
| 234 | catch(e){ |
| 235 | ok = false; |
| 236 | $.handleError(opts, xhr, 'error', e); |
| 237 | } |
| 238 | |
| 239 | if (ok) { |
| 240 | opts.success(data, 'success'); |
| 241 | if (g) $.event.trigger("ajaxSuccess", [xhr, opts]); |
| 242 | } |
| 243 | if (g) $.event.trigger("ajaxComplete", [xhr, opts]); |
| 244 | if (g && ! --$.active) $.event.trigger("ajaxStop"); |
| 245 | if (opts.complete) opts.complete(xhr, ok ? 'success' : 'error'); |
| 246 | |
| 247 | // clean up |
| 248 | setTimeout(function() { |
| 249 | $io.remove(); |
| 250 | xhr.responseXML = null; |
| 251 | }, 100); |
| 252 | }; |
| 253 | |
| 254 | function toXml(s, doc) { |
| 255 | if (window.ActiveXObject) { |
| 256 | doc = new ActiveXObject('Microsoft.XMLDOM'); |
| 257 | doc.async = 'false'; |
| 258 | doc.loadXML(s); |
| 259 | } |
| 260 | else |
| 261 | doc = (new DOMParser()).parseFromString(s, 'text/xml'); |
| 262 | return (doc && doc.documentElement && doc.documentElement.tagName != 'parsererror') ? doc : null; |
| 263 | }; |
| 264 | }; |
| 265 | }; |
| 266 | |
| 267 | $.fn.ajaxForm = function(options) { |
| 268 | return this.ajaxFormUnbind().bind('submit.form-plugin',function() { |
| 269 | $(this).ajaxSubmit(options); |
| 270 | return false; |
| 271 | }).each(function() { |
| 272 | $(":submit,input:image", this).bind('click.form-plugin',function(e) { |
| 273 | var form = this.form; |
| 274 | form.clk = this; |
| 275 | if (this.type == 'image') { |
| 276 | if (e.offsetX != undefined) { |
| 277 | form.clk_x = e.offsetX; |
| 278 | form.clk_y = e.offsetY; |
| 279 | } else if (typeof $.fn.offset == 'function') { // try to use dimensions plugin |
| 280 | var offset = $(this).offset(); |
| 281 | form.clk_x = e.pageX - offset.left; |
| 282 | form.clk_y = e.pageY - offset.top; |
| 283 | } else { |
| 284 | form.clk_x = e.pageX - this.offsetLeft; |
| 285 | form.clk_y = e.pageY - this.offsetTop; |
| 286 | } |
| 287 | } |
| 288 | // clear form vars |
| 289 | setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 10); |
| 290 | }); |
| 291 | }); |
| 292 | }; |
| 293 | |
| 294 | $.fn.ajaxFormUnbind = function() { |
| 295 | this.unbind('submit.form-plugin'); |
| 296 | return this.each(function() { |
| 297 | $(":submit,input:image", this).unbind('click.form-plugin'); |
| 298 | }); |
| 299 | |
| 300 | }; |
| 301 | |
| 302 | $.fn.formToArray = function(semantic) { |
| 303 | var a = []; |
| 304 | if (this.length == 0) return a; |
| 305 | |
| 306 | var form = this[0]; |
| 307 | var els = semantic ? form.getElementsByTagName('*') : form.elements; |
| 308 | if (!els) return a; |
| 309 | for(var i=0, max=els.length; i < max; i++) { |
| 310 | var el = els[i]; |
| 311 | var n = el.name; |
| 312 | if (!n) continue; |
| 313 | |
| 314 | if (semantic && form.clk && el.type == "image") { |
| 315 | // handle image inputs on the fly when semantic == true |
| 316 | if(!el.disabled && form.clk == el) |
| 317 | a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | var v = $.fieldValue(el, true); |
| 322 | if (v && v.constructor == Array) { |
| 323 | for(var j=0, jmax=v.length; j < jmax; j++) |
| 324 | a.push({name: n, value: v[j]}); |
| 325 | } |
| 326 | else if (v !== null && typeof v != 'undefined') |
| 327 | a.push({name: n, value: v}); |
| 328 | } |
| 329 | |
| 330 | if (!semantic && form.clk) { |
| 331 | // input type=='image' are not found in elements array! handle them here |
| 332 | var inputs = form.getElementsByTagName("input"); |
| 333 | for(var i=0, max=inputs.length; i < max; i++) { |
| 334 | var input = inputs[i]; |
| 335 | var n = input.name; |
| 336 | if(n && !input.disabled && input.type == "image" && form.clk == input) |
| 337 | a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y}); |
| 338 | } |
| 339 | } |
| 340 | return a; |
| 341 | }; |
| 342 | |
| 343 | $.fn.formSerialize = function(semantic) { |
| 344 | return $.param(this.formToArray(semantic)); |
| 345 | }; |
| 346 | |
| 347 | $.fn.fieldSerialize = function(successful) { |
| 348 | var a = []; |
| 349 | this.each(function() { |
| 350 | var n = this.name; |
| 351 | if (!n) return; |
| 352 | var v = $.fieldValue(this, successful); |
| 353 | if (v && v.constructor == Array) { |
| 354 | for (var i=0,max=v.length; i < max; i++) |
| 355 | a.push({name: n, value: v[i]}); |
| 356 | } |
| 357 | else if (v !== null && typeof v != 'undefined') |
| 358 | a.push({name: this.name, value: v}); |
| 359 | }); |
| 360 | return $.param(a); |
| 361 | }; |
| 362 | |
| 363 | |
| 364 | $.fn.fieldValue = function(successful) { |
| 365 | for (var val=[], i=0, max=this.length; i < max; i++) { |
| 366 | var el = this[i]; |
| 367 | var v = $.fieldValue(el, successful); |
| 368 | if (v === null || typeof v == 'undefined' || (v.constructor == Array && !v.length)) |
| 369 | continue; |
| 370 | v.constructor == Array ? $.merge(val, v) : val.push(v); |
| 371 | } |
| 372 | return val; |
| 373 | }; |
| 374 | |
| 375 | $.fieldValue = function(el, successful) { |
| 376 | var n = el.name, t = el.type, tag = el.tagName.toLowerCase(); |
| 377 | if (typeof successful == 'undefined') successful = true; |
| 378 | |
| 379 | if (successful && (!n || el.disabled || t == 'reset' || t == 'button' || |
| 380 | (t == 'checkbox' || t == 'radio') && !el.checked || |
| 381 | (t == 'submit' || t == 'image') && el.form && el.form.clk != el || |
| 382 | tag == 'select' && el.selectedIndex == -1)) |
| 383 | return null; |
| 384 | |
| 385 | if (tag == 'select') { |
| 386 | var index = el.selectedIndex; |
| 387 | if (index < 0) return null; |
| 388 | var a = [], ops = el.options; |
| 389 | var one = (t == 'select-one'); |
| 390 | var max = (one ? index+1 : ops.length); |
| 391 | for(var i=(one ? index : 0); i < max; i++) { |
| 392 | var op = ops[i]; |
| 393 | if (op.selected) { |
| 394 | var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value; |
| 395 | if (one) return v; |
| 396 | a.push(v); |
| 397 | } |
| 398 | } |
| 399 | return a; |
| 400 | } |
| 401 | return el.value; |
| 402 | }; |
| 403 | |
| 404 | $.fn.clearForm = function() { |
| 405 | return this.each(function() { |
| 406 | $('input,select,textarea', this).clearFields(); |
| 407 | }); |
| 408 | }; |
| 409 | |
| 410 | |
| 411 | $.fn.clearFields = $.fn.clearInputs = function() { |
| 412 | return this.each(function() { |
| 413 | var t = this.type, tag = this.tagName.toLowerCase(); |
| 414 | if (t == 'text' || t == 'password' || tag == 'textarea') |
| 415 | this.value = ''; |
| 416 | else if (t == 'checkbox' || t == 'radio') |
| 417 | this.checked = false; |
| 418 | else if (tag == 'select') |
| 419 | this.selectedIndex = -1; |
| 420 | }); |
| 421 | }; |
| 422 | |
| 423 | |
| 424 | $.fn.resetForm = function() { |
| 425 | return this.each(function() { |
| 426 | if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType)) |
| 427 | this.reset(); |
| 428 | }); |
| 429 | }; |
| 430 | |
| 431 | $.fn.enable = function(b) { |
| 432 | if (b == undefined) b = true; |
| 433 | return this.each(function() { |
| 434 | this.disabled = !b |
| 435 | }); |
| 436 | }; |
| 437 | |
| 438 | |
| 439 | $.fn.selected = function(select) { |
| 440 | if (select == undefined) select = true; |
| 441 | return this.each(function() { |
| 442 | var t = this.type; |
| 443 | if (t == 'checkbox' || t == 'radio') |
| 444 | this.checked = select; |
| 445 | else if (this.tagName.toLowerCase() == 'option') { |
| 446 | var $sel = $(this).parent('select'); |
| 447 | if (select && $sel[0] && $sel[0].type == 'select-one') { |
| 448 | // deselect all other options |
| 449 | $sel.find('option').selected(false); |
| 450 | } |
| 451 | this.selected = select; |
| 452 | } |
| 453 | }); |
| 454 | }; |
| 455 | |
| 456 | |
| 457 | function log() { |
| 458 | if ($.fn.ajaxSubmit.debug && window.console && window.console.log) |
| 459 | window.console.log('[jquery.form] ' + Array.prototype.join.call(arguments,'')); |
| 460 | }; |
| 461 | })(jQuery); |
| 462 |