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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 'use strict';
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 var Record = function(jid, child) {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 this.creator = jid;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 this.creationDate = new Date; //FIXME: non-standard?
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 this.update(jid, child);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 Record.prototype = {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 update: function(jid, child) {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 this.rid = child.getAttributeNS(null, 'rid') || this.rid;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 this.type = child.getAttributeNS(null, 'type') || this.type;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 this.version = child.getAttributeNS(null, 'version') || this.version || 0;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 this.parent = child.getAttributeNS(null, 'parent') || this.parent;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 this.primaryWeight = child.getAttributeNS(null, 'primary-weight') || this.primaryWeight || 0;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 this.lastModifiedDate = new Date; //FIXME: non-standard?
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 this.lastModifiedBy = jid; // only in 10.5.1
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 if (this.type === 'element' || this.type === 'attr') {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 this.ns = child.getAttributeNS(null, 'ns') || this.ns;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 this.name = child.getAttributeNS(null, 'name') || this.name;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 if (this.type === 'text' || this.type === 'attr' || this.type === 'comment')
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 this.chdata = child.getAttributeNS(null, 'chdata') || this.chdata;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 if (this.type === 'processinginstruction') {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 this.pitarget = child.getAttributeNS(null, 'pitarget') || this.pitarget;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 this.pidata = child.getAttributeNS(null, 'pidata') || this.pidata;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 },
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 if (this.parent)
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 dom = records[this.parent].dom;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 switch (this.type) {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 case 'processinginstruction':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 element = document.createProcessingInstruction(this.pitarget, this.pidata);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 case 'element':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if (this.ns)
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 element = document.createElementNS(this.ns, this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 else
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 element = document.createElement(this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 case 'attr':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 if (this.ns === 'xml') //FIXME: it’s ugly.
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 this.dom = document.createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:' + this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 else if (this.ns)
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 this.dom = document.createAttributeNS(this.ns, this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 else
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 this.dom = document.createAttribute(this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 this.dom.value = this.chdata;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 dom.setAttributeNodeNS(this.dom);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 return;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 case 'text':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 element = document.createTextNode(this.chdata);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 case 'comment':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 element = document.createComment(this.chdata);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
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
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 dom.appendChild(element);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 this.dom = element;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 },
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 remove: function(records) {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 if (this.parent)
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 var parentNode = records[this.parent].dom;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 switch (this.type) {
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 case 'processinginstruction':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 //TODO
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 case 'element':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 if (this.dom.children.length)
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 return; //TODO: use a better error reporting system.
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 parentNode.removeChild(this.dom);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 case 'attr':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 parentNode.removeAttributeNS(this.ns, this.name);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 case 'text':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 case 'comment':
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 parentNode.removeChild(this.node);
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 break;
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 }
03ef53b969bd Add XMPP support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115 };