Mercurial > psc
comparison forms.js @ 0:a2dab4544b2d default tip
Initial commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 28 Jan 2011 02:35:04 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a2dab4544b2d |
---|---|
1 /* | |
2 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
3 * | |
4 * This file is part of PSĜS, a PubSub server written in JavaScript. | |
5 * | |
6 * PSĜS is free software: you can redistribute it and/or modify | |
7 * it under the terms of the GNU Affero General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License. | |
10 * | |
11 * PSĜS is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU Affero General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU Affero General Public License | |
17 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | |
18 */ | |
19 | |
20 var xmpp = require('clxmpp') | |
21 | |
22 var parseBoolean = function(b) { | |
23 if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1') | |
24 return true; | |
25 return false; | |
26 } | |
27 | |
28 exports.build = function(type, desc, content, labels, title, instructions) { | |
29 var x = xmpp.stanza('x', {xmlns: 'jabber:x:data', type: type}); | |
30 | |
31 if (desc._TITLE) | |
32 x.s('title').t(desc._TITLE); | |
33 else if (title) | |
34 x.s('title').t(title); | |
35 | |
36 if (desc._INSTRUCTIONS) | |
37 x.s('instructions').t(desc._INSTRUCTIONS); | |
38 else if (instructions) | |
39 x.s('instructions').t(instructions); | |
40 | |
41 if (content == 'default') { | |
42 content = {}; | |
43 for (var i in desc) | |
44 content[i] = desc[i].value; | |
45 } | |
46 | |
47 for (var i in desc) { | |
48 if (i[0] == '_') | |
49 continue; | |
50 | |
51 var fieldAttr = {'var': i}; | |
52 | |
53 if (labels) { | |
54 if (desc[i].type) | |
55 fieldAttr.type = desc[i].type; | |
56 if (desc[i].label) | |
57 fieldAttr.label = desc[i].label; | |
58 } | |
59 var field = xmpp.stanza('field', fieldAttr); | |
60 | |
61 if (labels && | |
62 (desc[i].type == 'list-multi' || | |
63 desc[i].type == 'list-single')) { | |
64 for (var j in desc[i].options) { | |
65 var optAttr = {}; | |
66 if (desc[i].options[j].label) | |
67 optAttr.label = desc[i].options[j].label; | |
68 field.s('option', optAttr).c('value').t(j); | |
69 } | |
70 } | |
71 | |
72 if (i == 'FORM_TYPE') | |
73 field.s('value').t(desc[i].value); | |
74 else if (typeof content[i] != 'undefined') { | |
75 var md = content[i]; | |
76 if (desc[i].type == 'jid-multi' || | |
77 desc[i].type == 'list-multi' || | |
78 desc[i].type == 'text-multi') { | |
79 for (var j=0; j<md.length; j++) | |
80 field.s('value') | |
81 .t(md[j].toString()); | |
82 } else | |
83 field.s('value').t(md.toString()); | |
84 } | |
85 | |
86 x.cnode(field); | |
87 } | |
88 if (field) | |
89 return x; | |
90 return -1; //FIXME | |
91 } | |
92 | |
93 exports.parse = function(x, params) { | |
94 var form = {}; | |
95 | |
96 if (!params) { | |
97 var type = x.getAttribute('type'); | |
98 if (type) | |
99 form.type = type; | |
100 | |
101 var title = x.getChild('title'); | |
102 if (title) | |
103 form.title = title; | |
104 | |
105 var instructions = x.getChild('instructions'); | |
106 if (instructions) | |
107 form.instructions = instructions; | |
108 } | |
109 | |
110 form.fields = {}; | |
111 for (var i in x.tags) { | |
112 var field = x.tags[i]; | |
113 var name = field.getAttribute('var'); | |
114 if (params && name == 'FORM_TYPE') | |
115 continue; | |
116 | |
117 if (params) { | |
118 var type = field.getAttribute('type'); | |
119 if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') { | |
120 form.fields[name] = []; | |
121 for (var j in field.tags) { | |
122 var elem = field.tags[j]; | |
123 if (elem.name == 'value') | |
124 form.fields[name].push(elem.getText()); | |
125 } | |
126 } else if (type == 'boolean') { | |
127 var value = field.getChild('value'); | |
128 if (value) | |
129 form.fields[name] = parseBoolean(value.getText()); | |
130 } else { | |
131 var value = field.getChild('value'); | |
132 if (value) | |
133 form.fields[name] = value.getText(); | |
134 } | |
135 } else { | |
136 form.fields[name] = {}; | |
137 | |
138 var label = field.getAttribute('label'); | |
139 if (label) | |
140 form.fields[name].label = label; | |
141 | |
142 var type = field.getAttribute('type'); | |
143 if (type) | |
144 form.fields[name].type = type; | |
145 | |
146 if (field.tags.length) { | |
147 if (field.tags.length == 1) { | |
148 if (field.tags[0].name == 'value') { | |
149 var value = field.tags[0]; | |
150 if (type == 'boolean') | |
151 form.fields[name].value = parseBoolean(value.getText()); | |
152 else | |
153 form.fields[name].value = value.getText(); | |
154 } | |
155 } else { | |
156 form.fields[name].options = {}; | |
157 form.fields[name].values = []; | |
158 for (var j in field.tags) { | |
159 var elem = field.tags[j]; | |
160 if (elem.name == 'option') { | |
161 var value = elem.getChild('value'); | |
162 if (!value) | |
163 continue; | |
164 | |
165 value = value.getText(); | |
166 if (!value) | |
167 continue; | |
168 | |
169 form.fields[name].options[value] = {}; | |
170 | |
171 label = elem.getAttribute('label') | |
172 if (label) | |
173 form.fields[name].options[value].label = label; | |
174 } else if (elem.name == 'value') | |
175 form.fields[name].values.push(elem.getText()); | |
176 } | |
177 if (form.fields[name].values.length == 1) { | |
178 form.fields[name].value = form.fields[name].values[0]; | |
179 delete form.fields[name].values; | |
180 } | |
181 } | |
182 } | |
183 } | |
184 } | |
185 return form; | |
186 } | |
187 | |
188 exports.toString = function(form) { | |
189 var text = ''; | |
190 | |
191 var inspect = require('sys').inspect; | |
192 | |
193 for (var i in form.fields) { | |
194 if (i == 'FORM_TYPE') | |
195 continue; | |
196 | |
197 var field = form.fields[i]; | |
198 text += '\n\t' + i; | |
199 if (field.label) | |
200 text += ' (' + field.label + ')'; | |
201 if (field.type) | |
202 text += ', ' + field.type; | |
203 | |
204 text += ': '; | |
205 if (typeof field.value != 'undefined') | |
206 text += inspect(field.value); | |
207 else if (field.values) | |
208 text += inspect(field.values); | |
209 else | |
210 text += 'empty'; | |
211 | |
212 if (typeof field.options == 'object') | |
213 for (var j in field.options) | |
214 text += '\n\t\t' + j + ': ' + field.options[j].label; | |
215 } | |
216 return text; | |
217 } |