Mercurial > psgxs
comparison forms.js @ 0:9ee956af41e3
Initial commit
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 27 Jun 2010 22:05:12 +0200 |
parents | |
children | efe8dbd34780 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:9ee956af41e3 |
---|---|
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('xmpp') | |
21 var service_configuration = require('./configuration').service_configuration; | |
22 | |
23 var parseBoolean = function(b) { | |
24 if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1') | |
25 return true; | |
26 return false; | |
27 } | |
28 | |
29 exports.build = function(type, desc, content, labels, title, instructions) { | |
30 var x = xmpp.stanza('x', {xmlns: 'jabber:x:data', type: type}); | |
31 | |
32 if (typeof desc == 'string') | |
33 desc = service_configuration[desc]; | |
34 | |
35 if (desc._TITLE) | |
36 x.s('title').t(desc._TITLE); | |
37 else if (title) | |
38 x.s('title').t(title); | |
39 | |
40 if (desc._INSTRUCTIONS) | |
41 x.s('instructions').t(desc._INSTRUCTIONS); | |
42 else if (instructions) | |
43 x.s('instructions').t(instructions); | |
44 | |
45 if (content == 'default') { | |
46 content = {}; | |
47 for (var i in desc) | |
48 content[i] = desc[i].value; | |
49 } | |
50 | |
51 for (var i in desc) { | |
52 if (i[0] == '_') | |
53 continue; | |
54 | |
55 var fieldAttr = {'var': i}; | |
56 | |
57 if (labels) { | |
58 if (desc[i].type) | |
59 fieldAttr.type = desc[i].type; | |
60 if (desc[i].label) | |
61 fieldAttr.label = desc[i].label; | |
62 } | |
63 var field = xmpp.stanza('field', fieldAttr); | |
64 | |
65 if (labels && | |
66 (desc[i].type == 'list-multi' || | |
67 desc[i].type == 'list-single')) { | |
68 for (var j in desc[i].options) { | |
69 var optAttr = {}; | |
70 if (desc[i].options[j].label) | |
71 optAttr.label = desc[i].options[j].label; | |
72 field.s('option', optAttr).c('value').t(j); | |
73 } | |
74 } | |
75 | |
76 if (i == 'FORM_TYPE') | |
77 field.s('value').t(desc[i].value); | |
78 else if (typeof content[i] != 'undefined') { | |
79 var md = content[i]; | |
80 if (desc[i].type == 'jid-multi' || | |
81 desc[i].type == 'list-multi' || | |
82 desc[i].type == 'text-multi') { | |
83 for (var j=0; j<md.length; j++) | |
84 field.s('value') | |
85 .t(md[j].toString()); | |
86 } else | |
87 field.s('value').t(md.toString()); | |
88 } | |
89 | |
90 x.cx(field); | |
91 } | |
92 if (field) | |
93 return x; | |
94 return -1; //FIXME | |
95 } | |
96 | |
97 exports.parse = function(x, params) { | |
98 var form = {}; | |
99 | |
100 if (!params) { | |
101 var type = x.getAttribute('type'); | |
102 if (type) | |
103 form.type = type; | |
104 | |
105 var title = x.getChild('title'); | |
106 if (title) | |
107 form.title = title; | |
108 | |
109 var instructions = x.getChild('instructions'); | |
110 if (instructions) | |
111 form.instructions = instructions; | |
112 } | |
113 | |
114 form.fields = {}; | |
115 for (var i in x.tags) { | |
116 var field = x.tags[i]; | |
117 var name = field.getAttribute('var'); | |
118 if (params && name == 'FORM_TYPE') | |
119 continue; | |
120 | |
121 if (params) { | |
122 var type = field.getAttribute('type'); | |
123 if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') { | |
124 form.fields[name] = []; | |
125 for (var j in field.tags) { | |
126 var elem = field.tags[j]; | |
127 if (elem.name == 'value') | |
128 form.fields[name].push(elem.getText()); | |
129 } | |
130 } else if (type == 'boolean') { | |
131 var value = field.getChild('value'); | |
132 if (value) | |
133 form.fields[name] = parseBoolean(value.getText()); | |
134 } else { | |
135 var value = field.getChild('value'); | |
136 if (value) | |
137 form.fields[name] = value.getText(); | |
138 } | |
139 } else { | |
140 form.fields[name] = {}; | |
141 | |
142 var label = field.getAttribute('label'); | |
143 if (label) | |
144 form.fields[name].label = label; | |
145 | |
146 var type = field.getAttribute('type'); | |
147 if (type) | |
148 form.fields[name].type = type; | |
149 | |
150 if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') { | |
151 form.fields[name].options = {}; | |
152 form.fields[name].values = []; | |
153 for (var j in field.tags) { | |
154 var elem = field.tags[j]; | |
155 if (elem.name == 'option') { | |
156 var value = elem.getChild('value'); | |
157 if (!value) | |
158 continue; | |
159 | |
160 value = value.getText(); | |
161 if (!value) | |
162 continue; | |
163 | |
164 form.fields[name].options[value] = {}; | |
165 | |
166 label = elem.getAttribute('label') | |
167 if (label) | |
168 form.fields[name].options[value].label = label; | |
169 } else if (elem.name == 'value') | |
170 form.fields[name].values.push(elem.getText()); | |
171 } | |
172 } else if (type == 'boolean') { | |
173 var value = field.getChild('value'); | |
174 if (value) | |
175 form.fields[name].value = parseBoolean(value.getText()); | |
176 } else { | |
177 var value = field.getChild('value'); | |
178 if (value) | |
179 form.fields[name].value = value.getText(); | |
180 } | |
181 } | |
182 } | |
183 return form; | |
184 } |