Difference between revisions of "MediaWiki:Gadget-personaldata.js"

From Wikibase Personal data
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
// License: GPL
 
// License: GPL
 
// Indexeddb JavaScript  
 
// Indexeddb JavaScript  
console.log('Indb loading');
+
debuglog('Indb loading');
  
 
function idxdbContainer( ww ) {
 
function idxdbContainer( ww ) {
var DEBUG = false;
+
var Debug_switch = false;
 
hashCode = function(s) {
 
hashCode = function(s) {
 
if ( s == undefined) {
 
if ( s == undefined) {
Line 21: Line 21:
 
var idgen = hashCode(self);
 
var idgen = hashCode(self);
  
console.log(idgen);
+
debuglog(idgen);
 
// Take username for standard databasename format
 
// Take username for standard databasename format
 
var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
 
var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
Line 27: Line 27:
 
this.CurrentPerson = {id: idgen, name: {user: self}, age: 0};
 
this.CurrentPerson = {id: idgen, name: {user: self}, age: 0};
 
 
console.log(PersonalDataDataBaseName);
+
debuglog(PersonalDataDataBaseName);
console.log(this.CurrentPerson);
+
debuglog(this.CurrentPerson);
 
}
 
}
  
Line 88: Line 88:
  
 
getRecord.onsuccess = function() {
 
getRecord.onsuccess = function() {
if ( DEBUG ) {
+
debuglog( "Record found in database: \n <<");
console.log( "Record found in database: \n <<");
+
debuglog( getRecord.result );
console.log( getRecord.result );
+
debuglog( ">>");
console.log( ">>");
 
}
 
 
// Essential recordAdd
 
// Essential recordAdd
 
if ( sqitch === "aggressive push" ) {
 
if ( sqitch === "aggressive push" ) {
Line 101: Line 99:
 
 
 
if ( getRecord.result != undefined ) {
 
if ( getRecord.result != undefined ) {
if ( DEBUG ) {
+
debuglog("getting from store");
console.log("getting from store");
 
}
 
 
inp_obj = getRecord.result;
 
inp_obj = getRecord.result;
 
} else {
 
} else {
if ( DEBUG ) {
+
debuglog("overriding whatever in there");
console.log("overriding whatever in there");
 
}
 
 
inp_obj = record;
 
inp_obj = record;
 
store.put(inp_obj);
 
store.put(inp_obj);
Line 118: Line 112:
 
if ( sqitch === "get record" ) {
 
if ( sqitch === "get record" ) {
 
// if found, let's use that from now on
 
// if found, let's use that from now on
console.log("Getting record");
+
debuglog("Getting record");
 
// let's return it so that our addrecord can be used as a getter as well
 
// let's return it so that our addrecord can be used as a getter as well
 
return inp_obj;
 
return inp_obj;
Line 125: Line 119:
 
// endof onsuccess
 
// endof onsuccess
 
} else {
 
} else {
console.log("record not found");
+
debuglog("record not found");
 
if ( sqitch === "update record" ) {
 
if ( sqitch === "update record" ) {
 
inp_obj = record;
 
inp_obj = record;
Line 155: Line 149:
 
} else {
 
} else {
 
store.put(inp_obj);
 
store.put(inp_obj);
console.log("mark N");
+
debuglog("mark N");
console.log(inp_obj);
+
debuglog(inp_obj);
console.log(inp_obj[ control_1 ]);
+
debuglog(inp_obj[ control_1 ]);
console.log("mark NN")
+
debuglog("mark NN")
 
inp_obj = inp_obj[ control_1 ];
 
inp_obj = inp_obj[ control_1 ];
 
}
 
}
Line 167: Line 161:
  
 
}
 
}
console.log("return 3")
+
debuglog("return 3")
 
// return inp_obj;
 
// return inp_obj;
  
Line 187: Line 181:
 
}
 
}
  
console.log("return 2")
+
debuglog("return 2")
 
resolve( inp_obj );
 
resolve( inp_obj );
  
Line 193: Line 187:
  
 
tx.oncomplete = function() {
 
tx.oncomplete = function() {
console.log("return 1")
+
debuglog("return 1")
 
db.close();
 
db.close();
 
};
 
};
 
};
 
};
console.log("return 0")
+
debuglog("return 0")
  
 
}).then( function(result) {
 
}).then( function(result) {
console.log("Mark 3");
+
debuglog("Mark 3");
console.log(result);
+
debuglog(result);
 
return result;
 
return result;
 
});
 
});
 +
}
 +
 +
function debuglog( l0g ){
 +
if ( Debug_switch ) {
 +
debuglog( l0g );
 +
}
 
}
 
}
 
};
 
};

Revision as of 09:46, 20 May 2019

// License: GPL
// Indexeddb JavaScript 
debuglog('Indb loading');

function idxdbContainer( ww	) {
	var Debug_switch = false;
	hashCode = function(s) {
		if ( s == undefined) {
			s = '00'
		}
		var h = 0, l = s.length, i = 0;
		if ( l > 0 )
			while (i < l)
				h = (h << 5) - h + s.charCodeAt(i++) | 0;
			return h;
		};
		
		if ( ww.user != undefined ) {
			var self = ww.user.getName();
			
			var idgen = hashCode(self);																										

			debuglog(idgen);
			// Take username for standard databasename format
			var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
			// Use username in user to get up to date version of self
			this.CurrentPerson = {id: idgen, name: {user: self}, age: 0};
			
			debuglog(PersonalDataDataBaseName);
			debuglog(this.CurrentPerson);
		}

	// ADD MORE DOC

	// EnqueuePDIO a PDIOLocalDatabase:User database access function with modes
	// External accessible wrapper for Enquire
	
	this.EnqueuePDIO = async function( sw, c1, c2, c3 ) {
		var k = await Enquire( PersonalDataDataBaseName, this.CurrentPerson, sw, c1, c2, c3 );
		return k;
	}

	// Enquire, a general purpose indexeddb update function with modes

	// Mode 'aggressive push':
	// parameter record is pushed to database, overwriting whatever is there

	// Mode 'checkin':
	// record is retrieved from database, parameter record is pushed if not found

	// Mode 'get record'
	// updates record given as argument

	// Mode 'update record' field1 field2 field3
	// retrieve record, make record.field1.field2 = field3, push back

	// Mode 'update record' field1 field2
	// retrieve record, make record.field1 = field2, push back

	// Mode 'remove from record' field1 
	// retrieve record, splice/delete record.field1, push back
	
	// Mode 'remove from record' field1 field2 
	// retrieve record, splice/delete record.field1.field2, push back

	async function Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) {
		return new Promise(
			function(resolve, reject) {

				var pddb = window.indexedDB.open(pddbname, 3);
				var inp_obj;
				pddb.onupgradeneeded = function() {
					var db = pddb.result;
					var store = db.createObjectStore(pddbname, {keyPath: "id"});

						// shouldn't the index be the wiki.personaldata.io username? 

						var index = store.createIndex("NameIndex", ["id"]);
				};
				pddb.onsuccess = function() {
					var db = pddb.result;
					var tx = db.transaction(pddbname, "readwrite");
					var store = tx.objectStore(pddbname);
					var index = store.index("NameIndex");	

					// Get the original record from the db...
					var getRecord = index.get([record.id]);

					getRecord.onsuccess = function() {
							debuglog( "Record found in database: \n <<");
							debuglog( getRecord.result );
							debuglog( ">>");
						// Essential recordAdd																													
						if ( sqitch === "aggressive push" ) {
							inp_obj = record;
							store.put( inp_obj );
							return inp_obj;
						} else if ( sqitch === "checkin" ) {
							
							if ( getRecord.result != undefined ) {
									debuglog("getting from store");
								inp_obj = getRecord.result;
							} else {
								debuglog("overriding whatever in there");
								inp_obj = record;
								store.put(inp_obj);
							}

						} else {
							if ( getRecord.result != undefined ) {		
								inp_obj = getRecord.result;
								if ( sqitch === "get record" ) {
									// if found, let's use that from now on
									debuglog("Getting record");
									// let's return it so that our addrecord can be used as a getter as well
									return inp_obj;
								} 
									//
								// endof onsuccess
							} else {
								debuglog("record not found");
								if ( sqitch === "update record" ) {
									inp_obj = record;
								} else {
									return undefined;
								}
							}
									// endof resultdefined	
						} 
								// endofELSE 
						if ( sqitch === "update record" ) { // Record Update															
							if ( control_1 != undefined ) {
								if ( control_2 != undefined ) {
									if ( control_3 != undefined ) {
										if ( inp_obj[ control_1 ] == undefined ) { inp_obj[ control_1 ] = (isNaN( control_2 )) ? {} : [] };
										inp_obj[ control_1 ][ control_2 ] = control_3;	// => "Bob"
										store.put(inp_obj);
									} else {
										inp_obj[ control_1 ] = inp_obj[ control_1 ] || {};
										if ( Array.isArray( inp_obj[ control_1 ] ) ) {
											inp_obj[ control_1 ].push( control_2 );
										} else {
											inp_obj[ control_1 ] = control_2;
										}
										store.put(inp_obj);

									}
									// +3 
								} else {
									store.put(inp_obj);
									debuglog("mark N");
									debuglog(inp_obj);
									debuglog(inp_obj[ control_1 ]);
									debuglog("mark NN")
									inp_obj = inp_obj[ control_1 ];
								}
									// +2
							} else {

								//return inp_obj;

							}
							debuglog("return 3")
							// return inp_obj;

						};

						if ( sqitch === "remove from record" ) { // Record Update															
							if ( control_1 != undefined ) {
								if ( control_2 != undefined ) {
									if ( Array.isArray( inp_obj[ control_1 ] ) && !isNaN( control_2 ) ) {
										inp_obj[ control_1 ].splice( control_2, 1 );
									} else {
										delete inp_obj[ control_1 ][ control_2 ];
									}
								} else {
									delete inp_obj[ control_1 ];
								}
							}
							store.put(inp_obj);
						}

						debuglog("return 2")
						resolve( inp_obj );

					};

					tx.oncomplete = function() {
						debuglog("return 1")
						db.close();
					};
				};
				debuglog("return 0")

			}).then( function(result) {
				debuglog("Mark 3");
				debuglog(result);
				return result;
			});
	}

	function debuglog( l0g ){
		if ( Debug_switch ) {
			debuglog( l0g );
		}
	}
};