Move most of the JavaScript to sone.js.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 21 Oct 2010 12:36:56 +0000 (14:36 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 21 Oct 2010 12:36:56 +0000 (14:36 +0200)
src/main/resources/static/javascript/sone.js
src/main/resources/templates/include/head.html
src/main/resources/templates/include/viewSone.html
src/main/resources/templates/index.html
src/main/resources/templates/viewSone.html

index 5d75952..6420e5b 100644 (file)
@@ -56,3 +56,68 @@ function addCommentLinks() {
                });
        });
 }
+
+/**
+ * Fires off an AJAX request to retrieve the current status of a Sone.
+ *
+ * @param soneId
+ *            The ID of the Sone
+ */
+function getSoneStatus(soneId) {
+       $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
+               updateSoneStatus(soneId, data.status, data.modified, data.lastUpdated);
+               /* seconds! */
+               updateInterval = 60;
+               if (data.age < 600) {
+                       updateInterval = 5;
+               } else if (data.age < 86400) {
+                       updateInterval = 30;
+               }
+               setTimeout(function() {
+                       getSoneStatus(soneId);
+               }, updateInterval * 1000);
+       });
+}
+
+/**
+ * Updates the status of the given Sone.
+ *
+ * @param soneId
+ *            The ID of the Sone to update
+ * @param status
+ *            The status of the Sone (“idle”, “unknown”, “inserting”,
+ *            “downloading”)
+ * @param modified
+ *            Whether the Sone is modified
+ * @param lastUpdated
+ *            The date and time of the last update (formatted for display)
+ */
+function updateSoneStatus(soneId, status, modified, lastUpdated) {
+       $("#sone .sone#" + soneId).
+               toggleClass("unknown", status == "unknown").
+               toggleClass("idle", status == "idle").
+               toggleClass("inserting", status == "inserting").
+               toggleClass("downloading", status == "downloading").
+               toggleClass("modified", modified);
+       $("#sone .sone#" + soneId + " .last-update span.time").text(lastUpdated);
+}
+
+var watchedSones = {};
+
+/**
+ * Watches this Sone for updates to its status.
+ *
+ * @param soneId
+ *            The ID of the Sone to watch
+ */
+function watchSone(soneId) {
+       if (watchedSones[soneId]) {
+               return;
+       }
+       watchedSones[soneId] = true;
+       (function(soneId) {
+               setTimeout(function() {
+                       getSoneStatus(soneId);
+               }, 5000);
+       })(soneId);
+}
index 6909bb4..7798af4 100644 (file)
@@ -4,11 +4,29 @@
        <script src="javascript/sone.js" language="javascript"></script>
 
        <script language="javascript">
+               /* this initializes the status update input field. */
                $(document).ready(function() {
                        registerInputTextareaSwap("#sone #update-status .status-input", "WebInterface.DefaultText.StatusUpdate", "text", false);
                });
        </script>
 
+       <script language="javascript">
+               /* these functions are necessary for updating Sone statuses. */
+               $(document).ready(function() {
+                       $("#sone .sone").each(function() {
+                               watchSone($(this).attr("id"));
+                       });
+               });
+       </script>
+
+       <script language="javascript">
+               /* this initializes all reply input fields. */
+               $(document).ready(function() {
+                       registerInputTextareaSwap("#sone input.reply-input", "WebInterface.DefaultText.Reply", "text", false);
+                       addCommentLinks();
+               });
+       </script>
+
        <div id="main">
 
                <div id="profile" class="<%ifnull currentSone>offline<%else>online<%/if>">
index 0cf4545..e7599bd 100644 (file)
@@ -1,49 +1,3 @@
-<script language="javascript">
-
-       function getStatus(soneId) {
-               $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
-                       updateStatus(soneId, data.status, data.modified, data.lastUpdated);
-                       /* seconds! */
-                       updateInterval = 60;
-                       if (data.age < 600) {
-                               updateInterval = 5;
-                       } else if (data.age < 86400) {
-                               updateInterval = 30;
-                       }
-                       setTimeout(function() {
-                               getStatus(soneId);
-                       }, updateInterval * 1000);
-               });
-       }
-
-       function updateStatus(soneId, status, modified, lastUpdated) {
-               $("#sone .sone#" + soneId).
-                       toggleClass("unknown", status == "unknown").
-                       toggleClass("idle", status == "idle").
-                       toggleClass("inserting", status == "inserting").
-                       toggleClass("downloading", status == "downloading").
-                       toggleClass("modified", modified);
-               $("#sone .sone#" + soneId + " .last-update span.time").text(lastUpdated);
-       }
-
-       registeredSones = {};
-
-       $(document).ready(function() {
-               $("#sone .sone").each(function() {
-                       soneId = $(this).attr("id");
-                       if (registeredSones[soneId]) {
-                               return;
-                       }
-                       registeredSones[soneId] = true;
-                       (function(soneId) {
-                               setTimeout(function() {
-                                       getStatus(soneId);
-                               }, 5000);
-                       })(soneId);
-               });
-       });
-</script>
-
 <div id="<% sone.id|html>" class="sone <% sone.status|html><%if sone.modified> modified<%/if>">
        <div class="unknown-marker" title="<%= View.Sone.Status.Unknown|l10n|html>">?</div>
        <div class="modified-marker" title="<%= View.Sone.Status.Modified|l10n|html>">!</div>
index 8f18974..8ec8384 100644 (file)
@@ -1,13 +1,5 @@
 <%include include/head.html>
 
-       <script language="javascript">
-               /* register input field/textarea swap. */
-               $(document).ready(function() {
-                       registerInputTextareaSwap("#sone input.reply-input", "WebInterface.DefaultText.Reply", "text", false);
-                       addCommentLinks();
-               });
-       </script>
-
        <h1><%= Page.Index.PostList.Title|l10n|html></h1>
 
        <div id="posts">
index e8ed4f7..827aa11 100644 (file)
@@ -1,11 +1,5 @@
 <%include include/head.html>
 
-       <script language="javascript">
-               $(document).ready(function() {
-                       addCommentLinks();
-               });
-       </script>
-
        <%ifnull sone.name>
 
                <h1><%= Page.ViewSone.Page.TitleWithoutSone|l10n|html></h1>