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.

nacl_loading.js 6.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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()", 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() == null)
  30. AddTextStatus('Please wait for module loading');
  31. }
  32. //Module starts load
  33. function ModuleStartedLoad()
  34. {
  35. AddTextStatus('Module Started Loading');
  36. InitNaClModuleVar();
  37. }
  38. //Module progress event
  39. function ModuleLoadUpdate(event)
  40. {
  41. var progress_bar = GetProgressBar();
  42. if (progress_bar)
  43. {
  44. if (event.lengthComputable)
  45. {
  46. if (progress_bar.value < 0)
  47. g_var_progress_bar = -1;
  48. var loading_text = 'Please wait, loading [' + (Math.min(1.0, event.loaded / event.total) * 100.0).toFixed(0) + '%]';
  49. //update the progress status.
  50. progress_bar.max = event.total;
  51. progress_bar.value = event.loaded;
  52. g_var_progress_bar = AddTextStatus(loading_text, g_var_progress_bar);
  53. }
  54. //Load length is not usable.
  55. else
  56. {
  57. progress_bar.value = -1;
  58. g_var_progress_bar = AddTextStatus('Please wait, calculating load balance ...', g_var_progress_bar);
  59. }
  60. }
  61. }
  62. //Indicate module load success.
  63. function ModuleDidLoad()
  64. {
  65. if (GetNaClModuleVar() == null)
  66. InitNaClModuleVar();
  67. //Hide the progress div
  68. AddTextStatus('Module is live, thank you for your patience.');
  69. window.setTimeout('HideProgressStatus(true)', 4000);
  70. }
  71. //Module did crash
  72. function ModuleCrash(event)
  73. {
  74. RestartModule();
  75. AddTextStatus("Module has crashed ! Restarting ...");
  76. }
  77. //Module had an error
  78. function ModuleError(event)
  79. {
  80. RestartModule();
  81. AddTextStatus("Module Load/start Error ! Restarting ...");
  82. }
  83. //Used to restart module on crash/error/load fail ....
  84. function RestartModule()
  85. {
  86. var div_embed_data = GetDivEmbedData();
  87. var div_embed_data_save = GetDivEmbedDataSave();
  88. if (div_embed_data && div_embed_data_save)
  89. {
  90. HideProgressStatus(false);
  91. div_embed_data_save = div_embed_data.innerHTML;
  92. div_embed_data.innerHTML = '';
  93. window.setTimeout('RestartModuleRestoreEmbed()', 1000);
  94. }
  95. }
  96. function RestartModuleRestoreEmbed()
  97. {
  98. var div_embed_data = GetDivEmbedData();
  99. var div_embed_data_save = GetDivEmbedDataSave();
  100. if (div_embed_data && div_embed_data_save)
  101. {
  102. div_embed_data.innerHTML = div_embed_data_save;
  103. div_embed_data.innerHTML = '';
  104. HideProgressStatus(false);
  105. }
  106. }
  107. //-------------------------------------------------------------------------
  108. // MODULE COMMUNICATION FUNCTIONS
  109. //-------------------------------------------------------------------------
  110. //Handle message from the NaCl module
  111. function ModuleSentMessage(message)
  112. {
  113. alert('Module sent message: ' + message.data);
  114. }
  115. //Called by the "Send Mesh Command !" button
  116. function SendMessageToModule()
  117. {
  118. if (GetNaClModuleVar())
  119. GetNaClModuleVar().postMessage(GetTextAreaCodeSrc().value);
  120. else
  121. alert("Module not loaded !");
  122. }
  123. //-------------------------------------------------------------------------
  124. // PROGRESS BAR STATUS FUNCTIONS
  125. //-------------------------------------------------------------------------
  126. //Update progress bar status
  127. function UpdateTextStatus(seconds)
  128. {
  129. var div_progress_status = GetDivProgressStatus();
  130. if (div_progress_status)
  131. {
  132. div_progress_status.innerHTML = '';
  133. for (var i = 0; i < g_status_text.length; i++)
  134. {
  135. if (g_status_timer[i] == undefined || g_status_timer[i] < 0.0)
  136. {
  137. g_status_timer[i] = -1.0;
  138. g_status_text[i] = undefined;
  139. }
  140. else
  141. {
  142. g_status_timer[i] -= seconds;
  143. div_progress_status.innerHTML += g_status_text[i];
  144. if (i < g_status_text.length - 1)
  145. div_progress_status.innerHTML += '<br>';
  146. }
  147. }
  148. window.setTimeout("UpdateTextStatus()", 100);
  149. }
  150. }
  151. //Add text status to the stack
  152. function AddTextStatus(message, message_id)
  153. {
  154. if (message_id == undefined || message_id < 0)
  155. {
  156. g_status_text[g_status_text.length] = message;
  157. g_status_timer[g_status_timer.length] = 5.0;
  158. return g_status_timer.length - 1;
  159. }
  160. else
  161. {
  162. g_status_text[message_id] = message;
  163. g_status_timer[message_id] = 5.0;
  164. return message_id;
  165. }
  166. }
  167. //Hide Progress Status and empty status texts
  168. function HideProgressStatus(should_hide)
  169. {
  170. var progress_bar = GetProgressBar();
  171. var div_progress = GetDivProgress();
  172. if (div_progress && progress_bar)
  173. {
  174. if (should_hide == true)
  175. {
  176. div_progress.style.visibility = "hidden";
  177. g_status_text.splice(0, g_status_text.length);
  178. g_status_timer.splice(0, g_status_timer.length);
  179. progress_bar.value = -1;
  180. UpdateTextStatus(0.1);
  181. }
  182. else
  183. {
  184. div_progress.style.visibility = "visible";
  185. progress_bar.value = -1;
  186. AddTextStatus('Please wait for module loading');
  187. UpdateTextStatus(0.1);
  188. }
  189. }
  190. }