Author: jmorliaguet
Date: Mon Feb  6 21:49:16 2006
New Revision: 2310

Modified:
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py
   
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt
Log:

- simpler code (only the last 10 messages are saved). 

- the user's name is stored in a cookie



Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py
      (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/browser.py
      Mon Feb  6 21:49:16 2006
@@ -8,6 +8,8 @@
 
 cache = RAMCache()
 
+MAX_MESSAGES = 10
+
 class Views(BrowserView):
 
     def __init__(self, context, request):
@@ -19,22 +21,23 @@
         return json.write(data)
 
     def setData(self, data):
-        time.sleep(1)
         data = json.read(data)
 
         current_data = cache.query('data', {}, {})
         messages = current_data.get('messages', [])
+
         input = data.get('input')
         if input:
-            ip = self.request.get('REMOTE_ADDR')
-            messages.append({'text': input, 'ip': ip});
+            user = data.get('user')
+            date = time.strftime("%H:%M:%S", time.gmtime())
+            messages.insert(0, {'text': input, 'user': user, 'date': date});
+            if len(messages) > MAX_MESSAGES:
+                messages.pop()
             del data['input']
 
         data['status'] = ''
         data['messages'] = messages
-        data['lastupdated'] = int(time.time())
 
         cache.set(data, 'data', {})
-        print data
         return json.write(data)
 

Modified: 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt
==============================================================================
--- 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt
        (original)
+++ 
cpsskins/branches/jmo-perspectives/ui/framework/tests/zope3/functional/chat/cpsskins_chat.pt
        Mon Feb  6 21:49:16 2006
@@ -21,21 +21,35 @@
       background-color: #cd9;
     }
 
+    form {
+      padding: 1em 0;
+    }
+
     input {
-      margin-bottom: 0.5em;
-      margin-top: 0.5em;
-      border-width: 1px;
-      border-style: solid;
-      border-color: #000 #999 #999 #000;
-      width: 400px;
-      font: 1.5em arial, sans-serif;
+      border: 1px solid #000;
+      width: 200px;
+      padding: 0.2em;
+      margin: 0 1em 0 0;
+      font: 1.3em verdana, arial, sans-serif;
+    }
+
+    input:focus {
+      background-color: #efc;
+    }
+
+    button {
+      background-color: #efc;
+      border: 1px solid #009;
+      padding: 0.2em;
+      margin: 0 1em 0 0;
+      font: 1.2em arial, sans-serif;
     }
 
     div.message {
       border: 1px solid #999;
-      background-color: #efc;
+      background-color: #ffc;
       padding: 0.5em;
-      margin: 0.5em;
+      margin: 0.2em;
       font: 1.3em verdana, arial, sans-serif;
     }
 
@@ -80,17 +94,15 @@
       render: function(data) {
         var widget = this.widget;
         widget.innerHTML = '';
-
         var messages = data.messages;
 
-        $A(messages).reverse(false).each(function(m, index) {
-          if (index > 5) return;
-          widget.appendChild(
-            Canvas.createNode({
-              tag: "div",
-              classes: "message",
-              text: '[' + m.ip + '] ' + m.text
-          }));
+        $A(messages).each(function(m, index) {
+          var message = Canvas.createNode({
+            tag: "div",
+            classes: "message",
+          });
+          message.innerHTML = '[' + m.date + '] ' + m.user + ': ' + m.text
+          widget.appendChild(message);
         });
 
       }
@@ -102,7 +114,6 @@
 
       setup: function() {
         this.changeEvent = this.changeEvent.bindAsEventListener(this);
-        Event.observe(this.widget, "change", this.changeEvent);
       },
 
       render: function(data) {
@@ -119,19 +130,49 @@
           widget.appendChild(statusbox);
         }
 
+        var form = Canvas.createNode({
+          tag: "form"
+        });
+        widget.appendChild(form);
+
         var input = Canvas.createNode({
           tag: "input",
           attributes: {
+            "name": "input",
             "type": "text"
           }
         });
-        widget.appendChild(input);
+        form.appendChild(input);
+
+        var user = Canvas.createNode({
+          tag: "input",
+          attributes: {
+            "name": "user",
+            "type": "text",
+            "value": data.user
+          }
+        });
+        form.appendChild(user);
+
+        var button = Canvas.createNode({
+          tag: "button",
+          attributes: {
+            "type": "submit"
+          },
+          text: "SEND!"
+        });
+        form.appendChild(button);
+        Event.observe(form, "submit", this.changeEvent);
+        Form.focusFirstElement(form);
       },
 
       changeEvent: function(e) {
         var target = Event.element(e);
-        var value = target.value;
-        this.handleAction(target, 'change', value);
+        var values = {};
+        $A(Form.getInputs(target)).each(function(i) {
+          values[i.name] = Form.Element.getValue(i);
+        })
+        this.handleAction(target, 'send', values);
       }
 
     });
@@ -139,10 +180,11 @@
     /* Controller */
 
     function changeContent(model, context, value) {
-      model.setData({'status': 'Sending message ...', 'input': value});
+      value['status'] = 'Sending message ...';
+      model.setData(value);
     }
 
-    CPSSkins.registerHandlers({'changeContent': changeContent});
+    CPSSkins.registerHandlers({'submitContent': changeContent});
 
   </script>
 
@@ -154,7 +196,7 @@
   <ins class="controller">
   {"id": "controller",
    "handlers": {
-     "change": "changeContent"
+     "send": "submitContent"
   }}
   </ins>
 
@@ -187,6 +229,18 @@
     }}
     </ins>
 
+    <!-- current user -->
+    <ins class="model">
+    {"id": "user",
+     "data": {
+      "user": ""
+     },
+     "storage": {
+       "type": "local"
+       }
+    }}
+    </ins>
+
     <!-- input area / server response -->
     <ins class="model">
     {"id": "input",
@@ -221,11 +275,12 @@
     {"id": "form",
      "data": {
       "status": "",
-      "input": ""
+      "input": "",
+      "user": ""
      },
      "storage": {
        "type": "compound",
-       "partitions": ["status", "input"]
+       "partitions": ["status", "input", "user"]
        }
     }}
     </ins>
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to