You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2019/05/10 15:00:24 UTC

[myfaces-tobago] 02/04: TOBAGO-1633: Use TypeScript instead of JavaScript

This is an automated email from the ASF dual-hosted git repository.

lofwyr pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit bec284e5395327c035d88979af2b810f9284467c
Author: Udo Schnurpfeil <lo...@apache.org>
AuthorDate: Thu May 9 13:28:49 2019 +0200

    TOBAGO-1633: Use TypeScript instead of JavaScript
    
    * jsf adapter
---
 .../src/main/npm/ts/tobago-jsf.ts                  | 141 +++++++++++----------
 1 file changed, 71 insertions(+), 70 deletions(-)

diff --git a/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-jsf.ts b/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-jsf.ts
index 07d9dbf..e084b28 100644
--- a/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-jsf.ts
+++ b/tobago-theme/tobago-theme-standard/src/main/npm/ts/tobago-jsf.ts
@@ -15,81 +15,82 @@
  * limitations under the License.
  */
 
-Tobago4.Jsf = {
-  VIEW_STATE: "javax.faces.ViewState",
-  CLIENT_WINDOW: "javax.faces.ClientWindow",
-  VIEW_ROOT: "javax.faces.ViewRoot",
-  VIEW_HEAD: "javax.faces.ViewHead",
-  VIEW_BODY: "javax.faces.ViewBody",
-  RESOURCE: "javax.faces.Resource",
+namespace Tobago {
 
-  isId: function (id) {
-    switch (id) {
-      case Tobago4.Jsf.VIEW_STATE:
-      case Tobago4.Jsf.CLIENT_WINDOW:
-      case Tobago4.Jsf.VIEW_ROOT:
-      case Tobago4.Jsf.VIEW_HEAD:
-      case Tobago4.Jsf.VIEW_BODY:
-      case Tobago4.Jsf.RESOURCE:
-        return false;
-      default:
-        return true;
-    }
-  },
+  class Jsf {
 
-  isBody: function (id) {
-    switch (id) {
-      case Tobago4.Jsf.VIEW_ROOT:
-      case Tobago4.Jsf.VIEW_BODY:
-        return true;
-      default:
-        return false;
-    }
-  }
-};
+    static readonly VIEW_STATE: "javax.faces.ViewState";
+    static readonly CLIENT_WINDOW: "javax.faces.ClientWindow";
+    static readonly VIEW_ROOT: "javax.faces.ViewRoot";
+    static readonly VIEW_HEAD: "javax.faces.ViewHead";
+    static readonly VIEW_BODY: "javax.faces.ViewBody";
+    static readonly RESOURCE: "javax.faces.Resource";
 
-Tobago4.Jsf.init = function() {
-  jsf.ajax.addOnEvent(function (event) {
-    console.timeEnd("[tobago] jsf-ajax");
-    console.time("[tobago] jsf-ajax");
-    console.log("JSF event status: " + event.status);
-    if (event.status === "success") {
+    private static isId = function (id: string) {
+      switch (id) {
+        case Jsf.VIEW_STATE:
+        case Jsf.CLIENT_WINDOW:
+        case Jsf.VIEW_ROOT:
+        case Jsf.VIEW_HEAD:
+        case Jsf.VIEW_BODY:
+        case Jsf.RESOURCE:
+          return false;
+        default:
+          return true;
+      }
+    };
 
-      jQuery(event.responseXML).find("update").each(function () {
+    private static isBody = function (id) {
+      switch (id) {
+        case Jsf.VIEW_ROOT:
+        case Jsf.VIEW_BODY:
+          return true;
+        default:
+          return false;
+      }
+    };
 
-        const update = this;
-        const result: string[] = /<!\[CDATA\[(.*)]]>/s.exec(update.innerHTML);
-        const id = update.id;
-        if (result.length === 2 && result[1].startsWith("{\"reload\"")) {
-          // not modified on server, needs be reloaded after some time
-          console.debug("Found reload-JSON in response!");
-          Tobago.ReloadManager.instance.schedule(id, JSON.parse(result[1]).reload.frequency);
-        } else {
-          console.info("Update after jsf.ajax success: id='" + id + "'");
-          if (Tobago4.Jsf.isId(id)) {
-            console.debug("updating id: " + id);
-            Tobago.Listener.executeAfterUpdate(document.getElementById(id));
-          } else if (Tobago4.Jsf.isBody(id)) {
-            console.debug("updating body");
-            // there should be only one element with this class
-            Tobago.Listener.executeAfterUpdate(document.querySelector<HTMLElement>(".tobago-page"));
-          }
-        }
-      });
-    } else if (event.status === "complete") {
-      jQuery(event.responseXML).find("update").each(function () {
-        const update = this;
-        const id = update.id;
-        if ("javax.faces.ViewState" !== id) {
-          const oldElement = jQuery(Tobago4.Utils.escapeClientId(id));
-          console.info("Update after jsf.ajax complete: id='" + oldElement.attr("id") + "'");
-          if (oldElement.data("tobago-partial-overlay-set")) {
-            oldElement.overlay("destroy");
-          }
+    static init = function () {
+      jsf.ajax.addOnEvent(function (event) {
+        console.timeEnd("[tobago-jsf] jsf-ajax");
+        console.time("[tobago-jsf] jsf-ajax");
+        console.log("[tobago-jsf] JSF event status: " + event.status);
+        if (event.status === "success") {
+          event.responseXML.querySelectorAll("update").forEach(function (update: Element) {
+            const result: string[] = /<!\[CDATA\[(.*)]]>/s.exec(update.innerHTML);
+            const id = update.id;
+            if (result.length === 2 && result[1].startsWith("{\"reload\"")) {
+              // not modified on server, needs be reloaded after some time
+              console.debug("[tobago-jsf] Found reload-JSON in response!");
+              ReloadManager.instance.schedule(id, JSON.parse(result[1]).reload.frequency);
+            } else {
+              console.info("[tobago-jsf] Update after jsf.ajax success: #" + id);
+              if (Jsf.isId(id)) {
+                console.debug("[tobago-jsf] updating id: " + id);
+                Listener.executeAfterUpdate(document.getElementById(id));
+              } else if (Jsf.isBody(id)) {
+                console.debug("[tobago-jsf] updating body");
+                // there should be only one element with this class
+                Listener.executeAfterUpdate(document.querySelector<HTMLElement>(".tobago-page"));
+              }
+            }
+          });
+        } else if (event.status === "complete") {
+          event.responseXML.querySelectorAll("update").forEach(function (update: Element) {
+            const id = update.id;
+            if (Jsf.isId(id)) {
+              console.info("[tobago-jsf] Update after jsf.ajax complete: #" + id);
+              // XXX todo: re-implement overlay without jQuery!
+              const $oldElement = jQuery(document.getElementById(id));
+              if ($oldElement.data("tobago-partial-overlay-set")) {
+                 jQuery($oldElement).overlay("destroy");
+              }
+            }
+          });
         }
       });
-    }
-  });
-};
+    };
+  }
 
-Tobago.Listener.register(Tobago4.Jsf.init, Tobago.Phase.DOCUMENT_READY);
+  Listener.register(Jsf.init, Phase.DOCUMENT_READY);
+}