Last active
December 28, 2015 19:59
-
-
Save revolunet/7553725 to your computer and use it in GitHub Desktop.
Sample cordova with web fallback fonctions for AngularJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* angular-phonegap-bridges | |
* (c) 2013 Julien Bouquillon - [email protected] | |
* License: MIT | |
*/ | |
(function() { | |
'use strict'; | |
angular.module('revolunet.cordova', [ | |
'revolunet.formbuilder', | |
'pascalprecht.translate' | |
]). | |
factory('CordovaBridge', ['$rootScope', 'FormBuilder', '$document', '$translate', '$window', function ($rootScope, FormBuilder, $document, $translate, $window) { | |
document.addEventListener('deviceready', function () { | |
me.isCordova = true; | |
}); | |
var locale = ($window.navigator.language.substr(0,2)=='fr')?'fr':'en'; | |
var me = { | |
locale: locale, | |
isCordova: false, | |
callbackHost: 'http://cordova-callback.com/', | |
open: function(url, target, options) { | |
if (!target) target='_blank'; | |
if (!options) options='location=no,closebuttoncaption=Fermer'; | |
return window.open(url, target, options); | |
}, | |
alert: function(msg) { | |
if (me.isCordova) { | |
navigator.notification.alert(msg, angular.noop, 'Title'); | |
} else { | |
alert(msg); | |
} | |
}, | |
confirm: function(msg, cb, buttonsTexts) { | |
function confirmed(buttonIndex) { | |
return cb(buttonIndex===2); | |
} | |
if (me.isCordova) { | |
var okLabel = $translate('confirm.ok'), | |
cancelLabel = $translate('confirm.cancel'); | |
if (!buttonsTexts) buttonsTexts = cancelLabel + ',' + okLabel; | |
navigator.notification.confirm(msg, confirmed, 'Title', buttonsTexts); | |
} else { | |
cb(confirm(msg)); | |
} | |
}, | |
getBasePath: function() { | |
if (me.isCordova) { | |
return this.callbackHost; | |
} | |
return $document[0].location.origin + '/'; | |
}, | |
post: function(url, data, loadstart, options) { | |
if (me.isCordova) { | |
// abuse the inAppBrowser with this inline-exec | |
// open an empty page, then execute the javascript that POST data | |
var JSstr = "function doPost(url,data){var f=\"<form id='form-postForm' method='POST' action='\"+url+\"'>\";for(var i in data){if(data.hasOwnProperty(i)){f+='<input type=\"hidden\" name=\"'+i+'\" value=\"'+data[i]+'\"/>';}}; f+=\"</form>\";var div=document.createElement('div');div.innerHTML=f;document.body.appendChild(div.firstChild);document.getElementById('form-postForm').submit();};"; | |
JSstr += "doPost('" + url + "', " + JSON.stringify(data || {}) + ");"; | |
var win = null; | |
var closeCb = function() { | |
// remove listeners | |
if (loadstart) { | |
win.removeEventListener('loadstart', loadstart); | |
} | |
win.removeEventListener('exit', closeCb); | |
win.removeEventListener('loadstop', injector); | |
}; | |
var injector = function() { | |
// inject the JS in the window | |
win.executeScript({ | |
code: JSstr | |
}, function() { | |
// callback | |
}); | |
win.removeEventListener('loadstop', injector); | |
}; | |
win = this.open('about:blank', '_blank', options); | |
win.addEventListener('loadstop', injector); | |
if (loadstart) { | |
win.addEventListener('loadstart', loadstart); | |
} | |
win.addEventListener('exit',closeCb); | |
return win; | |
} else { | |
// regular POST in the same window | |
FormBuilder.submit(url, "POST", data); | |
} | |
}, | |
getLocale: function() { | |
return me.locale; | |
} | |
}; | |
return me; | |
}]); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment