-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmagic_prefs.js
More file actions
152 lines (127 loc) · 3.83 KB
/
magic_prefs.js
File metadata and controls
152 lines (127 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* For use with or without MagicFramework
* http://www.jeffmcfadden.com/Magic%20Framework/
* This work by Jeff McFadden is licensed under a Creative Commons Attribution 3.0 United States License.
* Please take it, use it, make it better, sell something with it, etc. Just give me some props somewhere.
*
*
*/
/**
* Config Saver Class
*
* Requires JSON2.js
*/
var MagicPrefs = function( db_name, callback ){
LogIt( 'MagicPrefs constructor' );
if( !window.openDatabase || '' == db_name ){
return null;
}
this.db = openDatabase( db_name );
var self = this;
this.db.transaction(
function(tx) {
tx.executeSql("SELECT COUNT(*) FROM SaveData", [],
function(result) {
self.LoadSaveData( callback );
},
function(tx, error) {
tx.executeSql("CREATE TABLE SaveData( name TEXT UNIQUE, data TEXT )", [], function(result) {
self.LoadSaveData( callback );
});
}
);
}
);
}
MagicPrefs.prototype.db = null;
MagicPrefs.prototype.data = [];
function dateFilter(k, v) {
return (typeof v == "string"
&& (k=v.match(/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z$/))) ? new Date(Date.UTC(k[1],k[2]-1,k[3],k[4],k[5],k[6])) : v;
}
MagicPrefs.prototype.LoadSaveData = function( callback ){
var self = this;
this.db.transaction(function(tx) {
tx.executeSql("SELECT name, data FROM SaveData", [], function(tx, result) {
for (var i = 0; i < result.rows.length; ++i) {
var row = result.rows.item(i);
if( row['name'] != 'undefined' ){
LogIt( 'Got saved data. ' + row['name'] + ':' + row['data'] );
self.data[row['name']] = JSON.parse( row['data'], dateFilter );
/*
self.data.push( {
name: row['name'],
data: JSON.parse( row['data'], dateFilter )
} );
*/
}
}
if (!result.rows.length){
//No data to load.
}
//Call the callback function.
setTimeout( callback, 0 );
},
function(tx, error) {
alert('Failed to retrieve notes from database - ' + error.message);
return;
}
);
});
}
MagicPrefs.prototype.SavePref = function( name, val ){
//this.data = [];
this.data[name] = null;
this.data[name] = val;
this.SaveData();
}
MagicPrefs.prototype.GetPref = function( name ){
if( this.data[name] ){
return this.data[name];
}else{
return null;
}
}
//Provided by Andy Edmonds - thanks Andy!
MagicPrefs.prototype.ErasePref = function(id){
//LogIt( 'EraseData' );
this.db.transaction(
function( tx ){
tx.executeSql( "DELETE FROM SaveData WHERE name= '" + id + "'" );
}
);
}
MagicPrefs.prototype.SaveData = function(){
function makeClosure(i, data) {
return function( tx ){
return tx.executeSql( "INSERT OR REPLACE INTO SaveData( name, data ) VALUES( ?, ? )", [i, data ] );
}
}
for( var i in this.data ){
if( i && i != 'undefined' ){
var this_data = this.data[i];
//Convert to JSON
this_data = JSON.stringify( this_data, function (key, value) {
return value;
});
LogIt( 'Saving item ' + i + ' value: ' + this_data );
this.db.transaction(
makeClosure( i, this_data ),
function( err ){
//alert( 'Error with statement: ' + err.message );
},
function( sec ){
//alert( 'Successful statement ' );
}
);
}
}
}
MagicPrefs.prototype.EraseData = function(){
LogIt( 'EraseData' );
this.db.transaction(
function( tx ){
tx.executeSql( "DROP TABLE SaveData" );
}
);
}