Difference between revisions of "User:Abel/OOInterface.js"

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(16 intermediate revisions by the same user not shown)
Line 37: Line 37:
 
// 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  
 
// 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['IDName'] ] } )['this_concerns'][0];
+
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  
 
// 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  
Line 109: Line 109:
 
 
 
function notify_porting() {
 
function notify_porting() {
 
var button_import = new OO.ui.ButtonWidget( { label: "Import Personal Data", classes: [ 'pduimark' ] } ),
 
button_export = new OO.ui.ButtonWidget( { label: "Export Personal Data", classes: [ 'pduimark' ] } );
 
  
button_import.on( 'click', function () {
+
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 );
 +
var input_object = event;
 +
readFileContent( input_object ).then( function(content) {
 +
var data = WikibaseProcessor.rxjson(content);
 +
indexedDBobject.EnqueuePDIO('import record', data);
 +
});
 +
});
 +
 
 +
button_export.on( 'click', function () {
 +
 
 +
var get_record = indexedDBobject.EnqueuePDIO('get record');
  
button_export.on( 'click', function () {
+
get_record.then( function(result) {
 +
console.log("happening");
 +
if ( result === undefined ) { window.alert("No local data to export") } else {  
  
var get_record = indexedDBobject.EnqueuePDIO('update record');
+
var filename_tosaveas = "pdiolocalexport_" + result.name.user + "_" + Date.now() + '.json';
 +
 +
delete result.id;
 +
delete 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);
 +
}
  
get_record.then( function(result) {
+
downloadLink.click();
var filename_tosaveas = "pdiolocalexport_" + result.name.user + "_" + Date.now();
 
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 } );
 
});
 
});
 +
}
  
mw.notify( $( button_import.$element, '<br>', button_export.$element ) );
+
function readFileContent(file) {
 
+
const reader = new FileReader()
 
+
return new Promise((resolve, reject) => {
 +
reader.onload = event => resolve(event.target.result)
 +
reader.onerror = error => reject(error)
 +
reader.readAsText(file)
 +
})
 
}
 
}
  
 
}
 
}

Latest revision as of 18:18, 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 );
					var input_object = event;
					readFileContent( input_object ).then( function(content) {
						var data = WikibaseProcessor.rxjson(content);
						indexedDBobject.EnqueuePDIO('import record', data);
					});
				});

			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';
						
						delete result.id;
						delete 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 }	);
		});
	}

	function readFileContent(file) {
		const reader = new FileReader()
		return new Promise((resolve, reject) => {
			reader.onload = event => resolve(event.target.result)
			reader.onerror = error => reject(error)
			reader.readAsText(file)
		})
	}

}