User:Quarenon/calc.js

/* */

/** * calc.js * * Provides a method for rendering templates with a configurable form using ajax. * REQUIRES: jQuery Framework (www.jquery.com) * * By Quarenon */

$(document).ready(function {	appendCSS('.jcInvalid { background: url(http://img12.imageshack.us/img12/508/1248624996error.png) center right no-repeat; background-color: #FFDEDE; } .jcError { font-weight: bold; color: red; }');

$('.jcConfig').each(function {		var lines = $(this).text.split('\n');		var template = ;		var formId = ;		var resultId = '';		var tplParams = [];

// Generate a globally unique ID for an input function getGuid(inputId) { return formId + resultId + inputId; }

// Give an error in the results area // TODO For errors on parsing a config, put the error after the config section function showError(str) { $('#' + resultId).empty.append(' ').addClass('jcError').text(str); }

// Parse config for (var i in lines) { var temp = lines[i].split('=', 2);

if (temp.length != 2) { continue; }

var vals = temp[1].split('|');

for (var j in vals) { vals[j] = $.trim(vals[j]); }

switch ($.trim(temp[0])) { case 'template': template = vals[0]; break; case 'form': formId = vals[0]; break; case 'result': resultId = vals[0]; break; case 'param': tplParams.push({name: vals[0], label: (vals[1] == '') ? vals[0] : vals[1], def: vals[2], type: vals[3]}); break; }		}

$form = $(' ').attr('action', '#').submit(function {			function dispResult(response) {				$('#' + resultId).empty.removeClass('jcError').html(response.parse.text['*']);			}

var code = '';

if (formError) { showError('One or more fields contains an invalid value.'); } else { $.ajax({					data: {						action: 'parse',						text: code,						prop: 'text',						title: 'JSCalc',						format: 'json',					},					dataType: 'json',					type: 'POST',					url: wgServer + '/api.php',					success: dispResult,					error: function(xhr, error) { showError(error); }				}); }

return false; });

var $table = $(' ').addClass('wikitable jcTable');

for (var i in tplParams) { if (tplParams[i].type == 'hidden') { continue; }

var $tr = $(' '); $tr.append($(' ').text(tplParams[i].label)); var $td = $(' '); if (tplParams[i].type == 'fixed') { $td.append(tplParams[i].def); } else { var guid = getGuid(tplParams[i].name); $td.append($(' ').attr({type: 'text', name: guid, id: guid, value: tplParams[i].def})); }			$tr.append($td); $table.append($tr); }

var $tr = $(' '); var $td = $(' ').attr({class: 'jcSubmit', colspan: '2'}); $td.append($(' ').attr({type: 'submit', value: 'Submit'})); $tr.append($td); $table.append($tr);

$form.append($table); $('#' + formId).empty.append($form); }); });

/* */