From 7dec57b38c6b9a816661a9e6435675fdbf730530 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Wed, 23 Nov 2016 17:37:12 +0100
Subject: [PATCH] selecting layout from url get param

---
 frontend-js/src/main/js/map/CustomMap.js      | 18 ++++++
 .../src/main/js/map/data/LayoutData.js        | 16 +++--
 frontend-js/src/main/js/minerva.js            | 33 ++++++----
 frontend-js/src/test/js/GuiConnector-mock.js  | 18 ++----
 frontend-js/src/test/js/google-map-mock.js    |  1 +
 frontend-js/src/test/js/helper.js             | 22 +++++--
 frontend-js/src/test/js/minerva-test.js       | 63 +++++++++++++++----
 .../WEB-INF/components/map/layoutPanel.xhtml  |  4 +-
 8 files changed, 129 insertions(+), 46 deletions(-)

diff --git a/frontend-js/src/main/js/map/CustomMap.js b/frontend-js/src/main/js/map/CustomMap.js
index 0d5246f100..e01b20caf7 100644
--- a/frontend-js/src/main/js/map/CustomMap.js
+++ b/frontend-js/src/main/js/map/CustomMap.js
@@ -302,6 +302,24 @@ CustomMap.prototype.openLayoutById = function(identifier) {
   }
 };
 
+CustomMap.prototype.openLayoutByName = function(name) {
+  logger.debug("Opening layout: " + name);
+  var index = null;
+  var layouts = this.getLayouts();
+  for (var i = 0; i < layouts.length; i++) {
+    var layout = layouts [i];
+    if (layout.getName() === name) {
+      index = 'cv' + layout.getId();
+    }
+  }
+  // if layout doesn't exist print error
+  if (index == null) {
+    alert("You have no privileges for selected layout");
+  } else {
+    this.openLayout(index);
+  }
+};
+
 CustomMap.prototype.createMapMenu = function() {
   var selfMap = this;
 
diff --git a/frontend-js/src/main/js/map/data/LayoutData.js b/frontend-js/src/main/js/map/data/LayoutData.js
index 8211340a08..29e501da20 100644
--- a/frontend-js/src/main/js/map/data/LayoutData.js
+++ b/frontend-js/src/main/js/map/data/LayoutData.js
@@ -38,14 +38,22 @@ LayoutData.prototype.getId = function() {
   return this.id;
 };
 
-LayoutData.prototype.getName = function() {
-  return this.name;
-};
-
 LayoutData.prototype.setId = function(id) {
   this.id = parseInt(id);
 };
 
+LayoutData.prototype.getInputDataAvailable = function() {
+  return this._inputDataAvailable;
+};
+
+LayoutData.prototype.setInputDataAvailable = function(inputDataAvailable) {
+  this._inputDataAvailable = inputDataAvailable;
+};
+
+LayoutData.prototype.getName = function() {
+  return this.name;
+};
+
 LayoutData.prototype.setName = function(name) {
   this.name = name;
 };
diff --git a/frontend-js/src/main/js/minerva.js b/frontend-js/src/main/js/minerva.js
index 24085c6b28..d78570bafb 100644
--- a/frontend-js/src/main/js/minerva.js
+++ b/frontend-js/src/main/js/minerva.js
@@ -13,8 +13,7 @@ var logger = require('./logger');
 function processUrlGetParams(configuration) {
   if (GuiConnector.getParams["submap"] === undefined
       || configuration.getId() === parseInt(GuiConnector.getParams["submap"])) {
-    if (GuiConnector.getParams["x"] !== undefined
-        && GuiConnector.getParams["y"] !== undefined) {
+    if (GuiConnector.getParams["x"] !== undefined && GuiConnector.getParams["y"] !== undefined) {
       ServerConnector.setCenterCoordinateX(GuiConnector.getParams["x"]);
       ServerConnector.setCenterCoordinateY(GuiConnector.getParams["y"]);
     }
@@ -47,8 +46,7 @@ function create(params) {
           + "Select 'Tools' menu item. \n"
           + "Select the 'Compatibility View' settings option. \n"
           + "Make sure the 'Display all websites in Compatibility View' check box is unchecked and that the 'Compatibility View; list of websites is cleared.\n"
-          + "\n"
-          + "Alternatively, please, use other browsers: Chrome, Firefox or Safari.";
+          + "\n" + "Alternatively, please, use other browsers: Chrome, Firefox or Safari.";
       alert(message);
       logger.warn(message);
     }
@@ -66,8 +64,7 @@ function create(params) {
   if (params.dataCollections !== undefined) {
     for (var i = 0; i < params.dataCollections.length; i++) {
       var collection = params.dataCollections[i];
-      ServerConnector.addOverlayCollection(new OverlayCollection(result,
-          collection.name, collection.allowSearchById,
+      ServerConnector.addOverlayCollection(new OverlayCollection(result, collection.name, collection.allowSearchById,
           collection.allowGeneralSearch));
     }
     result.refreshOverlays();
@@ -83,8 +80,22 @@ function create(params) {
     document.getElementById('comment_checkbox').click();
   }
   if (GuiConnector.getParams["layout"] !== undefined) {
-    document.getElementById('tabView:layoutForm3:hidden2').value = GuiConnector.getParams["layout"];
-    getVisualizedLayoutDetails();
+    var layouts = params.project.getModel().getLayouts();
+    var found = false;
+    logger.debug(GuiConnector.getParams["layout"]);
+    for (var j = 0; j < layouts.length; j++) {
+      var layout = layouts[j];
+      logger.debug(layout);
+      if (layout.getName() === GuiConnector.getParams["layout"]) {
+        found = true;
+        if (layout.getInputDataAvailable()) {
+          result.addSelectedLayout(layout.getId(), layout.getName());
+        } else {
+          result.openLayoutByName(layout.getName());
+        }
+      }
+    }
+    logger.debug("Layout found: " + found);
   }
 
   // when I try to hide legend from the beginning or in the same thread it's
@@ -100,11 +111,9 @@ function create(params) {
           GuiConnector.openDialog(submodelId);
           clearInterval(waitingForResponse);
           if (GuiConnector.getParams["zoom"] !== undefined) {
-            result
-                .setZoom(submodelId, parseInt(GuiConnector.getParams["zoom"]));
+            result.setZoom(submodelId, parseInt(GuiConnector.getParams["zoom"]));
           }
-          if (GuiConnector.getParams["x"] !== undefined
-              && GuiConnector.getParams["y"] !== undefined) {
+          if (GuiConnector.getParams["x"] !== undefined && GuiConnector.getParams["y"] !== undefined) {
             var x = GuiConnector.getParams["x"];
             var y = GuiConnector.getParams["y"];
             var point = new google.maps.Point(x, y);
diff --git a/frontend-js/src/test/js/GuiConnector-mock.js b/frontend-js/src/test/js/GuiConnector-mock.js
index c409ffd3c5..bfd775c81c 100644
--- a/frontend-js/src/test/js/GuiConnector-mock.js
+++ b/frontend-js/src/test/js/GuiConnector-mock.js
@@ -24,8 +24,7 @@ GuiConnectorMock.addListener = function(type, fun) {
   }
 
   if (typeof fun !== "function") {
-    throw "Second parameter must be a function but \"" + typeof (fun)
-        + "\" found.";
+    throw "Second parameter must be a function but \"" + typeof (fun) + "\" found.";
   }
 
   var listenerList = this.listeners[type];
@@ -42,8 +41,7 @@ GuiConnectorMock.removeListener = function(type, fun) {
   }
 
   if (typeof fun !== "function") {
-    throw "Second parameter must be a function but \"" + typeof (fun)
-        + "\" found.";
+    throw "Second parameter must be a function but \"" + typeof (fun) + "\" found.";
   }
 
   var listenerList = this.listeners[type];
@@ -61,8 +59,7 @@ GuiConnectorMock.callListeners = function(type) {
   }
   var listenerList = this.listeners[type];
   if (listenerList !== undefined) {
-    logger.debug("GuiConnector is calling: " + type + ". "
-        + listenerList.length + " listeners found.");
+    logger.debug("GuiConnector is calling: " + type + ". " + listenerList.length + " listeners found.");
     for ( var i in listenerList) {
       var e = {
         type : type,
@@ -100,17 +97,14 @@ GuiConnectorMock.getLoadingImg = function() {
 
 GuiConnectorMock.referenceToHtml = function(reference) {
   var result = "";
-  if (reference.summary !== null && reference.summary !== undefined
-      && reference.summary !== "") {
+  if (reference.summary !== null && reference.summary !== undefined && reference.summary !== "") {
     result = '<div title="' + reference.summary + '">';
-    result += '<a href="' + reference.link + '" target="_blank">'
-        + reference.name + "</a>";
+    result += '<a href="' + reference.link + '" target="_blank">' + reference.name + "</a>";
     // + reference.name + "(" + reference.type + ")</a>";
     result += "</div>";
     return result;
   } else {
-    result = '<div><a href="' + reference.link + '" target="_blank">'
-        + reference.name + "</a></div>";
+    result = '<div><a href="' + reference.link + '" target="_blank">' + reference.name + "</a></div>";
     // + reference.name + "(" + reference.type + ")</a></div>";
     return result;
   }
diff --git a/frontend-js/src/test/js/google-map-mock.js b/frontend-js/src/test/js/google-map-mock.js
index 8c06db3b2a..45549ab162 100644
--- a/frontend-js/src/test/js/google-map-mock.js
+++ b/frontend-js/src/test/js/google-map-mock.js
@@ -135,6 +135,7 @@ var google = {
         },
         setMapTypeId : function(id) {
           data.mapTypeId = id;
+          google.maps.event.trigger(this, "maptypeid_changed", id);
         },
         setOptions : function() {
         },
diff --git a/frontend-js/src/test/js/helper.js b/frontend-js/src/test/js/helper.js
index 2ebb3e06a1..a0cae9bfdb 100644
--- a/frontend-js/src/test/js/helper.js
+++ b/frontend-js/src/test/js/helper.js
@@ -20,6 +20,21 @@ Helper.prototype.createProject = function() {
   return result;
 };
 
+Helper.prototype.createOptions = function() {
+  var map = this.createGoogleMap();
+
+  var project = this.createProject();
+
+  var options = {
+    map : map,
+    project : project,
+    dataCollections : [ {
+      name : "testCollection" + this.idCounter++,
+    } ],
+  };
+  return options;
+};
+
 Helper.prototype.createModel = function() {
   var result = new Model();
   result.setId(this.idCounter++);
@@ -28,17 +43,16 @@ Helper.prototype.createModel = function() {
   result.setHeight(800);
   result.setMaxZoom(8);
   result.setMinZoom(2);
-  
+
   var layout = this.createLayout();
   result.addLayout(layout);
   return result;
 };
 
-
-Helper.prototype.createLayout= function() {
+Helper.prototype.createLayout = function() {
   var layout = new LayoutData();
   layout.setId(this.idCounter++);
-  layout.setName("testLayout"+layout.getId());
+  layout.setName("testLayout" + layout.getId());
   return layout;
 };
 
diff --git a/frontend-js/src/test/js/minerva-test.js b/frontend-js/src/test/js/minerva-test.js
index 8efb9fd849..7819c6344a 100644
--- a/frontend-js/src/test/js/minerva-test.js
+++ b/frontend-js/src/test/js/minerva-test.js
@@ -4,7 +4,6 @@ var Helper = require('./helper');
 
 require("./mocha-config.js");
 
-var Project = require('../../main/js/map/data/Project');
 var minerva = require('../../main/js/minerva');
 
 var chai = require('chai');
@@ -13,23 +12,63 @@ var logger = require('./logger');
 
 describe('minerva global', function() {
 
-
   var helper;
   before(function() {
     helper = new Helper();
   });
 
   it('create', function() {
-    var map = new google.maps.Map(testDiv, {
-      center : {
-        lat : -34.397,
-        lng : 150.644
-      },
-      scrollwheel : false,
-      zoom : 8
-    });
+    var options = helper.createOptions();
+
+    var result = minerva.create(options);
+    assert.ok(result);
+    assert.equal(logger.getWarnings().length, 1);
+  });
+
+  it('create with layout', function() {
+
     var project = helper.createProject();
-    
+    var map = helper.createGoogleMap();
+
+    var layout = helper.createLayout();
+    layout.setInputDataAvailable(false);
+    var layoutName = layout.getName();
+    var layoutId = layout.getId();
+
+    project.getModel().addLayout(layout);
+
+    GuiConnector.getParams["layout"] = layoutName;
+
+    var options = {
+      map : map,
+      project : project,
+      dataCollections : [ {
+        name : "testCollection"
+      } ],
+    };
+
+    var result = minerva.create(options);
+    assert.ok(result);
+    // input file is not available so it's the background
+    assert.equal(result.getSelectedLayouts().length, 0);
+    assert.equal(ServerConnector.getSelectedLayout(), "cv" + layoutId);
+    assert.equal(logger.getWarnings().length, 1);
+  });
+
+  it('create with layout 2', function() {
+
+    var project = helper.createProject();
+    var map = helper.createGoogleMap();
+
+    var layout = helper.createLayout();
+    layout.setInputDataAvailable(true);
+    var layoutName = layout.getName();
+    var layoutId = layout.getId();
+
+    project.getModel().addLayout(layout);
+
+    GuiConnector.getParams["layout"] = layoutName;
+
     var options = {
       map : map,
       project : project,
@@ -40,6 +79,8 @@ describe('minerva global', function() {
 
     var result = minerva.create(options);
     assert.ok(result);
+    // input file is available so it's not the background file but overlay
+    assert.equal(result.getSelectedLayouts().length, 1);
     assert.equal(logger.getWarnings().length, 1);
   });
 });
diff --git a/web/src/main/webapp/WEB-INF/components/map/layoutPanel.xhtml b/web/src/main/webapp/WEB-INF/components/map/layoutPanel.xhtml
index eaea8e5138..a405786425 100644
--- a/web/src/main/webapp/WEB-INF/components/map/layoutPanel.xhtml
+++ b/web/src/main/webapp/WEB-INF/components/map/layoutPanel.xhtml
@@ -69,7 +69,7 @@
 			</p:dataTable>	
 
 			<center>
-				<p:commandButton id="refreshLayoutsButton" actionListener="#{layoutMB.refreshCustomLayouts}" icon="ui-icon-refresh" oncomplete="updateConfig();" update="layoutDataTable">
+				<p:commandButton id="refreshLayoutsButton" actionListener="#{layoutMB.refreshCustomLayouts}" icon="ui-icon-refresh" update="layoutDataTable">
 				</p:commandButton>	
 			</center>
 
@@ -143,8 +143,6 @@
 	<h:inputHidden id="hidden2" value="#{layoutMB.visualizedLayout.name}" />
 	<h:inputHidden id="hidden_directory" value="#{layoutMB.visualizedLayout.directory}" />
 	<h:inputHidden id="hidden_layout_id" value="#{layoutMB.visualizedLayout.idObject}" />
-	<p:remoteCommand name="getVisualizedLayoutDetails" actionListener="#{layoutMB.getVisualizedLayoutDetails}" 
-	oncomplete="updateConfig(); customMap.openLayoutById(document.getElementById('tabView:layoutForm3:hidden_layout_id').value);" update="hidden_layout_id"/>
 </h:form>
 
 <h:form id="layoutAccessAliasForm">
-- 
GitLab