From 8b97a109c652503811514987abf0869c3f639759 Mon Sep 17 00:00:00 2001
From: Tobias Baden <tobias.baden@stud.hs-hannover.de>
Date: Wed, 29 Aug 2018 18:43:22 +0200
Subject: [PATCH] Fixed another path error - Select course with enter -> path
 error fixed - Enabled wildcard searching (3char limit bug) - Updated Version

---
 amd/build/jmodule.min.js    |  2 +-
 amd/build/listselect.min.js |  2 +
 amd/src/jmodule.js          | 14 +++++--
 amd/src/listselect.js       | 74 +++++++++++++++++++++++++++++++++++++
 block_hshcourselist.php     |  7 +---
 version.php                 |  2 +-
 6 files changed, 90 insertions(+), 11 deletions(-)
 create mode 100644 amd/build/listselect.min.js
 create mode 100644 amd/src/listselect.js

diff --git a/amd/build/jmodule.min.js b/amd/build/jmodule.min.js
index 19d5a90..aa1cf37 100644
--- a/amd/build/jmodule.min.js
+++ b/amd/build/jmodule.min.js
@@ -1,2 +1,2 @@
 /*! hshcourselist 2018-08-29 */
-define(["jquery"],function(a){function b(b,c,d,e,f,g,h,i,j){c&&(a("#hshcourseprogress").show(),a.ajax({type:"GET",url:j+"/blocks/hshcourselist/hshcourselist.php",data:"course="+b+"&instanceid="+e+"&sesskey="+f+"&contextid="+g+"&idSearching="+h+"&orderbysemester="+i,dataType:"json",success:function(b){var c=JSON.stringify(b),e=JSON.parse(c),f=a("<ul class='list-group' id='hshcourselistul'></ul>");e.length>0&&a.each(e,function(b,c){switch(d){case"0":displaystr=c.shortname;break;case"1":displaystr=c.fullname;break;case"2":displaystr=c.shortname+": "+c.fullname;break;default:displaystr=c.fullname}a('<li class="list-group-item hshcoursesearchlistitem " value="'+c.id+'"><a href="'+j+"/course/view.php?id="+c.id+'">'+displaystr+"</a> </li>").appendTo(f)}),a("#hshcourselistul").replaceWith(f),a("#hshcourseprogress").hide()},error:function(){console.error("error"),"abort"!==o.statusText&&(a("#hshcourseprogress").hide(),void 0!==o.statusText&&console.error("ABORT + STATUS : "+o.statusText))}}))}return{init:function(c,d){var e,c=c,f=c.instanceid,g=c.sesskey,h=c.displaymode,i=c.contextid,j=!1,k=!1,l=c.orderbysemester;a("#hshcourseprogress").hide(),a(".idSarchingLabel").hide(),a("#orderbysemestercheckbox_id").on("change",function(){l=document.getElementById("orderbysemestercheckbox_id").checked,j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):(a(".idSarchingLabel").hide(),k=!1),(1==k||e.length>=3)&&b(e,j,h,f,g,i,k,l,d)}),a("#hshcourselistsearch").on("keyup",function(c){var m=c.keyCode;l=document.getElementById("orderbysemestercheckbox_id").checked,40!==m&&38!==m&&13!==m&&(j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):(a(".idSarchingLabel").hide(),k=!1),(1==k||e.length>=3)&&b(e,j,h,f,g,i,k,l,d))}),a("#hshcourseform").on("submit",function(a){a.preventDefault,j=!0,k=!1,l=document.getElementById("orderbysemestercheckbox_id").checked,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(k=!0,e=e.slice(1)):k=!1,(1==k||e.length>=3)&&b(e,j,h,f,g,i,k,l,d)})}}});
\ No newline at end of file
+define(["jquery"],function(a){function b(b,c,d,e,f,g,h,i,j){c&&(a("#hshcourseprogress").show(),a.ajax({type:"GET",url:j+"/blocks/hshcourselist/hshcourselist.php",data:"course="+b+"&instanceid="+e+"&sesskey="+f+"&contextid="+g+"&idSearching="+h+"&orderbysemester="+i,dataType:"json",success:function(b){var c=JSON.stringify(b),e=JSON.parse(c),f=a("<ul class='list-group' id='hshcourselistul'></ul>");e.length>0&&a.each(e,function(b,c){switch(d){case"0":displaystr=c.shortname;break;case"1":displaystr=c.fullname;break;case"2":displaystr=c.shortname+": "+c.fullname;break;default:displaystr=c.fullname}a('<li class="list-group-item hshcoursesearchlistitem " value="'+c.id+'"><a href="'+j+"/course/view.php?id="+c.id+'">'+displaystr+"</a> </li>").appendTo(f)}),a("#hshcourselistul").replaceWith(f),a("#hshcourseprogress").hide()},error:function(){console.error("error"),"abort"!==o.statusText&&(a("#hshcourseprogress").hide(),void 0!==o.statusText&&console.error("ABORT + STATUS : "+o.statusText))}}))}return{init:function(c,d){var e,c=c,f=c.instanceid,g=c.sesskey,h=c.displaymode,i=c.contextid,j=!1,k=!1,l=!1,m=c.orderbysemester;a("#hshcourseprogress").hide(),a(".idSarchingLabel").hide(),a("#orderbysemestercheckbox_id").on("change",function(){m=document.getElementById("orderbysemestercheckbox_id").checked,j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)}),a("#hshcourselistsearch").on("keyup",function(c){var n=c.keyCode;m=document.getElementById("orderbysemestercheckbox_id").checked,40!==n&&38!==n&&13!==n&&(j=!0,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(a(".idSarchingLabel").show(),k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:(a(".idSarchingLabel").hide(),k=!1),(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d))}),a("#hshcourseform").on("submit",function(a){a.preventDefault,j=!0,k=!1,l=!1,m=document.getElementById("orderbysemestercheckbox_id").checked,e=document.getElementById("hshcourselistsearch").value,"#"===e.slice(0,1)?(k=!0,e=e.slice(1)):"%"===e.slice(0,1)?l=!0:k=!1,(1==l||1==k||e.length>=3)&&b(e,j,h,f,g,i,k,m,d)})}}});
\ No newline at end of file
diff --git a/amd/build/listselect.min.js b/amd/build/listselect.min.js
new file mode 100644
index 0000000..eae097d
--- /dev/null
+++ b/amd/build/listselect.min.js
@@ -0,0 +1,2 @@
+/*! hshcourselist 2018-08-29 */
+define(["jquery"],function(a){function b(b,c){var d=c+"/course/view.php?id="+b.val();a("#hshcourseform").attr("action",d)}function c(a){a.removeClass("active"),a.removeClass("activeCourseSearch")}function d(a){a.addClass("active"),a.addClass("activeCourseSearch")}function e(a){var b=window.innerHeight,c=a.getBoundingClientRect(),d=c.bottom,e=c.top;d>b&&a.scrollIntoView({block:"end",behavior:"smooth"}),e<0&&a.scrollIntoView({block:"start",behavior:"smooth"})}return{init:function(f){a("input").keydown(function(g){var h,i=a(".hshcoursesearchlistitem"),j=g.keyCode,k=i.filter(".activeCourseSearch");40!==j&&38!==j&&13!==j||(c(i),38===j?(h=!k.length||k.is(":first-child")?i.last():k.prev(),b(h,f)):40===j?(h=!k.length||k.is(":last-child")?i.eq(0):k.next(),b(h,f)):13===j&&(h=k,isNaN(h.val())&&(h=i.eq(0)),b(h,f)),d(h),e(h[0]))})}}});
\ No newline at end of file
diff --git a/amd/src/jmodule.js b/amd/src/jmodule.js
index 9aaa058..b1c7c87 100644
--- a/amd/src/jmodule.js
+++ b/amd/src/jmodule.js
@@ -63,6 +63,7 @@ define(['jquery'], function ($) {
             var contextid = jsdataobject.contextid;
             var reloadList = false;
             var idSearching = false;
+            var wildcardSearching = false;
             var searchstring;
             var orderbysemester = jsdataobject.orderbysemester;
             var displaystr;
@@ -78,11 +79,13 @@ define(['jquery'], function ($) {
                     $(".idSarchingLabel").show();
                     idSearching = true;
                     searchstring = searchstring.slice(1);
+                } else if (searchstring.slice(0, 1) === '%') {
+                    wildcardSearching = true;
                 } else {
                     $(".idSarchingLabel").hide();
                     idSearching = false;
                 }
-                if (idSearching == true || searchstring.length >= 3) {
+                if (wildcardSearching == true || idSearching == true || searchstring.length >= 3) {
                     search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester, wwwroot);
                 } else {
                     // Search start after 3 chars or searching by id
@@ -99,11 +102,13 @@ define(['jquery'], function ($) {
                         $(".idSarchingLabel").show();
                         idSearching = true;
                         searchstring = searchstring.slice(1);
+                    } else if (searchstring.slice(0, 1) === '%') {
+                        wildcardSearching = true;
                     } else {
                         $(".idSarchingLabel").hide();
                         idSearching = false;
                     }
-                    if (idSearching == true || searchstring.length >= 3) {
+                    if (wildcardSearching == true || idSearching == true || searchstring.length >= 3) {
                         search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester, wwwroot);
                     } else {
                         // Search start after 3 chars or searching by id
@@ -114,15 +119,18 @@ define(['jquery'], function ($) {
                 e.preventDefault;
                 reloadList = true;
                 idSearching = false;
+                wildcardSearching = false;
                 orderbysemester = document.getElementById('orderbysemestercheckbox_id').checked;
                 searchstring = document.getElementById('hshcourselistsearch').value;
                 if (searchstring.slice(0, 1) === '#') {
                     idSearching = true;
                     searchstring = searchstring.slice(1);
+                } else if (searchstring.slice(0, 1) === '%') {
+                    wildcardSearching = true;
                 } else {
                     idSearching = false;
                 }
-                if (idSearching == true || searchstring.length >= 3) {
+                if (wildcardSearching == true || idSearching == true || searchstring.length >= 3) {
                     search(searchstring, reloadList, displaymode, instanceid, sesskey, contextid, idSearching, orderbysemester, wwwroot);
                 } else {
                     // Search start after 3 chars or searching by id
diff --git a/amd/src/listselect.js b/amd/src/listselect.js
new file mode 100644
index 0000000..278e85d
--- /dev/null
+++ b/amd/src/listselect.js
@@ -0,0 +1,74 @@
+define(['jquery'], function ($) {
+    function changeActionString(current, wwwroot) {
+        var actionString = wwwroot + '/course/view.php?id=' + current.val();
+        $('#hshcourseform').attr('action', actionString);
+    }
+
+    function setInactive(target) {
+        target.removeClass('active');
+        target.removeClass('activeCourseSearch');
+    }
+
+    function setActive(target) {
+        target.addClass('active');
+        target.addClass('activeCourseSearch');
+    }
+
+    function scrollIntoViewIfNeeded(element) {
+        var windowSize = window.innerHeight;
+        var elementPosition = element.getBoundingClientRect();
+        var elementBottom = elementPosition.bottom;
+        var elementTop = elementPosition.top;
+
+        if (elementBottom > windowSize) {
+            // Scroll an den unteren Rand des Bildes
+            element.scrollIntoView({ block: "end", behavior: "smooth" });
+        }
+        if (elementTop < 0) {
+            // Scroll an den oberen Rand des Bildes
+            element.scrollIntoView({ block: "start", behavior: "smooth" });
+        }
+    }
+
+    return {
+        init: function (wwwroot) {
+            $('input').keydown(function (e) {
+                var $listItems = $('.hshcoursesearchlistitem');
+                var key = e.keyCode;
+                var $selected = $listItems.filter('.activeCourseSearch');
+                var $current;
+
+                if (key !== 40 && key !== 38 && key !== 13) {
+                    return;
+                }
+
+                setInactive($listItems);
+
+                if (key === 38) { // KEY UP
+                    if (!$selected.length || $selected.is(':first-child')) {
+                        $current = $listItems.last();
+                    } else {
+                        $current = $selected.prev();
+                    }
+                    changeActionString($current, wwwroot);
+                } else if (key === 40) { // KEY DOWN
+                    if (!$selected.length || $selected.is(':last-child')) {
+                        $current = $listItems.eq(0);
+                    } else {
+                        $current = $selected.next();
+                    }
+                    changeActionString($current, wwwroot);
+                } else if (key === 13) { // KEY ENTER
+                    $current = $selected;
+                    if (isNaN($current.val())) {
+                        $current = $listItems.eq(0);
+                    }
+                    changeActionString($current, wwwroot);
+                }
+
+                setActive($current);
+                scrollIntoViewIfNeeded($current[0]);
+            });
+        },
+    }
+});
diff --git a/block_hshcourselist.php b/block_hshcourselist.php
index 34664b5..2921f7c 100644
--- a/block_hshcourselist.php
+++ b/block_hshcourselist.php
@@ -27,7 +27,7 @@ class block_hshcourselist extends block_base {
         global $CFG, $DB, $OUTPUT;
 
         $this->page->requires->js('/blocks/hshcourselist/javascript/jquery-3.2.0.min.js');
-        $this->page->requires->js('/blocks/hshcourselist/javascript/listselect.js');
+        $this->page->requires->js_call_amd('block_hshcourselist/listselect', 'init', array($CFG->wwwroot));
         $this->page->requires->css('/blocks/hshcourselist/style.css');
 
         if ($this->content !== null) {
@@ -163,11 +163,6 @@ class block_hshcourselist extends block_base {
         $jsdata = array(
             'instanceid' => $this->instance->id,
             'sesskey' => sesskey(),
-            /*
-                Notice: Trying to get property of non-object in C:\Dev\xampp\htdocs\moodle\blocks\hshcourselist\block_hshcourselist.php on line 117 (122)
-
-                Notice: Trying to get property of non-object in C:\Dev\xampp\htdocs\moodle\blocks\hshcourselist\block_hshcourselist.php on line 118 (123)
-            */
             'displaymode' => $displaymode,
             'orderbysemester' => $orderbysemester,
             'contextid' => $this->page->context->id
diff --git a/version.php b/version.php
index f8614d1..31744bb 100644
--- a/version.php
+++ b/version.php
@@ -1,4 +1,4 @@
 <?php
 $plugin->component = 'block_hshcourselist';
-$plugin->version = 2018082803;  // YYYYMMDDHH
+$plugin->version = 2018082900;  // YYYYMMDDHH
 $plugin->requires = 2016112900; // YYYYMMDDHH
-- 
GitLab