changeset 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 853dcbe8f06f
files index.xhtml record.js script2.js sxe-document.js
diffstat 4 files changed, 72 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/index.xhtml
+++ b/index.xhtml
@@ -17,6 +17,8 @@
   <script type="text/javascript" src="config.js"></script>
 </head>
 <body>
+  <div id="state"/>
+  <button id="host">Host</button>
   <form id="toolbar">
     <dl>
       <dt> edit: </dt>
@@ -96,7 +98,7 @@
   </form>
   <pre id="breadcrumb"> </pre>
 
-  <section contenteditable="true">
+  <section id="document" contenteditable="true">
     <h2> Go ahead, edit away! </h2>
     <p> Here's a typical paragraph element </p>
     <ol><li> and now a list </li><li> with only      </li><li> three items    </li></ol>
--- a/record.js
+++ b/record.js
@@ -1,5 +1,26 @@
 '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?
@@ -31,7 +52,7 @@ Record.prototype = {
     }
   },
   toDOM: function(records, dom) {
-    var element;
+    var element = null;
     if (this.parent)
       dom = records[this.parent].dom;
 
@@ -61,6 +82,9 @@ Record.prototype = {
       case 'comment':
         element = document.createComment(this.chdata);
         break;
+      default:
+        console.log('BIG WARNING! Element type not supported.');
+        return;
     }
 
     dom.appendChild(element);
--- a/script2.js
+++ b/script2.js
@@ -109,15 +109,17 @@ var initiate = function(jid, sid) {
 };
 
 var accept = function(sid) {
-  var accept = "<iq to='" + documents[sid].initiator + "' type='set'>" +
+  var doc = documents[sid];
+  doc.empty();
+  var accept = "<iq to='" + doc.initiator + "' type='set'>" +
                  "<jingle xmlns='" + Lightstring.NS.jingle.main + "'" +
                         " action='session-accept'" +
-                        " initiator='" + documents[sid].initiator + "'" +
+                        " initiator='" + doc.initiator + "'" +
                         " sid='" + sid + "'>" +
                    "<content creator='initiator' name='" + documents[sid].name + "'>" +
                      "<description xmlns='" + Lightstring.NS.jingle.apps.xhtml + "'/>" +
                      "<transport xmlns='" + Lightstring.NS.jingle.transports.sxe + "'>" +
-                       "<host>" + documents[sid].host + "</host>" +
+                       "<host>" + doc.host + "</host>" +
                      "</transport>" +
                    "</content>" +
                  "</jingle>" +
@@ -293,6 +295,9 @@ conn.on('message/' + Lightstring.NS.sxe 
       conn.send(message);
       break;
     case 'accept-state':
+      var initialState = doc.createState([]).map(function(element) {
+        return Lightstring.DOM2XML(element);
+      }).join('');
       var message = "<message to='" + from + "'" +
                             " type='" + type + "'>" +
                       "<sxe xmlns='" + Lightstring.NS.sxe + "'" +
@@ -300,7 +305,7 @@ conn.on('message/' + Lightstring.NS.sxe 
                           " session='" + sid + "'>" +
                         "<state>" +
                           "<document-begin prolog='" + doc.prolog + "'/>" +
-                          //TODO: support non-empty documents.
+                           initialState +
                           "<document-end last-sender='' last-id=''/>" +
                         "</state>" +
                       "</sxe>" +
--- a/sxe-document.js
+++ b/sxe-document.js
@@ -1,5 +1,26 @@
 '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 Document = function(initiator, name, host, domId, prolog) {
   this.initiator = initiator;
   this.name = name;
@@ -81,13 +102,13 @@ Document.prototype = {
 
       switch (change) {
         case 'new':
-          doc.add(jid, child);
+          this.add(jid, child);
           break;
         case 'set':
-          doc.update(jid, child);
+          this.update(jid, child);
           break;
         case 'remove':
-          doc.remove(jid, child);
+          this.remove(jid, child);
           break;
         case 'document-end':
           this.state = 'started';
@@ -95,8 +116,11 @@ Document.prototype = {
       }
     }
   },
-  createState: function(root, parent, state) {
+  createState: function(state, root, parent) {
+    if (!root)
+      root = this.dom;
     var children = root.childNodes;
+
     for (var i = 0; i < children.length; i++) {
       var child = children[i];
       var element = document.createElementNS(Lightstring.NS.sxe, 'new');
@@ -116,6 +140,7 @@ Document.prototype = {
           //TODO: move that elsewhere, or make it prettier.
           var convertAttr = function(attr) {
             var element = document.createElementNS(Lightstring.NS.sxe, 'new');
+            element.setAttributeNS(null, 'type', 'attr');
             var arid = Lightstring.newId('GUID');
             element.setAttributeNS(null, 'rid', arid);
             element.setAttributeNS(null, 'parent', rid);
@@ -129,7 +154,7 @@ Document.prototype = {
           for (var j = 0; j < child.attributes.length; j++)
             convertAttr(child.attributes[j]);
 
-          state = this.createState(child, rid, state);
+          state = this.createState(state, child, rid);
           break;
 
         case 3:
@@ -153,5 +178,10 @@ Document.prototype = {
       }
     }
     return state;
+  },
+  empty: function() {
+    var children = this.dom.childNodes;
+    for (var i = children.length - 1; i >= 0; i--)
+      this.dom.removeChild(children[i]);
   }
 };