User:Quarenon/dynamicforms.js

var dtFormID = 0; var dtInputID = 0;

$(document).ready(function {	function validRange(x, range) {		if (range == null) {			return true;		}

var parts = range.split(','); if (parts[0] != '' && x < parseInt(parts[0])) { return false; }		if (parts[1] != '' && x > parseInt(parts[1])) { return false; }		return true; }

function submit { var formError = false; var $button = $(this);

var $dt = $button.parents('.dt'); var code = '';

$button.val('Loading...').attr('disabled', 'disabled');

$.ajax({			data: {				action: 'parse',				text: code,				prop: 'text',				title: wgPageName,				format: 'json'			},			dataType: 'json',			type: 'POST',			url: wgScriptPath + '/api.php',			success: function(response) {				$button.attr('disabled', null).val($button.data('value'));				var $html = parse($(response.parse.text['*']));				$('#' + $dt.data('conf').result).html($html);			},			timeout: 10000		}); }

function parse($el) {

// Iterate through DT blocks $('.dt', $el).each(function {			$(this).find('.dterror').empty;

var formID = 'dtform_' + (dtFormID++); var conf = []; conf.inputConf = [];

// Mainly required by mwsuggest //$(this).wrap($(' ').attr('id', formID)); $(this).find('.dtconfig .dtvar').each(function {				var temp = $(this).text.split('=', 2);				conf[$.trim(temp[0])] = $.trim(temp[1]);			});

if (typeof(conf.suggestns) == 'string') { var suggestNs = conf.suggestns.split(','); for (var i in suggestNs) { var $input = $(' ').attr({'type': 'hidden', 'name': 'ns' + suggestNs[i]}).val('1'); $(this).append($input); }			}			$(this).find('.dtinput').each(function {				var inputID = 'dtinput_' + (dtInputID++);

var inputConf = { 'id': inputID, 'name': inputID, 'size': 20 };

$(this).find('.dtvar').each(function {					var temp = $(this).text.split('=', 2);					inputConf[$.trim(temp[0])] = $.trim(temp[1]);				});

if (inputConf.type != 'button') { conf.inputConf.push(inputConf); }

if (inputConf.type == 'select') { if (typeof(inputConf.range) != 'string') { // Missing param } else { $select = $(' ').attr({'name' : inputConf.name}).css('cssText', inputConf.style); var opts = inputConf.range.split(','); for (i in opts) { var $opt = $(' ').val(opts[i]).text(opts[i]); if (opts[i] == inputConf.value) { $opt.attr('selected', 'selected'); }							$select.append($opt); }						$(this).after($select); }				} else { var type = 'text'; var value = '';

if (inputConf.type == 'submit') { type = 'button'; value = 'Submit'; } else if (inputConf.type == 'hidden') { type = 'hidden'; }					value = (inputConf.value == undefined) ? value : inputConf.value;

$input = $(' ').attr({'name': inputConf.name, 'type': type, 'value': value, 'id': inputID, 'size': inputConf.size}); $input.css('cssText', inputConf.style);

if (type == 'button') { $input.click(submit); $input.data('value', value); }

$(this).after($input); }			}); // foreach dtinput

// Store away for submittal $(this).data('conf', conf); }); // foreach dt

return $el;

} // parse

parse($('#bodyContent')); }); // onload