· 8 years ago · Feb 28, 2018, 06:18 PM
1// Copyright 2007, Google Inc.
2// =============
3// gears_offline
4// =============
5// Changed by: Alex(blackanger.z@gmail.com)
6// Time : 2009.01.23
7
8(function() {
9
10jQuery.fn.offline = {
11
12 STORE_NAME: "customer_management_offline_docset",
13 MANIFEST_FILENAME: "../cmmanifest.json",
14 DATABASE_NAME: "red-CM",
15
16 check: function(){
17 if (!window.google || !google.gears) {
18 alert("You must install Google Gears first.");
19 return false;
20 }
21 return true;
22 },
23
24 init: function(){
25 if(this.check){
26 try{
27 //create LocalServer
28 localServer = google.gears.factory.create("beta.localserver","1.0");
29 store = localServer.createManagedStore(this.STORE_NAME);
30
31 //create DataBase
32 db = google.gears.factory.create('beta.database', '1.0');
33
34 if(db){
35 db.open(this.DATABASE_NAME);
36 this.CreateTables();
37 // workerpoolDbSyncInit();
38 }
39
40 return true;
41 }catch(e){
42 alert('Could not create LocalServer: ' + e.message);
43 return false;
44 }
45
46 // this.textOut("Google Gears is installed, continue.");
47 }
48 },
49
50 offlineCreateStore: function(){
51 if (!this.check) return;
52 this.store = localServer.createManagedStore(this.STORE_NAME);
53 this.store.manifestUrl = this.MANIFEST_FILENAME;
54 this.store.checkForUpdate();
55
56 // if download error occur,this function will display a note.
57 var timerId = window.setInterval(function() {
58 // When the currentVersion property has a value, all of the resources
59 // listed in the manifest file for that version are captured. There is
60 // an open bug to surface this state change as an event.
61 if (this.store.currentVersion) {
62 window.clearInterval(timerId);
63 // this.textOut("Google Gears is installed, continue.");
64 } else if (this.store.updateStatus == 3) {
65 // this.textOut("Error: " +this.store.lastErrorMessage);
66 }
67 }, 500);
68 },
69
70 offlineRemoveStore: function(){
71 if (!this.check) return;
72 localServer.removeManagedStore(this.STORE_NAME);
73 // this.textOut("Done. The local store has been removed." +
74 // "You will now see online versions of the documents.");
75 },
76
77 CreateTables: function(){
78 //test
79 db.execute('create table if not exists Test (Phrase text, Timestamp int)');
80 db.execute('insert into Test values (?, ?)', ['Monkey!', new Date().getTime()]);
81 var rs = db.execute('select * from Test order by Timestamp desc');
82 while (rs.isValidRow()) {
83 alert(rs.field(0) + '@' + rs.field(1));
84 rs.next();
85 }
86 rs.close();
87 }
88};
89
90})(jQuery);