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

'use strict';

/** Copyright (c) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to
  * deal in the Software without restriction, including without limitation the
  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  * sell copies of the Software, and to permit persons to whom the Software is
  * furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  * IN THE SOFTWARE.
  */

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 = null;
    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;
      default:
        console.log('BIG WARNING! Element type not supported.');
        return;
    }

    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;
    }
  }
};