You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2012/10/23 01:21:20 UTC

[5/6] git commit: Merge core/spi:ElementWrapper.setAttribute() and .getAttribute() into just .attribute()

Merge core/spi:ElementWrapper.setAttribute() and .getAttribute() into just .attribute()


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/653eea21
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/653eea21
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/653eea21

Branch: refs/heads/5.4-js-rewrite
Commit: 653eea21d2a0bf29e10726bda3c06dfaec20347d
Parents: fc9e7b0
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Mon Oct 22 15:03:02 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Mon Oct 22 15:03:02 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/builder.coffee           |    2 +-
 .../META-INF/modules/core/fields.coffee            |   10 +-
 .../META-INF/modules/core/form-fragment.coffee     |    4 +-
 .../META-INF/modules/core/forms.coffee             |   10 +-
 .../coffeescript/META-INF/modules/core/grid.coffee |    2 +-
 .../coffeescript/META-INF/modules/core/spi.coffee  |   59 +++++++--------
 .../coffeescript/META-INF/modules/core/zone.coffee |    6 +-
 7 files changed, 44 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/builder.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/builder.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/builder.coffee
index adf23ab..ed42c93 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/builder.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/builder.coffee
@@ -73,7 +73,7 @@ define ["_", "core/spi"], (_, spi) ->
       else if name.startsWith "on"
         wrapper.on (name.substring 2), value
       else
-        wrapper.setAttribute name, value
+        wrapper.attribute name, value
 
     return null
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/fields.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/fields.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/fields.coffee
index d2b12e2..d1cb3d2 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/fields.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/fields.coffee
@@ -21,11 +21,11 @@ define ["_", "core/events", "core/spi", "core/builder"],
   (_, events, spi, builder) ->
 
     ensureFieldId = (field) ->
-      fieldId = field.getAttribute "id"
+      fieldId = field.attribute "id"
 
       unless fieldId
         fieldId = _.uniqueId "field"
-        field.setAttribute "id", fieldId
+        field.attribute "id", fieldId
 
       return fieldId
 
@@ -35,7 +35,7 @@ define ["_", "core/events", "core/spi", "core/builder"],
     #
     # * field - element wrapper for the field
     findHelpBlock = (field) ->
-      fieldId = field.getAttribute "id"
+      fieldId = field.attribute "id"
 
       # When the field has an id (the normal case!), search the body for
       # the matching help block.
@@ -63,9 +63,9 @@ define ["_", "core/events", "core/spi", "core/builder"],
           # Assign a unique (hopefully!) client id for the field, which will be
           # used to link the field and the label together.
           fieldId = _.uniqueId "field"
-          field.setAttribute "id", fieldId
+          field.attribute "id", fieldId
 
-        block.setAttribute "data-error-block-for", fieldId
+        block.attribute "data-error-block-for", fieldId
 
       return block
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
index 319cac2..1d130a5 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/form-fragment.coffee
@@ -35,12 +35,12 @@ define ["_", "core/spi", "core/events", "core/compat/tapestry"],
 
       _.each fragments, (frag) ->
 
-        fragmentId = frag.getAttribute "id"
+        fragmentId = frag.attribute "id"
 
         hidden = frag.findFirst "input[type=hidden][data-for-fragment=#{fragmentId}]"
 
         # If found (e.g., not alwaysSubmit), then enable/disable the field.
-        hidden && hidden.setAttribute "disabled", not frag.deepVisible()
+        hidden && hidden.attribute "disabled", not frag.deepVisible()
 
     # Again, a DOM event to make the FormFragment visible or invisible; this is useful
     # because of the didShow/didHide events ... but we're really just seeing the evolution

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
index 8bddbf9..020e18b 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/forms.coffee
@@ -23,7 +23,7 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
 
     isPreventSubmission = (element) ->
       (element.hasClass Tapestry.PREVENT_SUBMISSION) or
-      (element.getAttribute "data-prevent-submission")
+      (element.geattribute "data-prevent-submission")
 
     clearSubmittingHidden = (form) ->
       hidden = form.findFirst "[name='t:submit']"
@@ -47,10 +47,10 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
 
     defaultValidateAndSubmit = ->
 
-      if ((this.getAttribute "data-validate") is "submit") and
-         (not this.getAttribute SKIP_VALIDATION)
+      if ((this.attribute "data-validate") is "submit") and
+         (not this.attribute SKIP_VALIDATION)
 
-        this.removeAttribute SKIP_VALIDATION
+        this.attribute SKIP_VALIDATION
 
         memo = error: false
 
@@ -95,4 +95,4 @@ define ["core/events", "core/spi", "core/builder", "core/compat/tapestry"],
       setSubmittingElement: setSubmittingHidden
 
       skipValidation: (formWrapper) ->
-        formWrapper.setAttribute SKIP_VALIDATION, true
\ No newline at end of file
+        formWrapper.attribute SKIP_VALIDATION, true
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/grid.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/grid.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/grid.coffee
index e098e00..8b33dd7 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/grid.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/grid.coffee
@@ -25,7 +25,7 @@ define ["core/spi", "core/events"], (spi, events) ->
 
     zone = this.findContainer "[data-zone]"
 
-    zone.trigger events.zone.refresh, url: a.getAttribute "href"
+    zone.trigger events.zone.refresh, url: this.attribute "href"
 
     return false
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
index 8af53a2..ec369e1 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/spi.coffee
@@ -150,7 +150,7 @@ define ["_", "prototype"], (_) ->
 
       _.each elements, (element) =>
         _.each eventNames, (eventName) =>
-            @protoHandlers.push Event.on element, eventName, match, wrapped
+          @protoHandlers.push Event.on element, eventName, match, wrapped
 
     # Invoked after `stop()` to restart event listening.
     start: ->
@@ -168,10 +168,10 @@ define ["_", "prototype"], (_) ->
   # Exposes the original element as property `element`.
   class ElementWrapper
 
-    # Passed the DOM Element
+  # Passed the DOM Element
     constructor: (@element) ->
 
-    # Hides the wrapped element, setting its display to 'none'.
+      # Hides the wrapped element, setting its display to 'none'.
     hide: ->
       @element.hide()
 
@@ -189,29 +189,27 @@ define ["_", "prototype"], (_) ->
 
       return this
 
-    # Returns the value of an attribute as a string, or null if the attribute
-    # does not exist.
-    getAttribute: (name) ->
-      @element.readAttribute name
-
-    # Set the value of the attribute to the given value.
+    # Reads or updates an attribute. With one argument, returns the current value
+    # of the attribute. With two arguments, updates the attribute's value, and returns
+    # the previous value. Setting an attribute to null is the same as removing it.
     #
-    # Note: Prototype has special support for values null, true, and false that may not be duplicated by other
-    # implementations of the SPI.
-    setAttribute: (name, value) ->
-      # TODO: case where name is an object, i.e., multiple attributes in a single call.
-      # Well, you can just do it, but its not guaranteed to work the same across
-      # different SPIs.
-      @element.writeAttribute name, value
-
-      return this
+    # Alternately, the first attribute can be an object in which case all the keys
+    # and values of the object are applied as attributes, and this `ElementWrapper` is returned.
+    #
+    # * name - the attribute to read or update, or an object of keys and values
+    # * value - (optional) the new value for the attribute
+    attribute: (name) ->
 
-    # Removes the named attribute, if present.
-    removeAttribute: (name) ->
+      if _.isObject name
+        for name, value of name
+            @element.writeAttribute name, value
+        return this
 
-      @element.writeAttribute name, null
+      current = @element.readAttribute name
+      if arguments.length > 1
+        @element.writeAttribute name, arguments[1]
 
-      return this
+      return current
 
     # Returns true if the element has the indicated class name, false otherwise.
     hasClass: (name) ->
@@ -332,7 +330,6 @@ define ["_", "prototype"], (_) ->
     #
     # Note that in Tapestry 5.3, the search would stop at the nearest form element, not the document body.
     deepVisible: ->
-
       cursor = this
       while cursor
         return false unless cursor.visible()
@@ -368,17 +365,16 @@ define ["_", "prototype"], (_) ->
 
       return this
 
-    # Returns the current value of the element (which must be a form control element, such as `<input>` or
-    # `<textarea>`).
+    # With no parameters, returns the current value of the element (which must be a form control element, such as `<input>` or
+    # `<textarea>`). With one parameter, updates the field's value, and returns the previous value.
     # TODO: Define behavior for multi-named elements, such as `<select>`.
-    getValue: ->
-      @element.getValue()
+    value: ->
+      current = @element.getValue()
 
-    # Updates the value for the element (which must be a form control element).
-    setValue: (newValue) ->
-      @element.setValue newValue
+      if arguments.length > 0
+        @element.setValue arguments[0]
 
-      return this
+      return current
 
     # Adds an event handler for one or more events.
     #
@@ -499,7 +495,6 @@ define ["_", "prototype"], (_) ->
     domReady: (callback) ->
       # Hack for IE, which doesn't fire the dom:loaded event reliably.  However, we know that any code here
       # is invoked from the footer of the document, so the rest can be assumed to be loaded.
-
       if Prototype.Browser.IE
         document.loaded = true
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/653eea21/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
index 4c4dadc..d9527d1 100644
--- a/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/zone.coffee
@@ -24,7 +24,7 @@ define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
   (spi, events, ajax, console, _) ->
 
     findZone = (element) ->
-      zoneId = element.getAttribute "data-update-zone"
+      zoneId = element.attribute "data-update-zone"
 
       if zoneId is "^"
         zone = element.findContainer "[data-zone]"
@@ -46,7 +46,7 @@ define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
       zone = findZone this
 
       if zone
-        zone.trigger events.zone.refresh,  url: this.getAttribute "href"
+        zone.trigger events.zone.refresh,  url: this.attribute "href"
 
       return false
 
@@ -70,7 +70,7 @@ define ["core/spi", "core/events", "core/ajax", "core/console", "_"],
     spi.onDocument events.zone.refresh, (event) ->
 
       # A Zone inside a form will render some additional parameters to coordinate updates with the Form on the server.
-      attr = this.getAttribute "data-zone-parameters"
+      attr = this.attribute "data-zone-parameters"
 
       parameters = attr and JSON.parse attr