UNPKG

@owstack/ows-wallet-plugin-starter

Version:
1 lines 7.81 kB
"use strict";angular.module("owsWalletPlugin",["gettext","ionic","ngLodash","owsWalletPluginClient","owsWalletPlugin.api","owsWalletPlugin.controllers","owsWalletPlugin.services"]),angular.module("owsWalletPlugin.api",[]),angular.module("owsWalletPlugin.controllers",[]),angular.module("owsWalletPlugin.services",[]),angular.module("owsWalletPlugin").config(function($stateProvider){$stateProvider.state("home",{url:"/home",controller:"HomeCtrl",templateUrl:"views/home/home.html"}).state("detail",{url:"/detail/:id",controller:"DetailCtrl",templateUrl:"views/detail/detail.html"})}).run(function($rootScope,$state,pLog,starterService){owswallet.Plugin.ready(function(){starterService.init(function(){$state.go("home")})}),$rootScope.$on("$stateChangeStart",function(event,toState,toParams,fromState,fromParams){pLog.debug("Applet route change start from:",fromState.name||"-"," to:",toState.name)})}),angular.module("owsWalletPlugin").run(["gettextCatalog",function(gettextCatalog){}]),angular.module("owsWalletPlugin.services").service("movieService",function(pLog,lodash){var root={},movies=[{id:"the-avengers",title:"The Avengers",year:"2012",plot:"The Asgardian Loki encounters the Other, the leader of an extraterrestrial race known as the Chitauri. In exchange for retrieving the Tesseract,2 a powerful energy source of unknown potential, the Other promises Loki an army with which he can subjugate Earth...",episode:"The Avengers",likeCount:267,commentCount:186,price:{amount:5,currency:"USD"}},{id:"star-wars",title:"Star Wars",episode:"Episode IV - A New Hope",year:"1977",plot:"Near the orbit of the desert planet Tatooine, a Rebel spaceship is intercepted by the Empire. Aboard, the deadliest Imperial agent Darth Vader and his stormtroopers capture Princess Leia Organa, a secret member of the rebellion. Before her capture, Leia makes sure the astromech R2-D2, along with the protocol droid C-3PO, escapes with stolen Imperial blueprints stored inside and a holographic message for the retired Jedi Knight Obi-Wan Kenobi, who has been living in exile on Tatooine...",likeCount:582,commentCount:267,price:{amount:4,currency:"USD"}},{id:"2001-space-odyssey",title:"2001: A Space Odyssey",episode:"2001: A Space Odyssey",year:"1968",plot:"In an African desert millions of years ago, a tribe of hominids is driven away from its water hole by a rival tribe. They awaken to find a featureless black monolith has appeared before them. Seemingly influenced by the monolith, they discover how to use a bone as a weapon and drive their rivals away from the water hole...",likeCount:9343,commentCount:2067,price:{amount:3,currency:"USD"}}];return root.getMovies=function(){return movies},root.getMovie=function(id){return lodash.find(movies,function(movie){return id==movie.id})},root}),angular.module("owsWalletPlugin.services").service("popupService",function($log,$ionicPopup,$timeout,gettextCatalog,lodash){var isCordova=owswallet.Plugin.isCordova(),_ionicAlert=function(title,message,cb,okText){cb=cb||function(){},$ionicPopup.alert({title:title,subTitle:message,okType:"button-clear button-positive",okText:okText||gettextCatalog.getString("OK")}).then(cb)},_ionicConfirm=function(title,message,okText,cancelText,cb){cb=cb||function(){},$ionicPopup.confirm({title:title,subTitle:message,cancelText:cancelText,cancelType:"button-clear button-secondary",okText:okText,okType:"button-clear button-positive"}).then(function(res){return cb(res)})},_ionicPrompt=function(title,message,opts,cb){opts=opts||{},cb=cb||function(){},$ionicPopup.prompt({title:title,subTitle:message,cssClass:opts.class,template:'<input ng-model="data.response" type="'+opts.inputType+'" value ="" autocomplete="off" autofocus>',inputPlaceholder:opts.inputPlaceholder,defaultText:opts.defaultText}).then(function(res){return cb(res)})},_cordovaAlert=function(title,message,cb,okText){cb=cb||function(){},title=title||"",okText=okText||gettextCatalog.getString("OK"),navigator.notification.alert(message,cb,title,okText)},_cordovaConfirm=function(title,message,okText,cancelText,cb){cb=cb||function(){};var onConfirm=function(buttonIndex){return cb(2==buttonIndex?!0:!1)};okText=okText||gettextCatalog.getString("OK"),cancelText=cancelText||gettextCatalog.getString("Cancel"),title=title||"",navigator.notification.confirm(message,onConfirm,title,[cancelText,okText])},_cordovaPrompt=function(title,message,opts,cb){cb=cb||function(){};var onPrompt=function(results){return 1==results.buttonIndex?cb(results.input1):cb()},okText=gettextCatalog.getString("OK"),cancelText=gettextCatalog.getString("Cancel");title=title||"",navigator.notification.prompt(message,onPrompt,title,[okText,cancelText],opts.defaultText)};this.showAlert=function(title,message,cb,okText){title=lodash.isEmpty(title)?"":title,message=lodash.isEmpty(message)?"":message;var msg=message&&message.message?message.message:msg;$log.warn(title?title+": "+message:message),isCordova?_cordovaAlert(title,message,cb,okText):_ionicAlert(title,message,cb,okText)},this.showConfirm=function(title,message,okText,cancelText,cb){title=lodash.isEmpty(title)?"":title,message=lodash.isEmpty(message)?"":message,$log.warn(title?title+": "+message:message),isCordova?_cordovaConfirm(title,message,okText,cancelText,cb):_ionicConfirm(title,message,okText,cancelText,cb)},this.showPrompt=function(title,message,opts,cb){title=lodash.isEmpty(title)?"":title,message=lodash.isEmpty(message)?"":message,$log.warn(title?title+": "+message:message),opts=opts||{},isCordova&&!opts.forceHTMLPrompt?_cordovaPrompt(title,message,opts,cb):_ionicPrompt(title,message,opts,cb)}}),angular.module("owsWalletPlugin.services").service("starterService",function($rootScope,pLog,Session){var session,root={};return $rootScope.$on("$pre.beforeLeave",function(event,applet){saveData()}),root.data={},root.init=function(cb){session=Session.getInstance(),root.appletName=session.plugin.header.name,cb()},root.getData=function(cb){cb=cb||function(){},session.get("data").then(function(value){cb(null,value)}).catch(function(error){pLog.error("Failed to read preferences: "+error.message+" ("+error.statusCode+")"),cb(error)})},root.saveData=function(){session.set("data",data),session.flush()},root}),angular.module("owsWalletPlugin.controllers").controller("DetailCtrl",function($scope,pLog,movieService,BitPay,Session,Transaction,popupService){function doBuy(movie,wallet){function confirmHandler(){return new Promise(function(resolve,reject){var message="You are buying "+movie.title+" for "+movie.price+" "+movie.currency+".";popupService.showConfirm("Confirm Your Purchase",message,"Buy","Cancel",function(ok){ok&&resolve(),reject()})})}var payment={price:movie.price.amount,currency:movie.price.currency};bitpay.sendPayment(wallet,payment,confirmHandler).then(function(){pLog.debug("Payment sent")}).catch(function(error){pLog.error("Could not create payment request: "+error.message);popupService.showAlert("Oops! Our Fault","We are unable to process your purchase at this time. Please try again later.")})}var bitpay=new BitPay("movieStore");owswallet.Plugin.openForBusiness(BitPay.pluginId,function(){$scope.buyDiabled=!1}),$scope.$on("$ionicView.beforeEnter",function(event,data){$scope.buyDiabled=!0,$scope.movie=movieService.getMovie(data.stateParams.id)}),$scope.buy=function(movieId){Session.getInstance().chooseWallet().then(function(wallet){if(!wallet)return void pLog.info("User canceled");doBuy(movieService.getMovie(movieId),wallet)}).catch(function(error){pLog.error("Could not choose a wallet: "+JSON.stringify(error))})}}),angular.module("owsWalletPlugin.controllers").controller("HomeCtrl",function($scope,starterService,movieService){$scope.$on("$ionicView.beforeEnter",function(event,data){$scope.appletName=starterService.appletName,$scope.movies=movieService.getMovies()})});