coliver     2004/02/18 09:37:34

  Modified:    src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2
                        ScriptableWidget.java
               src/blocks/woody/samples/v2 woody_flow_example.js
  Log:
  Added 'onAddRow' and 'onRemoveRow' callbacks to Repeater and modified 
property handling to allow JS properties to supercede Widget id's. If there is 
a conflict between a widget id and a property then you can use getWidget(id) as 
an escape mechanism to access the widget
  
  Revision  Changes    Path
  1.12      +74 -17    
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java
  
  Index: ScriptableWidget.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/flow/javascript/v2/ScriptableWidget.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ScriptableWidget.java     16 Feb 2004 04:54:20 -0000      1.11
  +++ ScriptableWidget.java     18 Feb 2004 17:37:33 -0000      1.12
  @@ -161,15 +161,20 @@
   
       public boolean has(String id, Scriptable start) {
           if (delegate != null) {
  -            Widget sub = delegate.getWidget(id);
  -            if (sub != null) {
  -                return true;
  +            if (!(delegate instanceof Repeater)) {
  +                Widget sub = delegate.getWidget(id);
  +                if (sub != null) {
  +                    return true;
  +                }
               }
           } 
           return super.has(id, start);
       }
   
       public boolean has(int index, Scriptable start) {
  +        if (super.has(index, start)) {
  +            return true;
  +        }
           if (delegate instanceof Repeater) {
               Repeater repeater = (Repeater)delegate;
               return index >= 0 && index < repeater.getSize();
  @@ -178,38 +183,50 @@
               Object[] values = (Object[])delegate.getValue();
               return index >= 0 && index < values.length;
           }
  -        return super.has(index, start);
  +        return false;
       }
   
       public Object get(String id, Scriptable start) {
  +        Object result = super.get(id, start);
  +        if (result != NOT_FOUND) {
  +            return result;
  +        }
           if (delegate != null && !(delegate instanceof Repeater)) {
               Widget sub = delegate.getWidget(id);
               if (sub != null) {
                   return wrap(sub);
               }
           }
  -        return super.get(id, start);
  +        return NOT_FOUND;
       }
   
       public Object get(int index, Scriptable start) {
  +        Object result = super.get(index, start);
  +        if (result != NOT_FOUND) {
  +            return result;
  +        }
           if (delegate instanceof Repeater) {
               Repeater repeater = (Repeater)delegate;
               if (index >= 0) {
  -                while (index >= repeater.getSize()) {
  -                    repeater.addRow();
  +                int count = index + 1 - repeater.getSize();
  +                if (count > 0) {
  +                    ScriptableWidget[] rows = new ScriptableWidget[count];
  +                    for (int i = 0; i < count; i++) {
  +                        rows[i] = wrap(repeater.addRow());
  +                    }
  +                    for (int i = 0; i < count; i++) {
  +                        rows[i].notifyAddRow();
  +                    }
                   }
                   return wrap(repeater.getRow(index));
               }
  -        }
  -        if (delegate instanceof MultiValueField) {
  +        } else if (delegate instanceof MultiValueField) {
               Object[] values = (Object[])delegate.getValue();
               if (index >= 0 && index < values.length) {
                   return values[index];
  -            } else {
  -           return NOT_FOUND;
  -         }
  +            }
           }
  -        return super.get(index, start);
  +        return NOT_FOUND;
       }
   
       public Object[] getAllIds() {
  @@ -240,10 +257,48 @@
   
       private void deleteRow(Repeater repeater, int index) {
           Widget row = repeater.getRow(index);
  +        ScriptableWidget s = wrap(row);
  +        s.notifyRemoveRow();
           formWidget.deleteWrapper(row);
           repeater.removeRow(index);
       }
   
  +    private void notifyAddRow() {
  +        ScriptableWidget repeater = wrap(delegate.getParent());
  +        Object prop = getProperty(repeater, "onAddRow");
  +        if (prop instanceof Function) {
  +            try {
  +                Function fun = (Function)prop;
  +                Object[] args = new Object[1];
  +                Scriptable scope = getTopLevelScope(this);
  +                Scriptable thisObj = scope;
  +                Context cx = Context.getCurrentContext();
  +                args[0] = this;
  +                fun.call(cx, scope, thisObj, args);
  +            } catch (Exception exc) {
  +                throw Context.reportRuntimeError(exc.getMessage());
  +            }
  +        }
  +    }
  +
  +    private void notifyRemoveRow() {
  +        ScriptableWidget repeater = wrap(delegate.getParent());
  +        Object prop = getProperty(repeater, "onRemoveRow");
  +        if (prop instanceof Function) {
  +            try {
  +                Function fun = (Function)prop;
  +                Object[] args = new Object[1];
  +                Scriptable scope = getTopLevelScope(this);
  +                Scriptable thisObj = scope;
  +                Context cx = Context.getCurrentContext();
  +                args[0] = this;
  +                fun.call(cx, scope, thisObj, args);
  +            } catch (Exception exc) {
  +                throw Context.reportRuntimeError(exc.getMessage());
  +            }
  +        }
  +    }
  +
       public void delete(int index) {
           if (delegate instanceof Repeater) {
               Repeater repeater = (Repeater)delegate;
  @@ -293,7 +348,7 @@
                   }
               } else {
                   for (int i = size; i < len; ++i) {
  -                    repeater.addRow();
  +                    wrap(repeater.addRow()).notifyAddRow();
                   }
               }
           }
  @@ -527,10 +582,12 @@
       }
   
       public ScriptableWidget jsFunction_addRow() {
  +        ScriptableWidget result = null;
           if (delegate instanceof Repeater) {
  -            return wrap(((Repeater)delegate).addRow());
  +            result = wrap(((Repeater)delegate).addRow());
  +            result.notifyAddRow();
           }
  -        return null;
  +        return result;
       }
   
       public ScriptableObject jsFunction_getRow(int index) {
  
  
  
  1.6       +25 -9     
cocoon-2.1/src/blocks/woody/samples/v2/woody_flow_example.js
  
  Index: woody_flow_example.js
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/woody/samples/v2/woody_flow_example.js,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- woody_flow_example.js     16 Feb 2004 04:54:20 -0000      1.5
  +++ woody_flow_example.js     18 Feb 2004 17:37:33 -0000      1.6
  @@ -101,6 +101,29 @@
       wid.dieselprice.value = 2.00;
       wid.birthdate.value = new java.util.Date();
       //
  +    // You can perform actions when a new row is added to a repeater (like
  +    // setting the row's 'onChange' properties or initializing its values
  +    // by assigning a function to the repeater's 'onAddRow' property:
  +    //
  +    wid.contacts.onAddRow = function(row) {
  +        row.firstname.value = "<first name>";
  +        row.lastname.value = "<last name>";
  +        row.select.onChange = function(oldValue, newValue) {
  +            if (newValue) {
  +                print("you selected: " + row.firstname.value);
  +            } else {
  +                print("you deselected: " + row.firstname.value);
  +            }
  +        }
  +    }
  +    //
  +    // You can perform actions when a row is removed from a repeater
  +    // by assigning a function to the repeater's 'onRemoveRow' property:
  +    //
  +    wid.contacts.onRemoveRow = function(row) {
  +      print("you're about to remove: " + row.firstname.value);
  +    }
  +    //
       // The rows of a Repeater widget can be accessed using array syntax:
       //
       wid.contacts[0].firstname.value = "Jules";
  @@ -118,14 +141,7 @@
           //
           // You can add a row using the addRow() function of Repeater
           //
  -        var row = wid.contacts.addRow();
  -        row.select.onChange = function(oldValue, newValue) {
  -            if (newValue) {
  -                print("you selected: " + row.firstname.value);
  -            } else {
  -                print("you deselected: " + row.firstname.value);
  -            }
  -        }
  +        wid.contacts.addRow();
       }
       wid.removecontacts.onClick = function() {
           //
  @@ -153,7 +169,7 @@
   
       //
       // By calling the Form's setBookmark() function you can set the
  -    // point in your script to return to when it is redisplayed. This
  +    // point in your script to return to when the form is redisplayed. This
       // is useful if you need to acquire resources to process the 
       // pipeline used by the form but also have them be released
       // before the script is suspended e.g.:
  
  
  

Reply via email to