comparison record.js @ 5:03ef53b969bd

Add XMPP support.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 31 Jan 2012 01:09:41 +0100
parents
children 24aa8dccb170
comparison
equal deleted inserted replaced
4:95c6fa5f4715 5:03ef53b969bd
1 'use strict';
2
3 var Record = function(jid, child) {
4 this.creator = jid;
5 this.creationDate = new Date; //FIXME: non-standard?
6 this.update(jid, child);
7 }
8
9 Record.prototype = {
10 update: function(jid, child) {
11 this.rid = child.getAttributeNS(null, 'rid') || this.rid;
12 this.type = child.getAttributeNS(null, 'type') || this.type;
13 this.version = child.getAttributeNS(null, 'version') || this.version || 0;
14 this.parent = child.getAttributeNS(null, 'parent') || this.parent;
15 this.primaryWeight = child.getAttributeNS(null, 'primary-weight') || this.primaryWeight || 0;
16
17 this.lastModifiedDate = new Date; //FIXME: non-standard?
18 this.lastModifiedBy = jid; // only in 10.5.1
19
20 if (this.type === 'element' || this.type === 'attr') {
21 this.ns = child.getAttributeNS(null, 'ns') || this.ns;
22 this.name = child.getAttributeNS(null, 'name') || this.name;
23 }
24
25 if (this.type === 'text' || this.type === 'attr' || this.type === 'comment')
26 this.chdata = child.getAttributeNS(null, 'chdata') || this.chdata;
27
28 if (this.type === 'processinginstruction') {
29 this.pitarget = child.getAttributeNS(null, 'pitarget') || this.pitarget;
30 this.pidata = child.getAttributeNS(null, 'pidata') || this.pidata;
31 }
32 },
33 toDOM: function(records, dom) {
34 var element;
35 if (this.parent)
36 dom = records[this.parent].dom;
37
38 switch (this.type) {
39 case 'processinginstruction':
40 element = document.createProcessingInstruction(this.pitarget, this.pidata);
41 break;
42 case 'element':
43 if (this.ns)
44 element = document.createElementNS(this.ns, this.name);
45 else
46 element = document.createElement(this.name);
47 break;
48 case 'attr':
49 if (this.ns === 'xml') //FIXME: it’s ugly.
50 this.dom = document.createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:' + this.name);
51 else if (this.ns)
52 this.dom = document.createAttributeNS(this.ns, this.name);
53 else
54 this.dom = document.createAttribute(this.name);
55 this.dom.value = this.chdata;
56 dom.setAttributeNodeNS(this.dom);
57 return;
58 case 'text':
59 element = document.createTextNode(this.chdata);
60 break;
61 case 'comment':
62 element = document.createComment(this.chdata);
63 break;
64 }
65
66 dom.appendChild(element);
67 this.dom = element;
68 },
69 remove: function(records) {
70 if (this.parent)
71 var parentNode = records[this.parent].dom;
72
73 switch (this.type) {
74 case 'processinginstruction':
75 //TODO
76 break;
77 case 'element':
78 if (this.dom.children.length)
79 return; //TODO: use a better error reporting system.
80 parentNode.removeChild(this.dom);
81 break;
82 case 'attr':
83 parentNode.removeAttributeNS(this.ns, this.name);
84 break;
85 case 'text':
86 case 'comment':
87 parentNode.removeChild(this.node);
88 break;
89 }
90 }
91 };