comparison plugins/dataforms.js @ 58:bcb5b7c2c3d3

Add a dataforms plugin and make disco#info use this one.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 01 Feb 2012 17:23:08 +0100
parents
children 5dbf93cef55d
comparison
equal deleted inserted replaced
57:91f18fdc0e2c 58:bcb5b7c2c3d3
1 'use strict';
2
3 /**
4 Copyright (c) 2012, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /////////
20 //Disco//
21 /////////
22
23 (function() {
24 var form_types = ['cancel', 'form', 'result', 'submit'];
25 var field_types = ['boolean', 'fixed', 'hidden', 'jid-multi', 'jid-single', 'list-multi', 'list-single', 'text-multi', 'text-private', 'text-single'];
26
27 var parseFields = function(fieldsList) {
28 var fields = [];
29
30 for (var i = 0; i < fieldsList.length; i++) {
31 var field = {};
32 var child = fieldsList[i];
33
34 var type = child.getAttributeNS(null, 'type');
35 if (field_types.indexOf(type) === -1)
36 field.type = type;
37 else
38 field.type = 'text-single';
39
40 var label = child.getAttributeNS(null, 'label');
41 if (label)
42 field.label = label;
43
44 var _var = child.getAttributeNS(null, 'var');
45 if (_var)
46 field.var = _var;
47
48 var desc = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'desc');
49 if (desc.length > 1)
50 ; //TODO: emit a warning if there is more than one.
51 if (0 in desc)
52 field.desc = desc[0];
53
54 var required = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'required');
55 if (required.length > 1)
56 ; //TODO: emit a warning if there is more than one.
57 field.required = (0 in required)
58
59 var values = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'value');
60 if (values.length) {
61 field.values = [];
62 for (var j = 0; j < values.length; j++)
63 field.values.push(values[j].textContent);
64 }
65
66 var options = child.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'option');
67 if (options.length) {
68 field.options = [];
69 for (var j = 0; j < options.length; j++) {
70 var option = {};
71
72 var opt = options[j];
73
74 var val = opt.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'value');
75 if (val.length > 1)
76 ; //TODO: emit a warning if there is more than one.
77 if (0 in val)
78 option.value = val[0];
79
80 var optionLabel = opt.getAttributeNS(null, 'label');
81 if (optionLabel)
82 option.label = optionLabel;
83
84 field.options.push(option);
85 }
86 }
87
88 fields.push(field);
89 }
90
91 return fields;
92 };
93
94 Lightstring.plugins['disco'] = {
95 namespaces: {
96 dataforms: 'jabber:x:data'
97 },
98 methods: {
99 parse: function(x) {
100 if (x.namespaceURI !== Lightstring.namespaces['dataforms'] || x.localName !== 'x')
101 return null;
102
103 var form = {};
104
105 var type = x.getAttributeNS(null, 'type');
106 if (form_types.indexOf(type) === -1)
107 return; //TODO: emit a warning too?
108 form.type = type;
109
110 var title = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'title');
111 if (title.length > 1)
112 ; //TODO: emit a warning if there is more than one.
113 else if (0 in title)
114 form.title = title[0];
115
116 var fields = parseFields(x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields'));
117 if (fields)
118 form.fields = fields
119
120 var reported = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'reported');
121 if (reported.length > 1)
122 ; //TODO: emit a warning if there is more than one.
123 else if (0 in reported) {
124 var fields = parseFields(reported[0].getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields'));
125 if (fields)
126 form.reported = fields
127 }
128
129 var itemsList = x.getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'item');
130 if (itemsList) {
131 var fields = parseFields(itemsList[0].getElementsByTagNameNS(Lightstring.namespaces['dataforms'], 'fields'));
132 if (fields)
133 form.items = fields;
134 }
135
136 return form;
137 }
138 }
139 };
140 })();