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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(26 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 ) {
 
 
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);
+
 
 +
// 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;
  
console.log(idgen);
+
button_save[ extrafield ] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ] } );
// Take username for standard databasename format
+
var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
+
textInput[ extrafield ] = new OO.ui.TextInputWidget( {
// Use username in user to get up to date version of self
+
placeholder: default_value,
this.CurrentPerson = {id: idgen, name: {user: self}, age: 0};
+
classes: [ 'pduimark' ]
+
} );
console.log(PersonalDataDataBaseName);
+
 
console.log(this.CurrentPerson);
+
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 );
  
// ADD MORE DOC
+
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 () {
  
// EnqueuePDIO a PDIOLocalDatabase:User database access function with modes
+
indexedDBobject.EnqueuePDIO('update record', concern_origin, data, textInput[ data ].value );
// External accessible wrapper for Enquire
 
 
this.EnqueuePDIO = async function( sw, c1, c2, c3 ) {
 
var k = Enquire( PersonalDataDataBaseName, this.CurrentPerson, sw, c1, c2, c3 );
 
console.log("Mark K");
 
console.log(k);
 
return k.result;
 
}
 
  
// Enquire, a general purpose indexeddb update function with modes
+
personal_data_edit_interface_render();
 +
});
  
// Mode 'aggressive push':
+
button_remove[data].on( 'click', function () {
// parameter record is pushed to database, overwriting whatever is there
+
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 '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
+
function clear_render() {
// retrieve record, make record.field1.field2 = field3, push back
+
var ifel = document.getElementsByClassName('pduimark');
 +
while( ifel[0] ) {
 +
ifel[0].parentNode.removeChild(ifel[0]);
 +
};
 +
};
 +
 +
function notify_porting() {
  
// Mode 'update record' field1 field2
+
mw.loader.using( 'oojs-ui-core' ).done( function () {
// retrieve record, make record.field1 = field2, push back
 
  
function Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) {
+
var button_import = new OO.ui.SelectFileWidget( { label: "Import Personal Data", classes: [ 'pduimark' ] } ),  
return new Promise(
 
function(resolve, reject) {
 
  
var pddb = window.indexedDB.open(pddbname, 3);
+
button_export = new OO.ui.ButtonWidget( { label: "Export 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_import.on( 'change', function (event) {
  
var index = store.createIndex("NameIndex", ["id"]);
+
if (window.webkitURL != null) {
};
+
console.log("loading file on webkit")
pddb.onsuccess = function() {
+
} else {
var db = pddb.result;
+
console.log("loading file non webkit")
var tx = db.transaction(pddbname, "readwrite");
+
}
var store = tx.objectStore(pddbname);
 
var index = store.index("NameIndex");
 
  
// Get the original record from the db...
+
console.log( event );
var getRecord = index.get([record.id]);
 
  
getRecord.onsuccess = function() {
+
});
console.log( "Record found in database: \n <<");
 
console.log( getRecord.result );
 
console.log( ">>");
 
  
// Essential recordAdd
+
button_export.on( 'click', function () {
if ( sqitch === "aggressive push" ) {
 
inp_obj = record;
 
store.put( inp_obj );
 
return inp_obj;
 
} else if ( sqitch === "checkin" ) {
 
 
if ( getRecord.result != undefined ) {
 
console.log("getting from store");
 
inp_obj = getRecord.result;
 
} else {
 
console.log("overriding whatever in there");
 
inp_obj = record;
 
store.put(inp_obj);
 
}
 
  
} else {
+
var get_record = indexedDBobject.EnqueuePDIO('get record');
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 ) {
 
 
if ( Array.isArray( inp_obj[ control_1 ][ control_2 ] ) ) {
 
inp_obj[ control_1 ][ control_2 ].push( control_3 );
 
} else {
 
inp_obj[ control_1 ][ control_2 ] = control_3; // => "Bob"
 
}
 
store.put(inp_obj);
 
} else {
 
 
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);
 
  
}
+
get_record.then( function(result) {
// +3
+
console.log("happening");
} else {
+
if ( result === undefined ) { window.alert("No local data to export") } 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 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();
console.log("return 3")
 
// return inp_obj;
 
  
};
+
}
console.log("return 2")
 
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 }  );
		});
	}

}