Advertisement
stiansjogren

index.js

Jan 12th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.84 KB | None | 0 0
  1. var ipLoactionBaseURI = new String("http://api.ipinfodb.com/v3/ip-city/?");
  2. var key = new String(
  3. "key=24dcedc0edacb640057e309a5ba987cf9ae521492bdef0df1e6ef48cb666da91");
  4.  
  5. /**
  6. * Bind event "deviceready".
  7. */
  8. function init() {
  9. console.log("init called");
  10. document.addEventListener("deviceready", bindEvents, false);
  11. }
  12.  
  13. /**
  14. * Once DOM document is loaded, add some logic.
  15. */
  16. var bindEvents = function() {
  17. console.log("DomContentLoaded OK");
  18. document.getElementById("ipLocationRaw").addEventListener("click",
  19. function() {
  20. makeRestRequest("raw")
  21. }, false);
  22.  
  23. document.getElementById("ipLocationXML").addEventListener("click",
  24. function() {
  25. makeRestRequest("xml")
  26. }, false);
  27.  
  28. document.getElementById("ipLocationJSon").addEventListener("click",
  29. function() {
  30. makeRestRequest("json")
  31. }, false);
  32.  
  33. document.getElementById("soapDictService").addEventListener("click",
  34. function() {
  35. soapDictService()
  36. }, false);
  37. }
  38.  
  39. /**
  40. * Check the connectivity, show an alert if no wifi/3G network available.
  41. *
  42. * @returns true if ok, false otherwise.
  43. */
  44. function checkNetwork() {
  45. console.log("Check the connection");
  46.  
  47. if (navigator.network.connection.type === Connection.NONE) {
  48. console.log("No Internet connection");
  49. alert("Please check your connection");
  50. return false;
  51. } else {
  52. return true;
  53. }
  54. }
  55.  
  56. /**
  57. * Send the request to the ip location webservice, initialize the callback.
  58. *
  59. * @param format: response type accepted.
  60. */
  61. function makeRestRequest(format) {
  62. if (checkNetwork() != true) {
  63. console.log("No connection. No Request sent");
  64. return;
  65. }
  66.  
  67. var parameter = format === "xml" ? "&format=xml"
  68. : format === "json" ? "&format=json" : "";
  69.  
  70. var ajax = new XMLHttpRequest();
  71. ajax.onreadystatechange = function() {
  72. responseCallBack(ajax, format);
  73. }
  74.  
  75. var uri = ipLoactionBaseURI.concat(key, parameter);
  76. ajax.open("GET", uri);
  77. ajax.send();
  78.  
  79. console.log("Sent Request " + uri);
  80. }
  81.  
  82. /**
  83. * XMLHTTPRequest callback, if sate = 4 and response 200 call function injecting
  84. * html code to the element id = main.
  85. *
  86. * @parm ajax: the XMLHttpRequest.
  87. * @param format: format of the response.
  88. */
  89. function responseCallBack(ajax, format) {
  90. console.log("Callback called");
  91. if (ajax.readyState == 4 && (ajax.status == 200)) {
  92. console.log("Response " + ajax.responseText);
  93. console.log(format);
  94.  
  95. switch(format){
  96. case "raw" : formatText(ajax.responseText)
  97. break;
  98. case "xml" :formatXML(ajax.responseXML)
  99. break;
  100. default: formatJSON(JSON.parse(new String(ajax.responseText)));
  101. }
  102. }
  103. }
  104.  
  105. /**
  106. * Inject HTML code from a text response.
  107. *
  108. * @param text
  109. * response.
  110. */
  111. function formatText(text) {
  112. var sliced = text.split(";");
  113. var theHtml = new String("<h4> raw response</h4>");
  114. theHtml = theHtml.concat("<h2> IP: ", sliced[2], "</h2>");
  115. theHtml = theHtml.concat("<h2> Country: ", sliced[4], "(", sliced[3], ")",
  116. "</h2>");
  117. theHtml = theHtml.concat("<h2>", sliced[6], " - ", sliced[5], "</h2>");
  118. theHtml = theHtml.concat("<h2>", sliced[8], " ", sliced[9], "</h2>");
  119. theHtml = theHtml.concat("<h2> GMT ", sliced[10], "</h2>");
  120.  
  121. document.getElementById("main").innerHTML = theHtml;
  122. console.log("Formated raw text");
  123. }
  124.  
  125. /**
  126. * Inject HTML code from a XML response.
  127. *
  128. * @param DOM
  129. * response.
  130. */
  131. function formatXML(response) {
  132. var nodes = response.documentElement.childNodes;
  133. var theHtml = new String("<h4> XML response</h4>");
  134.  
  135. // format the xml into html
  136. // use a for loop from 0 to nodes.length
  137.  
  138. for(var i=0;i<nodes.length;i++){
  139. theHtml = theHtml.concat(nodes[i].tagName(), ":" , nodes[i].childNodes[0].nodeValue);
  140. }
  141.  
  142. document.getElementById('main').innerHTML=theHtml ;
  143. }
  144.  
  145. /**
  146. * Inject HTML code from a JSON object.
  147. *
  148. * @param json object.
  149. */
  150. function formatJSON(jsonT) {
  151. var keys = Object.keys(jsonT);
  152. var theHtml = new String("<h4> JSon response</h4>");
  153. // format the json into html
  154. // use a for loop from 0 to nodes.length key.length
  155. for(var i=0;i<keys.length;i++){
  156. theHtml = theHtml.concat("<h2>",keys[i]," : ", jsonT[keys[i]],"</h2>");
  157. }
  158.  
  159. document.getElementById('main').innerHTML=theHtml ;
  160.  
  161.  
  162. // use method x.innerHTML to add theHtml to the main
  163. }
  164.  
  165. /**
  166. * Sample of a SOAP webservice.
  167. * note that in soap, the envolpe is standard, while the soap body is proprietary
  168. */
  169. function soapDictService() {
  170. var wordToSearch = document.getElementById("wordSearch").value;
  171.  
  172. console.log("soapDictService called worldToSearch " + wordToSearch);
  173. if (wordToSearch.length === 0) {
  174. var theHtml = new String("<h4> No definition found. </h4>");
  175. document.getElementById("main").innerHTML = theHtml;
  176. return;
  177. }
  178.  
  179. var ajax = new XMLHttpRequest();
  180. var url = "http://services.aonaware.com/DictService/DictService.asmx";
  181.  
  182. var soapBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  183. + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
  184. + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
  185. + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
  186. + "<soap:Body>"
  187. + "<Define xmlns=\"http://services.aonaware.com/webservices/\">"
  188. + "<word>" + wordToSearch + "</word>" + "</Define>"
  189. + "</soap:Body>" + "</soap:Envelope>";
  190.  
  191. ajax.timeout = 6000;
  192.  
  193. ajax.onreadystatechange = function() {
  194. if (ajax.readyState == 4) {
  195. if (ajax.status == 200) {
  196. var documentResponse = ajax.responseXML;
  197. var definitions = documentResponse
  198. .getElementsByTagName("Definition");
  199. var theHtml = definitions.length === 0 ? "<h4>No definitions found</h4>"
  200. : soapformatDefinitions(definitions);
  201. document.getElementById("main").innerHTML = theHtml;
  202. } else {
  203. console
  204. .log("Dict service received response code"
  205. + ajax.status)
  206. alert("Unexpected error connecting service");
  207. }
  208. }
  209. }
  210.  
  211. ajax.open("POST", url);
  212. ajax.setRequestHeader("SOAPAction",
  213. "\"http://services.aonaware.com/webservices/Define\"");
  214. ajax.setRequestHeader("Content-Type", "text/xml");
  215. ajax.send(soapBody);
  216.  
  217. };
  218.  
  219. /**
  220. * Format the soap response.
  221. *
  222. * @param nodelist
  223. * @returns {String} html code.
  224. */
  225. function soapformatDefinitions(nodelist) {
  226. console.log("definitions length " + nodelist.length);
  227. // here, you receive a table that includes all the answers.
  228. // take the first answer
  229.  
  230. var theHtml = new String(nodelist[0].innerHTML);
  231.  
  232. // format the answer in theHtml var
  233.  
  234. return theHtml;
  235. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement