From 97138d984fb2896d69e7a1b60907f4ee4eda9628 Mon Sep 17 00:00:00 2001 From: Jerome Bonini Date: Wed, 30 May 2018 10:53:01 +0200 Subject: [PATCH] - fix chrome autocomplete --- views/front/preview.jade | 112 +++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 63 deletions(-) diff --git a/views/front/preview.jade b/views/front/preview.jade index 49ffba899..16a82a36a 100755 --- a/views/front/preview.jade +++ b/views/front/preview.jade @@ -803,73 +803,59 @@ block custom_js ///////// // Autocomplete ///////// - $(document).on("keyup", "input#instruction", function(e){ - if (e.ctrlKey) - return; - var newstr = $("#instruction").val(); - - function split( val ) { - return val.split( /,\s*/ ); - } - function extractLast( term ) { - return split( term ).pop(); - } - - $("#instruction").autocomplete({ - autoFocus: true, - delay: 200, - source: function(request, response) { - $.getJSON('/default/completion', { - "str": newstr - }, response); - }, - search: function() { - return true; - }, - focus: function() { - // prevent value inserted on focus - return false; - }, - select: function(event, ui) { - - var ENTERKEY = 13; - if (event.keyCode != ENTERKEY) { - var completeVal = ui.item.value; - // If complete value have already typed string in it, dont concat with current value - if (completeVal.indexOf(this.value) == 0) { - this.value = completeVal.split("[variable]").join("").trim(); - } else { - // Remove the last word of already typed instruction because it is also in the completed value - var parts = this.value.split(' '); - if (parts[parts.length - 1] == " ") - parts.pop(); - else { - compareNum = 0; - l = Math.min(completeVal.length, parts[parts.length - 1].length); - for (i = 0; i < l; i++) { - if (completeVal.charAt(i) == parts[parts.length - 1].charAt(i)) compareNum++; - } - if (compareNum <= completeVal.length && completeVal.substring(0, compareNum) == parts[parts.length - 1]) - parts.pop(); + $("#instruction").autocomplete({ + autoFocus: true, + delay: 200, + source: function(request, response) { + $.getJSON('/default/completion', { + "str": request.term + }, response); + }, + search: function() { + return true; + }, + focus: function() { + // prevent value inserted on focus + return false; + }, + select: function(event, ui) { + + var ENTERKEY = 13; + if (event.keyCode != ENTERKEY) { + var completeVal = ui.item.value; + // If complete value have already typed string in it, dont concat with current value + if (completeVal.indexOf(this.value) == 0) { + this.value = completeVal.split("[variable]").join("").trim(); + } else { + // Remove the last word of already typed instruction because it is also in the completed value + var parts = this.value.split(' '); + if (parts[parts.length - 1] == " ") + parts.pop(); + else { + compareNum = 0; + l = Math.min(completeVal.length, parts[parts.length - 1].length); + for (i = 0; i < l; i++) { + if (completeVal.charAt(i) == parts[parts.length - 1].charAt(i)) compareNum++; } - this.value = parts.join(' ') + ' ' + completeVal.split("[variable]").join("").trim(); + if (compareNum <= completeVal.length && completeVal.substring(0, compareNum) == parts[parts.length - 1]) + parts.pop(); } + this.value = parts.join(' ') + ' ' + completeVal.split("[variable]").join("").trim(); + } - var TABKEY = 9; - if (event.keyCode == TABKEY) { - event.preventDefault(); - this.value = this.value+" "; - $('#instruction').focus(); - } - return false; - } else { - event.preventDefault(); - $('#execute_instruction').click(); - return false; + var TABKEY = 9; + if (event.keyCode == TABKEY) { + event.preventDefault(); + this.value = this.value+" "; + $('#instruction').focus(); } - } - }); - // } + return false; + } else { + event.preventDefault(); + $('#execute_instruction').click(); + return false; + } + } }); /////////