comparison 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
comparison
equal deleted inserted replaced
1068:50d005dd22d8 1069:5f92fa5e683a
1 <?php
2 header("Content-Type: application/javascript");
3 require_once "mconfig.inc.php";
4 require_once "msite.inc.php";
5 require_once "msession.inc.php";
6 ?>
7 //
8 // FAPWeb - Simple Web-based Demoparty Management System
9 // Party main screen viewer
10 // (C) Copyright 2012-2017 Tecnic Software productions (TNSP)
11 //
12 <?php
13 stCommonAJAX("showajax.php", "show.php");
14 ?>
15
16 var failCount = 0;
17 var lastUpdate = 0;
18 var errorView = false;
19
20
21 function updateView(txt)
22 {
23 var view = document.getElementById("mainView");
24 if (view && txt != false && txt != "")
25 view.innerHTML = txt;
26 }
27
28
29 function displayError()
30 {
31 // Increase failure count
32 if (++failCount >= 3 && !errorView)
33 {
34 errorView = true;
35 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>");
36 }
37 }
38
39
40 //
41 // Update view when triggered by main tick
42 //
43 function viewChanged()
44 {
45 var msuccess2 = function(txt)
46 {
47 // Successfully fetched new data, initiate view update
48 updateView(txt);
49 }
50
51 var msuccess1 = function(txt)
52 {
53 lastUpdate = txt;
54 jsSendPOSTRequest("action=get&type=slide", msuccess2, displayError);
55 }
56
57 jsSendPOSTRequest("action=get&type=update", msuccess1, displayError);
58 }
59
60
61 function setTickUpdate(qtime)
62 {
63 if (!timeOutSet)
64 {
65 timeOutSet = true;
66 setTimeout(function() { tickMain(); }, qtime);
67 }
68 }
69
70
71 //
72 // Main tick function, check for updates from server
73 //
74 function tickMain()
75 {
76 timeOutSet = false;
77
78 var msuccess = function(txt)
79 {
80 failCount = 0;
81 if (txt == "changed")
82 {
83 viewChanged();
84 setTickUpdate(250);
85 }
86 else
87 if (txt == "reload")
88 {
89 location.reload();
90 }
91 else
92 {
93 setTickUpdate(500);
94 }
95 }
96
97 var mfail = function(txt)
98 {
99 displayError();
100 setTickUpdate(5000);
101 }
102
103 jsSendPOSTRequest("action=check&lastUpdate="+lastUpdate, msuccess, mfail);
104 }
105
106 var timeOutSet = false;
107
108 document.addEventListener("DOMContentLoaded",
109 function ()
110 {
111 setTickUpdate(100);
112 viewChanged();
113 });
114