-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrophe.private.js
192 lines (148 loc) · 3.91 KB
/
strophe.private.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
/**
* This plugin is distributed under the terms of the MIT licence.
* Please see the LICENCE file for details.
* Copyright (c) Markus Kohlhase, 2011
*/
/**
* File: strophe.private.js
* A Strophe plugin for XMPP Private XML Storage ( http://xmpp.org/extensions/xep-0049.html )
*/
Strophe.addConnectionPlugin('private',
{
_connection: null,
// called by the Strophe.Connection constructor
init: function( conn ){
this._connection = conn;
Strophe.addNamespace( 'PRIVATE', "jabber:iq:private" );
},
/**
* Function: set
*
* Parameters:
* (String) tag - the tag name
* (String) ns - the namespace
* (XML) data - the data you want to save
* (Function) success - Callback function on success
* (Function) error - Callback function on error
*/
set: function( tag, ns, data, success, error ){
var id = this._connection.getUniqueId('saveXML');
var iq = $iq({ type:'set', id: id })
.c('query', { xmlns: Strophe.NS.PRIVATE } )
.c( tag, { xmlns: ns } );
var d = this._transformData( data );
if( d ){
iq.cnode( d );
};
this._connection.sendIQ( iq, success, error );
},
/**
* Function: get
*
* Parameters:
* (String) tag - the tag name
* (String) ns - the namespace
* (Function) success - Callback function on success
* (Function) error - Callback function on error
*/
get: function( tag, ns, success, error ){
var id = this._connection.getUniqueId('loadXML');
var iq = $iq({ type:'get', id: id })
.c('query', { xmlns: Strophe.NS.PRIVATE } )
.c(tag, { xmlns: ns } );
this._connection.sendIQ( iq, function( iq ){
var data = iq;
for( var i=0; i<3; i++ ){
data = data.childNodes[0];
if( !data ){
break;
}
}
success( data, iq );
}, error );
},
/**
* PrivateFunction: _transformData
*/
_transformData: function( c ){
switch( typeof( c ) ){
case "number":
case "boolean": return Strophe.xmlTextNode( c + '' );
case "string":
var dom = this._textToXml( c );
if( dom !== null ){
return dom
} else {
return Strophe.xmlTextNode( c + '' );
}
case "object":
if( this._isNode(c) || this._isElement(c) ){
return c;
}
}
return null;
},
/**
* PrivateFunction: _textToXml
*
* Parameters:
* (String) text - XML String
*
* Returns:
* (Object) dom - DOM Object
*/
_textToXml: function( text ){
var doc = null;
if( window.DOMParser ){
var parser = new DOMParser();
doc = parser.parseFromString( text, 'text/xml');
} else if( window.ActiveXObject ){
doc = new ActiveXObject("MSXML2.DOMDocument");
doc.async = false;
doc.loadXML(text);
} else{
throw{
type: 'Parse error',
message: "No DOM parser object found."
};
}
var error = doc.getElementsByTagName("parsererror");
if( error.length > 0 ){
return null;
}
var node = document.importNode( doc.documentElement, true );
return node;
},
/**
* PrivateFunction: _isNode
*
* Parameters:
* ( Object ) obj - The object to test
*
* Returns:
* True if it is a DOM node
*/
_isNode: function( obj ){
if( typeof( Node ) === "object" ){
return ( obj instanceof Node );
}else{
return ( typeof( obj ) === "object" && typeof( obj.nodeType ) === "number" && typeof( obj.nodeName ) === "string");
}
},
/**
* PrivateFunction: _isElement
*
* Parameters:
* ( Object ) obj - The object to test
*
* Returns:
* True if it is a DOM element.
*/
_isElement: function( obj ){
if( typeof( HTMLElement ) === "object"){
return ( obj instanceof HTMLElement ); //DOM2
} else{
return ( typeof( obj ) === "object" && obj.nodeType === 1 && typeof( obj.nodeName ) === "string" );
}
}
});