refreshed.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) Copyright (C) 2008 - 2013 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. # Open Game Panel refreshed Class
  25. # Wrote by: Nirock
  26. # Sample Setup:
  27. /*
  28. // Using the refreshed class
  29. require_once("includes/refreshed.php");
  30. $refresh = new refreshed();
  31. $pos = $refresh->add("home.php?m=gamemanager&p=log2&type=pop&home_id=5",5000);
  32. echo $refresh->getdiv($pos,"height:500px;overflow : auto;");
  33. //Build:
  34. ?><script type="text/javascript">$(document).ready(function(){ <?php echo $refresh->build(); ?>} ); </script><?php
  35. */
  36. class refreshed
  37. {
  38. private $url;
  39. private $pos;
  40. public function __construct()
  41. {
  42. $this->url = array();
  43. $this->pos = 0;
  44. }
  45. public function add($url)
  46. {
  47. $this->url[$this->pos] = $url;
  48. $this->pos++;
  49. return $this->pos-1;
  50. }
  51. public function getdiv($pos,$style = "")
  52. {
  53. return "<div id='refreshed-".$pos."' style='".$style."'></div>";
  54. }
  55. public function build($time = "4000")
  56. {
  57. if($_GET['m'] == 'gamemanager' and $_GET['p'] == 'game_monitor')
  58. {
  59. $switch = "";
  60. for($i=0;$i<$this->pos;$i++)
  61. {
  62. $load = "$('#refreshed-$i').load('".$this->url[$i]."');\n";
  63. $refresh = "window.refreshId$i = setInterval(function() {\n".
  64. $load.
  65. "}, $time);\n";
  66. $stopRefresh = "clearInterval(window.refreshId$i);\n";
  67. if(isset($_GET['home_id']) or isset($_GET['home_id-mod_id-ip-port']))
  68. {
  69. $start_expanded = $load.$refresh;
  70. $isUndefined = " || typeof collapsible === 'undefined'";
  71. }
  72. else
  73. {
  74. $start_expanded = "";
  75. $isUndefined = " && typeof collapsible !== 'undefined'";
  76. }
  77. $switch .= "if( pos == $i && status == 'online' )\n{\n".
  78. "if(collapsible == 'expanded'$isUndefined){\n".
  79. $stopRefresh.
  80. "}\nelse\n{\n".
  81. $load.
  82. $refresh.
  83. "}\n".
  84. "}\n";
  85. }
  86. return "$.ajaxSetup({ cache: false });\n".
  87. $start_expanded.
  88. "$('.collapsible').click(function(){\n".
  89. "var collapsible = $(this).find('a').attr('class');\n".
  90. "var pos = $(this).attr('data-pos');\n".
  91. "var status = $(this).attr('data-status');\n".
  92. $switch.
  93. "});\n";
  94. }
  95. else
  96. {
  97. $first = "";
  98. $second = "";
  99. for($i=0;$i<$this->pos;$i++)
  100. {
  101. $ref = '#refreshed-'.$i;
  102. $first .= "\n".
  103. "\tjQuery.ajax({\n".
  104. "\t url: \"".$this->url[$i]."\",\n".
  105. "\t cache: false,\n".
  106. "\t beforeSend: function(xhr) {\n".
  107. "\t $('a').click(function() {\n".
  108. "\t xhr.abort();\n".
  109. "\t enableCallbacks = false;\n".
  110. "\t });\n".
  111. "\t },\n".
  112. "\t success: function(data, textStatus) {\n".
  113. "\t if (!enableCallbacks) return;\n".
  114. "\t $('".$ref."').html(data);\n";
  115. if($_GET['m'] == 'gamemanager' and $_GET['p'] == 'log')
  116. {
  117. $first .= "\t $('".$ref."').animate({ scrollTop: $('".$ref."').prop('scrollHeight')*3}, 3000);\n";
  118. }
  119. $first .= "\t }\n".
  120. "\t});\n";
  121. $second .= "\n".
  122. "\t\tjQuery.ajax({\n".
  123. "\t\t url: \"".$this->url[$i]."\",\n".
  124. "\t\t cache: false,\n".
  125. "\t\t beforeSend: function(xhr) {\n".
  126. "\t\t $('a').click(function() {\n".
  127. "\t\t xhr.abort();\n".
  128. "\t\t enableCallbacks = false;\n".
  129. "\t\t });\n".
  130. "\t\t },\n".
  131. "\t\t success: function(data, textStatus) {\n".
  132. "\t\t if (!enableCallbacks) return;\n".
  133. "\t\t $('".$ref."').html(data);\n";
  134. if($_GET['m'] == 'gamemanager' and $_GET['p'] == 'log')
  135. {
  136. $second .= "\t\t $('".$ref."').animate({ scrollTop: 9999});\n";
  137. }
  138. $second .= "\t\t }\n".
  139. "\t\t});\n";
  140. }
  141. return "var enableCallbacks = true;".
  142. $first.
  143. "\n\tvar refreshId = setInterval(function() {".
  144. $second.
  145. "\n\t}, $time);\n".
  146. "\t$('a').click(function() {\n".
  147. "\t clearInterval(refreshId);\n".
  148. "\t});\n";
  149. }
  150. }
  151. }
  152. ?>