application.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /* Copyright (C) 2019 Intel Corporation. All rights reserved.
  2. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  3. */
  4. /*
  5. * Dom Location
  6. *
  7. */
  8. function setDivCenter(divname)
  9. // make qn element center aligned
  10. {
  11. var Top =($(window).height()-$(divname).height())/2;
  12. var Left = ($(window).width()-$(divname).width())/2;
  13. var scrollTop = $(document).scrollTop();
  14. var scrollLeft = $(document).scrollLeft();
  15. $(divname).css({posisiton:'absolute','top':Top+scrollTop,'left':Left+scrollLeft});
  16. };
  17. setDivCenter(".middlebox");
  18. setDivCenter(".deletebox");
  19. function setmain(divname){
  20. // Set the pop-up window of apps for download at the right place
  21. var x = $('#btn').offset().top;
  22. var Top = x + $('#btn').height()+15;
  23. var y = $('#btn').offset().left;
  24. var Left = y + ($('#btn').width()/2)-($(divname).width()/2);
  25. console.log(Top,Left)
  26. $(divname).css({'top':Top,'left':Left});
  27. }
  28. setmain(".main")
  29. /*
  30. * download apps
  31. *
  32. */
  33. function getthis(val)
  34. //Telling background which app to be loaded from appstore_list and to be installed in the current device.
  35. {
  36. /* Get the ip adress and the port of a device, as well as the application ID to be downloaded on this device*/
  37. var ip,port,name,version;
  38. var ipArr=$("#IPs").text().split(":");
  39. ip=ipArr[1];
  40. var portArr=$("#ports").text().split(":");
  41. port=portArr[1];
  42. name = $(val).parent().find("#appsinfo1").text().split(":")[1];
  43. version = $(val).parent().find("#appsinfo2").text().split(":")[1];
  44. $(".main").fadeOut();
  45. for (num in alist){
  46. if (alist[num]['pname'].trim() == name.trim())
  47. {alert("This app has been downloaded.");
  48. return;}};
  49. $("#loading").fadeIn();
  50. var sNode = document.getElementById("APPS");
  51. var tempNode= sNode.cloneNode(true);
  52. sNode.parentNode.appendChild(tempNode);
  53. $("#appinfo1").html("Product Name : "+ name);
  54. $("#appinfo2").html("Status : "+"Installing");
  55. $("#appinfo3").html("Current_Version : "+ version);
  56. $.get("/appDownload/",{'ip':ip.trim(),'port':port.trim(),'name':name.trim(),},function (ret) {
  57. var status = $.trim(ret.split(":")[1].split("}")[0]);
  58. $(".loadapp").html(name+" is downloading now");
  59. var msg = JSON.parse(status)
  60. console.log(msg)
  61. if (JSON.parse(status)=="ok"){
  62. $(".middlebox").fadeIn();
  63. $(".sourceapp").fadeOut();
  64. $("#loading").fadeOut();
  65. $(".findapp").html("Download "+name +" successfully");
  66. $(".surebtn").click(function (){
  67. $(".middlebox").fadeOut();
  68. window.location.reload();
  69. })}
  70. else if (JSON.parse(status)=="Fail!"){
  71. alert("Download failed!");
  72. $("#loading").fadeOut();
  73. sNode.remove();
  74. }
  75. else {
  76. alert("Install app failed:" + msg)
  77. $("#loading").fadeOut();
  78. sNode.remove();
  79. }
  80. })
  81. };
  82. window.onload = function clone()
  83. //Add & Delete apps to the device.
  84. {
  85. /*Install Apps*/
  86. var sourceNode = document.getElementById("APPS");
  87. if (alist.length != 0)
  88. {
  89. $("#appinfo1").html("Product Name : "+ alist[0]['pname']);
  90. $("#appinfo2").html("Status : "+ alist[0]['status']);
  91. $("#appinfo3").html("Current_Version : "+ alist[0]['current_version']);
  92. $("#delete").attr('class','delet0');
  93. $("#APPS").attr('class','app0');
  94. for (var i=1; i<alist.length; i++)
  95. {
  96. var cloneNode= sourceNode.cloneNode(true);
  97. sourceNode.parentNode.appendChild(cloneNode);
  98. $("#appinfo1").html("Product Name : "+ alist[i]['pname']);
  99. $("#appinfo2").html("Status : "+ alist[i]['status']);
  100. $("#appinfo3").html("Current_Version : "+ alist[i]['current_version']);
  101. $("#delete").attr('class','delet'+i);
  102. $("#APPS").attr('class','app'+i);
  103. }
  104. }
  105. $("#IPs").html("IP : "+ dlist[0]['IP']);
  106. $("#ports").html("Port : "+ dlist[0]['Port']);
  107. $("#installs").html("Installed Apps : "+ dlist[0]['apps']);
  108. $(".mybtn").click(function ()
  109. {
  110. /*uninstall apps*/
  111. var thisitem = $(this).parent().attr('class');
  112. var indexa = thisitem.match(/\d+\b/);
  113. var pname = $(".app"+indexa).find('#appinfo1').text();
  114. var ip,port;
  115. var ipArr=$("#IPs").text().split(":");
  116. ip=ipArr[1];
  117. var portArr=$("#ports").text().split(":");
  118. port=portArr[1];
  119. var name = pname.split(':')[1].trim();
  120. $(".deletebox").fadeIn();
  121. $(".findapp").html("Are you sure to delete "+name);
  122. $(".suresbtn").click(function (){
  123. $(".app"+indexa).remove();
  124. $.get("/appDelete/",{'ip':ip.trim(),'port':port.trim(),"name":pname.split(':')[1].trim()},function (ret) {
  125. console.log(ret);});
  126. $(".deletebox").fadeOut();
  127. window.location.reload();
  128. })
  129. $(".cancelsbtn").click(function (){
  130. $(".deletebox").fadeOut(); })
  131. });
  132. };
  133. function getdownloadapps()
  134. {
  135. /*Acquire apps for download from Appstore simultaneously whenever appstore is updated*/
  136. if (search_node[0] == "Nothing find"){
  137. alert(search_node[0])
  138. }
  139. if (search_node.length == 1 && search_node[0] != "Nothing find" ){
  140. $("#appsinfo1").html("Product Name : "+ search_node[0]['ID']);
  141. $("#appsinfo2").html("Version : "+ search_node[0]['Version']);
  142. }
  143. else{
  144. var sourceNode = document.getElementById("Dapplications");
  145. if (llist.length != 0)
  146. {
  147. $("#appsinfo1").html("Product Name : "+ llist[0]['ID']);
  148. $("#appsinfo2").html("Version : "+ llist[0]['Version']);
  149. $("#Dapplications").attr('class','dapp0');
  150. for (var i=1; i<llist.length; i++)
  151. {
  152. var cloneNode= sourceNode.cloneNode(true);
  153. sourceNode.parentNode.appendChild(cloneNode);
  154. $("#appsinfo1").html("Product Name : "+ llist[i]['ID']);
  155. $("#appsinfo2").html("Version : "+ llist[i]['Version']);
  156. $("#Dapplications").attr('class','dapp'+i);
  157. }
  158. }};
  159. };
  160. getdownloadapps();
  161. function givevalue(){
  162. var ip=dlist[0]['IP'].trim();
  163. var port=dlist[0]['Port'].trim();
  164. document.getElementById("aa").value = ip;
  165. document.getElementById("bb").value = port;
  166. if (open_status == "open"){
  167. $(".main").fadeIn();
  168. $(".close").click(function(){
  169. $(".main").fadeOut();
  170. var newurl = "?"+"ip="+ip+"&port="+port;
  171. window.location.href= newurl;});
  172. $(".mybtn2").click(function(){
  173. if (alist.length >=3){
  174. alert("Install app failed: exceed max app installations.")
  175. }
  176. $(".main").fadeOut();
  177. getthis(".mybtn2");
  178. var newurl = "?"+"ip="+ip+"&port="+port;
  179. window.location.href= newurl;
  180. });
  181. }
  182. }
  183. givevalue();
  184. function popbox(){
  185. /*Open and close the "install apps" window*/
  186. $(".btn").click(function(){
  187. $(".main").fadeIn();
  188. });
  189. $(".close").click(function(){
  190. $(".main").fadeOut();
  191. });
  192. };
  193. popbox();