Skip to content

Instantly share code, notes, and snippets.

@abachuk
Last active January 2, 2017 21:29
Show Gist options
  • Save abachuk/5497770 to your computer and use it in GitHub Desktop.
Save abachuk/5497770 to your computer and use it in GitHub Desktop.
Phonegap + jQuery Mobile + android
/// PHONEGAP + jQUERY MOBILE (page from android app)
$(document).on("pageinit", ".ui-page", function () {
$('input').focus(function(){
$('#signin-footer').hide();
});
$('input').blur(function(){
$('#signin-footer').show();
})
Signin = {
login: function(){
$('#login-btn').click(function(){
var username = $('#signin-username').val();
var password = $('#signin-password').val();
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
type: 'POST',
url: 'https://www.giftfly.com/mobileapi/merchant-login',
data: {
'password' : password,
'username' : username
},
success: function(data) {
if(data.status == 'error'){
alert(data.error.message+', '+data.error.description);
} else {
localStorage.setItem('loggedinMerchant', JSON.stringify(data));
$( document ).delegate("#merchantHomePage", "pagecreate", function() {
Signin.merchantHome();
});
$.mobile.changePage( "merchantHome.html", { transition: "slide"} );
};
},
statusCode: {
500: function() {
alert('Error 500');
}
},
error: function(data) {
alert('Error')
}
}); //end ajax
}); //end click event
$('#forgot-pass-btn').click(function(){
$( document ).delegate("#forgot-page", "pagecreate", function() {
Signin.forgotPass();
});
$.mobile.changePage( "forgotPassword.html", { transition: "slide"} );
}); // end click event
},
merchantHome: function() {
var template = _.template( $( "script#merchantSummary" ).html() );
theMerchant = JSON.parse(localStorage.loggedinMerchant).merchant;
$('#merchantSummeryContainer').html(template( theMerchant ));
},
merchantScan: function() {
//console.log('loaded');
//$('input#scan-amount').focus();
$('#scanner-menu a').click(function(){
qr_amount = $('input#scan-amount').val();
Signin.runScan();
});
},
runScan: function() {
window.plugins.barcodeScanner.scan( function(result) {
var qr_code = result.text;
$( document ).delegate("#card-scan-result-page", "pagebeforecreate", function() {
Signin.scanResult(qr_code);
});
if(typeof(qr_code.transactions) != 'undefined'){
$.mobile.changePage( "cardScanResult.html", { transition: "slide"} );
} else {
alert('Giftcard Not Found');
}
});
},
scanResult: function(qr_code) {
var merchant_id = JSON.parse(localStorage.loggedinMerchant).merchant.id;
var theMerchant = JSON.parse(localStorage.loggedinMerchant).merchant;
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
type: 'POST',
url: 'https://www.giftfly.com/mobileapi/validate-giftcard',
data: {
'amount' : qr_amount,
'merchant_id' : merchant_id,
'card_qrcode' : qr_code
},
success: function(data) {
console.log(qr_code);
qr_scan = data;
var template = _.template( $( "script#scanSummary" ).html() );
$('#scanSummaryContainer').html(template( qr_scan ))
},
error: function() {
alert('Error');
}
});
},
historyDashboard: function() {
var transactions = [];
var d = new Date();
var currentYear = d.getFullYear();
var currentMonth = d.getMonth()+1;
var currentDay = d.getDate();
if (currentMonth < 10) { currentMonth = '0' + currentMonth; }
if (currentDay < 10) { currentDay = '0' + currentDay; }
var todayDate = currentYear + "-" + currentMonth + "-" + (currentDay/*+1*/);
var todayDateFormat = new Date(currentYear + "/" + currentMonth + "/" + currentDay);
var yesterdayDate = currentYear + "-" + currentMonth + "-" + (currentDay-1);
var sevenDate = new Date(todayDateFormat.getTime() - 7 * 24 * 60 * 60 * 1000).getFullYear() + '-'
+ ((new Date(todayDateFormat.getTime() - 7 * 24 * 60 * 60 * 1000).getMonth())+1) + '-'
+ new Date(todayDateFormat.getTime() - 7 * 24 * 60 * 60 * 1000).getDate();
var mtdDate = currentYear + "-" + currentMonth + "-" + '01';
var ytdDate = currentYear + "-" + '01' + "-" + '01';
var dateClicked = todayDate;
var apiClicked = 'sold';
var apiRequest = function(api, start, end) {
var merchantId = JSON.parse(localStorage.getItem('loggedinMerchant')).merchant.id;
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
type: 'POST',
url: 'https://www.giftfly.com/mobileapi/history/'+api,
data: {
'merchant_id': merchantId,
'range_start' : start,
'range_end': end
},
success: function(data) {
console.log(data);
apiRequestData = data.transactions;
sold = data.transactions.sold;
redeemed = data.transactions.redeemed;
refunded = data.transactions.refunded;
var totalAmountSoldTemplate = _.template($('script#TotalAmountSold').html());
var totalAmountRedeemedTemplate = _.template($('script#TotalAmountRedeemed').html());
var totalAmountRefundedTemplate = _.template($('script#TotalAmountRefunded').html());
var listCardsSold = _.template($('script#ListSold').html());
var listCardsRedeemed = _.template($('script#ListRedeemed').html());
var listCardsRefunded = _.template($('script#ListRefunded').html());
$('#soldSummary h3').html(totalAmountSoldTemplate(sold));
$('#redeemedSummary h3').html(totalAmountRedeemedTemplate(redeemed));
$('#refundedSummary h3').html(totalAmountRefundedTemplate(refunded));
$('ul#transactionListSold').html(listCardsSold(sold));
$('ul#transactionListRedeemed').html(listCardsRedeemed(redeemed));
$('ul#transactionListRefunded').html(listCardsRefunded(refunded));
},
error: function(data) {
console.log(start);
console.log(end);
console.log(merchantId);
alert('Error');
//alert('http://staging1.giftfly.com/mobileapi/history/'+api);
}
});
};
apiRequest('all', todayDate, todayDate);
$('a#today').click(function(e){
$('ul.ui-listview').html('');
apiRequest('all', todayDate, todayDate);
});
$('a#yesterday').click(function(e){
$('ul.ui-listview').html('')
apiRequest('all', yesterdayDate, todayDate);
});
$('a#sevenday').click(function(e){
$('ul.ui-listview').html('')
apiRequest('all', sevenDate, todayDate);
});
$('a#mtd').click(function(e){
$('ul.ui-listview').html('')
apiRequest('all', mtdDate, todayDate);
});
$('a#yrd').click(function(e){
$('ul.ui-listview').html('')
apiRequest('all', ytdDate, todayDate);
});
$('#soldSummary').click(function(){
$('.ui-listview').hide();
$('#transactionListSold').fadeIn();
$('.history-summary-block').removeClass('active');
$(this).addClass('active');
});
$('#redeemedSummary').click(function(){
$('.ui-listview').hide();
$('#transactionListRedeemed').fadeIn();
$('.history-summary-block').removeClass('active');
$(this).addClass('active');
});
$('#refundedSummary').click(function(){
$('.ui-listview').hide();
$('#transactionListRefunded').fadeIn();
$('.history-summary-block').removeClass('active');
$(this).addClass('active');
});
},
cardDetails: function(){
var urlID = (($.mobile.activePage[0].baseURI).split('?'))[1];
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
url: 'https://www.giftfly.com/mobileapi/history/card-details',
type: 'POST',
data: {
'card_id' : urlID
},
success: function(data){
cardTransactions = data.transactions;
console.log(data);
var listCardsTemplate = _.template($('script#cardTransactions').html());
$('ul.ui-listview').html(listCardsTemplate(cardTransactions));
},
error: function(){
alert('Error');
}
});
$('#card-transaction-list').on('click', 'li', function(){
$('#card-transaction-list li').removeClass('selected');
listNumber = cardTransactions[$(this).index()];
console.log(listNumber);
if(listNumber.amount > 0) {
$('a#issue-refund').hide();
};
$(this).addClass('selected');
});
$('a#issue-refund').click(function(){
$( document ).delegate("#history-refund-page", "pagebeforecreate", function() {
Signin.issueRefund(listNumber);
});
$.mobile.changePage( "historyRefund.html", { transition: "pop"} );
});
$('#resend').click(function(){
$('#resend-dialog.hidden').animate({
'bottom':'0'
}, function(){
$(this).removeClass('hidden');
$(this).addClass('visible');
});
});
$('body').click(function(e){
if (e.target.id != "resend-dialog") {
$('#resend-dialog.visible').animate({
'bottom':'-26em'
}, function(){
$(this).removeClass('visible');
$(this).addClass('hidden');
});
}
});
$('.resend-confirm').click(function(){
$.ajax({
url: 'https://www.giftfly.com/mobileapi/resend-card',
type: 'POST',
data: {
'card_id': urlID
},
success: function(data) {
console.log(data);
alert('Sent to '+data.email);
},
error: function() {
alert('Error resend confirm');
}
})
//mobileapi/resend-card
});
},
issueRefund: function(listNumber){
$('#refunded-amount').html('$ '+Math.abs(listNumber.amount).toFixed(2));
$('#confirm-refund').click(function(){
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
url: 'https://www.giftfly.com/mobileapi/refund-card-transaction',
type: 'POST',
data: {
'card_transaction_id' : listNumber.id
},
success: function(data) {
console.log(data);
history.back();
alert('The transaction was successfully refunded!');
},
error: function() {
alert('Error resend confirm');
}
})
});
},
changePassword: function(){
var merchantId = JSON.parse(localStorage.getItem('loggedinMerchant')).merchant.id;
$('#changePasswordSubmit').click(function(){
if($('#currentPass').val()=='') {
alert('Please enter current password');
} else if($('#newPass').val() == '') {
alert('Plase enter new password');
} else if($('#repeatNewPass').val() != $('#newPass').val()) {
alert('Make sure passwords match');
} else {
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
url: 'https://www.giftfly.com/mobileapi/merchant-change-password',
type: 'POST',
data: {
'merchant_id' : merchantId,
'current_password': $('#currentPass').val(),
'new_password' : $('#newPass').val()
},
success: function(data){
if(data.status == 'error'){
alert('Error, ' + data.error.description);
//console.log(data);
} else {
alert('Your password has been updated');
$( document ).delegate("#signin-page", "pagebeforecreate", function() {
Signin.login();
});
$.mobile.changePage( "signin.html", { transition: "slide"} );
}
},
error: function(data){
alert('Error changing password');
//console.log(data);
}
});
} //end if else
}); // end click event
}, // end changePassword
forgotPass: function(){
$('#forgotPasswordSubmit').click(function(){
if( $('#currentEmail').val() == '' ){
alert('Plase enter account email');
} else {
$.ajax({
beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner
complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner
url: 'https://www.giftfly.com/mobileapi/password-recovery',
type: 'POST',
data: {
'merchant_email' : $('#currentEmail').val()
},
success: function(data){
if(data.status == 'error'){
alert('Error, '+ data.error.description);
console.log(data);
} else {
alert('Reset link sent to '+$('#currentEmail').val());
console.log(data);
$( document ).delegate("#signin-page", "pagebeforecreate", function() {
Signin.login();
});
$.mobile.changePage( "signin.html", { transition: "slide"} );
}
},
error: function(data){
alert('Error changing password');
//console.log(data);
}
});
}; //end else
}); // end click event
} //end forgotPass
} //end Signin obj
Signin.login();
});
$( document ).on("pageinit", "#signin-scan-page", function() {
Signin.merchantScan();
});
$( document ).on("pageinit", "#history-page", function() {
Signin.historyDashboard();
});
$( document ).delegate("#history-card-details", "pageinit", function() {
Signin.cardDetails();
});
$( document ).on("pageinit", "#password-page", function() {
Signin.changePassword();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment