Animate icon on activity.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 251105a..a465b24 100644 (file)
@@ -1028,6 +1028,7 @@ function resetActivity() {
        if (title.indexOf('(') == 0) {
                document.title = title.substr(title.indexOf(' ') + 1);
        }
+       changeIcon("images/icon.png");
 }
 
 function setActivity() {
@@ -1036,9 +1037,43 @@ function setActivity() {
                if (title.indexOf('(') != 0) {
                        document.title = "(!) " + title;
                }
+               setTimeout(toggleIcon, 1500);
        }
 }
 
+/** Whether the icon is currently showing activity. */
+var iconActive = false;
+
+/**
+ * Toggles the icon. If the window has gained focus and the icon is still
+ * showing the activity state, it is returned to normal.
+ */
+function toggleIcon() {
+       if (focus) {
+               if (iconActive) {
+                       changeIcon("images/icon.png");
+                       iconActive = false;
+               }
+       } else {
+               iconActive = !iconActive;
+               console.log("showing icon: " + iconActive);
+               changeIcon(iconActive ? "images/icon-activity.png" : "images/icon.png");
+               setTimeout(toggleIcon, 1500);
+       }
+}
+
+/**
+ * Changes the icon of the page.
+ *
+ * @param iconUrl
+ *            The new URL of the icon
+ */
+function changeIcon(iconUrl) {
+       $("link[rel=icon]").remove();
+       $("head").append($("<link>").attr("rel", "icon").attr("type", "image/png").attr("href", iconUrl));
+       $("iframe[id=icon-update]")[0].src += "";
+}
+
 /**
  * Creates a new notification.
  *