[Rt-commit] rt branch, lcore, updated. d2855f140bccc8f4b2e42907355b558ee71b9b89

clkao at bestpractical.com clkao at bestpractical.com
Thu Oct 15 13:25:11 EDT 2009


The branch, lcore has been updated
       via  d2855f140bccc8f4b2e42907355b558ee71b9b89 (commit)
       via  8b51e0328bf313196b4c0ceee30286ebab439784 (commit)
       via  0759d7269ee85ce21ff7f1bf8bfbd1e73550338e (commit)
      from  f8103049e59b60e07d78480a60162b75ee6104d9 (commit)

Summary of changes:
 share/web/static/js/rulebuilder.js |  258 ++++++++++++++++++++++--------------
 1 files changed, 161 insertions(+), 97 deletions(-)

- Log -----------------------------------------------------------------
commit 0759d7269ee85ce21ff7f1bf8bfbd1e73550338e
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Oct 16 00:09:46 2009 +0800

    split selfeval specific code.

diff --git a/share/web/static/js/rulebuilder.js b/share/web/static/js/rulebuilder.js
index d05c2b3..7767c40 100644
--- a/share/web/static/js/rulebuilder.js
+++ b/share/web/static/js/rulebuilder.js
@@ -88,11 +88,8 @@ RuleBuilder.prototype.load_expressions = function (node, ctx) {
     }
     else if (node.type == 'self_evaluating') {
         jQuery('span.enter-value', ctx.element).hide();
-        jQuery._input_({ 'type': 'text', 'class': 'enter-value', 'value': node.value})
-            .change(function() { ctx.update_return_type(ctx.return_type_from_val(this.value)) } )
-            .appendTo(ctx.element).trigger('focus');
-        ctx.self_eval = true;
-
+        ctx.expcontext = new RuleBuilder2.SelfEvalContext( { context: ctx } );
+        ctx.expcontext.set_value(node.value);
     }
     else {
         alert('unknown node type');
@@ -330,6 +327,42 @@ RuleBuilder.prototype.filter_expression_type = function (type) {
 };
 
 Module("RuleBuilder2", function(m) {
+    Class("SelfEvalContext", {
+        has: { context: { is: "rw" },
+             },
+        after: {
+            initialize: function() {
+                var that = this;
+                jQuery._input_({ 'type': 'text', class: 'enter-value'})
+                    .change(function() {
+                        that.context.update_return_type(that.return_type(this.value))
+                    } )
+                    .appendTo(this.context.element)
+                    .trigger('focus');
+            },
+        },
+        methods: {
+            set_value: function(val) {
+                jQuery('input.enter-value', this.context.element)
+                    .val(val)
+                    .trigger('change');
+            },
+            return_type: function(val) {
+                // XXX
+                return 'Str';
+            },
+            serialize: function() {
+                var val = jQuery('input.enter-value', this.context.element).val();
+                if (this.context.expected_type == 'Str') {
+                    return '"'+val+'"';
+                }
+                else {
+                    return val;
+                }
+            }
+        }
+    });
+
     Class("Context", {
         has: {
             expected_type: { is: "rw" },
@@ -352,10 +385,7 @@ Module("RuleBuilder2", function(m) {
                     .text("Enter a value")
                     .click(function(e) {
                         jQuery(this).html('').unbind('click');
-                        jQuery._input_({ 'type': 'text', class: 'enter-value'})
-                            .change(function() { that.update_return_type(that.return_type_from_val(this.value)) } )
-                            .appendTo(this).trigger('focus');
-                        that.self_eval = true;
+                        that.expcontext = new m.SelfEvalContext( { context: that } );
                         return true;
                     })
                     .appendTo(this.element);
@@ -430,11 +460,6 @@ Module("RuleBuilder2", function(m) {
                 return child;
             },
 
-            return_type_from_val: function(val) {
-                // XXX
-                return 'Str';
-            },
-
             transform: function(func_name) {
                 var rb = this.rb;
                 var func = rb.functions[func_name];
@@ -532,7 +557,7 @@ Module("RuleBuilder2", function(m) {
             },
 
             state: function() {
-                if( this.self_eval ) {
+                if( this.expcontext instanceof m.SelfEvalContext ) {
                 }
                 else if ( this.expression ) {
                 }
@@ -575,7 +600,8 @@ Module("RuleBuilder2", function(m) {
                 jQuery('span.expression', this.element).remove();
                 jQuery('span.transform', this.element).hide();
                 jQuery('span.enter-value', this.element).hide();
-                this.self_eval = false;
+
+                this.expcontext = null;
                 this.expression = null;
                 this.func_name = null;
             },
@@ -588,14 +614,8 @@ Module("RuleBuilder2", function(m) {
             },
 
             serialize: function() {
-                if( this.self_eval ) {
-                    var val = jQuery('input.enter-value', this.element).val();
-                    if (this.expected_type == 'Str') {
-                        return '"'+val+'"';
-                    }
-                    else {
-                        return val;
-                    }
+                if( this.expcontext instanceof m.SelfEvalContext ) {
+                    return this.expcontext.serialize();
                 }
                 else if ( this.expression ) {
                     return this.expression;

commit 8b51e0328bf313196b4c0ceee30286ebab439784
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Oct 16 00:52:24 2009 +0800

    move most application specific context code out

diff --git a/share/web/static/js/rulebuilder.js b/share/web/static/js/rulebuilder.js
index 7767c40..0b7d46a 100644
--- a/share/web/static/js/rulebuilder.js
+++ b/share/web/static/js/rulebuilder.js
@@ -363,6 +363,71 @@ Module("RuleBuilder2", function(m) {
         }
     });
 
+    Class("ApplicationContext", {
+        has: { context: { is: "rw" },
+               func_name: { is: "rw" },
+               func: { is: "rw" }
+             },
+        after: {
+            initialize: function() {
+                var that = this.context;
+                that.children = [];
+                that.update_return_type(this.func.return_type);
+                jQuery('span.transform', that.element).show();
+                jQuery._div({'class': 'application'})
+                    ._div_({'class': 'application-function function'})
+                    ._div_({'class': 'application-params signature'})
+                    .div_()
+                    .appendTo(that.element);
+
+                jQuery('div.application-function',that.element).html(this.func_name);
+                jQuery('div.application', that.element).show();
+                jQuery('div.application-params', that.element).html('');
+                var params = jQuery('div.application-params', that.element);
+                jQuery.each(this.func.parameters,
+                            function(idx, val) {
+                                var x = jQuery._div_({'class': 'context'})
+                                    .appendTo(params);
+
+                                var child = new RuleBuilder2.Context({ expected_type: val.type,
+                                                                       element: x.get(0),
+                                                                       parent: that,
+                                                                       rb: that.rb });
+
+                                that.children.push(child);
+                            });
+                if (that.children.length) {
+                    that.rb.focus(that.children[0]);
+                }
+            }
+        },
+        methods: {
+            serialize: function() {
+                var args = jQuery.map(this.context.children, function(val) { return val.serialize() });
+                args.unshift(this.func_name);
+                return '('+args.join(' ')+')';
+            },
+            get_state: function() {
+                var type_complete = false;
+                var children = this.context.children;
+                for (var i in children) {
+                    var child = children[i];
+                    var state = child.state();
+                    if (state == 'pending')
+                        return 'pending';
+                    if (state == 'type-complete')
+                        type_complete = true;
+                }
+                if (!type_complete)
+                    return "complete";
+
+                var el = jQuery("span.return-type", this.context.element);
+                return el.hasClass('matched') ? 'type-complete' : 'complete';
+            }
+
+        }
+    });
+
     Class("Context", {
         has: {
             expected_type: { is: "rw" },
@@ -561,18 +626,8 @@ Module("RuleBuilder2", function(m) {
                 }
                 else if ( this.expression ) {
                 }
-                else if ( this.func_name ) {
-                    var type_complete = false;
-                    for (var i in this.children) {
-                        var child = this.children[i];
-                        var state = child.state();
-                        if (state == 'pending')
-                            return 'pending';
-                        if (state == 'type-complete')
-                            type_complete = true;
-                    }
-                    if (!type_complete)
-                        return "complete";
+                else if ( this.expcontext instanceof m.ApplicationContext ) {
+                    return this.expcontext.get_state();
                 }
                 else {
                     return 'pending';
@@ -603,12 +658,11 @@ Module("RuleBuilder2", function(m) {
 
                 this.expcontext = null;
                 this.expression = null;
-                this.func_name = null;
             },
 
             traverse: function(fn) {
                 fn(this);
-                if ( this.func_name ) {
+                if ( this.expcontext instanceof m.ApplicationContext ) {
                     jQuery.each(this.children, function(idx, val) { fn(this) } );
                 }
             },
@@ -617,14 +671,12 @@ Module("RuleBuilder2", function(m) {
                 if( this.expcontext instanceof m.SelfEvalContext ) {
                     return this.expcontext.serialize();
                 }
+                else if( this.expcontext instanceof m.ApplicationContext ) {
+                    return this.expcontext.serialize();
+                }
                 else if ( this.expression ) {
                     return this.expression;
                 }
-                else if ( this.func_name ) {
-                    var args = jQuery.map(this.children, function(val) { return val.serialize() });
-                    args.unshift(this.func_name);
-                    return '('+args.join(' ')+')';
-                }
                 else if ( this.arraybuilder ) {
                     var args = jQuery.map(this.children, function(val) { return val.serialize() });
                     return args.join(' ');
@@ -644,37 +696,10 @@ Module("RuleBuilder2", function(m) {
 
             set_application: function(func_name, func) {
                 this.clear();
-                this.func_name = func_name;
-                this.children = [];
-                this.update_return_type(func.return_type);
-                jQuery('span.transform', this.element).show();
-                jQuery._div({'class': 'application'})
-                    ._div_({'class': 'application-function function'})
-                    ._div_({'class': 'application-params signature'})
-                    .div_()
-                    .appendTo(this.element);
-
-                jQuery('div.application-function',this.element).html(func_name);
-                jQuery('div.application', this.element).show();
-                jQuery('div.application-params', this.element).html('');
-                var params = jQuery('div.application-params', this.element);
-                var that = this;
-                jQuery.each(func.parameters,
-                            function(idx, val) {
-                                var x = jQuery._div_({'class': 'context'})
-                                    .appendTo(params);
 
-                                var child = new RuleBuilder2.Context({ expected_type: val.type,
-                                                                       element: x.get(0),
-                                                                       parent: that,
-                                                                       rb: that.rb });
-
-                                that.children.push(child);
-                            });
-                if (this.children.length) {
-                    this.rb.focus(this.children[0]);
-                }
+                this.expcontext = new m.ApplicationContext( { context: this, func_name: func_name, func: func } );
             }
+
         }
     })
 });

commit d2855f140bccc8f4b2e42907355b558ee71b9b89
Author: Chia-liang Kao <clkao at bestpractical.com>
Date:   Fri Oct 16 01:23:16 2009 +0800

    interim arraycontext refactor.

diff --git a/share/web/static/js/rulebuilder.js b/share/web/static/js/rulebuilder.js
index 0b7d46a..caab1fd 100644
--- a/share/web/static/js/rulebuilder.js
+++ b/share/web/static/js/rulebuilder.js
@@ -363,6 +363,48 @@ Module("RuleBuilder2", function(m) {
         }
     });
 
+    Class("ArrayContext", {
+        has: { context: { is: "rw" },
+               builder: { is: "rw" },
+               inner_type: { is: "rw" },
+             },
+        after: {
+            initialize: function() {
+            }
+        },
+        methods: {
+            helper: function(context, inner_type) {
+                context.inner_type = inner_type;
+                context.children = [];
+                var builder = jQuery._div({'class': 'arraybuilder'})
+                    ._div_({'class': 'array-item-container'})
+                    .div_()
+                    .appendTo(context.element)
+                    .hide();
+                jQuery._span_({'class': 'arraybuilder-icon'})
+                    .text("Array builder")
+                    .appendTo(context.element)
+                    .click(function(e) {
+                        context.arraybuilder = builder;
+                        context.expcontext = new m.ArrayContext({ context: context,
+                                                                  builder: builder,
+                                                                  inner_type: inner_type });
+                        var child = this.context.mk_array_item_context(this.inner_type,
+                                                                       jQuery('div.array-item-container', this.builder), 0);
+                        this.context.children.push(child);
+                        builder.show();
+                        jQuery(this).hide();
+                        context.rb.focus(child);
+                        return false;
+                    });
+            },
+            serialize: function() {
+                var args = jQuery.map(this.context.children, function(val) { return val.serialize() });
+                return args.join(' ');
+            }
+        }
+    });
+
     Class("ApplicationContext", {
         has: { context: { is: "rw" },
                func_name: { is: "rw" },
@@ -458,26 +500,7 @@ Module("RuleBuilder2", function(m) {
 
             var matched = /^ArrayRef\[(.*)\]$/.exec(expected_type);
             if (matched) {
-                this.inner_type = matched[1];
-                this.children = [];
-                var builder = jQuery._div({'class': 'arraybuilder'})
-                    ._div_({'class': 'array-item-container'})
-                    .div_()
-                    .appendTo(this.element)
-                    .hide();
-                jQuery._span_({'class': 'arraybuilder-icon'})
-                    .text("Array builder")
-                    .appendTo(this.element)
-                    .click(function(e) {
-                        that.arraybuilder = builder;
-                        var child = that.mk_array_item_context(that.inner_type,
-                                                               jQuery('div.array-item-container', builder), 0);
-                        that.children.push(child);
-                        builder.show();
-                        jQuery(this).hide();
-                        that.rb.focus(child);
-                        return false;
-                    });
+                m.ArrayContext.prototype.helper(this, matched[1]);
             }
         }
                },
@@ -564,6 +587,9 @@ Module("RuleBuilder2", function(m) {
                     var builder = jQuery('div.arraybuilder', parent.element).show();
                     jQuery("span.arraybuilder-icon", parent.element).hide();
                     parent.arraybuilder = builder;
+                    parent.expcontext = new m.ArrayContext({ context: parent,
+                                                             builder: builder,
+                                                             inner_type: parent.inner_type });
                     var container = jQuery('div.array-item-container', builder);
                     first_param = parent.mk_array_item_context(parent.inner_type,
                                                                container, null);
@@ -668,18 +694,11 @@ Module("RuleBuilder2", function(m) {
             },
 
             serialize: function() {
-                if( this.expcontext instanceof m.SelfEvalContext ) {
-                    return this.expcontext.serialize();
-                }
-                else if( this.expcontext instanceof m.ApplicationContext ) {
-                    return this.expcontext.serialize();
-                }
-                else if ( this.expression ) {
+                if ( this.expression ) {
                     return this.expression;
                 }
-                else if ( this.arraybuilder ) {
-                    var args = jQuery.map(this.children, function(val) { return val.serialize() });
-                    return args.join(' ');
+                else {
+                    return this.expcontext.serialize();
                 }
             },
 

-----------------------------------------------------------------------


More information about the Rt-commit mailing list