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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(44 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);
 
  
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
+
 
var 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(CurrentPerson);
+
 
}
+
mw.loader.using( 'oojs-ui-core' ).done( function () {
 +
 
 +
button_save = [],
 +
button_remove = [],
 +
textInput = [];
  
// ADD MORE DOC
+
var extrafield = ( result[concern_origin] != undefined ) ? Object.keys(result[concern_origin]).length : 0;
  
// EnqueuePDIO a PDIOLocalDatabase:User database access function with modes
+
button_save[ extrafield ] = new OO.ui.ButtonWidget( { label: "Save", classes: [ 'pduimark' ] } );
// External accessible wrapper for Enquire
+
+
textInput[ extrafield ] = new OO.ui.TextInputWidget( {  
this.EnqueuePDIO = function( sw, c1, c2, c3 ) {
+
placeholder: default_value,
return Enquire( PersonalDataDataBaseName, CurrentPerson, sw, c1, c2, c3 );
+
classes: [ 'pduimark' ]
}
+
} );
  
// Enquire, a general purpose indexeddb update function with modes
+
button_save[ extrafield ].on( 'click', function () {
 +
indexedDBobject.EnqueuePDIO('update record', concern_origin, extrafield, textInput[ extrafield ].value );
 +
personal_data_edit_interface_render();
 +
});
  
// Mode 'aggressive push':
+
$( '#mw-content-text' ).prepend( button_save[ extrafield ].$element );
// parameter record is pushed to database, overwriting whatever is there
+
$( '#mw-content-text' ).prepend( textInput[ extrafield ].$element );
  
// Mode 'checkin':
+
if ( extrafield > 0) {
// record is retrieved from database, parameter record is pushed if not found
+
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 'get record'
+
indexedDBobject.EnqueuePDIO('update record', concern_origin, data, textInput[ data ].value );
// tries to get record from db, returns undefined if not found
 
  
// Mode 'update record' field1 field2 field3
+
personal_data_edit_interface_render();
// retrieve record, make record.field1.field2 = field3, push back
+
});
  
// Mode 'update record' field1 field2
+
button_remove[data].on( 'click', function () {
// retrieve record, make record.field1 = field2, push back
+
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 Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) {
+
});
var pddb = window.indexedDB.open(pddbname, 3);
 
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"]);
+
function clear_render() {
 +
var ifel = document.getElementsByClassName('pduimark');
 +
while( ifel[0] ) {
 +
ifel[0].parentNode.removeChild(ifel[0]);
 
};
 
};
pddb.onsuccess = function() {
+
};
var db = pddb.result;
+
var tx = db.transaction(pddbname, "readwrite");
+
function notify_porting() {
var store = tx.objectStore(pddbname);
 
var index = store.index("NameIndex");
 
  
// Get the original record from the db...
+
mw.loader.using( 'oojs-ui-core' ).done( function () {
var getRecord = index.get([record.id]);
 
var inp_obj;
 
  
getRecord.onsuccess = function() {
+
var button_import = new OO.ui.SelectFileWidget( { label: "Import Personal Data", classes: [ 'pduimark' ] } ),
console.log( "Record found in database: \n <<");
 
console.log( getRecord.result );
 
console.log( ">>");
 
  
// Essential recordAdd
+
button_export = new OO.ui.ButtonWidget( { label: "Export Personal Data", classes: [ 'pduimark' ] } );
if ( sqitch === "aggressive push" ) {
+
 
inp_obj = record;
+
button_import.on( 'change', function (event) {
store.put( inp_obj );
+
 
return inp_obj;
+
if (window.webkitURL != null) {
} else if ( sqitch === "checkin" ) {
+
console.log("loading file on webkit")
 
if ( getRecord.result != undefined ) {
 
console.log("getting from store");
 
inp_obj = getRecord.result;
 
 
} else {
 
} else {
console.log("overriding whatever in there");
+
console.log("loading file non webkit")
inp_obj = record;
 
store.put(inp_obj);
 
 
}
 
}
  
} else {
+
console.log( event );
if ( getRecord.result != undefined ) {
+
 
inp_obj = getRecord.result;
+
});
if ( sqitch === "get record" ) {
+
 
// if found, let's use that from now on
+
button_export.on( 'click', function () {
console.log("Getting record");
+
 
// let's return it so that our addrecord can be used as a getter as well
+
var get_record = indexedDBobject.EnqueuePDIO('get record');
return inp_obj;
+
 
}
+
get_record.then( function(result) {
//
+
console.log("happening");
// endof onsuccess
+
if ( result === undefined ) { window.alert("No local data to export") } else {  
} else {
+
 
console.log("record not found");
+
var filename_tosaveas = "pdiolocalexport_" + result.name.user + "_" + Date.now() + '.json';
if ( sqitch === "update record" ) {
+
inp_obj = record;
+
result.id = '';
} else {
+
result.name.user = '';
return undefined;
+
 +
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
// endof resultdefined
+
{
}
+
// Firefox requires the link to be added to the DOM
// endofELSE
+
// before it can be clicked.
if ( sqitch === "update record" ) { // Record Update
+
downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
if ( control_1 != undefined ) {
+
downloadLink.style.display = "none";
if ( control_2 != undefined ) {
+
document.body.appendChild(downloadLink);
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);
 
 
 
}
 
// +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;
+
downloadLink.click();
  
 
}
 
}
  
};
+
});
  
};
+
});
  
return inp_obj;
+
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 }  );
		});
	}

}