view forms.js @ 11:f4422eafb0f8

Make forms building actually working.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 02 Nov 2011 10:02:31 -0700
parents f62b5c395a48
children
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var Element = require('ltx').Element;

var parseBoolean = function(b) {
	if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1')
		return true;
	return false;
}

var forms = exports || {};

forms.build = function(type, desc, content, labels, title, instructions) {
	var x = Element('x', {xmlns: 'jabber:x:data', type: type});

	if (desc._TITLE)
		x.c('title').t(desc._TITLE).up();
	else if (title)
		x.c('title').t(title).up();

	if (desc._INSTRUCTIONS)
		x.c('instructions').t(desc._INSTRUCTIONS).up();
	else if (instructions)
		x.c('instructions').t(instructions).up();

	if (content == 'default') {
		content = {};
		for (var i in desc)
			content[i] = desc[i].value;
	}

	for (var i in desc) {
		if (i[0] == '_')
			continue;

		var fieldAttr = {'var': i};

		if (labels) {
			if (desc[i].type)
				fieldAttr.type = desc[i].type;
			if (desc[i].label)
				fieldAttr.label = desc[i].label;
		}
		x.c('field', fieldAttr);

		if (labels &&
		    (desc[i].type == 'list-multi' ||
		     desc[i].type == 'list-single')) {
			for (var j in desc[i].options) {
				var optAttr = {};
				if (desc[i].options[j].label)
					optAttr.label = desc[i].options[j].label;
				x.c('option', optAttr).c('value').t(j).up().up();
			}
		}

		if (i == 'FORM_TYPE')
			x.c('value').t(desc[i].value).up();
		else if (typeof content[i] != 'undefined') {
			var md = content[i];
			if (desc[i].type == 'jid-multi' ||
			    desc[i].type == 'list-multi' ||
			    desc[i].type == 'text-multi') {
				for (var j=0; j<md.length; j++)
					x.c('value')
						.t(md[j].toString()).up();
			} else
				x.c('value').t(md.toString()).up();
		}

		x.up();
	}
	return x;
}

forms.parse = function(x, params) {
	var form = {};

	x.constructor.prototype.getAttribute = function(a) {
		return this.attrs[a];
	}

	if (!params) {
		var type = x.getAttribute('type');
		if (type)
			form.type = type;

		var title = x.getChild('title');
		if (title)
			form.title = title;

		var instructions = x.getChild('instructions');
		if (instructions)
			form.instructions = instructions;
	}

	form.fields = {};
	for (var i in x.children) {
		var field = x.children[i];
		var name = field.getAttribute('var');
		if (params && name == 'FORM_TYPE')
			continue;

		if (params) {
			var type = field.getAttribute('type');
			if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') {
				form.fields[name] = [];
				for (var j in field.children) {
					var elem = field.children[j];
					if (elem.name == 'value')
						form.fields[name].push(elem.getText());
				}
			} else if (type == 'boolean') {
				var value = field.getChild('value');
				if (value)
					form.fields[name] = parseBoolean(value.getText());
			} else {
				var value = field.getChild('value');
				if (value)
					form.fields[name] = value.getText();
			}
		} else {
			form.fields[name] = {};

			var label = field.getAttribute('label');
			if (label)
				form.fields[name].label = label;

			var type = field.getAttribute('type');
			if (type)
				form.fields[name].type = type;

			if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') {
				form.fields[name].options = {};
				form.fields[name].values = [];
				for (var j in field.children) {
					var elem = field.children[j];
					if (elem.name == 'option') {
						var value = elem.getChild('value');
						if (!value)
							continue;

						value = value.getText();
						if (!value)
							continue;

						form.fields[name].options[value] = {};

						label = elem.getAttribute('label')
						if (label)
							form.fields[name].options[value].label = label;
					} else if (elem.name == 'value')
						form.fields[name].values.push(elem.getText());
				}
			} else if (type == 'boolean') {
				var value = field.getChild('value');
				if (value)
					form.fields[name].value = parseBoolean(value.getText());
			} else {
				var value = field.getChild('value');
				if (value)
					form.fields[name].value = value.getText();
			}
		}
	}
	return form;
}