dphelper 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/init.js +6 -1
- package/package.json +1 -1
- package/scripts/cookie.js +69 -0
- package/scripts/indexedDB.js +176 -0
- package/scripts/storage.js +28 -0
package/init.js
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
const pjson = require('./package.json');
|
|
13
13
|
|
|
14
14
|
// CREATE ROOT STORE
|
|
15
|
-
|
|
15
|
+
window.dphelper = {};
|
|
16
16
|
|
|
17
17
|
// FIRST MESSAGE
|
|
18
18
|
console.groupCollapsed( `%c${pjson.name} v${pjson.version}%c`,"color:orange","" );
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
// CURRENCY
|
|
37
37
|
require('./scripts/currency.js');
|
|
38
38
|
|
|
39
|
+
// MEMORY
|
|
40
|
+
require('./scripts/storage.js');
|
|
41
|
+
require('./scripts/cookie.js');
|
|
42
|
+
require('./scripts/indexedDB.js');
|
|
43
|
+
|
|
39
44
|
// NUMBERS
|
|
40
45
|
require('./scripts/randomNumberTmr.js');
|
|
41
46
|
require('./scripts/randomNumber.js');
|
package/package.json
CHANGED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* stPRO <https://github.com/passariello/stPRO>
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021, Dario Passariello.
|
|
5
|
+
* Licensed under the Apache-2.0 License.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/***********************************************************************/
|
|
9
|
+
|
|
10
|
+
var CookieType = "Lax";
|
|
11
|
+
var CookieSecure = "false";
|
|
12
|
+
var CookieSameSite = "false";
|
|
13
|
+
|
|
14
|
+
if (location.protocol === 'https:') {
|
|
15
|
+
CookieSecure = "Secure";
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
stpro.func.cookie = {
|
|
19
|
+
|
|
20
|
+
// CREATE THE COOKIE
|
|
21
|
+
|
|
22
|
+
set: function( cname , value , time , path = '/' ) {
|
|
23
|
+
var d = new Date();
|
|
24
|
+
d.setTime( d.getTime() + 3600 * 1000 * 24 * 365 );
|
|
25
|
+
|
|
26
|
+
if( !time ) time = d.toGMTString();
|
|
27
|
+
if( cname ){
|
|
28
|
+
document.cookie = cname + '=' + value + ';expires=' + time + ';path=' + path + ';SameSite=' + CookieSameSite + ';requireSSL='+ CookieSecure +';' + CookieSecure;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
// GET THE COOKIE
|
|
33
|
+
|
|
34
|
+
get: function( cname ) {
|
|
35
|
+
var asCookies = document.cookie.split( "; " );
|
|
36
|
+
|
|
37
|
+
for ( var i = 0; i < asCookies.length; i++ ){
|
|
38
|
+
var asCookie = asCookies[ i ].split( "=" );
|
|
39
|
+
if ( cname === asCookie[0] ) return ( unescape( asCookie[1] ) );
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return null;
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
// DELETE THE COOKIE
|
|
46
|
+
|
|
47
|
+
delete: function( cname ) {
|
|
48
|
+
document.cookie = cname + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/;SameSite=' + CookieSameSite + ';requireSSL='+ CookieSecure +';' + CookieSecure;
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
// CLEAR ALL COOKIE
|
|
52
|
+
|
|
53
|
+
clearAll: function() {
|
|
54
|
+
var cookies = document.cookie.split(";");
|
|
55
|
+
|
|
56
|
+
for ( var i = 0; i < cookies.length; i++ ) {
|
|
57
|
+
|
|
58
|
+
var cookie = cookies[i];
|
|
59
|
+
var eqPos = cookie.indexOf("=");
|
|
60
|
+
var cname = eqPos > -1 ? cookie.slice( 0 , eqPos ) : cookie;
|
|
61
|
+
stpro.cookie.delete( cname );
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// START COOKIE DB
|
|
69
|
+
stpro.func.cookie.set( "stPRO", 'active' );
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* stPRO <https://github.com/passariello/stPRO>
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021, Dario Passariello.
|
|
5
|
+
* Licensed under the Apache-2.0 License.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/***********************************************************************/
|
|
9
|
+
|
|
10
|
+
stpro.func.indexedDB = {};
|
|
11
|
+
const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
|
12
|
+
|
|
13
|
+
if (!indexedDB) {
|
|
14
|
+
console.debug(e, "stPRO : Your browser doesn't support a skey version of IndexedDB. Such and such feature will not be available.");
|
|
15
|
+
console.debug( 'stPRO : Something went badly wrong with your indexedDB!' );
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
stpro.func.indexedDB = {
|
|
19
|
+
|
|
20
|
+
open: ( storeName ) => {
|
|
21
|
+
|
|
22
|
+
var request = window.indexedDB.open( storeName , 4 );
|
|
23
|
+
|
|
24
|
+
request.onsuccess = ( e ) => {
|
|
25
|
+
console.debug( 'indexedDB "' + storeName + '" open' );
|
|
26
|
+
return e.target.result;
|
|
27
|
+
//request.close();
|
|
28
|
+
};
|
|
29
|
+
request.onerror = ( e ) => {
|
|
30
|
+
console.debug( "Database open error: " + e.target.errorCode );
|
|
31
|
+
return;
|
|
32
|
+
};
|
|
33
|
+
return request;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
/************************************************************************/
|
|
37
|
+
|
|
38
|
+
//CREATE: indexedDB.create('myDB','MyTable')
|
|
39
|
+
create: ( storeName, table ) => {
|
|
40
|
+
|
|
41
|
+
var request = window.indexedDB.open( storeName, 4 );
|
|
42
|
+
|
|
43
|
+
request.onerror = ( e ) => {
|
|
44
|
+
console.debug(e, "Database create error: " + e.target.errorCode);
|
|
45
|
+
return;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
request.onupgradeneeded = ( e ) => {
|
|
49
|
+
var db = e.target.result;
|
|
50
|
+
var objectStore = db.createObjectStore( storeName, { autoIncrement:true } );
|
|
51
|
+
objectStore.createIndex( table, table, { unique: true });
|
|
52
|
+
//objectStore.createIndex( value, value, { unique: false });
|
|
53
|
+
console.debug( db );
|
|
54
|
+
//db.close();
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/************************************************************************/
|
|
60
|
+
|
|
61
|
+
//STORE: indexedDB.store('myDB','MyTable')
|
|
62
|
+
store: ( storeName, table ) => {
|
|
63
|
+
var request = indexedDB.open( storeName );
|
|
64
|
+
|
|
65
|
+
request.onsuccess = function (e){
|
|
66
|
+
|
|
67
|
+
var database = e.target.result;
|
|
68
|
+
var version = parseInt(database.version);
|
|
69
|
+
database.close();
|
|
70
|
+
|
|
71
|
+
var secondRequest = indexedDB.open( storeName , version + 1 );
|
|
72
|
+
|
|
73
|
+
secondRequest.onupgradeneeded = function (e) {
|
|
74
|
+
var database = e.target.result;
|
|
75
|
+
var objectStore = database.createObjectStore( table, {
|
|
76
|
+
keyPath: 'id'
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
secondRequest.onsuccess = function (e) {
|
|
81
|
+
e.target.result.close();
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
/************************************************************************/
|
|
88
|
+
//INSERT: indexedDB.insert('myDB','Mytable','Mykey','MyValue')
|
|
89
|
+
insert: ( storeName, table, key, value ) => {
|
|
90
|
+
|
|
91
|
+
console.log( storeName, table, key, value );
|
|
92
|
+
|
|
93
|
+
var request = window.indexedDB.open( storeName, 4 );
|
|
94
|
+
|
|
95
|
+
request.onerror = ( e ) => {
|
|
96
|
+
console.debug(e, "Database insert error: " + e.target.errorCode);
|
|
97
|
+
return;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
request.onsuccess = ( e ) => {
|
|
101
|
+
try{
|
|
102
|
+
var db = e.target.result;
|
|
103
|
+
var StoreObj = db.transaction( storeName , "readwrite").objectStore( storeName );
|
|
104
|
+
var StoreObjInsert = StoreObj.add( value, key, value );
|
|
105
|
+
db.close();
|
|
106
|
+
}catch( e ){
|
|
107
|
+
console.debug( e, "IndexDB insert not work" );
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
/************************************************************************/
|
|
113
|
+
|
|
114
|
+
//UPDATE: indexedDB.update('myDB','Mykey','MyValue')
|
|
115
|
+
update: ( storeName, table, key, value ) => {
|
|
116
|
+
|
|
117
|
+
var request = window.indexedDB.open( storeName, 4 );
|
|
118
|
+
request.onerror = ( e ) => {
|
|
119
|
+
console.debug(e, "Database update error: " + e.target.errorCode);
|
|
120
|
+
return;
|
|
121
|
+
};
|
|
122
|
+
request.onsuccess = ( e ) => {
|
|
123
|
+
try{
|
|
124
|
+
var db = e.target.result;
|
|
125
|
+
var StoreObj = db.transaction( storeName , "readwrite").objectStore( storeName );
|
|
126
|
+
var StoreObjUpdate = StoreObj.put( value, key, value );
|
|
127
|
+
db.close();
|
|
128
|
+
}catch( e ){
|
|
129
|
+
console.debug( e, "IndexDB update not work" );
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/************************************************************************/
|
|
135
|
+
|
|
136
|
+
//GET: indexedDB.get('value')
|
|
137
|
+
get: ( storeName, table, key ) => {
|
|
138
|
+
|
|
139
|
+
var request = window.indexedDB.open( storeName, 4 );
|
|
140
|
+
|
|
141
|
+
request.onerror = ( e ) => {
|
|
142
|
+
console.debug( e , "Database get error: " + e.target.errorCode );
|
|
143
|
+
return;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
request.onsuccess = ( e ) => {
|
|
147
|
+
|
|
148
|
+
var db = e.target.result;
|
|
149
|
+
var Store = db.transaction( [storeName] , "readwrite" );
|
|
150
|
+
var obj = Store.objectStore( storeName );
|
|
151
|
+
var req = obj.get( key );
|
|
152
|
+
|
|
153
|
+
req.onsuccess = function( e ){
|
|
154
|
+
//console.log( e.target.result )
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// CHECK IF INDEXEDDB EXIST
|
|
164
|
+
// ***************************************************************************************************/
|
|
165
|
+
|
|
166
|
+
stpro.dbExist = () => {
|
|
167
|
+
var result = indexedDB.databases();
|
|
168
|
+
return result.length;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// START DEFAULT DB
|
|
172
|
+
stpro.func.indexedDB.open( "stPRO" );
|
|
173
|
+
stpro.func.indexedDB.store( "stPRO" );
|
|
174
|
+
stpro.func.indexedDB.insert( "stPRO" , 'active' , 'test[0]' , [] );
|
|
175
|
+
|
|
176
|
+
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* stPRO <https://github.com/passariello/stPRO>
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021, Dario Passariello.
|
|
5
|
+
* Licensed under the Apache-2.0 License.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/***********************************************************************/
|
|
9
|
+
|
|
10
|
+
stpro.func.storage = {
|
|
11
|
+
|
|
12
|
+
get: function( name ) {
|
|
13
|
+
return localStorage.getItem( name );
|
|
14
|
+
},
|
|
15
|
+
set: function( name, value ) {
|
|
16
|
+
localStorage.setItem( name, value );
|
|
17
|
+
},
|
|
18
|
+
delete: function( name ) {
|
|
19
|
+
localStorage.removeItem( name );
|
|
20
|
+
},
|
|
21
|
+
clearAll: function() {
|
|
22
|
+
localStorage.clear();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// START STORAGE DB
|
|
28
|
+
stpro.func.storage.set( "stPRO", 'active' );
|