Add dynamic loading of javascript configurations (closes #173)

Change-Id: Icfb6082aee88930eca2361196b8d91ff5a796172
diff --git a/Gruntfile.js b/Gruntfile.js
index 106e241..e3601bd 100644
--- a/Gruntfile.js
+++ b/Gruntfile.js
@@ -18,17 +18,13 @@
 
 module.exports = function(grunt) {
   var config;
-  var includeFile = 'default';
-  if (grunt.file.exists('kalamar.conf.js')) {
-    includeFile = 'root/kalamar.conf';
-  };
 
   // Generate requireJS files for l10n
   var reqTasks = [];
   var uglyFiles = {
     'public/js/korap-plugin-<%= pkg.pluginVersion %>.js': ['dev/js/src/plugin/client.js']
   };
-  
+
   for (var i in {'en' : 0, 'de' : 1}) {
 
     reqTasks.push({
@@ -43,7 +39,7 @@
         wrap: true,
         // dir : 'public/js',
         name: 'lib/almond',
-        include : [includeFile, "app/" + i],
+        include : ["app/" + i],
         out: 'public/js/kalamar-<%= pkg.version %>-' + i + '.js'
       }
     });
@@ -115,6 +111,44 @@
       },
       main: {
 	      files:[
+            {
+            expand: true,
+            src: 'kalamar.conf.js',
+            dest:'public/js/hintc/',
+            filter: 'isFile',
+            timestamp: true
+          },
+          {
+            expand: true,
+            cwd: 'dev/js/src/',
+            src: ['default.js','util.js'],
+            dest:'public/js/hintc/',
+            filter: 'isFile',
+            timestamp: true
+          },
+          {
+            expand: true,
+            cwd: 'dev/js/lib/',
+            src: 'require.js',
+            dest:'public/js/hintc/',
+            filter: 'isFile',
+            timestamp: true
+          },
+          {
+            expand: true,
+            cwd: 'dev/js/src/hint/foundries',
+            src: '**',
+            dest:'public/js/hintc/hint/foundries',
+            timestamp: true
+          },
+          {
+            expand: true,
+            cwd: 'dev/js/src/hint/',
+            src: 'foundries.js',
+            dest:'public/js/hintc/hint',
+            filter: 'isFile',
+            timestamp: true
+          },
 	        {
 	          expand: true,
 	          cwd: 'dev/font/',
diff --git a/dev/js/src/hint.js b/dev/js/src/hint.js
index e700b94..ee5e344 100644
--- a/dev/js/src/hint.js
+++ b/dev/js/src/hint.js
@@ -25,11 +25,15 @@
   'hint/contextanalyzer',
   'hint/alert',
   'util'
-], function (inputClass, 
+], function (inputClass,
              menuClass,
              analyzerClass,
              alertClass) {
 
+  //needed for localization
+  const loc   = KorAP.Locale;
+  loc.HINT_noAnnot =  loc.HINT_noAnnot || 'The assistant can not be displayed.';
+
   // Initialize hint array
 
   /**
@@ -266,10 +270,13 @@
      * @param {boolean} Boolean value to indicate if context
      *        is necessary (true) or if the main context should
      *        be shown if context fails.
-     *        
+     *
      */
     show : function (ifContext) {
-
+      if(KorAP.annotationHelper["-"].length == 0){
+        this.alert(0,loc.HINT_noAnnot);
+        return;
+      }
       // Remove the active object
       this._unshow();
       
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 1e9a805..24c2348 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -15,6 +15,7 @@
  * - obj.alignment() // toggle
  */
 
+
 "use strict";
 define([
   'match',
@@ -95,21 +96,46 @@
     gt.addEventListener('click', function(){
       tourClass.gTstartSearch().start();
     });
-    
+
     KorAP.tourshowR = function(){
       tourClass.gTshowResults().start();
     };
   };
 
+
+  addHintM = function(scripte, file){
+    KorAP.annotationHelper = KorAP.annotationHelper || { '-' : [] };
+    let scriptEl = scripte;
+    scriptEl.setAttribute("type", "text/javascript");
+    scriptEl.setAttribute("async", "async");
+    scriptEl.setAttribute("defer", "defer");
+    scriptEl.setAttribute("src", "require.js");
+    scriptEl.setAttribute("data-main", file);
+    document.getElementsByTagName("head")[0].appendChild(scriptEl);
+  };
+
   domReady(function (event) {
-      
+
     var obj = {};
 
     // What should be visible in the beginning?
     var show = KorAP.session.get('show') || {};
-    
+
     KorAP.Panel = KorAP.Panel || {}
 
+    var scriptElement =document.createElement("script")
+    fetch("kalamar.conf.js", { method: "HEAD" })
+      .then(response => {
+         if (response.ok) {
+          addHintM(scriptElement, "kalamar.conf.js");
+         } else {
+          addHintM(scriptElement, "default.js");
+         }
+     })
+     .catch(error => {
+         KorAP.log(0, "No kalamar.conf.js or default.js file");
+      });
+
     /**
      * Release notifications
      */
@@ -270,9 +296,8 @@
       sb.insertBefore(resultPanel.element(), sb.firstChild);
     };
 
-    
     // There is a koralQuery
-    if (KorAP.koralQuery !== undefined) {    
+    if (KorAP.koralQuery !== undefined) {
 
       // Add KoralQuery view to result panel
       if (resultInfo !== null) {
@@ -406,13 +431,12 @@
         d.getElementById('qsubmit').classList.add("loading");
       });
     };
- 
-    
+
     //Starts the guided tour at the next page
     if(KorAP.session.get("tour")){
       tourClass.gTshowResults().start();
     }
-    
+
     /**
      * Init hint helper
      * has to be final because of
@@ -420,13 +444,12 @@
      */
     // Todo: Pass an element, so this works with
     // tutorial pages as well!
+    scriptElement.onload = function(){
     if (obj.hint === undefined)
       obj.hint = hintClass.create();
-
-    // Add the hinthelper to the KorAP object to make it manipulatable globally
-    KorAP.Hint = obj.hint;
-
-
+      // Add the hinthelper to the KorAP object to make it manipulatable globally
+      KorAP.Hint = obj.hint;
+    };
     /**
      * Add query panel
      */
diff --git a/dev/js/src/loc/de.js b/dev/js/src/loc/de.js
index c5f83cc..d01479b 100644
--- a/dev/js/src/loc/de.js
+++ b/dev/js/src/loc/de.js
@@ -103,5 +103,6 @@
 
   // Pagination panel
   loc.RANDOM_PAGE = 'Zufallsseite';
-
+  
+  loc.HINT_noAnnot = 'Der Assistent kann nicht angezeigt werden.';
 });
diff --git a/lib/Kalamar.pm b/lib/Kalamar.pm
index f79c77c..bd117ae 100644
--- a/lib/Kalamar.pm
+++ b/lib/Kalamar.pm
@@ -202,6 +202,10 @@
     push @{$self->static->paths}, 'dev';
   };
 
+
+  # Add another "public" directory
+  push @{$self->static->paths}, 'public/js/hintc';
+
   # Set proxy timeouts
   if ($conf->{proxy_inactivity_timeout}) {
     $self->ua->inactivity_timeout($conf->{proxy_inactivity_timeout});