diff forms.js @ 0:f62b5c395a48

Initial commit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 04 Jun 2011 05:02:47 +0200
parents
children f4422eafb0f8
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/forms.js
@@ -0,0 +1,184 @@
+/*
+ *  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 xmpp = require('xmpp')
+
+var parseBoolean = function(b) {
+	if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1')
+		return true;
+	return false;
+}
+
+exports.build = function(type, desc, content, labels, title, instructions) {
+	var x = xmpp.stanza('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;
+}
+
+exports.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;
+}