· 6 years ago · Dec 19, 2019, 10:36 AM
1const app = new Framework7({
2 root: '#app', //root element pada index.html
3 name: 'Kamus Oil', //nama berfungsi sebagai default judul dialog
4 routes: router, //list router daftarnya ada di file router.js
5 theme: 'ios'
6})
7
8const mainView = app.views.create('.view-main',{
9 url: '/',
10 preloadPreviousPage: false,
11 iosSwipeBack: false,
12 allowDuplicateUrls: true
13})
14
15$$ = Dom7 //load dom7 https://framework7.io/docs/dom7.html
16
17const api = 'http://example.com' //ganti dengan alamat localhost anda
18
19let db = ''
20
21document.addEventListener('deviceready', function(){
22 // membuka database
23 db = window.openDatabase('MyDatabase', '1.0', 'Data', 2*1024*1024);
24
25 // buat table jika tidak ada atau belum dibuat
26 db.transaction(function(tx) {
27 tx.executeSql('CREATE TABLE IF NOT EXISTS KamusOil (id_oil,nama_oil,deskripsi_oil,gambar_oil)')
28 tx.executeSql('CREATE TABLE IF NOT EXISTS note (id_note, fk_id_oil, content_note, status)')
29 }, function(error) {
30 console.log('transaction error' + error.message)
31 }, function() {
32 console.log('Success Create DB if not exists')
33 })
34
35 // handle backbutton
36 document.addEventListener('backbutton', function(){
37 if(app.views.main.router.history.length == 1){
38 app.dialog.confirm('Apakah anda ingin keluar dari aplikasi ?', function(){
39 navigator.app.exitApp()
40 })
41 } else {
42 app.views.main.router.back()
43 }
44 })
45
46 // configuration image cache
47
48 ImgCache.options.debug = true;
49
50 ImgCache.options.chromeQuota = 50*1024*1024;
51
52 ImgCache.options.cordovaFilesystemRoot = cordova.file.dataDirectory;
53
54 ImgCache.init(function () {
55 console.log('ImgCache init: success!');
56 }, function () {
57 console.log('ImgCache init: error! Check the log for errors');
58 });
59
60 // sync to server, kasih timeout 1 detik baru run script agar tidak tumpang tindih
61 // saat splashscreen
62 setTimeout(function(){
63 const networkState = navigator.connection.type
64
65 if(networkState != Connection.NONE) {
66 app.dialog.preloader('Sync to Server')
67 db.transaction(function(tx) {
68 tx.executeSql('DROP TABLE IF EXISTS KamusOil')
69 tx.executeSql('CREATE TABLE IF NOT EXISTS KamusOil (id_oil,nama_oil,deskripsi_oil,gambar_oil)')
70 }, function(error){
71 console.log(error.message)
72 }, async function(){
73 // synckron semua data
74 await syncOil()
75
76 app.dialog.close()
77 })
78 }
79 }, 1000)
80
81})
82
83
84function syncOil() {
85 return new Promise(function(resolve, reject){
86 app.request.get(api + '/oil', function(res) {
87 const data = JSON.parse(res)
88 console.log('sync oil done')
89 if (data.status) {
90 data.data.map(function(item){
91 db.transaction(function(tx){
92 tx.executeSql('INSERT INTO KamusOil VALUES (?,?,?,?)', [item.id_oil, item.nama_oil, item.deskripsi_oil, item.gambar_oil])
93 }, function(error){
94 console.log(error.message)
95 reject(error.message)
96 })
97 })
98
99 }
100 resolve('done')
101 })
102 })
103}