Mercurial > eldonilo > barbecue
annotate record.js @ 6:24aa8dccb170
Make XMPP actually work.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 31 Jan 2012 15:59:28 +0100 |
parents | 03ef53b969bd |
children |
rev | line source |
---|---|
5 | 1 'use strict'; |
2 | |
6
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
3 /** Copyright (c) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
4 * |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
5 * Permission is hereby granted, free of charge, to any person obtaining a copy |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
6 * of this software and associated documentation files (the "Software"), to |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
7 * deal in the Software without restriction, including without limitation the |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
9 * sell copies of the Software, and to permit persons to whom the Software is |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
10 * furnished to do so, subject to the following conditions: |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
11 * |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included in |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
13 * all copies or substantial portions of the Software. |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
14 * |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
21 * IN THE SOFTWARE. |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
22 */ |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
23 |
5 | 24 var Record = function(jid, child) { |
25 this.creator = jid; | |
26 this.creationDate = new Date; //FIXME: non-standard? | |
27 this.update(jid, child); | |
28 } | |
29 | |
30 Record.prototype = { | |
31 update: function(jid, child) { | |
32 this.rid = child.getAttributeNS(null, 'rid') || this.rid; | |
33 this.type = child.getAttributeNS(null, 'type') || this.type; | |
34 this.version = child.getAttributeNS(null, 'version') || this.version || 0; | |
35 this.parent = child.getAttributeNS(null, 'parent') || this.parent; | |
36 this.primaryWeight = child.getAttributeNS(null, 'primary-weight') || this.primaryWeight || 0; | |
37 | |
38 this.lastModifiedDate = new Date; //FIXME: non-standard? | |
39 this.lastModifiedBy = jid; // only in 10.5.1 | |
40 | |
41 if (this.type === 'element' || this.type === 'attr') { | |
42 this.ns = child.getAttributeNS(null, 'ns') || this.ns; | |
43 this.name = child.getAttributeNS(null, 'name') || this.name; | |
44 } | |
45 | |
46 if (this.type === 'text' || this.type === 'attr' || this.type === 'comment') | |
47 this.chdata = child.getAttributeNS(null, 'chdata') || this.chdata; | |
48 | |
49 if (this.type === 'processinginstruction') { | |
50 this.pitarget = child.getAttributeNS(null, 'pitarget') || this.pitarget; | |
51 this.pidata = child.getAttributeNS(null, 'pidata') || this.pidata; | |
52 } | |
53 }, | |
54 toDOM: function(records, dom) { | |
6
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
55 var element = null; |
5 | 56 if (this.parent) |
57 dom = records[this.parent].dom; | |
58 | |
59 switch (this.type) { | |
60 case 'processinginstruction': | |
61 element = document.createProcessingInstruction(this.pitarget, this.pidata); | |
62 break; | |
63 case 'element': | |
64 if (this.ns) | |
65 element = document.createElementNS(this.ns, this.name); | |
66 else | |
67 element = document.createElement(this.name); | |
68 break; | |
69 case 'attr': | |
70 if (this.ns === 'xml') //FIXME: it’s ugly. | |
71 this.dom = document.createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:' + this.name); | |
72 else if (this.ns) | |
73 this.dom = document.createAttributeNS(this.ns, this.name); | |
74 else | |
75 this.dom = document.createAttribute(this.name); | |
76 this.dom.value = this.chdata; | |
77 dom.setAttributeNodeNS(this.dom); | |
78 return; | |
79 case 'text': | |
80 element = document.createTextNode(this.chdata); | |
81 break; | |
82 case 'comment': | |
83 element = document.createComment(this.chdata); | |
84 break; | |
6
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
85 default: |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
86 console.log('BIG WARNING! Element type not supported.'); |
24aa8dccb170
Make XMPP actually work.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
87 return; |
5 | 88 } |
89 | |
90 dom.appendChild(element); | |
91 this.dom = element; | |
92 }, | |
93 remove: function(records) { | |
94 if (this.parent) | |
95 var parentNode = records[this.parent].dom; | |
96 | |
97 switch (this.type) { | |
98 case 'processinginstruction': | |
99 //TODO | |
100 break; | |
101 case 'element': | |
102 if (this.dom.children.length) | |
103 return; //TODO: use a better error reporting system. | |
104 parentNode.removeChild(this.dom); | |
105 break; | |
106 case 'attr': | |
107 parentNode.removeAttributeNS(this.ns, this.name); | |
108 break; | |
109 case 'text': | |
110 case 'comment': | |
111 parentNode.removeChild(this.node); | |
112 break; | |
113 } | |
114 } | |
115 }; |