Mercurial > eldonilo > blog
comparison 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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f62b5c395a48 |
|---|---|
| 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 'use strict'; | |
| 21 | |
| 22 var xmpp = require('xmpp') | |
| 23 | |
| 24 var parseBoolean = function(b) { | |
| 25 if (b == 'true' || b == 'True' || b == 'TRUE' || b == '1') | |
| 26 return true; | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 exports.build = function(type, desc, content, labels, title, instructions) { | |
| 31 var x = xmpp.stanza('x', {xmlns: 'jabber:x:data', type: type}); | |
| 32 | |
| 33 if (desc._TITLE) | |
| 34 x.c('title').t(desc._TITLE).up(); | |
| 35 else if (title) | |
| 36 x.c('title').t(title).up(); | |
| 37 | |
| 38 if (desc._INSTRUCTIONS) | |
| 39 x.c('instructions').t(desc._INSTRUCTIONS).up(); | |
| 40 else if (instructions) | |
| 41 x.c('instructions').t(instructions).up(); | |
| 42 | |
| 43 if (content == 'default') { | |
| 44 content = {}; | |
| 45 for (var i in desc) | |
| 46 content[i] = desc[i].value; | |
| 47 } | |
| 48 | |
| 49 for (var i in desc) { | |
| 50 if (i[0] == '_') | |
| 51 continue; | |
| 52 | |
| 53 var fieldAttr = {'var': i}; | |
| 54 | |
| 55 if (labels) { | |
| 56 if (desc[i].type) | |
| 57 fieldAttr.type = desc[i].type; | |
| 58 if (desc[i].label) | |
| 59 fieldAttr.label = desc[i].label; | |
| 60 } | |
| 61 x.c('field', fieldAttr); | |
| 62 | |
| 63 if (labels && | |
| 64 (desc[i].type == 'list-multi' || | |
| 65 desc[i].type == 'list-single')) { | |
| 66 for (var j in desc[i].options) { | |
| 67 var optAttr = {}; | |
| 68 if (desc[i].options[j].label) | |
| 69 optAttr.label = desc[i].options[j].label; | |
| 70 x.c('option', optAttr).c('value').t(j).up().up(); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 if (i == 'FORM_TYPE') | |
| 75 x.c('value').t(desc[i].value).up(); | |
| 76 else if (typeof content[i] != 'undefined') { | |
| 77 var md = content[i]; | |
| 78 if (desc[i].type == 'jid-multi' || | |
| 79 desc[i].type == 'list-multi' || | |
| 80 desc[i].type == 'text-multi') { | |
| 81 for (var j=0; j<md.length; j++) | |
| 82 x.c('value') | |
| 83 .t(md[j].toString()).up(); | |
| 84 } else | |
| 85 x.c('value').t(md.toString()).up(); | |
| 86 } | |
| 87 | |
| 88 x.up(); | |
| 89 } | |
| 90 return x; | |
| 91 } | |
| 92 | |
| 93 exports.parse = function(x, params) { | |
| 94 var form = {}; | |
| 95 | |
| 96 x.constructor.prototype.getAttribute = function(a) { | |
| 97 return this.attrs[a]; | |
| 98 } | |
| 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.children) { | |
| 116 var field = x.children[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.children) { | |
| 126 var elem = field.children[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.children) { | |
| 154 var elem = field.children[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 } |
