Files
FlexSurvey/www/js/index.js

161 lines
6.3 KiB
JavaScript
Raw Permalink Normal View History

2017-02-07 20:04:47 +00:00
/*
* 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();
app.templates.load("textInput","picture","linebreak","horizline","email","number").done(function (){
$("#allForm").append(app.templates.get("textInput")({id:"firstname",label:"First Name",style:"width:300px"}));
$("#allForm").append(app.templates.get("textInput")({id:"lastname",label:"Last Name",style:"width:300px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("textInput")({id:"company",label:"Company",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("textInput")({id:"url",label:"Web-site url",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("email")({id:"email",label:"E-mail",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("textInput")({id:"address1",label:"Address1",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("textInput")({id:"address2",label:"Address2",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("textInput")({id:"address3",label:"Address3",style:"width:600px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("number")({id:"zipcode",label:"Zip",style:"width:200px"}));
$("#allForm").append(app.templates.get("textInput")({id:"suburb",label:"Suburb",style:"width:400px"}));
$("#allForm").append(app.templates.get("horizline")());
$("#allForm").append(app.templates.get("picture")({id:"Photo",label:"(re)take picture of you",btnStyle:"width:450px"}));
$("#allForm").append(app.templates.get("linebreak")());
$("#allForm").append(app.templates.get("picture")({id:"Businesscard",label:"(re)take picture of your Business Card",btnStyle:"width:450px"}));
});
},
// 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) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.templates = (function(){
var templates=[],
load = function () {
var deferreds = [];
$.each(arguments, function (index,tpl) {
deferreds.push($.get("templates/" + tpl + ".html", function (text) {
templates[tpl] = Handlebars.compile(text);
}).fail(function (event, error){ window.console.log("Error loading Tempalte",tpl,event,error);}), 'html')
});
return $.when.apply(null, deferreds);
},
get = function(name) {
return templates[name];
};
// the public API
return {
load: load,
get : get
};
}());
app.myCamHandler=function(){
var _imageData="";
var _picElem=null;
var takePic= function (selfRef){
console.log("Taking Picture");
navigator.camera.getPicture(onSuccess, onFail, { quality: 50, destinationType: Camera.DestinationType.DATA_URL });
_picElem=selfRef;
},
getPicUri=function(){
return(_imageData)
};
function onSuccess(imageData) {
console.log("Success " + imageData);
_imageData=imageData;
$(_picElem).attr("value",imageData);
var picId=$(_picElem).attr( 'id');
$("#" + picId + "__image").attr("src","data:image/jpeg;base64," + imageData);
};
function onFail(message) {
console.log('Failed because: ' + message);
};
return {
takePic:takePic,
getPicUri:getPicUri
};
}
$.fn.toJSO = function () {
var obj = {},
$kids = $(this).children('[name]');
if (!$kids.length) {
return $(this).val();
}
$kids.each(function () {
var $el = $(this),
name = $el.attr('name');
if ($el.siblings("[name=" + name + "]").length) {
if (!/radio|checkbox/i.test($el.attr('type')) || $el.prop('checked')) {
obj[name] = obj[name] || [];
obj[name].push($el.toJSO());
}
} else {
obj[name] = $el.toJSO();
}
});
return obj;
};