Author: jmorliaguet
Date: Fri Feb 10 13:12:40 2006
New Revision: 2335

Added:
   cpsskins/branches/jmo-perspectives/ui/framework/ctal.js   (contents, props 
changed)
Modified:
   cpsskins/branches/jmo-perspectives/ui/framework/configure.zcml
Log:

- added the CTAL interpreter by Balazs Ree



Modified: cpsskins/branches/jmo-perspectives/ui/framework/configure.zcml
==============================================================================
--- cpsskins/branches/jmo-perspectives/ui/framework/configure.zcml      
(original)
+++ cpsskins/branches/jmo-perspectives/ui/framework/configure.zcml      Fri Feb 
10 13:12:40 2006
@@ -11,19 +11,23 @@
       name="cpsskins.css" file="cpsskins.css"
       layer="cpsskins" />
 
-
   <!-- Utilities, libraries -->
   <resource
       name="prototype.js" file="prototype.js"
       layer="cpsskins" />
 
+  <!-- JSON serializer -->
   <resource
       name="json.js" file="json.js"
       layer="cpsskins" />
 
+  <!-- client-side TAL interpreter -->
+  <resource
+      name="ctal.js" file="ctal.js"
+      layer="cpsskins" />
 
-  <!-- Testing -->
 
+  <!-- Testing -->
   <resource
       name="unittest.js" file="tests/unittest.js"
       layer="cpsskins" />

Added: cpsskins/branches/jmo-perspectives/ui/framework/ctal.js
==============================================================================
--- (empty file)
+++ cpsskins/branches/jmo-perspectives/ui/framework/ctal.js     Fri Feb 10 
13:12:40 2006
@@ -0,0 +1,253 @@
+/*
+
+ Copyright (c) 2006 Balazs Ree <[EMAIL PROTECTED]>
+ All Rights Reserved.
+
+ This software is subject to the provisions of the Zope Public License,
+ Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+ THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+ WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+ FOR A PARTICULAR PURPOSE.
+
+*/
+
+
+function ctal(){};
+
+/* exception handling */
+
+ctal.exceptionFactory = function(name, prefix) {
+       var exc = function (msg) {
+               var fullmsg = prefix + msg;
+               var err = new Error(fullmsg);
+               // take care of IE5/5.5
+               if (!err.message) {
+                       err.message = fullmsg;
+               }
+               err.name = name;
+               return err
+       }
+       return exc
+}
+
+ctal.TALError = ctal.exceptionFactory("TALError", "TAL error: ");
+
+ctal.TALESError = ctal.exceptionFactory("TALESError", "TAL expression error: 
");
+
+/* DOM manipulation utilities */
+
+ctal.node_insertbefore = function(parent, value, marker) {
+       var nodes = new Array();
+       if (value != null) {
+               // decide if we are a DomNode or a DomNodeList
+               if (typeof value.nodeName == "undefined") {
+                       // a DomNodeList
+                       for (i=0; i<value.length; i++) {
+                               nodes[i] = value[i];
+                       }
+               } else {
+                       nodes[0] = value;
+               }
+       }
+       for (;node=nodes.shift();) {
+               if (marker) {
+                       parent.insertBefore(node, marker);
+               } else {
+                       parent.appendChild(node);
+               }
+       }
+}
+
+ctal.node_replace = function(value, old) {
+       var parent = old.parentNode;
+       var nextsibling = old.nextSibling;
+       parent.removeChild(old);
+       ctal.node_insertbefore(parent, value, nextsibling);
+}
+
+ctal.node_replacechildren = function(value, old) {
+       while (old.firstChild) {
+               old.removeChild(old.firstChild);
+       }
+       ctal.node_insertbefore(old, value, null);
+}
+
+/* TAL handlers */
+
+ctal.tal_attributes = function(tmpl, value, data) {
+       var attrs = value.split(";");
+       var attr = null;
+       for (;attr=attrs.shift();) {
+               var nmx = ctal.get_nameexpr(attr);
+               var aname = nmx[0]
+               var avalue = ctal.eval_expr(data, nmx[1])
+               var newattr = tmpl.ownerDocument.createAttribute(aname);
+               newattr.value = avalue;
+               tmpl.setAttributeNode(newattr);
+       }
+}
+
+ctal.content_or_replace  = function(tmpl, expr, data, func) {
+       var expr = expr.split(' ').join(' ');
+       var colpos = expr.indexOf(' ')
+       if (colpos > -1) {
+               var qual = expr.substring(0, colpos);
+               qual = qual.split(' ').join(' ');
+       }
+       if (colpos > -1 && qual.indexOf(':') == -1) {
+               var value = expr.substring(colpos + 1);
+       } else {
+               var qual = null;
+               var value = expr;
+       }
+       if (qual == 'dom') {
+               var evalue = ctal.eval_expr(data, value);
+               var newnode = evalue;
+       } else if (qual == null) {
+               var evalue = ctal.eval_expr(data, value);
+               var newnode = tmpl.ownerDocument.createTextNode(evalue);
+       } else {
+               throw new ctal.TALESError('TALES Expression qualifier "' + qual 
+ '" is unimplemented')
+       }
+       func(newnode, tmpl);
+}
+
+ctal.tal_content = function(tmpl, value, data) {
+       ctal.content_or_replace(tmpl, value, data, ctal.node_replacechildren);
+}
+
+ctal.tal_replace = function(tmpl, value, data) {
+       ctal.content_or_replace(tmpl, value, data, ctal.node_replace);
+}
+
+ctal.tal_condition = function(tmpl, value, data) {
+       var cond = ctal.eval_expr(data, value)
+       if (! ctal.isTrue(cond)) {
+               tmpl.parentNode.removeChild(tmpl);
+       }
+}
+
+ctal.tal_omit_tag = function(tmpl, value, data) {
+       var cond = ctal.eval_expr(data, value);
+       if (ctal.isTrue(cond)) {
+               ctal.node_replace(tmpl.childNodes, tmpl);
+       }
+}
+
+ctal.tal_repeat = function(tmpl, value, data) {
+       var nmx = ctal.get_nameexpr(value);
+       try {
+               var datas = ctal.eval_expr(data, nmx[1]);
+       } catch (e) {
+               if (e.name == ctal.TALESError.name) {
+                       throw new ctal.TALError(nmx[1]+" is not an Array.")
+               } else {
+                       throw e
+               }
+       }
+       var parent = tmpl.parentNode;
+       marker = tmpl.nextSibling;
+       parent.removeChild(tmpl);
+       for (var i=0; i<datas.length; i++) {
+               data[nmx[0]] = datas[i];
+               var newnode = tmpl.cloneNode(true);
+               ctal.node_insertbefore(parent, newnode, marker);
+               // recurse
+               ctal.process_ctal(newnode, data);
+       }
+}
+
+/* general processor */
+
+ctal.process_ctal = function(tmpl, data) {
+       var recurse = true;
+       if (tmpl.nodeType == 1) {
+               for (i=0; i<tmpl.attributes.length; i++) {
+                       var attrnode = tmpl.attributes.item(i);
+                       var splitname = attrnode.nodeName.split(':');
+                       if (splitname.length == 2 && splitname[0] == 'ctal') {
+                               // remove the attr right away
+                               tmpl.removeAttributeNode(attrnode);
+                               // process it
+                               var attrname = splitname[1];
+                               if (attrname == "repeat") {
+                                       ctal.tal_repeat(tmpl, attrnode.value, 
data);
+                                       recurse = false;
+                               } else if (attrname == "condition") {
+                                       ctal.tal_condition(tmpl, 
attrnode.value, data);
+                               } else if (attrname == "content") {
+                                       ctal.tal_content(tmpl, attrnode.value, 
data);
+                                       recurse = false;
+                               } else if (attrname == "replace") {
+                                       ctal.tal_replace(tmpl, attrnode.value, 
data);
+                                       recurse = false;
+                               } else if (attrname == "attributes") {
+                                       ctal.tal_attributes(tmpl, 
attrnode.value, data);
+                               } else if (attrname == "omit-tag") {
+                                       ctal.tal_omit_tag(tmpl, attrnode.value, 
data);
+                               } else {
+                                       throw new ctal.TALError('TAL attribute 
"' + attrname + '" is unimplemented')
+                               }
+                       }
+               }
+       }
+       if (recurse) {
+               //      recursion
+               for (var i=0; i<tmpl.childNodes.length; i++) {
+                       ctal.process_ctal(tmpl.childNodes[i], data);
+               }
+       }
+}
+
+/* expression handling */
+
+ctal.eval_expr = function(data, expr) {
+       colpos = expr.indexOf(':')
+       if (colpos > -1) {
+               var etype = expr.substring(0, colpos);
+               etype = etype.split(' ').join(' ');
+               var value = expr.substring(colpos + 1);
+       } else {
+               var etype = 'path';
+               var value = expr;
+       }
+       if (etype == 'string') {
+               return value
+       } else if (etype == 'path') {
+               return ctal.eval_pathexpr(data, value)
+       } else if (etype == 'javascript') {
+               return eval(value)
+       } else {
+               throw new ctal.TALESError('TALES Expression type "' + etype + 
'" is unimplemented')
+       }
+}
+
+ctal.eval_pathexpr = function(data, path) {
+       var pathelems = path.split("/");
+       var traverse = data;
+       for (var i=0; i < pathelems.length; i++) {
+               if (typeof traverse[pathelems[i]] == "undefined") {
+                       throw new ctal.TALESError('No data found for "' + path 
+ '"')
+               }
+               else {
+                       traverse = traverse[pathelems[i]];
+               }
+       }
+       return traverse;
+}
+
+ctal.get_nameexpr = function(value) {
+       var splitnx = value.split(/ /);
+       var first = "";
+       // ltrim
+       while (first == "") {
+               first = splitnx.shift();
+       }
+       return [first, splitnx.join(" ")];
+}
+
+ctal.isTrue = function(cond) {
+       return cond && (cond != "")
+}
+
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to