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/25 23:41:59 UTC

[1/13] git commit: Reimplemented alerts as an AMD module

Updated Branches:
  refs/heads/5.4-js-rewrite 5d4c89709 -> b1bd02711


Reimplemented alerts as an AMD module


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

Branch: refs/heads/5.4-js-rewrite
Commit: b1bd02711fcd0a2215cde245f0c83775239d7724
Parents: cfd4815
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Thu Oct 25 14:41:46 2012 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Thu Oct 25 14:41:46 2012 -0700

----------------------------------------------------------------------
 .../META-INF/modules/core/alert.coffee             |  120 +++++++++++++++
 .../java/org/apache/tapestry5/alerts/Alert.java    |    3 +-
 .../tapestry5/corelib/components/Alerts.java       |   48 +++---
 .../internal/alerts/AlertManagerImpl.java          |    2 +-
 .../integration/app1/pages/AlertsDemo.tml          |    1 -
 5 files changed, 146 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b1bd0271/tapestry-core/src/main/coffeescript/META-INF/modules/core/alert.coffee
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/coffeescript/META-INF/modules/core/alert.coffee b/tapestry-core/src/main/coffeescript/META-INF/modules/core/alert.coffee
new file mode 100644
index 0000000..167c6db
--- /dev/null
+++ b/tapestry-core/src/main/coffeescript/META-INF/modules/core/alert.coffee
@@ -0,0 +1,120 @@
+# Copyright 2012 The Apache Software Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http:#www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# ##core/ajax
+#
+# Support for the core/Alerts components.
+#
+define ["core/spi", "core/console", "core/messages", "core/builder", "core/ajax", "_"],
+  (spi, console, messages, builder, ajax, _) ->
+
+    severityToClass =
+      warn: "alert alert-warning"
+      error: "alert alert-error"
+
+    getURL = (container) -> container.attribute "data-dismiss-url"
+
+    removeAlert = (container, alert) ->
+      alert.remove()
+
+      if container.find(".alert").length is 0
+        container.update null
+
+    dismissAll = (container) ->
+      console.debug "dismiss all"
+
+      alerts = container.find "[data-alert-id]"
+
+      if alerts.length is 0
+        container.update null
+        return
+
+      ajax (getURL container),
+        onsuccess: -> container.update null
+
+    dismissOne = (container, button) ->
+      console.debug "dismiss single"
+
+      alert = button.container()
+
+      id = alert.attribute "data-alert-id"
+
+      unless id
+        removeAlert container, alert
+        return
+
+      ajax (getURL container),
+        parameters: { id }
+        onsuccess: -> removeAlert container, alert
+
+    setupUI = (container) ->
+
+      clickHandler = ->
+        dismissAll container
+        return false
+
+      container.update builder ".well",
+        ["div", "data-container-type": "inner"],
+        [".row-fluid > button.btn.btn-mini.pull-right",
+            onclick: clickHandler
+            ["strong", "\u00d7 "],
+            messages "core-dismiss-label"
+        ]
+
+      container.on "click button.close", ->
+        dismissOne container, this
+        return false
+
+    findInnerContainer = ->
+      outer = spi.body().findFirst "[data-container-type=alerts]"
+
+      unless outer
+        console.error "Unable to locate alert container element to present an alert."
+        return null
+
+      # Set up the inner content when needed
+      unless outer.element.firstChild
+        setupUI outer
+
+      return outer?.findFirst "[data-container-type=inner]"
+
+    # The `data` for the alert has a number of keys to control its behavior
+
+    alert = (data) ->
+
+      container = findInnerContainer()
+
+      return unless container
+
+      className = severityToClass[data.severity] or "alert"
+
+      # Note that `data-dismiss=alert` is purposely excluded
+      # - we want to handle closes w/ notifications to the server if not transient
+      # - we don't want to rely on bootstrap.js, as that will drag jQuery into the application
+      element = builder "div", class: className,
+        ["button.close", "\u00d7"]
+        data.message
+
+      if data.id
+        element.attribute "data-alert-id", data.id
+
+      container.append element
+
+      if data.transient
+        _.delay removeAlert, exports.TRAINSIENT_DURATION, container, element
+
+    alert.TRAINSIENT_DURATION = 5000
+
+    # Export the alert function
+    exports = alert

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b1bd0271/tapestry-core/src/main/java/org/apache/tapestry5/alerts/Alert.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/alerts/Alert.java b/tapestry-core/src/main/java/org/apache/tapestry5/alerts/Alert.java
index 824bbb4..b7a8d8d 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/alerts/Alert.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/alerts/Alert.java
@@ -81,7 +81,8 @@ public class Alert implements Serializable
 
     public JSONObject toJSON()
     {
-        JSONObject result = new JSONObject("message", message, "class", severity.cssClass);
+        JSONObject result = new JSONObject("message", message,
+                "severity", severity.name().toLowerCase() );
 
         if (duration == Duration.TRANSIENT)
         {

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b1bd0271/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Alerts.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Alerts.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Alerts.java
index bc13695..885e772 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Alerts.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Alerts.java
@@ -15,69 +15,67 @@
 package org.apache.tapestry5.corelib.components;
 
 import org.apache.tapestry5.BindingConstants;
-import org.apache.tapestry5.ClientElement;
-import org.apache.tapestry5.ComponentResources;
+import org.apache.tapestry5.Link;
 import org.apache.tapestry5.MarkupWriter;
 import org.apache.tapestry5.alerts.Alert;
 import org.apache.tapestry5.alerts.AlertStorage;
 import org.apache.tapestry5.annotations.*;
+import org.apache.tapestry5.corelib.base.BaseClientElement;
 import org.apache.tapestry5.ioc.annotations.Inject;
 import org.apache.tapestry5.json.JSONObject;
-import org.apache.tapestry5.services.javascript.InitializationPriority;
-import org.apache.tapestry5.services.javascript.JavaScriptSupport;
+import org.apache.tapestry5.services.compatibility.DeprecationWarning;
 
 /**
  * Renders out an empty {@code <div>} element and provides JavaScript initialization to make the element
  * the container for alerts. After rendering markup (and initialization JavaScript), it
  * {@linkplain org.apache.tapestry5.alerts.AlertStorage#dismissNonPersistent() removes all non-persistent alerts}.
- * 
+ * <p/>
  * Alerts are created using the {@link org.apache.tapestry5.alerts.AlertManager} service.
  *
  * @tapestrydoc
  * @since 5.3
  */
 @SupportsInformalParameters
-public class Alerts implements ClientElement
+@Import(stack = "core")
+public class Alerts extends BaseClientElement
 {
 
-    @Parameter(value="message:core-dismiss-label", defaultPrefix=BindingConstants.LITERAL)
+    /**
+     * Allows the button used to dismiss all alerts to be customized (and localized).
+     *
+     * @deprecated Deprecated in Tapestry 5.4; override the {@code core-dismiss-label} message key in
+     *             your application's message catalog. This parameter is now ignored.
+     */
+    @Parameter(value = "message:core-dismiss-label", defaultPrefix = BindingConstants.LITERAL)
     private String dismissText;
 
-    @Inject
-    private ComponentResources resources;
-
-    @Environmental
-    private JavaScriptSupport javaScriptSupport;
-
     @SessionState(create = false)
     private AlertStorage storage;
 
-    private String clientId;
+    @Inject
+    private DeprecationWarning deprecationWarning;
 
-    public String getClientId()
+    void onPageLoaded()
     {
-        return clientId;
+        deprecationWarning.ignoredComponentParameters(resources, "dismissText");
     }
 
     boolean beginRender(MarkupWriter writer)
     {
-        clientId = javaScriptSupport.allocateClientId(resources);
+        Link dismissLink = resources.createEventLink("dismiss");
+
+        storeElement(writer.element("div",
+                "data-container-type", "alerts",
+                "data-dismiss-url", dismissLink));
 
-        writer.element("div", "id", clientId);
         resources.renderInformalParameters(writer);
         writer.end();
 
-        JSONObject spec = new JSONObject("id", clientId,
-                "dismissURL", resources.createEventLink("dismiss").toURI(),
-                "dismissText", dismissText);
-
-        javaScriptSupport.addInitializerCall(InitializationPriority.EARLY, "alertManager", spec);
-
         if (storage != null)
         {
             for (Alert alert : storage.getAlerts())
             {
-                javaScriptSupport.addInitializerCall("addAlert", alert.toJSON());
+                javaScriptSupport.require("core/alert").with(alert.toJSON());
             }
 
             storage.dismissNonPersistent();

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b1bd0271/tapestry-core/src/main/java/org/apache/tapestry5/internal/alerts/AlertManagerImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/alerts/AlertManagerImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/alerts/AlertManagerImpl.java
index 3c1acce..6e56245 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/alerts/AlertManagerImpl.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/alerts/AlertManagerImpl.java
@@ -79,7 +79,7 @@ public class AlertManagerImpl implements AlertManager
         {
             public void run(JavaScriptSupport javascriptSupport)
             {
-                javascriptSupport.addInitializerCall("addAlert", alert.toJSON());
+                javascriptSupport.require("core/alert").with(alert.toJSON());
             }
         });
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/b1bd0271/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/AlertsDemo.tml
----------------------------------------------------------------------
diff --git a/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/AlertsDemo.tml b/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/AlertsDemo.tml
index 372d446..0f2445f 100644
--- a/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/AlertsDemo.tml
+++ b/tapestry-core/src/test/resources/org/apache/tapestry5/integration/app1/pages/AlertsDemo.tml
@@ -26,5 +26,4 @@
     ]
 </p>
 
-<t:alerts class='alert-class' dismissText="Zenbu Kesu"/>
 </html>