Last active
December 23, 2015 22:59
-
-
Save lcaballero/6707118 to your computer and use it in GitHub Desktop.
Create a form from input elements and attributes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function($) { | |
/** | |
* Creates a form element that can be appended to the body. These | |
* are the default parameters that can be provided to this function | |
* via an options object: | |
* | |
* Options: | |
* { | |
* method: "post" | "get" | |
* classes: defaults to "none" | |
* action: "~/".resolveUrl(), should always be provided | |
* } | |
*/ | |
$.createForm = function(opts) { | |
var defaults = { method: "post", classes: "none" }; | |
var config = $.extend({}, opts, defaults); | |
var html = [ | |
'<form method="', config.method, '"', | |
' action="', config.action, '"', | |
' class="', config.classes, '"', | |
' ></form>' | |
]; | |
return $(html.join("")); | |
}; | |
/** | |
* Creates a textarea element from the options object provided, that | |
* should specify the name and value to be used to construct the | |
* textarea. | |
*/ | |
$.createParam = function(opts) { | |
var html = [ | |
'<textarea class="none" name="', opts.name, '" >', opts.value, '</textarea>' | |
]; | |
return $(html.join("")); | |
}; | |
/** | |
* | |
*/ | |
$.createAndSubmitForm = function(opts) { | |
var form = $.createForm({ action: opts.action }); | |
var props = $.createPropertyJson(void (0)); | |
$.each(props, function(k, v) { | |
form.append( | |
$.createParam({ | |
name: k, | |
value: v | |
})); | |
}); | |
$("body:first").append(form); | |
if (/localhost/.test(String(location.host)) || /wks/.test(String(location.host))) { | |
if (confirm("OK?")) { | |
form.submit(); | |
} | |
} else { | |
form.submit(); | |
} | |
}; | |
/** | |
* Creates a properties object from each of the roots provided. | |
*/ | |
$.createPropertiesJson = function(roots) { | |
var data = []; | |
roots.each(function(i, e) { | |
e = $(e); | |
data.push($.createPropertyJson(e)); | |
}); | |
return data; | |
}; | |
/** | |
* Creates a properties object from the root provided. | |
*/ | |
$.createPropertyJson = function(root) { | |
var data = {}; | |
$("[data-property]", root).each( | |
function(i, e) { | |
e = $(e); | |
var prop = e.attr("data-property"); | |
var val = | |
e.is(":checkbox") ? (e.is(":checked") ? "on" : "off") : | |
e.is(":radio") ? (e.is(":checked") ? e.val() : "") : | |
e.val(); | |
val = (!!val && val.constructor == Array) | |
? "|" + val.join("|") + "|" | |
: val; | |
var def = e.attr("defaulttxt"); | |
var isSelect = e.is("select"); | |
var isBadVal = | |
(e.is(":radio") && !val) | |
|| val == def | |
|| val === null | |
|| (isSelect && val == "--") | |
|| (isSelect && val == "Select One") | |
|| (isSelect && val == "Select Multiple"); | |
if (!isBadVal) { | |
data[prop] = isBadVal ? "" : val.toString(); | |
} | |
}); | |
return data; | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment