Commit 9287a4ae authored by Mathieu Rodic's avatar Mathieu Rodic

[FEATURE] in `gargantext.angular.js`, datasets are now removable

[OPTI] in `gargantext.angular.js`, only new queries are reloaded from server
[CODE] in `gargantext.angular.js`,  cleaned a lot, grouped variables
parent b09df773
...@@ -267,7 +267,7 @@ gargantext.controller("DatasetController", function($scope, $http) { ...@@ -267,7 +267,7 @@ gargantext.controller("DatasetController", function($scope, $http) {
} }
// event firing to parent(s) // event firing to parent(s)
$scope.$emit('updateDataset', { $scope.$emit('updateDataset', {
datasetId: $scope.$id, datasetIndex: $scope.$index,
url: url, url: url,
filters: filters, filters: filters,
mesured: $scope.mesured mesured: $scope.mesured
...@@ -280,8 +280,6 @@ gargantext.controller("DatasetController", function($scope, $http) { ...@@ -280,8 +280,6 @@ gargantext.controller("DatasetController", function($scope, $http) {
gargantext.controller("GraphController", function($scope, $http, $element) { gargantext.controller("GraphController", function($scope, $http, $element) {
// initialization // initialization
$scope.datasets = [{}]; $scope.datasets = [{}];
$scope.resultsList = [];
$scope.queries = {};
$scope.groupingKey = 'year'; $scope.groupingKey = 'year';
$scope.graph = { $scope.graph = {
data: [], data: [],
...@@ -305,22 +303,22 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -305,22 +303,22 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
// remove a dataset // remove a dataset
$scope.removeDataset = function(datasetIndex) { $scope.removeDataset = function(datasetIndex) {
$scope.datasets.shift(datasetIndex); $scope.datasets.shift(datasetIndex);
$scope.query();
}; };
// show results on the graph // show results on the graph
$scope.showResults = function(keys) { $scope.showResults = function() {
// Format specifications // Format specifications
var xKey = 0;
var yKey = 1;
var grouping = groupings.datetime[$scope.groupingKey]; var grouping = groupings.datetime[$scope.groupingKey];
var convert = function(x) {return new Date(x);}; var convert = function(x) {return new Date(x);};
// Find extrema for X // Find extrema for X
var xMin, xMax; var xMin, xMax;
angular.forEach($scope.resultsList, function(results){ angular.forEach($scope.datasets, function(dataset){
if (results.length == 0) { if (!dataset.results) {
return false; return false;
} }
var xMinTmp = results[0][xKey]; var results = dataset.results;
var xMaxTmp = results[results.length - 1][xKey]; var xMinTmp = results[0][0];
var xMaxTmp = results[results.length - 1][0];
if (xMin === undefined || xMinTmp < xMin) { if (xMin === undefined || xMinTmp < xMin) {
xMin = xMinTmp; xMin = xMinTmp;
} }
...@@ -334,17 +332,18 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -334,17 +332,18 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
xMax = grouping.truncate(xMax); xMax = grouping.truncate(xMax);
for (var x=xMin; x<=xMax; x=grouping.next(x)) { for (var x=xMin; x<=xMax; x=grouping.next(x)) {
var row = []; var row = [];
angular.forEach($scope.resultsList, function(results){ angular.forEach($scope.datasets, function(){
row.push(0); row.push(0);
}); });
dataObject[x] = row; dataObject[x] = row;
} }
// Fill the dataObject with results // Fill the dataObject with results
angular.forEach($scope.resultsList, function(results, resultsIndex){ angular.forEach($scope.datasets, function(dataset, datasetIndex){
var results = dataset.results;
angular.forEach(results, function(result, r){ angular.forEach(results, function(result, r){
var x = grouping.truncate(result[xKey]); var x = grouping.truncate(result[0]);
var y = result[yKey]; var y = result[1];
dataObject[x][resultsIndex] += parseFloat(y); dataObject[x][datasetIndex] += parseFloat(y);
}); });
}); });
// Convert this object back to a sorted array // Convert this object back to a sorted array
...@@ -359,7 +358,7 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -359,7 +358,7 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
} }
// Finally, update the graph // Finally, update the graph
var series = []; var series = [];
for (var i=0, n=$scope.resultsList.length; i<n; i++) { for (var i=0, n=$scope.datasets.length; i<n; i++) {
series.push({ series.push({
y: 'y'+i, y: 'y'+i,
axis: 'y', axis: 'y',
...@@ -371,14 +370,18 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -371,14 +370,18 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
}; };
// perform a query on the server // perform a query on the server
$scope.query = function() { $scope.query = function() {
// reinitialize data // number of requests made to the server
$scope.resultsList = new Array($scope.datasets.length); var requestsCount = 0;
$scope.indexById = {}; // reinitialize graph data
$scope.graph.data = []; $scope.graph.data = [];
// add all the server request to the queue // queue all the server requests
var index = 0; angular.forEach($scope.datasets, function(dataset, datasetIndex) {
angular.forEach($scope.queries, function(query, datasetId) { // if the results are already present, don't send a query
$scope.indexById[datasetId] = index++; if (dataset.results !== undefined) {
return;
}
// format data to be sent as a query
var query = dataset.query;
var data = { var data = {
filters: query.filters, filters: query.filters,
sort: ['metadata.publication_date.day'], sort: ['metadata.publication_date.day'],
...@@ -387,57 +390,63 @@ gargantext.controller("GraphController", function($scope, $http, $element) { ...@@ -387,57 +390,63 @@ gargantext.controller("GraphController", function($scope, $http, $element) {
list: ['metadata.publication_date.day', query.mesured] list: ['metadata.publication_date.day', query.mesured]
} }
}; };
// request to the server
$http.post(query.url, data, {cache: true}).success(function(response) { $http.post(query.url, data, {cache: true}).success(function(response) {
var index = $scope.indexById[datasetId]; dataset.results = response.results;
$scope.resultsList[index] = response.results; for (var i=0, n=$scope.datasets.length; i<n; i++) {
for (var i=0, n=$scope.resultsList.length; i<n; i++) { if ($scope.datasets[i].results == undefined) {
if ($scope.resultsList[i] == undefined) {
return; return;
} }
} }
$scope.showResults(response.retrieve); $scope.showResults();
}).error(function(response) { }).error(function(response) {
console.error('An error occurred while retrieving the query response'); console.error('An error occurred while retrieving the query response');
}); });
requestsCount++;
}); });
// if no request have been made at all, refresh the chart
if (requestsCount == 0) {
$scope.showResults();
}
}; };
// update the queries (catches the vent thrown by children dataset controllers) // update the datasets (catches the vent thrown by children dataset controllers)
$scope.$on('updateDataset', function(e, data) { $scope.$on('updateDataset', function(e, data) {
$scope.queries[data.datasetId] = { var dataset = $scope.datasets[data.datasetIndex]
dataset.query = {
url: data.url, url: data.url,
filters: data.filters, filters: data.filters,
mesured: data.mesured mesured: data.mesured
}; };
dataset.results = undefined;
$scope.query(); $scope.query();
}); });
}); });
// // For debugging only! // For debugging only!
// setTimeout(function(){ setTimeout(function(){
// var corpusId = $('div.corpus select option').last().val(); // first dataset
// // first dataset $('div.corpus select').change();
// $('div.corpus select').val(corpusId).change(); // setTimeout(function(){
// setTimeout(function(){ // $('div.filters button').last().click();
// $('div.filters button').last().click(); // var d = $('li.dataset').last();
// var d = $('li.dataset').last(); // d.find('select').last().val('metadata').change();
// d.find('select').last().val('metadata').change(); // d.find('select').last().val('publication_date').change();
// d.find('select').last().val('publication_date').change(); // d.find('select').last().val('>').change();
// d.find('select').last().val('>').change(); // d.find('input').last().val('2010').change();
// d.find('input').last().val('2010').change();
// // second dataset // // second dataset
// // $('button.add').first().click(); // // $('button.add').first().click();
// // var d = $('li.dataset').last(); // // var d = $('li.dataset').last();
// // d.find('select').change(); // // d.find('select').change();
// // // second dataset's filter // // // second dataset's filter
// // d.find('div.filters button').last().click(); // // d.find('div.filters button').last().click();
// // d.find('select').last().val('metadata').change(); // // d.find('select').last().val('metadata').change();
// // d.find('select').last().val('abstract').change(); // // d.find('select').last().val('abstract').change();
// // d.find('select').last().val('contains').change(); // // d.find('select').last().val('contains').change();
// // d.find('input').last().val('dea').change(); // // d.find('input').last().val('dea').change();
// // refresh // // refresh
// $('button.refresh').first().click(); // // $('button.refresh').first().click();
// }, 500); // }, 500);
// }, 500); }, 250);
\ No newline at end of file \ No newline at end of file
...@@ -280,7 +280,7 @@ ...@@ -280,7 +280,7 @@
<option ng-repeat="mode in ['bundle', 'linear']" value="{{ mode }}">{{ mode }}</option> <option ng-repeat="mode in ['bundle', 'linear']" value="{{ mode }}">{{ mode }}</option>
</select> </select>
<span ng-if="graph.options.lineMode != 'linear'"> <span ng-if="graph.options.lineMode != 'linear'">
with a smoothing of smoothing: with a tension of
<input type="text" disabled="disabled" ng-model="graph.options.tension" /> <input type="text" disabled="disabled" ng-model="graph.options.tension" />
<input type="range" min="0" max="2" step=".1" ng-model="graph.options.tension" /> <input type="range" min="0" max="2" step=".1" ng-model="graph.options.tension" />
</span> </span>
......
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