From aed17a63b91eb512509862939abacf0e1f80f0c9 Mon Sep 17 00:00:00 2001 From: p-a-s-c-a-l Date: Mon, 27 Jan 2020 14:22:54 +0100 Subject: [PATCH 1/3] #19 improve comments and log messages --- app/scripts/connectors/nodeConnector.js | 6 ++++++ app/scripts/services/drupalService.js | 4 ++-- karma.conf.js | 1 - 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/scripts/connectors/nodeConnector.js b/app/scripts/connectors/nodeConnector.js index 89192e2..e39a7a0 100644 --- a/app/scripts/connectors/nodeConnector.js +++ b/app/scripts/connectors/nodeConnector.js @@ -23,6 +23,12 @@ window.Drupal.behaviors.myBehavior = { } else { console.error('no global csisHelpers object found, probably not connected to Drupal!'); } + + // ugly workaround for https://github.com/clarity-h2020/csis-helpers-module/issues/9#issuecomment-539436236 + //var baseTag = document.createElement('base'); + //var prependURL = 'https://csis.myclimateservice.eu/apps/scenario-analysis/app/'; + //baseTag.href = prependURL ; + //$('#scenario-analysis').contents().find('head').append(baseTag); var connectCount = 0; // for some unknown the reason angular directive controler of the embedded child iframe diff --git a/app/scripts/services/drupalService.js b/app/scripts/services/drupalService.js index 4efc602..5b4b1b9 100644 --- a/app/scripts/services/drupalService.js +++ b/app/scripts/services/drupalService.js @@ -153,12 +153,12 @@ angular.module( }); } else { - console.log('error retrieving meta.links.me: null from ' + apiResponse.data.meta.links.me.href); + console.error('error retrieving meta.links.me: null from ' + $this.drupalRestApi.host + '/jsonapi/', apiResponse); $q.reject(apiResponse); } }, function error(apiErrorResponse) { $this.emikatRestApi.emikatCredentials = undefined; - console.log('error retrieving meta.links.me from ' + apiErrorResponse.data.meta.links.me.href + ': ' + apiErrorResponse); + console.error('error retrieving meta.links.me from ' + $this.drupalRestApi.host + '/jsonapi/', apiErrorResponse); $q.reject(apiErrorResponse); }); }; diff --git a/karma.conf.js b/karma.conf.js index dd7142a..289b3d1 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -22,7 +22,6 @@ basePath: '', 'target/dist/bower_components/bootstrap/dist/js/bootstrap.js', 'target/dist/bower_components/angular/angular.js', 'target/dist/bower_components/angular-resource/angular-resource.js', - 'target/dist/bower_components/angular-cookies/angular-cookies.min.js', 'target/dist/bower_components/ng-table/ng-table.js', 'target/dist/bower_components/angular-bootstrap/ui-bootstrap.js', 'target/dist/bower_components/angular-bootstrap/ui-bootstrap-tpls.js', From 8ea01097cd2699208865a8035d7a853a462da393 Mon Sep 17 00:00:00 2001 From: p-a-s-c-a-l Date: Mon, 27 Jan 2020 15:26:40 +0100 Subject: [PATCH 2/3] #19 ignore missing events, improve error handling --- ...rupalContextProviderDirectiveController.js | 9 ++++- .../fileContextProviderDirectiveController.js | 6 ++- app/scripts/controllers/mainController.js | 3 -- app/scripts/services/drupalService.js | 39 ++++++++++++++----- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/app/scripts/controllers/drupalContextProviderDirectiveController.js b/app/scripts/controllers/drupalContextProviderDirectiveController.js index adf39b0..7770be5 100644 --- a/app/scripts/controllers/drupalContextProviderDirectiveController.js +++ b/app/scripts/controllers/drupalContextProviderDirectiveController.js @@ -283,7 +283,11 @@ angular.module( if (!containsIndicator) { msg = 'Could not load indicator file ' + worldstateDummy.name + '. It contains no indicator data for ' + indicator; console.error(msg, worldstateDummy); - showIndicatorFileLoadingError(msg); + /** + * Silently ignore. It's not a bug - it's a feature! ;-) + * See https://github.com/clarity-h2020/scenario-analysis/issues/19 + */ + // showIndicatorFileLoadingError(msg); return; } } @@ -564,7 +568,8 @@ angular.module( loadIndicatorObjects(worldstates); }, function (error) { - console.error(error.message, error); + showIndicatorFileLoadingError(error); + console.error(error); }); }; diff --git a/app/scripts/controllers/fileContextProviderDirectiveController.js b/app/scripts/controllers/fileContextProviderDirectiveController.js index bfe31d1..9eb208d 100644 --- a/app/scripts/controllers/fileContextProviderDirectiveController.js +++ b/app/scripts/controllers/fileContextProviderDirectiveController.js @@ -219,7 +219,11 @@ angular.module( if (!containsIndicator) { msg = 'Could not load indicator file ' + file.name + '. It contains no indicator data for ' + indicator; console.error(msg); - showIndicatorFileLoadingError(msg); + /** + * Silently ignore. It's not a bug - it's a feature! ;-) + * See https://github.com/clarity-h2020/scenario-analysis/issues/19 + */ + // showIndicatorFileLoadingError(msg); return; } } diff --git a/app/scripts/controllers/mainController.js b/app/scripts/controllers/mainController.js index 772182b..81c5909 100644 --- a/app/scripts/controllers/mainController.js +++ b/app/scripts/controllers/mainController.js @@ -24,9 +24,6 @@ angular.module( function ($window, $scope, $resource, $http, $timeout, $q, IcmmPersistanceService, FilesPersistanceService, drupalService, ngDialog) { 'use strict'; - var restApi = drupalService.restApi; - - var createChartModels; // we bind to the container object since the provider directives are nested in angular-bootstrap tabs // tabs create a own scope and thus override every "simple" property. using an container object the binding diff --git a/app/scripts/services/drupalService.js b/app/scripts/services/drupalService.js index 5b4b1b9..38f14c1 100644 --- a/app/scripts/services/drupalService.js +++ b/app/scripts/services/drupalService.js @@ -131,7 +131,10 @@ angular.module( $this.drupalRestApi.initEmikatCredentials = function () { return $http({method: 'GET', url: $this.drupalRestApi.host + '/jsonapi/'}).then(function success(apiResponse) { - if (apiResponse !== null && apiResponse.data !== null && apiResponse.data.meta.links.me !== null && + // FIXME: this will throw an exception, if no user is logged in. -> meta.links is null + // TypeError: Cannot read property 'links' of undefined + if (apiResponse !== null && apiResponse.data !== null && apiResponse.data.meta && apiResponse.data.meta.links && + apiResponse.data.meta.links.me !== null && apiResponse.data.meta.links.me.meta !== null && apiResponse.data.meta.links.me.meta.id !== null) { // FIXME: Ugly workaround for https://github.com/clarity-h2020/docker-drupal/issues/57 apiResponse.data.meta.links.me.href = $this.drupalRestApi.host + '/jsonapi/user/user/' + apiResponse.data.meta.links.me.meta.id; @@ -154,7 +157,7 @@ angular.module( } else { console.error('error retrieving meta.links.me: null from ' + $this.drupalRestApi.host + '/jsonapi/', apiResponse); - $q.reject(apiResponse); + return $q.reject('error retrieving meta.links.me: null from ' + $this.drupalRestApi.host + '/jsonapi/'); } }, function error(apiErrorResponse) { $this.emikatRestApi.emikatCredentials = undefined; @@ -170,6 +173,7 @@ angular.module( if (!$this.drupalRestApi.emikatCredentials || $this.drupalRestApi.emikatCredentials === null || $this.drupalRestApi.emikatCredentials === undefined) { return $this.drupalRestApi.initEmikatCredentials(); } else { + // Calling $q.when takes a promise or any other type, if it is not a promise then it will wrap it in a promise and call resolve. $q.when($this.drupalRestApi.emikatCredentials); } }; @@ -246,7 +250,7 @@ angular.module( }, function credentialsErrorCallback(response) { console.error(response); - return $q.reject(response); + return $q.reject('You are not logged-in in CSIS and EMIKAT, respectively.'); }); } else { console.debug('emikat credentials already avilable, loading Impact Scenario Data for scenario ' + scenarioId + ' and view ' + viewId); @@ -302,7 +306,12 @@ angular.module( * @param {type} icon * @returns {Array} */ - $this.emikatHelper.transformImpactScenario = function (scenarioData, damageClasses, aggregate = false, icon = 'flower_injured_16.png') { + $this.emikatHelper.transformImpactScenario = function (scenarioData, damageClasses, aggregate, icon) { + // Yes, NetBeans will yell at function(aggregate = false, icon = 'flower_injured_16.png') + // See https://stackoverflow.com/questions/45562266/expected-but-found-in-warning-in-netbeans-for-js-function-with-default-param + aggregate = (typeof aggregate !== 'undefined') ? aggregate : false; + icon = (typeof icon !== 'undefined') ? icon : 'flower_injured_16.png'; + damageClasses = null; aggregate = false; @@ -335,9 +344,14 @@ angular.module( TIME_PERIODS[column[criteriaMap['TIME_PERIOD']]] + ')'; + /** + * Don't make functions within a loop. + */ + /* jshint ignore:start */ var worldstate = worldstates.find(function (ws) { return ws.name === scenarioName; }); + /* jshint ignore:end */ if (!worldstate || worldstate === null) { worldstate = {}; @@ -431,8 +445,8 @@ angular.module( * @param {type} imageName * @returns {unresolved} */ - var createReportImageFileResource = function (token, imageName = 'scenario-analysis.png') { - + var createReportImageFileResource = function (token, imageName) { + imageName = (typeof imageName !== 'undefined') ? imageName : 'scenario-analysis.png'; return $resource($this.drupalRestApi.host + '/jsonapi/node/report_image/field_image', { //_format: 'hal_json' @@ -460,11 +474,16 @@ angular.module( * @param {type} foreignObjectRendering * @returns {undefined} */ - $this.screenshotHelper.uploadScreenshot = function (elementId, title = elementId, imageName = elementId + '.png', comment = title, foreignObjectRendering = false) { - + $this.screenshotHelper.uploadScreenshot = function (elementId, title, comment, foreignObjectRendering) { + // ARGH. See https://stackoverflow.com/questions/45562266/expected-but-found-in-warning-in-netbeans-for-js-function-with-default-param + title = (typeof title !== 'undefined') ? title : elementId; + imageName = (typeof imageName !== 'undefined') ? imageName : elementId + '.png'; + comment = (typeof comment !== 'undefined') ? comment : title; + foreignObjectRendering = (typeof foreignObjectRendering !== 'undefined') ? foreignObjectRendering : false; + // STEP #1: // html2canvas screenshot function: take snapshot of HTML element $elementId (e.g.
- $window.html2canvas(document.getElementById(elementId), {logging: true, foreignObjectRendering: foreignObjectRendering}).then(canvas => { + $window.html2canvas(document.getElementById(elementId), {logging: true, foreignObjectRendering: foreignObjectRendering}).then(function(canvas) { canvas.toBlob(function uploadImage(imageBlob) { // function is invoked on button press, so we can safely assume that the token promise was resolved. // TODO: add some error checking before going live @@ -515,7 +534,7 @@ angular.module( }, data: glStepTemplate } - ).then(function successCallback(glStepResponse) { + ).then(function successCallback() { console.log('report image ' + reportImageResponse.data.id + ' successfully assigned to GL Step ' + $this.drupalRestApi.studyInfo.step_uuid); }, function errorCallback(glStepErrorResponse) { console.log('error updating GL Step ' + $this.drupalRestApi.studyInfo.step_uuid + ': ' + glStepErrorResponse); From 24697e0aa0c10a717f09d672e62ecc0cd717f833 Mon Sep 17 00:00:00 2001 From: p-a-s-c-a-l Date: Mon, 27 Jan 2020 16:22:25 +0100 Subject: [PATCH 3/3] #19 don't select more than 5 indicators by default --- .../drupalContextProviderDirectiveController.js | 12 +++++++----- .../fileContextProviderDirectiveController.js | 5 ++++- app/templates/drupalContextProviderTemplate.html | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/scripts/controllers/drupalContextProviderDirectiveController.js b/app/scripts/controllers/drupalContextProviderDirectiveController.js index 7770be5..a69994a 100644 --- a/app/scripts/controllers/drupalContextProviderDirectiveController.js +++ b/app/scripts/controllers/drupalContextProviderDirectiveController.js @@ -215,7 +215,6 @@ angular.module( try { /* - * * accept two differnt kind of files. * 1. A plain icc data object. * In that case we apply a standard name to this object @@ -228,7 +227,7 @@ angular.module( origLoadedIndicators = indicatorObject.iccdata; worldstateDummy.iccdata = { // this is a total mess: serialise the deserialized icc data again - // so that it can be deserilaized by icmm helper library + // so that it can be deserialized by icmm helper library actualaccessinfo: JSON.stringify(worldstateDummy.iccdata) }; } else { @@ -308,8 +307,8 @@ angular.module( } // an excellent example on technical debt and accidental complexity: - // instead of adressing the root cause of the problem, we - // introduce additional inadequateness and ambiguity + // instead of adressing the root cause of the problem, sb + // introduced additional inadequateness and ambiguity Icmm.convertToCorrectIccDataFormat(worldstateDummy); if ($scope.worldstates) { @@ -324,7 +323,10 @@ angular.module( // when indicator objects are added we want them to be selected by default $scope.selectedWorldstates.splice(0, $scope.selectedWorldstates.length); $scope.worldstates.forEach(function (object, index) { - $scope.toggleSelection(index); + // but not more than 5 indicators! + if(index < 5) { + $scope.toggleSelection(index); + } }); //$scope.$apply(); diff --git a/app/scripts/controllers/fileContextProviderDirectiveController.js b/app/scripts/controllers/fileContextProviderDirectiveController.js index 9eb208d..ca8e589 100644 --- a/app/scripts/controllers/fileContextProviderDirectiveController.js +++ b/app/scripts/controllers/fileContextProviderDirectiveController.js @@ -258,7 +258,10 @@ angular.module( // when indicator objects are added we want them to be selected by default $scope.selectedWorldstates.splice(0, $scope.selectedWorldstates.length); $scope.worldstates.forEach(function (object, index) { - $scope.toggleSelection(index); + // but not more than 5 indicators! + if(index < 5) { + $scope.toggleSelection(index); + } }); $scope.$apply(); diff --git a/app/templates/drupalContextProviderTemplate.html b/app/templates/drupalContextProviderTemplate.html index d6435f1..be71a94 100644 --- a/app/templates/drupalContextProviderTemplate.html +++ b/app/templates/drupalContextProviderTemplate.html @@ -16,7 +16,7 @@

ng-click="indicatorFileCollapsed = !indicatorFileCollapsed"> - Indicator files + Indicators