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

From Wikibase Personal data
Jump to navigation Jump to search
m
m
 
(57 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
// License: GPL
 
// License: GPL
 
// Experimental Main
 
// Experimental Main
 
 
console.log("'⌣'");
 
console.log("'⌣'");
  
 
var mwEStruct = {
 
var mwEStruct = {
      wbXntity: 'config.values.wbEntity'
+
wbXntity: 'config.values.wbEntity'
    },
+
},
 
 
    language = "en",
 
 
 
wbEStruct = {
 
        subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 
        defaultValRelation: 'claims.P108',
 
        concernsRelation: 'claims.P110',
 
        instanceOfRelations: 'claims.P3',
 
        IDPropLoc:  'mainsnak.datavalue.value', // location of ID properties in related entity
 
        IDName:  'numeric-id', // ID property name which checked
 
        qIDPropName: 'id',
 
        standardValue: 'labels' + '.' + language + '.' + 'value'
 
    },
 
 
 
relational_id_list = {
 
  controller_id: [ wbEStruct['instanceOfRelations'], 96 ],
 
  personal_data_id: [ wbEStruct['subclassOfRelations'], 421 ],
 
  interface_button_id: [ wbEStruct ['instanceOfRelations'], 487 ]
 
};
 
 
 
var wbproc = new wbContainer(mw, mwEStruct['wbXntity']),
 
  
// Check whether the current page is in a relation of interest to an id of interest
+
language = "en",
current_entity = wbproc.checkentity( relational_id_list, [ wbEStruct['IDPropLoc'], wbEStruct['IDName'] ] );
 
// This should return an object of relations and ids
 
  
console.log(current_entity);
+
WBEStructure = {
 +
subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
 +
defaultValRelation: 'claims.P108',
 +
concernsRelation: 'claims.P110',
 +
instanceOfRelations: 'claims.P3',
 +
IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
 +
IDName: 'numeric-id', // ID property name which checked
 +
qIDPropName: 'id',
 +
standardValue: 'labels' + '.' + language + '.' + 'value'
 +
},
  
// for each match, render the corresponding ui elements
+
Relational_id_list = {
// current_entity.forEach(pdooui.render);
+
controller_id: [ WBEStructure['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 96 ],
 +
personal_data_id: [ WBEStructure['subclassOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 421 ],
 +
interface_button_id: [ WBEStructure ['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 487 ]
 +
},
  
// License: GPL
+
PDWikibaseProcessor,
// Wikibase Processor
+
PDindexedDB,
 +
PDOOUI,
 +
Current_entity;
  
    function wbContainer( universe, location ) {
 
  
        this.main = interjson( check( universe, location ) );
+
// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
        var container = this;
+
$.when(
 +
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
 +
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/PersonalData.js&action=raw&ctype=text/javascript" ),
 +
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/OOInterface.js&action=raw&ctype=text/javascript" ),
 +
    $.Deferred( function( deferred ){
 +
        $( deferred.resolve );
 +
    })
 +
).done( function(){
  
        this.checkentity = function ( rel_id_list, paths ) {
+
console.log('Main thread');
            var return_obj = [],
 
                temporary_obj = [],
 
                baseRelation;
 
            if ( Object.keys(rel_id_list).length > 0 ) {
 
                Object.keys(rel_id_list).forEach( function( item ){
 
                    temporary_obj = [];
 
                    baseRelation = checkdive( container.main, rel_id_list[item][0] );
 
                    rise( temporary_obj, depthdive( baseRelation, paths ));
 
                    if ( Object.keys(temporary_obj).length > 0 ) {
 
                        Object.keys(temporary_obj).forEach( function (hit) {
 
                            if ( temporary_obj[hit] == rel_id_list[item][1] ) {
 
                                return_obj.push(rel_id_list[item]);
 
                            }
 
                        });
 
                    } else if ( temporary_obj == rel_id_list[item][1] ) {
 
                        return_obj.push(rel_id_list[item]);
 
                    }
 
                });
 
            }
 
            return return_obj;
 
        }
 
  
        // the list of private helper functions, could not be accessed from outside
+
// WBproc reads current mediawiki Entity upon instantiation
        function indexinterpolate(obj,i) { return  (obj[i] !== undefined) ? obj[i] : obj};
+
PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);
        // helper function for checking a part of an array exists
 
        function indexcheck(obj,i) {  if ( obj !== undefined) { return  (obj[i] !== undefined) ? obj[i] : undefined } };
 
        // dive selects matrix.a.b.c.d from the array called matrix and 'a.b.c.d' as string
 
        // if a.b.c.d does not exists, it returns the substructure until the substructure exists, if d does not exist, it returns matrix.a.b.c e.g.
 
       
 
        function dive(array, read){
 
            return (read + '').split('.').reduce(indexinterpolate, array);
 
        }
 
       
 
        function check(array, read){
 
            if ( array !== undefined ) {
 
                return (read + '').split('.').reduce(indexcheck, array);
 
            }
 
        }
 
       
 
        function checkdive( array, read){
 
            if ( check(array, read) !== undefined) {
 
                return dive(array, read);
 
            } else if ( Array.isArray(array) ) {
 
                return complexcheck( array, read );
 
            }
 
        }
 
  
        function complexcheck(array, read){
+
// PDxDB takes mediawiki, reads user name from it, creates a hash, and 'checks in' the user upon instantiation
            var ret_obj;
+
// (i.e. read record belonging to user, if it does not exist, create it)
            Object.keys(array).forEach( function(item){
+
PDindexedDB = new IndexedDBContainer( mw );
                rise( ret_obj, checkdive(array[item], read)) ;
+
            });
+
// Contains classes for creating UI elements, no instantiation consequences
            return ret_obj;
+
PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );
        }
 
  
        function depthdive(array, read){
+
// Check if the current entity belongs to any path of interest.
            var ret_obj,
+
// For each element in object argument, a corresponding element in the return object will contain one of the following:
                temp_obj = array;
+
// - undefined - the path didn't exist even partly)
            if ( Object.keys(read).length > 0 ) {
+
// - [ value or object ] - if the path existed fully, it returns what is at the end of the path. If last element in path is a value, it will return the value if found
                Object.keys(read).forEach( function(path) {
 
                    temp_obj = checkdive(temp_obj, path);
 
                });
 
                rise(ret_obj, temp_obj);
 
                return ret_obj;
 
            } else {
 
                return checkdive(array, read);
 
            }
 
        }
 
  
        function rise(array, to_push) {
+
Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
            if ( to_push !== undefined) {
+
                if ( Object.keys(to_push).length > 0 ){
+
// Business logicish part.
                    Object.keys(to_push).forEach( function(item){
+
// If any path of interest returned anything...
                        if ( array !== undefined ) {
+
if ( Current_entity != undefined ) {
                            array.push(to_push[item]);
+
Object.keys(Current_entity).forEach( function ( entity_checked ){
                        }
+
//... for each path that returned something
                    });
+
if ( Current_entity[entity_checked] != undefined ){
                } else {
+
if ( Object.keys( Current_entity[entity_checked] ).length > 0 ) {
                    array.push(to_push);
+
PDOOUI.renderInterface( entity_checked );
                }
+
//... call the OOUI renderer to render what belongs to this input
            }
+
}
        }
+
}
 +
});
 +
}
  
        function interjson( obj ) {
+
});
            try {
 
                return JSON.parse( obj )
 
            } catch (e) {
 
            // if not JSON, do silly things
 
                console.log('not a json ¯\_(ツ)_/¯');
 
            };
 
        }
 
       
 
    }
 

Latest revision as of 11:14, 24 May 2019

// License: GPL
// Experimental Main
console.log("'⌣'");

var mwEStruct = {
		wbXntity: 'config.values.wbEntity'
	},

	language = "en",

	WBEStructure = {
		subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
		defaultValRelation: 'claims.P108',
		concernsRelation: 'claims.P110',
		instanceOfRelations: 'claims.P3',
		IDPropLoc: 'mainsnak.datavalue.value', // location of ID properties in related entity
		IDName: 'numeric-id', // ID property name which checked 
		qIDPropName: 'id',
		standardValue: 'labels' + '.' + language + '.' + 'value'
	},

	Relational_id_list = { 
			controller_id: [ WBEStructure['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 96 ],
			personal_data_id: [ WBEStructure['subclassOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 421 ],
			interface_button_id: [ WBEStructure ['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 487 ]
	},

	PDWikibaseProcessor,
	PDindexedDB,
	PDOOUI,
	Current_entity;


// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
$.when(
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/PersonalData.js&action=raw&ctype=text/javascript" ),
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/OOInterface.js&action=raw&ctype=text/javascript" ),
    $.Deferred( function( deferred ){
        $( deferred.resolve );
    })
).done( function(){

	console.log('Main thread');

	// WBproc reads current mediawiki Entity upon instantiation
	PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);

	// PDxDB takes mediawiki, reads user name from it, creates a hash, and 'checks in' the user upon instantiation
	// (i.e. read record belonging to user, if it does not exist, create it)
	PDindexedDB = new IndexedDBContainer( mw );
	
	// Contains classes for creating UI elements, no instantiation consequences
	PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );

	// Check if the current entity belongs to any path of interest. 
	// For each element in object argument, a corresponding element in the return object will contain one of the following:
	// - undefined - the path didn't exist even partly)
	// - [ value or object ] - if the path existed fully, it returns what is at the end of the path. If last element in path is a value, it will return the value if found

	Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
	
	// Business logicish part.
	// If any path of interest returned anything...
	if ( Current_entity != undefined ) {
		Object.keys(Current_entity).forEach( function ( entity_checked ){
			//... for each path that returned something
			if ( Current_entity[entity_checked] != undefined ){
				if ( Object.keys( Current_entity[entity_checked] ).length > 0 ) {
					PDOOUI.renderInterface( entity_checked );
					//... call the OOUI renderer to render what belongs to this input
				}
			}
		});
	}

});