-
Notifications
You must be signed in to change notification settings - Fork 10
/
strophe.forms.js
194 lines (153 loc) · 5.52 KB
/
strophe.forms.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// XMPP plugins for Strophe v1.0.5
// (c) 2012-2013 Yiorgis Gozadinos.
// strophe.plugins is distributed under the MIT license.
// http://github.com/ggozad/strophe.plugins
// Helpers for dealing with
// [XEP-0004: Data Forms](http://xmpp.org/extensions/xep-0004.html)
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', 'underscore', 'strophe'], function ($, _, wrapper) {
// Also create a global in case some scripts
// that are loaded still are looking for
// a global even when an AMD loader is in use.
return (wrapper.Strophe.x = factory($, _, wrapper.Strophe, wrapper.$build));
});
} else {
// Browser globals
root.Strophe.x = factory(root.$, root._, root.Strophe, root.$build);
}
}(this,function ($, _, Strophe, $build) {
// **Option** contructor
var Option = function (opts) {
opts = opts || {};
this.value = opts.value || '';
this.label = opts.label;
};
// **Option.prototype** **toXMl** and **toJSON** extensions.
_.extend(Option.prototype, {
toXML: function () {
var el, attrs = {};
if (this.label) attrs.label = this.label;
el = $build('option', attrs)
.c('value').t(this.value.toString());
return el.tree();
},
toJSON: function () {
return {
label: this.label,
value: this.value
};
}
});
// Creates an **Option** from XML
Option.fromXML = function (xml) {
return new Option({
label: ($(xml)).attr('label'),
value: ($(xml)).text()
});
};
// **Field** constructor
var Field = function (opts) {
opts = opts || {};
this.type = opts.type || 'text-single';
this['var'] = opts['var'] || 'undefined';
this.desc = opts.desc;
this.label = opts.label;
this.required = opts.required === true || opts.required === 'true' || false;
this.options = opts.options || [];
this.values = opts.values || [];
if (opts.value) this.values.push(opts.value);
return this;
};
// **Field.prototype** **toXMl** and **toJSON** extensions.
_.extend(Field.prototype, {
toXML: function() {
var attrs = {
type: this.type,
'var': this['var']
};
if (this.label) attrs.label = this.label;
var xml = $build('field', attrs);
if (this.desc) xml.c('desc').t(this.desc).up();
if (this.required) xml.c('required').up();
_.each(this.values, function (value) {
xml.c('value').t(value.toString()).up();
});
_.each(this.options, function (option) {
xml.cnode(option.toXML()).up();
});
return xml.tree();
},
toJSON: function () {
return {
type: this.type,
'var': this['var'],
desc: this.desc,
label: this.label,
required: this.required,
options: _.map(this.options, function (option) { return option.toJSON(); }),
values: this.values
};
}
});
// Creates a **Field** from XML
Field.fromXML = function (xml) {
xml = $(xml);
return new Field({
type: xml.attr('type'),
'var': xml.attr('var'),
label: xml.attr('label'),
desc: xml.find('desc').text(),
required: xml.find('required').length === 1,
options: _.map($('option', xml), function (option) { return new Option.fromXML(option);}),
values: _.map($('>value', xml), function (value) { return $(value).text(); })
});
};
// **Form** constructor
var Form = function (opts) {
opts = opts || {};
this.type = opts.type || 'form';
this.fields = opts.fields || [];
this.title = opts.title;
this.instructions = opts.instructions;
return this;
};
// **Form.prototype** **toXMl** and **toJSON** extensions.
_.extend(Form.prototype, {
toXML: function () {
var xml = $build('x', {
xmlns: 'jabber:x:data',
type: this.type
});
if (this.title) xml.c('title').t(this.title.toString()).up();
if (this.instructions) xml.c('instructions').t(this.instructions.toString()).up();
_.each(this.fields, function (field) { xml.cnode(field.toXML()).up(); });
return xml.tree();
},
toJSON: function () {
return {
type: this.type,
title: this.title,
instructions: this.instructions,
fields: _.map(this.fields, function (field) { return field.toJSON(); })
};
}
});
// Creates a **Form** from XML
Form.fromXML = function (xml) {
xml = $(xml);
return new Form({
type: xml.attr('type'),
title: xml.find('title').text(),
instructions: xml.find('instructions').text(),
fields: _.map($('>field', xml), function (field) { return new Field.fromXML(field); })
});
};
// Attach to **Strophe** as `x`. No need for a plugin.
return {
Form: Form,
Field: Field,
Option: Option
};
}));