Mercurial > eldonilo > lightstring
annotate stanza.js @ 108:5cb4733c5189
many api changes
author | Sonny Piers <sonny@fastmail.net> |
---|---|
date | Fri, 13 Jul 2012 15:26:18 +0200 |
parents | 704ce44c1a22 |
children |
rev | line source |
---|---|
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
1 'use strict'; |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
2 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
3 /** |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
4 Copyright (c) 2011, Sonny Piers <sonny at fastmail dot net> |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
5 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
6 Permission to use, copy, modify, and/or distribute this software for any |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
7 purpose with or without fee is hereby granted, provided that the above |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
8 copyright notice and this permission notice appear in all copies. |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
9 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
17 */ |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
18 |
108 | 19 (function() { |
20 | |
21 if (typeof define !== 'undefined') { | |
22 define(function() { | |
23 return { | |
24 'stanza': Stanza, | |
25 'presence': Presence, | |
26 'iq': IQ, | |
27 'message': Message, | |
28 'doc': doc, | |
29 }; | |
30 }); | |
31 } | |
32 else { | |
33 Lightstirng.Stanza = Stanza; | |
34 Lightstirng.Presence = Presence; | |
35 Lightstirng.IQ = IQ; | |
36 Lightstirng.Message = Message; | |
37 Lightstirng.doc = doc; | |
38 } | |
39 | |
40 /** | |
41 * @private | |
42 */ | |
43 var doc = document.implementation.createDocument(null, 'dummy', null); | |
44 /** | |
45 * @private | |
46 */ | |
47 var parser = new DOMParser(); | |
48 /** | |
49 * @private | |
50 */ | |
51 var serializer = new XMLSerializer(); | |
52 /** | |
53 * @function Transforms a XML string to a DOM object. | |
54 * @param {String} aString XML string. | |
55 * @return {Object} Domified XML. | |
56 */ | |
57 var parse = function(aString) { | |
58 var el = parser.parseFromString(aString, 'text/xml').documentElement; | |
59 if (el.tagName === 'parsererror') | |
60 ;//do something | |
61 return el; | |
62 }; | |
63 /** | |
64 * @function Transforms a DOM object to a XML string. | |
65 * @param {Object} aString DOM object. | |
66 * @return {String} Stringified DOM. | |
67 */ | |
68 var serialize = function(aElement) { | |
69 var string = null; | |
70 try { | |
71 string = serializer.serializeToString(aElement); | |
72 } | |
73 catch (e) { | |
74 //TODO: error | |
75 } | |
76 finally { | |
77 return string; | |
78 }; | |
79 }; | |
80 | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
81 |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
82 /** |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
83 * @constructor Creates a new Stanza object. |
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
84 * @param {String|Object} [aStanza] The XML or DOM content of the stanza |
108 | 85 */ |
86 var Stanza = function(aStanza) { | |
87 this.createEl(aStanza); | |
88 }; | |
89 /** | |
90 * @constructor Creates a new Message stanza object. | |
91 * @param {String|Object} [aStanza] The XML or DOM content of the stanza | |
92 */ | |
93 var Message = function(aStanza) { | |
94 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element))) | |
95 aStanza.name = 'message'; | |
96 this.createEl(aStanza); | |
97 }; | |
98 Message.prototype = Stanza.prototype; | |
99 /** | |
100 * @constructor Creates a new IQ stanza object. | |
101 * @param {String|Object} [aStanza] The XML or DOM content of the stanza | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
102 */ |
108 | 103 var IQ = function(aStanza) { |
104 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element))) | |
105 aStanza.name = 'iq'; | |
106 this.createEl(aStanza); | |
107 }; | |
108 IQ.prototype = Stanza.prototype; | |
109 /** | |
110 * @constructor Creates a new Presence stanza object. | |
111 * @param {String|Object} [aStanza] The XML or DOM content of the stanza | |
112 */ | |
113 var Presence = function(aStanza) { | |
114 if ((typeof aStanza === 'object') && (!(aStanza instanceof Element))) | |
115 aStanza.name = 'presence'; | |
116 this.createEl(aStanza); | |
117 }; | |
118 Presence.prototype = Stanza.prototype; | |
119 Stanza.prototype.createEl = function(aStanza) { | |
120 if (typeof aStanza === 'string') { | |
121 this.el = parse(aStanza); | |
122 } | |
106 | 123 else if (aStanza instanceof Element) |
124 this.el = aStanza; | |
108 | 125 else if (typeof aStanza === 'object') { |
126 var el = doc.createElement(aStanza.name); | |
127 this.el = el; | |
128 delete aStanza.name; | |
129 for (var i in aStanza) { | |
130 this[i] = aStanza[i]; | |
131 } | |
132 } | |
106 | 133 else |
134 this.el = null;//TODO error | |
29
1e6d2ca2daae
Adds a Lightstring.Stanza object and use it.
Sonny Piers <sonny.piers@gmail.com>
parents:
diff
changeset
|
135 }; |
108 | 136 Stanza.prototype.toString = function() { |
137 return serialize(this.el); | |
138 }; | |
139 Stanza.prototype.reply = function(aProps) { | |
140 var props = aProps || {}; | |
141 | |
142 props.name = this.name; | |
143 var reply = new Stanza(props); | |
144 | |
145 if (this.from) | |
146 reply.to = this.from; | |
147 | |
148 | |
149 if (reply.name !== 'iq') | |
150 return reply; | |
151 | |
152 if (this.id) | |
153 reply.id = this.id; | |
154 | |
155 reply.type = 'result'; | |
156 | |
157 return reply; | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
158 }; |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
159 |
108 | 160 //from attribute |
161 Object.defineProperty(Stanza.prototype, "from", { | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
162 get : function(){ |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
163 return this.el.getAttribute('from'); |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
164 }, |
108 | 165 set : function(aString) { |
166 this.el.setAttribute('from', aString); | |
167 }, | |
168 enumerable : true, | |
169 configurable : true | |
170 }); | |
171 //stanza tag name | |
172 Object.defineProperty(Stanza.prototype, "name", { | |
173 get : function(){ | |
174 return this.el.localName; | |
175 }, | |
176 //FIXME | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
177 // set : function(newValue){ bValue = newValue; }, |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
178 enumerable : true, |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
179 configurable : true |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
180 }); |
108 | 181 //id attribute |
182 Object.defineProperty(Stanza.prototype, "id", { | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
183 get : function(){ |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
184 return this.el.getAttribute('id'); |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
185 }, |
108 | 186 set : function(aString) { |
187 this.el.setAttribute('id', aString); | |
188 }, | |
189 enumerable : true, | |
190 configurable : true | |
191 }); | |
192 //to attribute | |
193 Object.defineProperty(Stanza.prototype, "to", { | |
194 get : function(){ | |
195 return this.el.getAttribute('to'); | |
196 }, | |
197 set : function(aString) { | |
198 this.el.setAttribute('to', aString); | |
199 }, | |
200 enumerable : true, | |
201 configurable : true | |
202 }); | |
203 //type attribute | |
204 Object.defineProperty(Stanza.prototype, "type", { | |
205 get : function(){ | |
206 return this.el.getAttribute('type'); | |
207 }, | |
208 set : function(aString) { | |
209 this.el.setAttribute('type', aString); | |
210 }, | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
211 enumerable : true, |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
212 configurable : true |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
213 }); |
108 | 214 //body |
215 Object.defineProperty(Stanza.prototype, "body", { | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
216 get : function(){ |
108 | 217 var bodyEl = this.el.querySelector('body').textContent; |
218 if (!bodyEl) | |
219 return null; | |
220 else | |
221 return bodyEl.textContent; | |
222 }, | |
223 set : function(aString) { | |
224 var bodyEl = this.el.querySelector('body'); | |
225 if (!bodyEl) { | |
226 bodyEl = doc.createElement('body'); | |
227 bodyEl = this.el.appendChild(bodyEl); | |
228 } | |
229 bodyEl.textContent = aString; | |
230 }, | |
107
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
231 enumerable : true, |
704ce44c1a22
Add several properties to the Lightstring.Stanza object
Sonny Piers <sonny@fastmail.net>
parents:
106
diff
changeset
|
232 configurable : true |
108 | 233 }); |
234 //subject | |
235 Object.defineProperty(Stanza.prototype, "subject", { | |
236 get : function(){ | |
237 var subjectEl = this.el.querySelector('subject').textContent; | |
238 if (!subjectEl) | |
239 return null; | |
240 else | |
241 return subjectEl.textContent; | |
242 }, | |
243 set : function(aString) { | |
244 var subjectEl = this.el.querySelector('subject'); | |
245 if (!subjectEl) { | |
246 subjectEl = doc.createElement('subject'); | |
247 subjectEl = this.el.appendChild(subjectEl); | |
248 } | |
249 subjectEl.textContent = aString; | |
250 }, | |
251 enumerable : true, | |
252 configurable : true | |
253 }); | |
254 Stanza.prototype.replyWithSubscribed = function(aProps) { | |
255 var reply = this.reply(aProps); | |
256 reply.type = 'subscribed'; | |
257 | |
258 return reply; | |
259 }; | |
260 | |
261 })(); |