Skip to content

Commit

Permalink
Merge pull request #94 from latin-language-toolkit/config_delegators
Browse files Browse the repository at this point in the history
Config delegators
  • Loading branch information
LFDM committed May 27, 2014
2 parents abad471 + 95f5322 commit 2cab154
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 95 deletions.
47 changes: 33 additions & 14 deletions app/js/arethusa.core/configurator.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
*
*
*
* As of now a valid conf file contains five sections
* As of now a valid conf file contains four sections
* main
* navbar
* plugins
* retrievers
* resources
*
* Uses $http to retrieve additional configuration files that are embedded
Expand Down Expand Up @@ -117,8 +116,7 @@ angular.module('arethusa.core').service('configurator', function($injector, $htt
});
};

// Receives an external file and resolves it to a valid configuration
// file.
// Receives an external file and resolves it to a valid configuration file.
// The second param is optional.
this.defineConfiguration = function(confFile, location) {
var conf = this.loadConfFile(confFile, location);
Expand Down Expand Up @@ -147,7 +145,6 @@ angular.module('arethusa.core').service('configurator', function($injector, $htt
main: {},
navbar: {},
plugins: {},
retrievers: {},
resources: {}
};
};
Expand All @@ -173,7 +170,6 @@ angular.module('arethusa.core').service('configurator', function($injector, $htt
// Arrays are flat-pushed.
// Strings and Numbers are overwritten.
// a is extended with properties in b, that are not present in a.
//
this.mergeConfigurations = function(a, b) {
var that = this;
angular.forEach(b, function(value, key) {
Expand Down Expand Up @@ -212,30 +208,53 @@ angular.module('arethusa.core').service('configurator', function($injector, $htt
}
};

// this.configuration is set from outside on page load
// right now very hacky, not sure about the design of the conf file atm
// we therefore just tell the service where the conf for specific things
// is to be found in the JSON tree.
// I guess the key is to abstract the conf file a little more.
this.configurationFor = function(plugin) {
var conf = this.configuration;
return conf[plugin] ||
conf.plugins[plugin] ||
conf.retrievers[plugin] ||
conf.resources[plugin];
};

function standardProperties() {
return [
'name',
'main',
'template',
'external',
'listener',
'contextMenu',
'contextMenuTemplate',
'noView'
];
}
// Delegates a set of standard properties to the given object to allow
// a more direct access.
this.delegateConf = function(obj, otherKeys) {
var props = arethusaUtil.pushAll(standardProperties(), otherKeys);
angular.forEach(props, function(property, i) {
obj[property] = obj.conf[property];
});
};

this.getConfAndDelegate = function(name, obj, keys) {
obj.conf = self.configurationFor(name);
self.delegateConf(obj, keys);
return obj;
};

this.getRetrievers = function(retrievers) {
return arethusaUtil.inject({}, retrievers, function(memo, name, conf) {
var Retriever = self.getService(name);
memo[name] = new Retriever(conf);
});
};

// right now very hacky, not sure about the design of the conf file atm
// we therefore just tell the service where the conf for specific things
// is to be found in the JSON tree.
// I guess the key is to abstract the conf file a little more.
this.provideResource = function(name) {
var conf = this.configuration.resources[name];
// we get the resource factory through the injector, and not by regular
// dependency injection, because we always want to return a new instance!
return new Resource(conf);
};
});
10 changes: 7 additions & 3 deletions app/js/arethusa.core/state.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

angular.module('arethusa.core').service('state', function(configurator, navigator, $rootScope) {
var self = this;
var tokenRetrievers;

var conf = configurator.configurationFor('main');
var tokenRetrievers = configurator.getRetrievers(conf.retrievers);
function configure() {
var conf = configurator.configurationFor('main');
tokenRetrievers = configurator.getRetrievers(conf.retrievers);
}

// We hold tokens locally during retrieval phase.
// Once we are done, they will be exposed through
Expand Down Expand Up @@ -260,6 +263,7 @@ angular.module('arethusa.core').service('state', function(configurator, navigato
};

this.init = function() {
this.retrieveTokens();
configure();
self.retrieveTokens();
};
});
11 changes: 7 additions & 4 deletions app/js/arethusa.dep_tree/dep_tree.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@

angular.module('arethusa.depTree').service('depTree', function(state, configurator, $rootScope) {
var self = this;
this.conf = configurator.configurationFor('depTree');
this.template = this.conf.template;
this.main = this.conf.main;

this.diffMode = false;
function configure() {
configurator.getConfAndDelegate('depTree', self);
self.diffMode = false;
}

configure();

this.toggleDiff = function() {
self.diffMode = ! self.diffMode;
Expand Down Expand Up @@ -81,5 +83,6 @@ angular.module('arethusa.depTree').service('depTree', function(state, configurat
};

this.init = function() {
configure();
};
});
13 changes: 7 additions & 6 deletions app/js/arethusa.exercise/fill_in_the_blank.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

angular.module('arethusa.exercise').service('fillInTheBlank', function(configurator, morph, state) {
var self = this;
this.conf = configurator.configurationFor('fillInTheBlank');
this.template = this.conf.template;
this.name = this.conf.name;
this.main = this.conf.main;

this.started = false;
function configure() {
configurator.getConfAndDelegate('fillInTheBlank', self);
self.started = false;
self.answers = {};
}

this.answers = {};
configure();

function createExercise() {
return arethusaUtil.inject({}, state.tokens, function(memo, id, token) {
Expand Down Expand Up @@ -63,6 +63,7 @@ angular.module('arethusa.exercise').service('fillInTheBlank', function(configura
};

this.init = function() {
configure();
delete self.report;
self.exercises = createExercise();
};
Expand Down
18 changes: 12 additions & 6 deletions app/js/arethusa.exercise/instructor.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

angular.module('arethusa.exercise').service('instructor', function(fillInTheBlank, configurator) {
var self = this;
this.conf = configurator.configurationFor('instructor');
this.template = this.conf.template;
this.name = this.conf.name;

this.started = false;
function configure() {
configurator.getConfAndDelegate('instructor', self);
}

configure();

this.start = function() {
self.startedAt = new Date();
fillInTheBlank.started = true;
Expand Down Expand Up @@ -34,11 +36,15 @@ angular.module('arethusa.exercise').service('instructor', function(fillInTheBlan
return aU.formatNumber(minutes, 2) + ':' + aU.formatNumber(seconds, 2);
};

this.init = function() {
self.started = false;
function reset() {
self.done = false;
self.startedAt = false;
self.stoppedAt = false;
self.report = {};
}

this.init = function() {
configure();
reset();
};
});
52 changes: 23 additions & 29 deletions app/js/arethusa.morph/morph.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

angular.module('arethusa.morph').service('morph', function(state, configurator) {
var self = this;
this.conf = configurator.configurationFor('morph');
this.attributes = this.conf.attributes;
this.template = this.conf.template;
this.name = this.conf.name;
this.noView = this.conf.noView;
this.postagSchema = this.conf.postagSchema;
this.styledThrough = this.conf.styledThrough;
var morphRetrievers;

this.contextMenu = this.conf.contextMenu;
this.contextMenuTemplate = this.conf.contextMenuTemplate;
function configure() {
var props = ['postagSchema', 'attributes', 'styledThrough'];
configurator.getConfAndDelegate('morph', self, props);
self.analyses = {};
morphRetrievers = configurator.getRetrievers(self.conf.retrievers);
}

this.analyses = {};

var morphRetrievers = configurator.getRetrievers(this.conf.retrievers);
configure();

this.seedAnalyses = function(tokens) {
return arethusaUtil.inject({}, tokens, function(obj, id, token) {
Expand All @@ -24,11 +20,10 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)
};

this.postagToAttributes = function(form) {
var that = this;
var attrs = {};
angular.forEach(form.postag, function(postagVal, i) {
var postagClass = that.postagSchema[i];
var possibleVals = that.attributeValues(postagClass);
var postagClass = self.postagSchema[i];
var possibleVals = self.attributeValues(postagClass);
var attrObj = arethusaUtil.findObj(possibleVals, function(obj) {
return obj.postag === postagVal;
});
Expand Down Expand Up @@ -56,9 +51,8 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)

this.attributesToPostag = function(attrs) {
var postag = "";
var that = this;
var postagArr = arethusaUtil.map(this.postagSchema, function(el) {
var attrVals = that.attributeValues(el);
var attrVals = self.attributeValues(el);
var val = attrs[el];
var valObj = arethusaUtil.findObj(attrVals, function(e) {
return e.short === val;
Expand Down Expand Up @@ -88,7 +82,7 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)
}
};

var mapAttributes = function(attrs, that) {
var mapAttributes = function(attrs) {
// We could use inject on attrs directly, but this wouldn't give us
// the correct order of properties inside the newly built object.
// Let's iterate over the postag schema for to guarantee it.
Expand All @@ -97,10 +91,10 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)
// This solution comes at a price - if we cannot find a key (not every
// form has a tense attribute for example), we might stuff lots of undefined
// stuff into this object. We pass over this with a conditional.
return arethusaUtil.inject({}, that.postagSchema, function(memo, k) {
return arethusaUtil.inject({}, self.postagSchema, function(memo, k) {
var v = attrs[k];
if (v) {
var values = that.attributeValues(k);
var values = self.attributeValues(k);
var obj = arethusaUtil.findObj(values, function(el) {
return (el.short === v || el.long === v);
});
Expand All @@ -109,25 +103,25 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)
});
};

this.getExternalAnalyses = function(analysisObj, that) {
this.getExternalAnalyses = function(analysisObj) {
angular.forEach(morphRetrievers, function(retriever, name) {
retriever.getData(analysisObj.string, function(res) {
res.forEach(function(el) {
// need to parse the attributes now
el.attributes = mapAttributes(el.attributes, that);
el.attributes = mapAttributes(el.attributes);
// and build a postag
el.postag = that.attributesToPostag(el.attributes);
el.postag = self.attributesToPostag(el.attributes);
});
arethusaUtil.pushAll(analysisObj.forms, res);
});
});
};

this.loadInitalAnalyses = function(that) {
var analyses = that.seedAnalyses(state.tokens);
this.loadInitalAnalyses = function() {
var analyses = self.seedAnalyses(state.tokens);
angular.forEach(analyses, function(val, id) {
that.getExternalAnalyses(val, that);
that.getAnalysisFromState(val, id);
self.getExternalAnalyses(val);
self.getAnalysisFromState(val, id);
val.analyzed = true;
});
return analyses;
Expand Down Expand Up @@ -172,9 +166,8 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)

this.concatenatedAttributes = function(form) {
var res = [];
var that = this;
angular.forEach(form.attributes, function(value, key) {
res.push(that.abbrevAttributeValue(key, value));
res.push(self.abbrevAttributeValue(key, value));
});
return res.join('.');
};
Expand Down Expand Up @@ -204,6 +197,7 @@ angular.module('arethusa.morph').service('morph', function(state, configurator)
};

this.init = function() {
configure();
this.analyses = this.loadInitalAnalyses(this);
};
});
Expand Down
13 changes: 8 additions & 5 deletions app/js/arethusa.relation/relation.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
angular.module('arethusa.relation').service('relation', function(state, configurator) {
var self = this;

this.conf = configurator.configurationFor('relation');
this.template = this.conf.template;
this.name = this.conf.name;
this.relationValues = this.conf.relations;
this.relations = {};
function configure() {
configurator.getConfAndDelegate('relation', self);
self.relationValues = self.conf.relations;
self.relations = {};
}

configure();

// Currently selected labels

Expand Down Expand Up @@ -102,6 +104,7 @@ angular.module('arethusa.relation').service('relation', function(state, configur
};

this.init = function() {
configure();
self.relations = self.createInternalState();
self.resetSearchedLabel();
self.resetMultiChanger();
Expand Down
Loading

0 comments on commit 2cab154

Please sign in to comment.