view 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
line wrap: on
line source

'use strict';

var Record = function(jid, child) {
  this.creator = jid;
  this.creationDate = new Date; //FIXME: non-standard?
  this.update(jid, child);
}

Record.prototype = {
  update: function(jid, child) {
    this.rid = child.getAttributeNS(null, 'rid') || this.rid;
    this.type = child.getAttributeNS(null, 'type') || this.type;
    this.version = child.getAttributeNS(null, 'version') || this.version || 0;
    this.parent = child.getAttributeNS(null, 'parent') || this.parent;
    this.primaryWeight = child.getAttributeNS(null, 'primary-weight') || this.primaryWeight || 0;

    this.lastModifiedDate = new Date; //FIXME: non-standard?
    this.lastModifiedBy = jid; // only in 10.5.1

    if (this.type === 'element' || this.type === 'attr') {
      this.ns = child.getAttributeNS(null, 'ns') || this.ns;
      this.name = child.getAttributeNS(null, 'name') || this.name;
    }

    if (this.type === 'text' || this.type === 'attr' || this.type === 'comment')
      this.chdata = child.getAttributeNS(null, 'chdata') || this.chdata;

    if (this.type === 'processinginstruction') {
      this.pitarget = child.getAttributeNS(null, 'pitarget') || this.pitarget;
      this.pidata = child.getAttributeNS(null, 'pidata') || this.pidata;
    }
  },
  toDOM: function(records, dom) {
    var element;
    if (this.parent)
      dom = records[this.parent].dom;

    switch (this.type) {
      case 'processinginstruction':
        element = document.createProcessingInstruction(this.pitarget, this.pidata);
        break;
      case 'element':
        if (this.ns)
          element = document.createElementNS(this.ns, this.name);
        else
          element = document.createElement(this.name);
        break;
      case 'attr':
        if (this.ns === 'xml') //FIXME: it’s ugly.
          this.dom = document.createAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:' + this.name);
        else if (this.ns)
          this.dom = document.createAttributeNS(this.ns, this.name);
        else
          this.dom = document.createAttribute(this.name);
        this.dom.value = this.chdata;
        dom.setAttributeNodeNS(this.dom);
        return;
      case 'text':
        element = document.createTextNode(this.chdata);
        break;
      case 'comment':
        element = document.createComment(this.chdata);
        break;
    }

    dom.appendChild(element);
    this.dom = element;
  },
  remove: function(records) {
    if (this.parent)
      var parentNode = records[this.parent].dom;

    switch (this.type) {
      case 'processinginstruction':
        //TODO
        break;
      case 'element':
        if (this.dom.children.length)
          return; //TODO: use a better error reporting system.
        parentNode.removeChild(this.dom);
        break;
      case 'attr':
        parentNode.removeAttributeNS(this.ns, this.name);
        break;
      case 'text':
      case 'comment':
        parentNode.removeChild(this.node);
        break;
    }
  }
};