tyrion

index.js

Dec 1st, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. var app = {
  20.  
  21.     // Application Constructor
  22.     initialize: function() {
  23.         this.bindEvents();
  24.  
  25.         var self = this;
  26.  
  27.         $(document).ajaxError(function(event, request, settings, exception) {
  28.             // alert("Errore nella richiesta a "+settings.url);
  29.             if (exception != null) {
  30.                 // alert("Eccezione " + JSON.stringify(exception));
  31.             }
  32.         });
  33.  
  34.  
  35.         $("#offerride").click(function() {
  36.             var req = {
  37.                 isStartingTime: true,
  38.                 maxTreshold: parseInt($("#maxtreshold").val()),
  39.                 numberPlaces: parseInt($("#offer-persons").val()),
  40.                 pathRequest: [],
  41.                 phoneNumber: self.getMyPhone(),
  42.                 userName: "Pippo",
  43.                 vehicleType: $("#veicolo input:checked").val()
  44.             };
  45.             origine = $("#offer-origine").val(),
  46.             destinazione = $("#offer-destinazione").val();
  47.             if (origine == "" || destinazione == "") {
  48.                 self.showDialog("Form Incompleto", "I campi <strong>Origine</strong> e <strong>Destinazione</strong> non possono essere vuoti!");
  49.                 return;
  50.             }
  51.             var data = /\d\d\/\d\d\/\d{4} \d\d:\d\d/.exec($("#offer-data").val());
  52.             if (data == null) {
  53.                 self.showDialog("Data Invalida", "La data deve essere nel formato <strong>dd/mm/yyyy hh:mm</strong>");
  54.                 return;
  55.             }
  56.             req.requestTime = data[0];
  57.             if (self.getCoords("#offer-origine", req) === false) return;
  58.             if (self.getCoords("#offer-destinazione", req) === false) return;
  59.             console.dir(req);
  60.             console.log(JSON.stringify(req));
  61.  
  62.             var url = "http://poolmeup.appspot.com/rest/offers";
  63.             var idOffer = null;
  64.             $.post(url, req, function(data) {
  65.                 idOffer = data.idOffer;
  66.                 var s = self.parseXML(data.pathResponse);
  67.                 $.mobile.changePage("#viewpath");
  68.                 $("#actionlist").append(s).listview("refresh");
  69.  
  70.                 self.timeoutID = setInterval(function() { self.longPolling(idOffer); }, 1000*30);
  71.             });
  72.         });
  73.  
  74.  
  75.         $("#findride").click(function() {
  76.             var req = {
  77.                 friends: self.getMyFriends(),
  78.                 numberOfPerson: $("#find-persons").val(),
  79.                 idUtente: self.getMyPhone(),
  80.                 userName: "Pippo",
  81.                 pathRequest: []
  82.             },
  83.             origine = $("#find-origine").val(),
  84.             destinazione = $("#find-destinazione").val();
  85.             if (origine == "" || destinazione == "") {
  86.                 self.showDialog("Form Incompleto", "I campi <strong>Origine</strong> e <strong>Destinazione</strong> non possono essere vuoti!");
  87.                 return;
  88.             }
  89.             if (self.getCoords("#find-origine", req) === false) return;
  90.             if (self.getCoords("#find-destinazione", req) === false) return;
  91.             console.log(req);
  92.         });
  93.  
  94.     },
  95.  
  96.     getCoords: function(id, req) {
  97.         var obj = {};
  98.         // var url = "addr.json";
  99.         var url = "http://maps.googleapis.com/maps/api/geocode/json";
  100.         $.get(url, {sensor: false, address: $(id).val()}, function (data) {
  101.             var status = data.status;
  102.             if (status == "ZERO_RESULTS") {
  103.                 self.showDialog("Zero Risultati", "L'indirizzo insierito è sconosciuto. Riprovare.");
  104.                 return false;
  105.             }
  106.             data = data.results[0];
  107.             $(id).val(data.formatted_address);
  108.             obj.name = data.formatted_address;
  109.             obj.lat = data.geometry.location.lat;
  110.             obj.long = data.geometry.location.lng;
  111.             req.pathRequest.push(obj);
  112.         }, "json");
  113.         return obj;
  114.     },
  115.  
  116.     showDialog: function(title, message) {
  117.        $("#dialogtitle").text(title);
  118.        $("#dialogmessage").html(message);
  119.        $.mobile.changePage("#dialog");
  120.     },
  121.  
  122.     ICONS: {
  123.             "Start": "",
  124.             "KeepStraight": "img/direction_up.png",
  125.             "TurnSightlyLeft": "img/direction_upleft.png",
  126.             "TurnLeft": "img/direction_upthenleft.png",
  127.             "TurnRight": "img/direction_upthenright.png",
  128.             "TurnSightlyRight": "img/direction_upright.png",
  129.             "EnterRoundabout": ""
  130.     },
  131.     parseXML: function(url) {
  132.         var self = this;
  133.         var s = "";
  134.         $.get(url, function(data) {
  135.             $.each($("actn", data), function(i, el) {
  136.                 var el = $(el);
  137.                 var kind = el.attr("kind");
  138.                 if (kind != null) {
  139.                     kind = kind.replace("{street}", el.attr("addr")).replace("{dir}", el.attr("dir"));
  140.                     var link = el.parent(),
  141.                         dist = /\d+(?:[.]\d{2})?/.exec(link.attr("dist"))[0],
  142.                         time = /\d+(?:[.]\d{2})?/.exec(link.attr("time"))[0];
  143.                     s += "<li><a href=\"#\"><img src=\""+self.ICONS[el.attr("actc")]+"\">"+
  144.                          "<h3>"+kind+"</h3><p>percorsi "+dist+"m, sono passati "+time+ " secondi</p></a></li>";
  145.                 }
  146.             });
  147.         });
  148.         return s;
  149.     },
  150.  
  151.     longPolling: function(idOffer) {
  152.         var self = this;
  153.         var url = "http://poolmeup.appspot.com/rest/offers/";
  154.         $.get(url+idOffer, function(data) {
  155.             if (data != null && data.pathResponse != null) {
  156.                 clearInterval(self.timeoutID);
  157.                 self.showDialog("Richiesta trovata", "Il percorso sarà aggiornato!");
  158.                 var s = self.parseXML(data.pathResponse);
  159.                 $.mobile.changePage("#viewpath");
  160.                 $("#actionlist").html(s).listview("refresh");
  161.             }
  162.         });
  163.     },
  164.  
  165.     getMyFriends: function() {
  166.         return ["pippo", "pluto", "paperino"];
  167.     },
  168.  
  169.     getMyPhone: function() {
  170.         return 333333333333;
  171.     },
  172.  
  173.     // Bind Event Listeners
  174.     //
  175.     // Bind any events that are required on startup. Common events are:
  176.     // 'load', 'deviceready', 'offline', and 'online'.
  177.     bindEvents: function() {
  178.         document.addEventListener('deviceready', this.onDeviceReady, false);
  179.     },
  180.  
  181.     // deviceready Event Handler
  182.     //
  183.     // The scope of 'this' is the event. In order to call the 'receivedEvent'
  184.     // function, we must explicity call 'app.receivedEvent(...);'
  185.     onDeviceReady: function() {
  186.         app.receivedEvent('deviceready');
  187.     },
  188.  
  189.     // Update DOM on a Received Event
  190.     receivedEvent: function(id) {
  191.         console.log('Received Event: ' + id);
  192.     }
  193. };
Advertisement
Add Comment
Please, Sign In to add comment