Mercurial > psgxs
annotate forms.js @ 58:b98e545a94f7 default tip
Always use children instead of tags. Might break something.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 13 Sep 2011 00:54:55 +0200 |
parents | addbf6bbfaa8 |
children |
rev | line source |
---|---|
0 | 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 | |
43
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
20 'use strict'; |
023f767662d3
Fix compatibility with strict mode of node 0.4 and some files without licence header.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
23
diff
changeset
|
21 |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
22 var Element = require('ltx').Element; |
0 | 23 var service_configuration = require('./configuration').service_configuration; |
24 | |
25 var parseBoolean = function(b) { | |
26 if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1') | |
27 return true; | |
28 return false; | |
29 } | |
30 | |
31 exports.build = function(type, desc, content, labels, title, instructions) { | |
56
99bd1d1ac071
Migration to node-xmpp, done!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
43
diff
changeset
|
32 var x = new Element('x', {xmlns: 'jabber:x:data', type: type}); |
0 | 33 |
34 if (typeof desc == 'string') | |
35 desc = service_configuration[desc]; | |
36 | |
37 if (desc._TITLE) | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
38 x.c('title').t(desc._TITLE).up(); |
0 | 39 else if (title) |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
40 x.c('title').t(title).up(); |
0 | 41 |
42 if (desc._INSTRUCTIONS) | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
43 x.c('instructions').t(desc._INSTRUCTIONS).up(); |
0 | 44 else if (instructions) |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
45 x.c('instructions').t(instructions).up(); |
0 | 46 |
47 if (content == 'default') { | |
48 content = {}; | |
49 for (var i in desc) | |
50 content[i] = desc[i].value; | |
51 } | |
52 | |
53 for (var i in desc) { | |
54 if (i[0] == '_') | |
55 continue; | |
56 | |
57 var fieldAttr = {'var': i}; | |
58 | |
59 if (labels) { | |
60 if (desc[i].type) | |
61 fieldAttr.type = desc[i].type; | |
62 if (desc[i].label) | |
63 fieldAttr.label = desc[i].label; | |
64 } | |
57
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
65 |
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
66 var field = new Element('field', fieldAttr); |
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
67 x.cnode(field); |
0 | 68 |
69 if (labels && | |
70 (desc[i].type == 'list-multi' || | |
71 desc[i].type == 'list-single')) { | |
72 for (var j in desc[i].options) { | |
73 var optAttr = {}; | |
74 if (desc[i].options[j].label) | |
75 optAttr.label = desc[i].options[j].label; | |
57
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
76 field.c('option', optAttr).c('value').t(j).up().up(); |
0 | 77 } |
78 } | |
79 | |
80 if (i == 'FORM_TYPE') | |
57
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
81 field.c('value').t(desc[i].value).up(); |
0 | 82 else if (typeof content[i] != 'undefined') { |
83 var md = content[i]; | |
84 if (desc[i].type == 'jid-multi' || | |
85 desc[i].type == 'list-multi' || | |
86 desc[i].type == 'text-multi') { | |
87 for (var j=0; j<md.length; j++) | |
57
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
88 field.c('value') |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
89 .t(md[j].toString()).up(); |
0 | 90 } else |
57
addbf6bbfaa8
Various fixes for the migration to ltx.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
56
diff
changeset
|
91 field.c('value').t(md.toString()).up(); |
0 | 92 } |
93 | |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
94 x.up(); |
0 | 95 } |
23
5fc4ee90c1bc
A lot of refactorization. First attempt to modularize the server.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
96 return x; |
0 | 97 } |
98 | |
99 exports.parse = function(x, params) { | |
100 var form = {}; | |
101 | |
102 if (!params) { | |
103 var type = x.getAttribute('type'); | |
104 if (type) | |
105 form.type = type; | |
106 | |
107 var title = x.getChild('title'); | |
108 if (title) | |
109 form.title = title; | |
110 | |
111 var instructions = x.getChild('instructions'); | |
112 if (instructions) | |
113 form.instructions = instructions; | |
114 } | |
115 | |
116 form.fields = {}; | |
58
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
117 for (var i in x.children) { |
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
118 var field = x.children[i]; |
0 | 119 var name = field.getAttribute('var'); |
120 if (params && name == 'FORM_TYPE') | |
121 continue; | |
122 | |
123 if (params) { | |
124 var type = field.getAttribute('type'); | |
125 if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') { | |
126 form.fields[name] = []; | |
58
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
127 for (var j in field.children) { |
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
128 var elem = field.children[j]; |
0 | 129 if (elem.name == 'value') |
130 form.fields[name].push(elem.getText()); | |
131 } | |
132 } else if (type == 'boolean') { | |
133 var value = field.getChild('value'); | |
134 if (value) | |
135 form.fields[name] = parseBoolean(value.getText()); | |
136 } else { | |
137 var value = field.getChild('value'); | |
138 if (value) | |
139 form.fields[name] = value.getText(); | |
140 } | |
141 } else { | |
142 form.fields[name] = {}; | |
143 | |
144 var label = field.getAttribute('label'); | |
145 if (label) | |
146 form.fields[name].label = label; | |
147 | |
148 var type = field.getAttribute('type'); | |
149 if (type) | |
150 form.fields[name].type = type; | |
151 | |
152 if (type == 'jid-multi' || type == 'list-multi' || type == 'text-multi') { | |
153 form.fields[name].options = {}; | |
154 form.fields[name].values = []; | |
58
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
155 for (var j in field.children) { |
b98e545a94f7
Always use children instead of tags. Might break something.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
57
diff
changeset
|
156 var elem = field.children[j]; |
0 | 157 if (elem.name == 'option') { |
158 var value = elem.getChild('value'); | |
159 if (!value) | |
160 continue; | |
161 | |
162 value = value.getText(); | |
163 if (!value) | |
164 continue; | |
165 | |
166 form.fields[name].options[value] = {}; | |
167 | |
168 label = elem.getAttribute('label') | |
169 if (label) | |
170 form.fields[name].options[value].label = label; | |
171 } else if (elem.name == 'value') | |
172 form.fields[name].values.push(elem.getText()); | |
173 } | |
174 } else if (type == 'boolean') { | |
175 var value = field.getChild('value'); | |
176 if (value) | |
177 form.fields[name].value = parseBoolean(value.getText()); | |
178 } else { | |
179 var value = field.getChild('value'); | |
180 if (value) | |
181 form.fields[name].value = value.getText(); | |
182 } | |
183 } | |
184 } | |
185 return form; | |
186 } |