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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
// License: GPL
 
// License: GPL
// Indexeddb JavaScript
+
// User Interface Rendering
console.log('Indb loading');
+
console.log('Interface handler loading');
  
function idxdbContainer( ww ) {
+
function InterfaceMediaContainer ( WikibaseProcessor, indexedDBobject ) {
var DEBUG = false;
+
hashCode = function(s) {
+
this.renderInterface = function( sqitch ) {
if ( s == undefined) {
+
switch( sqitch ) {
s = '00'
+
case 'controller_id':
 +
controller_render();
 +
break;
 +
case 'personal_data_id':
 +
pd_site_render();
 +
break;
 +
case 'interface_button_id':
 +
personal_data_edit_interface_render();
 +
break;
 
}
 
}
var h = 0, l = s.length, i = 0;
+
}
if ( l > 0 )
+
 
while (i < l)
+
var button_save = [],
h = (h << 5) - h + s.charCodeAt(i++) | 0;
+
button_remove = [],
return h;
+
textInput = [];
};
+
 
 +
async function controller_render(){
 +
console.log("this function is done by the current page controller for now");
 +
notify_porting();
 +
}
 +
 
 +
async function pd_site_render(){
 +
 
 +
};
 +
 
 +
async function personal_data_edit_interface_render(){
 +
clear_render();
 
 
if ( ww.user != undefined ) {
+
// Get concerns relation :: Parts of this must be moved upstream, MAIN should know that this is the data the renderer will need and provide it
var self = ww.user.getName();
+
 
+
var concern_origin = WikibaseProcessor.checkentity( { this_concerns: [ WBEStructure['concernsRelation'], WBEStructure['IDPropLoc'], WBEStructure['qIDPropName'] ] } )['this_concerns'][0];
var idgen = hashCode(self);
 
  
console.log(idgen);
+
// Get defval for entity :: Parts of this must be moved upstream, MAIN should know that this is the data the renderer will need and provide it
// Take username for standard databasename format
+
 
var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
+
var default_value = WikibaseProcessor.checkentity( { default_value: [ WBEStructure['defaultValRelation'], WBEStructure['IDPropLoc'] ] } )['default_value'][0]; //defal
// Use username in user to get up to date version of self
+
 
this.CurrentPerson = {id: idgen, name: {user: self}, age: 0};
+
var get_record = indexedDBobject.EnqueuePDIO('update record');
+
 
console.log(PersonalDataDataBaseName);
+
get_record.then( function(result) {
console.log(this.CurrentPerson);
+
 
}
+
mw.loader.using( 'oojs-ui-core' ).done( function () {
 +
 
 +
button_save = [],
 +
button_remove = [],
 +
textInput = [];
 +
 
 +
var extrafield = ( result[concern_origin] != undefined ) ? Object.keys(result[concern_origin]).length : 0;
 +
 
 +
button_save[ extrafield ] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ] } );
 +
 +
textInput[ extrafield ] = new OO.ui.TextInputWidget( {
 +
placeholder: default_value,
 +
classes: [ 'pduimark' ]
 +
} );
  
// ADD MORE DOC
+
button_save[ extrafield ].on( 'click', function () {
 +
indexedDBobject.EnqueuePDIO('update record', concern_origin, extrafield, textInput[ extrafield ].value );
 +
personal_data_edit_interface_render();
 +
});
  
// EnqueuePDIO a PDIOLocalDatabase:User database access function with modes
+
$( '#mw-content-text' ).prepend( button_save[ extrafield ].$element );
// External accessible wrapper for Enquire
+
$( '#mw-content-text' ).prepend( textInput[ extrafield ].$element );
 
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
+
if ( extrafield > 0) {
 +
Object.keys( result[concern_origin] ).forEach( function(data) {
 +
button_save[data] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ] } );
 +
button_remove[data] = new OO.ui.ButtonWidget( { label: "Delete", classes: [ 'pduimark' ] } );
 +
textInput[data] = new OO.ui.TextInputWidget( {
 +
value: result[concern_origin][data], ///
 +
placeholder: default_value,
 +
classes: [ 'pduimark' ]
 +
} );
 +
button_save[data].on( 'click', function () {
  
// Mode 'aggressive push':
+
indexedDBobject.EnqueuePDIO('update record', concern_origin, data, textInput[ data ].value );
// parameter record is pushed to database, overwriting whatever is there
 
  
// Mode 'checkin':
+
personal_data_edit_interface_render();
// record is retrieved from database, parameter record is pushed if not found
+
});
  
// Mode 'get record'
+
button_remove[data].on( 'click', function () {
// updates record given as argument
+
indexedDBobject.EnqueuePDIO('remove from record', concern_origin, data );
 +
personal_data_edit_interface_render();
 +
});
 +
$( '#mw-content-text' ).prepend( button_remove[data].$element );
 +
$( '#mw-content-text' ).prepend( button_save[data].$element );
 +
$( '#mw-content-text' ).prepend( textInput[data].$element );
  
// 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
+
function clear_render() {
// retrieve record, splice/delete record.field1, push back
+
var ifel = document.getElementsByClassName('pduimark');
 +
while( ifel[0] ) {
 +
ifel[0].parentNode.removeChild(ifel[0]);
 +
};
 +
};
 
 
// Mode 'remove from record' field1 field2
+
function notify_porting() {
// retrieve record, splice/delete record.field1.field2, push back
 
  
async function Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) {
+
mw.loader.using( 'oojs-ui-core' ).done( function () {
return new Promise(
 
function(resolve, reject) {
 
  
var pddb = window.indexedDB.open(pddbname, 3);
+
var button_import = new OO.ui.SelectFileWidget( { label: "Import Personal Data", classes: [ 'pduimark' ] } ),
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?
+
button_export = new OO.ui.ButtonWidget( { label: "Export Personal Data", classes: [ 'pduimark' ] } );
  
var index = store.createIndex("NameIndex", ["id"]);
+
button_import.on( 'change', function (event) {
};
 
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...
+
if (window.webkitURL != null) {
var getRecord = index.get([record.id]);
+
console.log("loading file on webkit")
 +
} else {
 +
console.log("loading file non webkit")
 +
}
  
getRecord.onsuccess = function() {
+
console.log( event );
if ( DEBUG ) {
 
console.log( "Record found in database: \n <<");
 
console.log( getRecord.result );
 
console.log( ">>");
 
}
 
// Essential recordAdd
 
if ( sqitch === "aggressive push" ) {
 
inp_obj = record;
 
store.put( inp_obj );
 
return inp_obj;
 
} else if ( sqitch === "checkin" ) {
 
 
if ( getRecord.result != undefined ) {
 
if ( DEBUG ) {
 
console.log("getting from store");
 
}
 
inp_obj = getRecord.result;
 
} else {
 
if ( DEBUG ) {
 
console.log("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
 
console.log("Getting record");
 
// let's return it so that our addrecord can be used as a getter as well
 
return inp_obj;
 
}
 
//
 
// endof onsuccess
 
} else {
 
console.log("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 ) {
 
inp_obj[ control_1 ] = 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);
 
  
}
+
button_export.on( 'click', function () {
// +3
 
} else {
 
store.put(inp_obj);
 
console.log("mark N");
 
console.log(inp_obj);
 
console.log(inp_obj[ control_1 ]);
 
console.log("mark NN")
 
inp_obj = inp_obj[ control_1 ];
 
}
 
// +2
 
} else {
 
  
//return inp_obj;
+
var get_record = indexedDBobject.EnqueuePDIO('get record');
  
}
+
get_record.then( function(result) {
console.log("return 3")
+
console.log("happening");
// return inp_obj;
+
if ( result === undefined ) { window.alert("No local data to export") } else {
  
};
+
var filename_tosaveas = "pdiolocalexport_" + result.name.user + "_" + Date.now() + '.json';
 
+
if ( sqitch === "remove from record" ) { // Record Update
+
result.id = '';
if ( control_1 != undefined ) {
+
result.name.user = '';
if ( control_2 != undefined ) {
+
if ( Array.isArray( inp_obj[ control_1 ] ) && !isNaN( control_2 ) ) {
+
var export_to_text = JSON.stringify(result);
inp_obj[ control_1 ].splice( control_2, 1 );
+
var textFileAsBlob = new Blob([export_to_text], {type:'text/plain'});
} else {
+
var downloadLink = document.createElement("a");
delete inp_obj[ control_1 ][ control_2 ];
+
downloadLink.download = filename_tosaveas;
}
+
if (window.webkitURL != null)
} else {
+
{
delete inp_obj[ control_1 ];
+
// Chrome allows the link to be clicked
}
+
// without actually adding it to the DOM.
}
+
downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
store.put(inp_obj);
+
}
 +
else
 +
{
 +
// Firefox requires the link to be added to the DOM
 +
// before it can be clicked.
 +
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
 +
downloadLink.style.display = "none";
 +
document.body.appendChild(downloadLink);
 
}
 
}
  
console.log("return 2")
+
downloadLink.click();
resolve( inp_obj );
 
  
};
+
}
  
tx.oncomplete = function() {
+
});
console.log("return 1")
 
db.close();
 
};
 
};
 
console.log("return 0")
 
  
}).then( function(result) {
 
console.log("Mark 3");
 
console.log(result);
 
return result;
 
 
});
 
});
 +
 +
mw.notify( $( button_import.$element ), { autoHide: false }  );
 +
mw.notify( $( button_export.$element ), { autoHide: false }  );
 +
});
 
}
 
}
};
+
 
 +
}

Latest revision as of 17:46, 27 May 2019

// License: GPL
// User Interface Rendering 
console.log('Interface handler loading');

function InterfaceMediaContainer ( WikibaseProcessor, indexedDBobject ) {
	
	this.renderInterface = function( sqitch ) {
		switch( sqitch ) {
			case 'controller_id':
				controller_render();
				break;
			case 'personal_data_id':
				pd_site_render();
				break;
			case 'interface_button_id':
				personal_data_edit_interface_render();
				break;
		}
	}

	var button_save = [],
		button_remove = [],
		textInput = [];

	async function controller_render(){
		console.log("this function is done by the current page controller for now");
		notify_porting();
	}

	async function pd_site_render(){

	};

	async function personal_data_edit_interface_render(){
		clear_render();
		
		// Get concerns relation :: Parts of this must be moved upstream, MAIN should know that this is the data the renderer will need and provide it 

		var concern_origin = WikibaseProcessor.checkentity( { this_concerns: [ WBEStructure['concernsRelation'], WBEStructure['IDPropLoc'], WBEStructure['qIDPropName'] ] } )['this_concerns'][0];

		// Get defval for entity :: Parts of this must be moved upstream, MAIN should know that this is the data the renderer will need and provide it 

		var default_value = WikibaseProcessor.checkentity( { default_value: [ WBEStructure['defaultValRelation'], WBEStructure['IDPropLoc'] ] } )['default_value'][0]; //defal

		var get_record = indexedDBobject.EnqueuePDIO('update record');

		get_record.then( function(result) {

			mw.loader.using( 'oojs-ui-core' ).done( function () {

				button_save = [],
				button_remove = [],
				textInput = [];

				var extrafield = ( result[concern_origin] != undefined ) ? Object.keys(result[concern_origin]).length : 0;

				button_save[ extrafield ] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ] } );
				
				textInput[ extrafield ] = new OO.ui.TextInputWidget( { 
						placeholder: default_value,
						classes: [ 'pduimark' ]
				} );

				button_save[ extrafield ].on( 'click', function () {
							indexedDBobject.EnqueuePDIO('update record', concern_origin, extrafield, textInput[ extrafield ].value );
							personal_data_edit_interface_render();
				});

				$( '#mw-content-text' ).prepend( button_save[ extrafield ].$element );
				$( '#mw-content-text' ).prepend( textInput[ extrafield ].$element );

				if ( extrafield > 0) {
					Object.keys( result[concern_origin] ).forEach( function(data) {
						button_save[data] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ]	} );
						button_remove[data] = new OO.ui.ButtonWidget( { label: "Delete", classes: [ 'pduimark' ] } );
						textInput[data] = new OO.ui.TextInputWidget( { 
							value: result[concern_origin][data],				///
							placeholder: default_value,
							classes: [ 'pduimark' ]
						} );
						button_save[data].on( 'click', function () {

							indexedDBobject.EnqueuePDIO('update record', concern_origin, data, textInput[ data ].value );

							personal_data_edit_interface_render();
						});

						button_remove[data].on( 'click', function () {
							indexedDBobject.EnqueuePDIO('remove from record', concern_origin, data );
							personal_data_edit_interface_render();
						});
						$( '#mw-content-text' ).prepend( button_remove[data].$element );
						$( '#mw-content-text' ).prepend( button_save[data].$element );
						$( '#mw-content-text' ).prepend( textInput[data].$element );

					});

				}
			});
		});
	};

	function clear_render() {
		var ifel = document.getElementsByClassName('pduimark');
		while( ifel[0] ) {
			ifel[0].parentNode.removeChild(ifel[0]);
		};
	};
	
	function notify_porting() {

		mw.loader.using( 'oojs-ui-core' ).done( function () {

			var button_import = new OO.ui.SelectFileWidget( { label: "Import Personal Data", classes: [ 'pduimark' ] } ), 

				button_export = new OO.ui.ButtonWidget( { label: "Export Personal Data", classes: [ 'pduimark' ] } );

				button_import.on( 'change', function (event) {

					if (window.webkitURL != null) {
						console.log("loading file on webkit")
					} else {
						console.log("loading file non webkit")
					}

					console.log( event );

				});

			button_export.on( 'click', function () {

				var get_record = indexedDBobject.EnqueuePDIO('get record');

				get_record.then( function(result) {
					console.log("happening");
					if ( result === undefined ) { window.alert("No local data to export") } else { 

						var filename_tosaveas = "pdiolocalexport_" + result.name.user + "_" + Date.now() + '.json';
						
						result.id = '';
						result.name.user = '';
						
						var export_to_text = JSON.stringify(result);
						var textFileAsBlob = new Blob([export_to_text], {type:'text/plain'});
						var downloadLink = document.createElement("a");
						downloadLink.download = filename_tosaveas;
						if (window.webkitURL != null)
						{
							// Chrome allows the link to be clicked
							// without actually adding it to the DOM.
							downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
						}
						else
						{
							// Firefox requires the link to be added to the DOM
							// before it can be clicked.
							downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
							downloadLink.style.display = "none";
							document.body.appendChild(downloadLink);
						}

						downloadLink.click();

					}

				});

			});

			mw.notify( $( button_import.$element ), { autoHide: false }  );
			mw.notify( $( button_export.$element ), { autoHide: false }  );
		});
	}

}