You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

233 rivejä
6.7 KiB

  1. g_status_text = [null];
  2. g_status_timer = [-1.0];
  3. g_var_progress_bar = -1;
  4. //window.setTimeout("RegisterListener()", 200);
  5. function RegisterListener()
  6. {
  7. //Register all the correct functions to the listener
  8. var div_listener = GetNaClListenerDiv();
  9. if (div_listener)
  10. {
  11. div_listener.addEventListener('loadstart', ModuleStartedLoad, true);
  12. div_listener.addEventListener('load', ModuleDidLoad, true);
  13. div_listener.addEventListener('error', ModuleError, true);
  14. div_listener.addEventListener('progress', ModuleLoadUpdate, true);
  15. div_listener.addEventListener('message', ModuleSentMessage, true);
  16. div_listener.addEventListener('crash', ModuleCrash, true);
  17. window.setTimeout("UpdateTextStatus(0.1)", 100);
  18. }
  19. else if (IsUsingNaCl())
  20. window.setTimeout("RegisterListener()", 200);
  21. }
  22. //-------------------------------------------------------------------------
  23. // MODULE LOADING FUNCTIONS
  24. //-------------------------------------------------------------------------
  25. //Indicate page has been loaded.
  26. function PageDidLoad()
  27. {
  28. //Page did load before NaCl module
  29. if (!GetNaClModuleVar())
  30. AddTextStatus('Please wait for module loading');
  31. RegisterListener();
  32. }
  33. //Module starts load
  34. function ModuleStartedLoad()
  35. {
  36. AddTextStatus('Module Started Loading');
  37. InitNaClModuleVar();
  38. }
  39. //Module progress event
  40. function ModuleLoadUpdate(event)
  41. {
  42. var progress_bar = GetProgressBar();
  43. if (progress_bar)
  44. {
  45. if (event.lengthComputable)
  46. {
  47. if (progress_bar.value < 0)
  48. g_var_progress_bar = -1;
  49. var loading_text = 'Please wait, loading [' + (Math.min(1.0, event.loaded / event.total) * 100.0).toFixed(0) + '%]';
  50. //update the progress status.
  51. progress_bar.max = event.total;
  52. progress_bar.value = event.loaded;
  53. g_var_progress_bar = AddTextStatus(loading_text, g_var_progress_bar);
  54. }
  55. //Load length is not usable.
  56. else
  57. {
  58. progress_bar.value = -1;
  59. g_var_progress_bar = AddTextStatus('Please wait, calculating load balance ...', g_var_progress_bar);
  60. }
  61. }
  62. }
  63. //Indicate module load success.
  64. function ModuleDidLoad()
  65. {
  66. if (!GetNaClModuleVar())
  67. InitNaClModuleVar();
  68. //Hide the progress div
  69. AddTextStatus('Module is live, thank you for your patience.');
  70. var hide_timer = 0.1;
  71. for (var i = 0; i < g_status_timer.length; i++)
  72. if (g_status_timer[i])
  73. hide_timer = Math.max(g_status_timer[i], hide_timer);
  74. window.setTimeout('HideProgressStatus(true)', hide_timer * 1000);
  75. }
  76. //Module did crash
  77. function ModuleCrash(event)
  78. {
  79. RestartModule();
  80. AddTextStatus("Module has crashed ! Restarting ...");
  81. }
  82. //Module had an error
  83. function ModuleError(event)
  84. {
  85. RestartModule();
  86. AddTextStatus("Module Load/start Error ! Restarting ...");
  87. }
  88. //Used to restart module on crash/error/load fail ....
  89. function RestartModule()
  90. {
  91. var id_frame_embed = GetFrameData();
  92. if (id_frame_embed)
  93. {
  94. HideProgressStatus(false);
  95. window.setTimeout('GetFrameData().contentDocument.location.reload(true)', 1000);
  96. }
  97. /*
  98. var div_embed_data = GetDivEmbedData();
  99. var div_embed_data_save = GetDivEmbedDataSave();
  100. if (div_embed_data)
  101. {
  102. HideProgressStatus(false);
  103. div_embed_data_save = div_embed_data.innerHTML;
  104. div_embed_data.innerHTML = '';
  105. }
  106. */
  107. }
  108. //Called after the Restart to effectively do it
  109. function RestartModuleRestoreEmbed()
  110. {
  111. /*
  112. var div_embed_data = GetDivEmbedData();
  113. var div_embed_data_save = GetDivEmbedDataSave();
  114. if (div_embed_data)
  115. {
  116. div_embed_data.innerHTML = div_embed_data_save;
  117. div_embed_data_save = '';
  118. HideProgressStatus(false);
  119. }
  120. */
  121. }
  122. //-------------------------------------------------------------------------
  123. // MODULE COMMUNICATION FUNCTIONS
  124. //-------------------------------------------------------------------------
  125. //Handle message from the NaCl module
  126. function ModuleSentMessage(message)
  127. {
  128. alert('Module sent message: ' + message.data);
  129. }
  130. //Called by the "Send Mesh Command !" button
  131. function SendMessageToModule()
  132. {
  133. if (GetNaClModuleVar())
  134. GetNaClModuleVar().postMessage(GetTextAreaCodeSrc().value);
  135. else
  136. alert("Module not loaded !");
  137. }
  138. //-------------------------------------------------------------------------
  139. // PROGRESS BAR STATUS FUNCTIONS
  140. //-------------------------------------------------------------------------
  141. //Update progress bar status
  142. function UpdateTextStatus(seconds)
  143. {
  144. var div_progress_status = GetDivProgressStatus();
  145. if (div_progress_status)
  146. {
  147. div_progress_status.innerHTML = '';
  148. for (var i = 0; i < g_status_text.length; i++)
  149. {
  150. if (g_status_timer[i] == undefined || g_status_timer[i] < 0.0)
  151. {
  152. g_status_timer[i] = -1.0;
  153. g_status_text[i] = undefined;
  154. }
  155. else
  156. {
  157. g_status_timer[i] -= seconds;
  158. div_progress_status.innerHTML += g_status_text[i];
  159. //div_progress_status.innerHTML += '[' + g_status_timer[i].toFixed(2) + ']';
  160. if (i < g_status_text.length - 1)
  161. div_progress_status.innerHTML += '<br>';
  162. }
  163. }
  164. window.setTimeout("UpdateTextStatus(0.1)", 100);
  165. }
  166. }
  167. //Add text status to the stack
  168. function AddTextStatus(message, message_id)
  169. {
  170. var msg_timer = 2.0;
  171. if (message_id == undefined || message_id < 0)
  172. {
  173. for (var i = g_status_timer.length - 1; i >= 0; i--)
  174. if (g_status_timer[i])
  175. msg_timer = Math.max(msg_timer, g_status_timer[i] + 0.5);
  176. g_status_text[g_status_text.length] = message;
  177. g_status_timer[g_status_timer.length] = msg_timer;
  178. return g_status_timer.length - 1;
  179. }
  180. else
  181. {
  182. g_status_text[message_id] = message;
  183. g_status_timer[message_id] = msg_timer;
  184. return message_id;
  185. }
  186. }
  187. //Hide Progress Status and empty status texts
  188. function HideProgressStatus(should_hide)
  189. {
  190. var progress_bar = GetProgressBar();
  191. var div_progress = GetDivProgress();
  192. if (div_progress && progress_bar)
  193. {
  194. if (should_hide == true)
  195. {
  196. div_progress.style.visibility = "hidden";
  197. g_status_text.splice(0, g_status_text.length);
  198. g_status_timer.splice(0, g_status_timer.length);
  199. progress_bar.value = -1;
  200. UpdateTextStatus(1.0);
  201. }
  202. else
  203. {
  204. div_progress.style.visibility = "visible";
  205. progress_bar.value = -1;
  206. AddTextStatus('Please wait for module loading');
  207. UpdateTextStatus(1.0);
  208. }
  209. }
  210. }