Commit 705cc861 authored by Romain Loth's avatar Romain Loth

annotations: generalize/rationalize use of the new common function 'makeChainedCalls'

parent bad5e8c5
......@@ -22,7 +22,7 @@
* Main function
* GET the document node and all its ngrams
*/
window.annotationsApp.run(function ($rootScope) {
window.annotationsApp.run(function ($rootScope, NgramListHttpService) {
// ex: projects/1/corpora/2/documents/9/
// ex: projects/1/corpora/2/documents/9/focus=2677 (to highlight ngram 2677 more)
var path = window.location.pathname.match(/\/projects\/(.*)\/corpora\/(.*)\/documents\/(.*)\/(?:focus=([0-9,]+))?/);
......@@ -40,8 +40,22 @@
$rootScope.focusNgrams = []
// -------------------------------
// shared toolbox (functions useful for several modules) -------------------
$rootScope.mafonction = function (bidule) {console.warn(bidule)}
$rootScope.refresh = function() {
// Refresh the annotations
NgramListHttpService.get(
{'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId},
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
$rootScope.refreshDisplay();
},
function(data) {
console.error("unable to refresh the list of ngrams");
}
);
}
// chained recursion to do several AJAX actions and then a callback (eg refresh)
$rootScope.makeChainedCalls =
......
......@@ -88,6 +88,8 @@
function toggleMenu(context, annotation) {
$timeout(function() {
$scope.$apply(function() {
$scope.menuItems = []; // init empty menu
var mainlist_id = $rootScope.listIds.MAINLIST;
var stoplist_id = $rootScope.listIds.STOPLIST;
var maplist_id = $rootScope.listIds.MAPLIST;
......@@ -128,9 +130,11 @@
// NB: remember that shown mainlist items are actually main 'without map'
// otherwise the menu for mainlist items can hide the menu for map items
if ($rootScope.lists[annotation.list_id] == "MAPLIST") {
var sourceList = $rootScope.lists[annotation.list_id]
$scope.menuItems.push({
if ( sourceList == "MAPLIST") {
$scope.menuItems = [
{
// "tgtListName" is just used to render the GUI explanation
'tgtListName': 'STOPLIST',
// crudCalls is an array of rest/DB actions
......@@ -143,18 +147,20 @@
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':stoplist_id, 'ngramIdList': targetId} }
]
});
$scope.menuItems.push({
},
{
'tgtListName': 'MAINLIST',
'crudCalls':[
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
'params' : {'listId':maplist_id, 'ngramIdList': targetId} }
]
});
}
];
}
else if ($rootScope.lists[annotation.list_id] == "MAINLIST") {
$scope.menuItems.push({
else if (sourceList == "MAINLIST") {
$scope.menuItems = [
{
'tgtListName': "STOPLIST",
'crudCalls':[
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
......@@ -162,18 +168,20 @@
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':stoplist_id, 'ngramIdList': targetId} }
]
});
$scope.menuItems.push({
},
{
'tgtListName': "MAPLIST",
'crudCalls':[
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':maplist_id, 'ngramIdList': targetId} }
]
});
}
];
}
else if ($rootScope.lists[annotation.list_id] == "STOPLIST") {
$scope.menuItems.push({
else if (sourceList == "STOPLIST") {
$scope.menuItems = [
{
'tgtListName': "MAINLIST",
'crudCalls':[
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
......@@ -181,8 +189,8 @@
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':mainlist_id, 'ngramIdList': targetId} }
]
});
$scope.menuItems.push({
},
{
'tgtListName': "MAPLIST",
'crudCalls':[
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
......@@ -192,7 +200,8 @@
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':maplist_id, 'ngramIdList': targetId} }
]
});
}
];
}
// show the menu
......@@ -305,28 +314,13 @@
* (1 intention => list of actions => MainApiChangeNgramHttpService CRUDs)
* post/delete
*/
$scope.onMenuClick = function($event, crudCalls) {
$scope.onMenuClick = function($event, todoCrudCalls) {
// console.warn('in onMenuClick')
// console.warn('item.crudCalls', crudCalls)
var lastCallback = function() {
// Refresh the annotationss
NgramListHttpService.get(
{'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId},
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
$rootScope.refreshDisplay();
},
function(data) {
console.error("unable to refresh the list of ngrams");
}
);
}
// run the loop by calling the initial recursion step
$rootScope.makeChainedCalls(0, crudCalls, lastCallback)
// syntax: (step_to_run, list_of_steps, lastCallback)
$rootScope.makeChainedCalls(0, todoCrudCalls, $rootScope.refresh)
// syntax: (step_to_run_first, list_of_steps, lastCallback)
// hide the highlighted text and the menu element
$(".text-panel").removeClass("selection");
......
......@@ -21,89 +21,37 @@
if (listName == 'MAPLIST') {
crudActions = [
["delete", thisListId]
// only 'remove' is needed here
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
'params' : {'listId':thisListId, 'ngramIdList': thisNgramId} }
]
}
else if (listName == 'MAINLIST') {
crudActions = [
["delete", thisListId],
["put", $rootScope.listIds.STOPLIST],
// remove
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
'params' : {'listId':thisListId, 'ngramIdList': thisNgramId} },
// consequence: add to opposite list
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':$rootScope.listIds.STOPLIST, 'ngramIdList': thisNgramId} }
]
}
else if (listName == 'STOPLIST') {
crudActions = [
["delete", thisListId],
["put", $rootScope.listIds.MAINLIST],
]
// remove
{'service': MainApiChangeNgramHttpService, 'action': 'delete',
'params' : {'listId':thisListId, 'ngramIdList': thisNgramId} },
// consequence: add to opposite list
{'service': MainApiChangeNgramHttpService, 'action': 'put',
'params' : {'listId':$rootScope.listIds.MAINLIST, 'ngramIdList': thisNgramId} }
]
}
// console.log(crudActions)
// using recursion to make chained calls,
// todo factorize with highlight.js
var lastCallback = function() {
// Refresh the annotationss
NgramListHttpService.get(
{'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId},
function(data) {
// $rootScope.annotations
// ----------------------
// is the union of all lists, one being later "active"
// (then used for left-side flatlist AND inline annots)
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
// TODO £NEW : lookup obj[list_id][term_text] = {terminfo}
// $rootScope.lookup =
$rootScope.refreshDisplay();
},
function(data) {
console.error("unable to refresh the list of ngrams");
}
);
}
// chained recursion to do several actions then callback (eg refresh)
function makeChainedCalls (i, listOfActions, finalCallback) {
// each action couple has 2 elts
var action = listOfActions[i][0]
var listId = listOfActions[i][1]
MainApiChangeNgramHttpService[action](
{'listId': listId,
'ngramIdList': thisNgramId},
// on success
function(data) {
// case NEXT
// ----
// when chained actions
if (listOfActions.length > i+1) {
// console.log("calling next action ("+(i+1)+")")
// ==============================================
makeChainedCalls(i+1, listOfActions, finalCallback)
// ==============================================
}
// case LAST
// ------
// when last action
else {
finalCallback()
}
},
// on error
function(data) {
console.error("unable to edit the Ngram \""+ngramText+"\""
+"(ngramId "+ngramId+")"+"at crud no "+i
+" ("+action+" on list "+listId+")");
}
);
}
// run the loop by calling the initial recursion step
makeChainedCalls(0, crudActions, lastCallback)
$rootScope.makeChainedCalls(0, crudActions, $rootScope.refresh)
};
}]);
......@@ -184,64 +132,74 @@
if (already_in_list) { return ; }
// ---------------------------------------------------------------
var listName = $rootScope.lists[listId]
alert("listId: " + listId +"\n listName: "+ listName)
console.log("dir $rootScope :")
console.dir($rootScope)
// run the loop by calling the initial recursion step
// $rootScope.makeChainedCalls(0, crudCalls, lastCallback)
// AddNgram
// --------
// creation will return an ngramId
// (checks if there's a preexisting ngramId for this value
// otherwise creates a new one and indexes the ngram in corpus)
MainApiAddNgramHttpService.put(
{
// text <=> str to create the new ngram
'text': value,
'corpusId': $rootScope.corpusId
},
// on AddNgram success
function(data) {
var newNgramId = data.id
console.log("OK created new ngram for '"+value+"' with id: "+newNgramId)
// ChangeNgram
// -----------
// add to listId after creation
// TODO: if maplist => also add to miam
MainApiChangeNgramHttpService["put"](
{
'listId': listId,
'ngramIdList': newNgramId
},
// on ChangeNgram success
function(data) {
// Refresh the annotations (was broken: TODO FIX)
console.warn("refresh attempt");
angular.element(inputEltId).val(""); // what for ???
NgramListHttpService.get(
{
'corpusId': $rootScope.corpusId,
'docId': $rootScope.docId
},
// on refresh success
function(data) {
$rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
$rootScope.refreshDisplay();
},
// on refresh error
function(data) {
console.error("unable to get the list of ngrams");
}
);
},
// on ChangeNgram error
function(data) {
console.error("unable to edit the Ngram"+ngramId+") on list "+listId+")");
}
);
},
// on AddNgram error
function(data) {
angular.element(inputEltId).parent().addClass("has-error");
console.error("error adding Ngram "+ value);
}
);
// MainApiAddNgramHttpService.put(
// {
// // text <=> str to create the new ngram
// 'text': value,
// 'corpusId': $rootScope.corpusId
// },
// // on AddNgram success
// function(data) {
// var newNgramId = data.id
// console.log("OK created new ngram for '"+value+"' with id: "+newNgramId)
//
// // ChangeNgram
// // -----------
// // add to listId after creation
// // TODO: if maplist => also add to miam
// MainApiChangeNgramHttpService["put"](
// {
// 'listId': listId,
// 'ngramIdList': newNgramId
// },
// // on ChangeNgram success
// function(data) {
// // Refresh the annotations (was broken: TODO FIX)
// console.warn("refresh attempt");
// angular.element(inputEltId).val(""); // what for ???
// NgramListHttpService.get(
// {
// 'corpusId': $rootScope.corpusId,
// 'docId': $rootScope.docId
// },
// // on refresh success
// function(data) {
// $rootScope.annotations = data[$rootScope.corpusId.toString()][$rootScope.docId.toString()];
// $rootScope.refreshDisplay();
// },
// // on refresh error
// function(data) {
// console.error("unable to get the list of ngrams");
// }
// );
// },
// // on ChangeNgram error
// function(data) {
// console.error("unable to edit the Ngram"+ngramId+") on list "+listId+")");
// }
// );
// },
// // on AddNgram error
// function(data) {
// angular.element(inputEltId).parent().addClass("has-error");
// console.error("error adding Ngram "+ value);
// }
// );
}; // onListSubmit
}]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment