diff showajax.js.php @ 1069:5f92fa5e683a

Refactor how the "AJAX" stuff works.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 24 Jan 2017 17:25:48 +0200
parents
children 76e11ae923a7
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/showajax.js.php	Tue Jan 24 17:25:48 2017 +0200
@@ -0,0 +1,114 @@
+<?php
+header("Content-Type: application/javascript");
+require_once "mconfig.inc.php";
+require_once "msite.inc.php";
+require_once "msession.inc.php";
+?>
+//
+// FAPWeb - Simple Web-based Demoparty Management System
+// Party main screen viewer
+// (C) Copyright 2012-2017 Tecnic Software productions (TNSP)
+//
+<?php
+stCommonAJAX("showajax.php", "show.php");
+?>
+
+var failCount = 0;
+var lastUpdate = 0;
+var errorView = false;
+
+
+function updateView(txt)
+{
+  var view = document.getElementById("mainView");
+  if (view && txt != false && txt != "")
+    view.innerHTML = txt;
+}
+
+
+function displayError()
+{
+  // Increase failure count
+  if (++failCount >= 3 && !errorView)
+  {
+    errorView = true;
+    updateView("<div class=\"slideHeader\"><div class=\"slideHeaderDiv\"></div></div><div class=\"slideText\"><div class=\"guru\">Software Failure.&nbsp;&nbsp;&nbsp;Press left mouse button to continue.<br />Guru Meditation #00000004.0000AAC0</div></div>");
+  }
+}
+
+
+//
+// Update view when triggered by main tick
+//
+function viewChanged()
+{
+  var msuccess2 = function(txt)
+  {
+    // Successfully fetched new data, initiate view update
+    updateView(txt);
+  }
+
+  var msuccess1 = function(txt)
+  {
+    lastUpdate = txt;
+    jsSendPOSTRequest("action=get&type=slide", msuccess2, displayError);
+  }
+
+  jsSendPOSTRequest("action=get&type=update", msuccess1, displayError);
+}
+
+
+function setTickUpdate(qtime)
+{
+  if (!timeOutSet)
+  {
+    timeOutSet = true;
+    setTimeout(function() { tickMain(); }, qtime);
+  }
+}
+
+
+//
+// Main tick function, check for updates from server
+//
+function tickMain()
+{
+  timeOutSet = false;
+
+  var msuccess = function(txt)
+  {
+    failCount = 0;
+    if (txt == "changed")
+    {
+      viewChanged();
+      setTickUpdate(250);
+    }
+    else
+    if (txt == "reload")
+    {
+      location.reload();
+    }
+    else
+    {
+      setTickUpdate(500);
+    }
+  }
+  
+  var mfail = function(txt)
+  {
+    displayError();
+    setTickUpdate(5000);
+  }
+
+  jsSendPOSTRequest("action=check&lastUpdate="+lastUpdate, msuccess, mfail);
+}
+
+var timeOutSet = false;
+
+document.addEventListener("DOMContentLoaded",
+function ()
+{
+  setTickUpdate(100);
+  viewChanged();
+});
+