Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- var app = {
- // Application Constructor
- initialize: function() {
- this.bindEvents();
- var self = this;
- $(document).ajaxError(function(event, request, settings, exception) {
- // alert("Errore nella richiesta a "+settings.url);
- if (exception != null) {
- // alert("Eccezione " + JSON.stringify(exception));
- }
- });
- $("#offerride").click(function() {
- var req = {
- isStartingTime: true,
- maxTreshold: parseInt($("#maxtreshold").val()),
- numberPlaces: parseInt($("#offer-persons").val()),
- pathRequest: [],
- phoneNumber: self.getMyPhone(),
- userName: "Pippo",
- vehicleType: $("#veicolo input:checked").val()
- };
- origine = $("#offer-origine").val(),
- destinazione = $("#offer-destinazione").val();
- if (origine == "" || destinazione == "") {
- self.showDialog("Form Incompleto", "I campi <strong>Origine</strong> e <strong>Destinazione</strong> non possono essere vuoti!");
- return;
- }
- var data = /\d\d\/\d\d\/\d{4} \d\d:\d\d/.exec($("#offer-data").val());
- if (data == null) {
- self.showDialog("Data Invalida", "La data deve essere nel formato <strong>dd/mm/yyyy hh:mm</strong>");
- return;
- }
- req.requestTime = data[0];
- if (self.getCoords("#offer-origine", req) === false) return;
- if (self.getCoords("#offer-destinazione", req) === false) return;
- console.dir(req);
- console.log(JSON.stringify(req));
- var url = "http://poolmeup.appspot.com/rest/offers";
- var idOffer = null;
- $.post(url, req, function(data) {
- idOffer = data.idOffer;
- var s = self.parseXML(data.pathResponse);
- $.mobile.changePage("#viewpath");
- $("#actionlist").append(s).listview("refresh");
- self.timeoutID = setInterval(function() { self.longPolling(idOffer); }, 1000*30);
- });
- });
- $("#findride").click(function() {
- var req = {
- friends: self.getMyFriends(),
- numberOfPerson: $("#find-persons").val(),
- idUtente: self.getMyPhone(),
- userName: "Pippo",
- pathRequest: []
- },
- origine = $("#find-origine").val(),
- destinazione = $("#find-destinazione").val();
- if (origine == "" || destinazione == "") {
- self.showDialog("Form Incompleto", "I campi <strong>Origine</strong> e <strong>Destinazione</strong> non possono essere vuoti!");
- return;
- }
- if (self.getCoords("#find-origine", req) === false) return;
- if (self.getCoords("#find-destinazione", req) === false) return;
- console.log(req);
- });
- },
- getCoords: function(id, req) {
- var obj = {};
- // var url = "addr.json";
- var url = "http://maps.googleapis.com/maps/api/geocode/json";
- $.get(url, {sensor: false, address: $(id).val()}, function (data) {
- var status = data.status;
- if (status == "ZERO_RESULTS") {
- self.showDialog("Zero Risultati", "L'indirizzo insierito è sconosciuto. Riprovare.");
- return false;
- }
- data = data.results[0];
- $(id).val(data.formatted_address);
- obj.name = data.formatted_address;
- obj.lat = data.geometry.location.lat;
- obj.long = data.geometry.location.lng;
- req.pathRequest.push(obj);
- }, "json");
- return obj;
- },
- showDialog: function(title, message) {
- $("#dialogtitle").text(title);
- $("#dialogmessage").html(message);
- $.mobile.changePage("#dialog");
- },
- ICONS: {
- "Start": "",
- "KeepStraight": "img/direction_up.png",
- "TurnSightlyLeft": "img/direction_upleft.png",
- "TurnLeft": "img/direction_upthenleft.png",
- "TurnRight": "img/direction_upthenright.png",
- "TurnSightlyRight": "img/direction_upright.png",
- "EnterRoundabout": ""
- },
- parseXML: function(url) {
- var self = this;
- var s = "";
- $.get(url, function(data) {
- $.each($("actn", data), function(i, el) {
- var el = $(el);
- var kind = el.attr("kind");
- if (kind != null) {
- kind = kind.replace("{street}", el.attr("addr")).replace("{dir}", el.attr("dir"));
- var link = el.parent(),
- dist = /\d+(?:[.]\d{2})?/.exec(link.attr("dist"))[0],
- time = /\d+(?:[.]\d{2})?/.exec(link.attr("time"))[0];
- s += "<li><a href=\"#\"><img src=\""+self.ICONS[el.attr("actc")]+"\">"+
- "<h3>"+kind+"</h3><p>percorsi "+dist+"m, sono passati "+time+ " secondi</p></a></li>";
- }
- });
- });
- return s;
- },
- longPolling: function(idOffer) {
- var self = this;
- var url = "http://poolmeup.appspot.com/rest/offers/";
- $.get(url+idOffer, function(data) {
- if (data != null && data.pathResponse != null) {
- clearInterval(self.timeoutID);
- self.showDialog("Richiesta trovata", "Il percorso sarà aggiornato!");
- var s = self.parseXML(data.pathResponse);
- $.mobile.changePage("#viewpath");
- $("#actionlist").html(s).listview("refresh");
- }
- });
- },
- getMyFriends: function() {
- return ["pippo", "pluto", "paperino"];
- },
- getMyPhone: function() {
- return 333333333333;
- },
- // Bind Event Listeners
- //
- // Bind any events that are required on startup. Common events are:
- // 'load', 'deviceready', 'offline', and 'online'.
- bindEvents: function() {
- document.addEventListener('deviceready', this.onDeviceReady, false);
- },
- // deviceready Event Handler
- //
- // The scope of 'this' is the event. In order to call the 'receivedEvent'
- // function, we must explicity call 'app.receivedEvent(...);'
- onDeviceReady: function() {
- app.receivedEvent('deviceready');
- },
- // Update DOM on a Received Event
- receivedEvent: function(id) {
- console.log('Received Event: ' + id);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment