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

From Wikibase Personal data
Jump to navigation Jump to search
m
 
(207 intermediate revisions by 2 users not shown)
Line 1: Line 1:
console.log("~");
+
// License: GPL
 +
// Experimental Main
 +
console.log("'⌣'");
  
// ADD MORE DOC
+
var mwEStruct = {
 +
wbXntity: 'config.values.wbEntity'
 +
},
  
var PersonalDataDataBaseName = "PDIO2";
+
language = "en",
var Jonathan = {id: 11345, name: {first: "Jonatha", last: "Lemmings"}, age: 32};
 
  
recordAdd(PersonalDataDataBaseName, Jonathan);
+
WBEStructure = {
recordUpdate(PersonalDataDataBaseName, Jonathan, 'phone', '+313851777');
+
subclassOfRelations: 'claims.P4', // location of P4 relations in wbEntity
recordCheck(PersonalDataDataBaseName, Jonathan);
+
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'
 +
},
  
function recordAdd(pddbname, record){
+
Relational_id_list = {  
  var pddb = window.indexedDB.open(pddbname, 3);
+
controller_id: [ WBEStructure['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 96 ],
  pddb.onupgradeneeded = function() {
+
personal_data_id: [ WBEStructure['subclassOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 421 ],
    var db = pddb.result;
+
interface_button_id: [ WBEStructure ['instanceOfRelations'], WBEStructure['IDPropLoc'], WBEStructure['IDName'], 487 ]
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
+
},
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
  };
 
  pddb.onsuccess = function() {
 
    var db = pddb.result;
 
    var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
 
    var store = tx.objectStore(PersonalDataDataBaseName);
 
    var index = store.index("NameIndex"); 
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[ Essential recordAdd  ]||||||||||||||||||||||||||||||
 
    var getRecord = index.get([record.name.last, record.name.first]);
 
    getRecord.onsuccess = function() {
 
        if (getRecord.result != undefined) {
 
            console.log("updating");
 
            inp_obj = getRecord.result;
 
        } else {
 
          inp_obj = record;
 
          store.put(inp_obj);
 
        }
 
        var sanitycheck = index.get([inp_obj.name.last, inp_obj.name.first]);
 
        sanitycheck.onsuccess = function() {
 
            console.log(sanitycheck);
 
        }
 
    }
 
    //|||||||||||||||||||||||||||||||||||||||||||||
 
    tx.oncomplete = function() {
 
      db.close();
 
    }; 
 
  };
 
};
 
  
function recordUpdate(pddbname, record, field, value){
+
PDWikibaseProcessor,
  var pddb = window.indexedDB.open(pddbname, 3);
+
PDindexedDB,
  pddb.onupgradeneeded = function() {
+
PDOOUI,
    var db = pddb.result;
+
Current_entity;
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
 
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
  };
 
  pddb.onsuccess = function() {
 
        // Start a new transaction
 
        var db = pddb.result;
 
        var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
 
        var store = tx.objectStore(PersonalDataDataBaseName);
 
        var index = store.index("NameIndex");
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[  Essential recordUpdate  ]||||||||||||||||||||||||||||||
 
        var inp_obj;
 
        var getRecord = index.get([record.name.last, record.name.first]);
 
        getRecord.onsuccess = function() {
 
            if( getRecord.result != undefined ) {
 
              console.log("updating");
 
              inp_obj = getRecord.result;
 
            } else {
 
              inp_obj = record;
 
            }
 
  
            inp_obj[field] = value;  // => "Bob"
 
            store.put(inp_obj);
 
  
            getRecordAgain = index.get([record.name.last, record.name.first]);
+
// JQ.'when' loads the current scripts asynchronously, continues when loading is 'done'
            getRecordAgain.onsuccess = function() {
+
$.when(
                console.log(getRecordAgain);  // => "Bob"
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/WbProcessor.js&action=raw&ctype=text/javascript" ),
                if ( getRecordAgain.result[field] == value ) {
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/PersonalData.js&action=raw&ctype=text/javascript" ),
                  console.log("Update succeeded");
+
    $.getScript( "//wiki.personaldata.io/w/index.php?title=User:Abel/OOInterface.js&action=raw&ctype=text/javascript" ),
                } else {
+
    $.Deferred( function( deferred ){
                  console.log("Update unsuccessful");
+
         $( deferred.resolve );
                }
+
     })
              }; 
+
).done( function(){
            };
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 
      tx.oncomplete = function() {
 
         db.close();
 
      };
 
     };
 
  };
 
  
function recordCheck(pddbname, record){
+
console.log('Main thread');
  var pddb = window.indexedDB.open(pddbname, 3);
+
 
  pddb.onupgradeneeded = function() {
+
// WBproc reads current mediawiki Entity upon instantiation
    var db = pddb.result;
+
PDWikibaseProcessor = new WikibaseProcessorContainer(mw, mwEStruct['wbXntity']);
    var store = db.createObjectStore(PersonalDataDataBaseName, {keyPath: "id"});
+
 
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
+
// 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)
  pddb.onsuccess = function() {
+
PDindexedDB = new IndexedDBContainer( mw );
    var db = pddb.result;
+
    var tx = db.transaction(PersonalDataDataBaseName, "readwrite");
+
// Contains classes for creating UI elements, no instantiation consequences
    var store = tx.objectStore(PersonalDataDataBaseName);
+
PDOOUI = new InterfaceMediaContainer( PDWikibaseProcessor, PDindexedDB );
    var index = store.index("NameIndex")
+
 
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||[ Essential recordCheck ]||||||||||||||
+
// Check if the current entity belongs to any path of interest.  
    var recordcheck = index.get([record.name.last, record.name.first]);
+
// For each element in object argument, a corresponding element in the return object will contain one of the following:
    recordcheck.onsuccess = function() {
+
// - undefined - the path didn't exist even partly)
      if ( recordcheck.result != undefined ) {
+
// - [ 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
        console.log(recordcheck);
+
 
      } else {
+
Current_entity = PDWikibaseProcessor.checkentity( Relational_id_list );
        console.log("...else undefined");
+
      }
+
// Business logicish part.
//||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
// If any path of interest returned anything...
    }
+
if ( Current_entity != undefined ) {
    tx.oncomplete = function() {
+
Object.keys(Current_entity).forEach( function ( entity_checked ){
      db.close();
+
//... 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
 +
}
 +
}
 +
});
 +
}
 +
 
 +
});

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
				}
			}
		});
	}

});