Bugs fixxed:

- I've renamed $.eval to $.execute (since $.$$exec is allready there?)
- Commited sam's form function
- Removed $.fn.formValues()
- Removed $.fn.update()
- Removed $.fn.serialize()
This commit is contained in:
Gilles van den Hoven 2006-06-08 17:31:57 +00:00
parent 69ae9aeab0
commit 6656522705
2 changed files with 198 additions and 106 deletions

View file

@ -121,128 +121,220 @@ $.fn.load = function(a,o,f) {
} }
var self = this; var self = this;
$.xml(t,a,o,function(h){ $.xml(t,a,o,function(h){
h = h.responseText; // Get HTTP data
h = $.httpData(h);
// Assign it and execute all scripts
self.html(h).find("script").each(function(){ self.html(h).find("script").each(function(){
try { try { $.execute( this.text || this.textContent || this.innerHTML || ""); } catch(e){}
$.eval( this.text || this.textContent || this.innerHTML || "");
} catch(e){}
}); });
if(f){f(h);}
// Callback function
if (f && f.constructor == Function)
f(h);
}); });
return this; return this;
}; };
/** /**
* name: $.fn.formValues * Initial frontend function to submit form variables. This function
* example: $('#frmLogin').formValues('sButton') * is for registering coordinates, in the case of an image being used
* docs: Gets form values and creates a key=>value array of the found values. * as the submit element, and sets up an event to listen and wait for
* Optionally adds the button which is clicked if you provide it. * a form submit click. It then calls any following chained functions
* Only does this for ENABLED elements in the order of the form. * to actually gather the variables and submit them.
*
* Usage examples, when used with getForm().putForm():
*
* 1. Just eval the results returned from the backend.
* $('#form-id').form();
*
* 2. Render backend results directly to target ID (expects (x)HTML).
* $('#form-id').form('#target-id');
*
* 3. Submit to backend URL (form action) then call this function.
* $('#form-id').form(post_callback);
*
* 4. Load target ID with backend results then call a function.
* $('#form-id').form('#target-id', null, post_callback);
*
* 5. Call a browser function (for validation) and then (optionally)
* load server results to target ID.
* $('#form-id').form('#target-id', pre_callback);
*
* 6. Call validation function first then load server results to
* target ID and then also call a browser function.
* $('#form-id').form('#target-id', pre_callback, post_callback);
*
* @param target arg for the target id element to render
* @param pre_cb callback function before submission
* @param post_cb callback after any results are returned
* @return "this" object
* @see getForm(), putForm()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett
* @version 20060606
*/ */
$.fn.formValues = function(sButton) { $.fn.form = function(target, pre_cb, post_cb) {
var a = []; $('input[@type="submit"],input[@type="image"]', this).click(function(ev){
var ok = {INPUT:true, TEXTAREA:true, OPTION:true}; this.form.clicked = this;
if (ev.offsetX != undefined) {
this.form.clicked_x = ev.offsetX;
this.form.clicked_y = ev.offsetY;
} else {
this.form.clicked_x = ev.pageX - this.offsetLeft;
this.form.clicked_y = ev.pageY - this.offsetTop;
}
});
this.submit(function(e){
e.preventDefault();
$(this).getForm().putForm(target, pre_cb, post_cb);
return this;
});
};
// Loop the shite /**
$('*', this).each(function() { * This function gathers form element variables into an array that
// Skip elements not of the types in ok * is embedded into the current "this" variable as "this.vars". It
if (!ok[this.tagName.toUpperCase()]) * is normally used in conjunction with form() and putForm() but can
* be used standalone as long as an image is not used for submission.
*
* Standalone usage examples:
*
* 1. Gather form vars and return array to LHS variable.
* var myform = $('#form-id').getForm();
*
* 2. Provide a serialized URL-ready string (after 1. above).
* var mystring = $.param(myform.vars);
*
* 3. Gather form vars and send to RHS plugin via "this.vars".
* $('#form-id').getForm().some_other_plugin();
*
* @return "this" object
* @see form(), putForm()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett
* @version 20060606
*/
$.fn.getForm = function() {
var a = [];
var ok = {INPUT:true, TEXTAREA:true, OPTION:true};
$('*', this).each(function() {
if (this.disabled || this.type == 'reset' || (this.type == 'checkbox' && !this.checked) || (this.type == 'radio' && !this.checked))
return;
if (this.type == 'submit' || this.type == 'image') {
if (this.form.clicked != this)
return;
if (this.type == 'image') {
if (this.form.clicked_x) {
a.push({name: this.name+'_x', value: this.form.clicked_x});
a.push({name: this.name+'_y', value: this.form.clicked_y});
return;
}
}
}
if (!ok[this.nodeName.toUpperCase()])
return; return;
// Skip disabled elements var par = this.parentNode;
if (this.disabled) var p = par.nodeName.toUpperCase();
if ((p == 'SELECT' || p == 'OPTGROUP') && !this.selected)
return; return;
// Skip submit buttons and image elements var n = this.name || par.name;
if ((this.type == 'submit') || (this.type == 'image')) if (!n && p == 'OPTGROUP' && (par = par.parentNode))
return; n = par.name;
// Skip non-selected options if (n == undefined)
var oParent = this.parentNode;
var sNn = oParent.nodeName.toUpperCase();
if (((sNn == 'SELECT') || (sNn == 'OPTGROUP')) && (!this.selected))
return; return;
// Skip non-checked nodes a.push({name: n, value: this.value});
if (((this.type == 'radio') || (this.type == 'checkbox')) && (!this.checked))
return;
// If we come here, everything is fine
var sKey = this.name || this.id || oParent.name || oParent.id;
var sValue = this.value;
// If we don't have an ID, and the parent is an OPTGROUP,
// get the NAME or ID of the OPTGROUP's parent
if ((!sKey) && (sNn == 'OPTGROUP') && (oParent = oParent.parentNode))
sKey = oParent.name || oParent.id;
// Add the data
a.push({ name: sKey, value: sValue });
}); });
// Add submit button if needed this.vars = a;
if (sButton && (sButton !== null)) {
var el = $(sButton).get(0); return this;
a.push({ }
name: el.name || el.id || el.parentNode.name || el.parentNode.id,
value: el.value /**
}); * Final form submission plugin usually used in conjunction with
* form() and getForm(). If a second argument is a valid function
* then it will be called before the form vars are sent to the
* backend. If this pre-submit function returns exactly "false"
* then it will abort further processing otherwise the process
* will continue according to the first and third arguments.
*
* If the first argument is a function, and it exists, then the form
* values will be submitted and that callback function called. If
* the first argument is a string value then the "load()" plugin
* will be called which will populate the innerHTML of the indicated
* element and a callback will be called if there is third argument.
* If there are no arguments then the form values are submitted with
* an additional variable (evaljs=1) which indicates to the backend
* to to prepare the returned results for evaluation, ie; the result
* needs to be valid javascript all on a single line.
*
* Usage example:
*
* $.fn.myvars = function() {
* this.vars = [];
* for (var i in this) {
* if (this[i] instanceof Function || this[i] == null) continue;
* this.vars.push({name: i, value: this[i].length});
* }
* return this;
* }
*
* precb = function(vars) {
* return confirm('Submit these values?\n\n'+$.param(vars));
* }
*
* $('*').myvars().putForm('#mytarget',precb,null,'myhandler.php');
*
* @param target arg for the target id element to render
* @param pre_cb callback function before submission
* @param post_cb callback after any results are returned
* @param url form action override
* @param mth form method override
* @return "this" object
* @see form(), getForm(), load(), xml()
* @author Mark Constable (markc@renta.net)
* @author G. vd Hoven, Mike Alsup, Sam Collett
* @version 20060606
*/
$.fn.putForm = function(target, pre_cb, post_cb, url, mth) {
if (pre_cb && pre_cb.constructor == Function)
if (pre_cb(this.vars) === false)
return;
var f = this.get(0);
var url = url || f.action || '';
var mth = mth || f.method || 'POST';
if (target && target.constructor == Function) {
$.xml(mth, url, $.param(this.vars), target);
} else if (target && target.constructor == String) {
$(target).load(url, this.vars, post_cb);
} else {
this.vars.push({name: 'evaljs', value: 1});
$.xml(mth, url, $.param(this.vars), function(r) { $.execute(r.responseText); });
} }
// Done return this;
return a; }
};
/** // ------------------------------------------------------
* name: $.fn.update
* example: $('someJQueryObject').update('sURL', 'sAction', 'aValues', 'fCallback');
* docs: Calls sURL with sAction and sends the aValues
* Puts the results from that call in the jQuery object and calls fCallback
*/
$.fn.update = function(sURL, sMethod, aValues, fCallback) {
var el = this;
// Process $.fn.changer = function(form, target, pre_cb, post_cb) {
$.xml( $(form).submit(function(e) {
sMethod || "GET", e.preventDefault();
sURL || "", return false;
$.param(aValues), });
function(sResult) { this.change(function(e) {
sResult = $.httpData(sResult); e.preventDefault();
$(form).getForm().putForm(target, pre_cb, post_cb);
// Update the element with the new HTML return this;
el.html(sResult); });
}
// Evaluate the scripts AFTER this (so you can allready modify the new HTML!)
el.html(sResult).find("script").each(function(){
try { $.eval( this.text || this.textContent || this.innerHTML || "" ); } catch(e) { }
});
// And call the callback handler :)
if (fCallback && (fCallback.constructor == Function))
fCallback();
}
);
};
/**
* name: $.fn.serialize
* example: $('someJQueryObject').serialize('sButton', 'fCallback');
* docs: Calls the form's action with the correct method and the serialized values.
* Optionally adds the button which is clicked if you provide it.
* When there are results, the fCallback function is called.
*/
$.fn.serialize = function(sButton, fCallback) {
var el = this.get(0);
// Process
$.xml(
el.method || "GET",
el.action || "",
$.param(this.formValues(sButton)),
function(sResult) {
if (fCallback && (fCallback.constructor == Function))
fCallback($.httpData(sResult));
}
);
};

6
jquery/jquery.js vendored
View file

@ -318,7 +318,7 @@ function $(a,c) {
return self; return self;
} }
$.eval = eval; $.execute = eval;
$.apply = function(o,f,a) { $.apply = function(o,f,a) {
a = a || []; a = a || [];
@ -330,7 +330,7 @@ $.apply = function(o,f,a) {
p[i] = 'a['+i+']'; p[i] = 'a['+i+']';
} }
o.$$exec = this; o.$$exec = this;
var r = $.eval('o.$$exec(' + p.join(',') + ')'); var r = $.execute('o.$$exec(' + p.join(',') + ')');
o.$$exec = null; o.$$exec = null;
return r; return r;
} }
@ -625,7 +625,7 @@ $.filter = function(t,r,not) {
} }
if ( f !== null ) { if ( f !== null ) {
$.eval('f = function(a,i){return ' + f + '}'); $.execute('f = function(a,i){return ' + f + '}');
r = g( r, f ); r = g( r, f );
} }
} }