From fa6c82a5335e2828ed4bc76b3d7980a0d1db1958 Mon Sep 17 00:00:00 2001 From: Franzz Date: Mon, 16 Sep 2019 19:10:44 +0200 Subject: [PATCH] clean input autocomplete engine --- readme.md | 4 ++- scripts/catc.js | 84 +++++++++++++++++++++++++++++++++++++++++++++-- scripts/common.js | 84 +---------------------------------------------- 3 files changed, 86 insertions(+), 86 deletions(-) diff --git a/readme.md b/readme.md index 27f32d9..cf1b5e0 100644 --- a/readme.md +++ b/readme.md @@ -6,4 +6,6 @@ Prise de notes pour les cours du Collège des Arts Thérapeutiques Chinois * [ ] View docs online: audio, video, word, pdf * [x] Take notes on courses * [ ] Quick view of muscles / nerves schemas -* [x] Dictionary \ No newline at end of file +* [x] Dictionary +* [ ] Progress bar +* [ ] Redo feedback \ No newline at end of file diff --git a/scripts/catc.js b/scripts/catc.js index 88379fa..22afe9b 100644 --- a/scripts/catc.js +++ b/scripts/catc.js @@ -55,9 +55,89 @@ function CATC(asGlobals) this.initMenu = function() { - //Search + //Search - Input Autocomplete self.refreshDefs(); - self.elem.$Menu.find('#search').addSearch('defs'); + self.elem.$Menu.find('#search') + .autocomplete({ + autoFocus: true, + delay: 0, + minLength: 2, + classes:{'ui-autocomplete':'list-group'}, + source: function(oRequest, fResponse) { + var sTerm = removeDiacritics(oRequest.term); + var rMatcher = new RegExp($.ui.autocomplete.escapeRegex(sTerm), 'i'); + + var asResults = []; + $.each(self.vars('lov-defs'), function(iDefId, oItem) { + var oTitleMatch = oItem.safe_title.match(rMatcher); + var oLabelMatch = oItem.safe_label.match(rMatcher); + var oMatch = {}; + + oItem.field = ''; + if(oTitleMatch) { + oMatch = oTitleMatch; + oItem.field = 'title'; + } + else if(oLabelMatch) { + oMatch = oLabelMatch; + oItem.field = 'label'; + } + + if(oItem.field != '') { + oItem.index = parseInt(oMatch.index, 10); + oItem.size = oMatch[0].length; + asResults.push(oItem); + } + oItem.id = iDefId; + }); + + self.elem.$Menu.find('#add_def') + .toggleClass('btn-outline-primary', (asResults.length > 0)) + .toggleClass('btn-primary', (asResults.length == 0)); + + fResponse(asResults); + }, + select: function(event, ui) { + return false; //Do not update input value + } + }) + .data("ui-autocomplete")._renderItem = function(ul, item) { + var fGetHighLightedContent = function(sContent, iIndex, iSize){ + return $('') + .append(sContent.substr(0, iIndex)) + .append($('', {'class':'highlight text-secondary'}).text(sContent.substr(iIndex, iSize))) + .append(sContent.substr(iIndex + iSize)); + }; + var $Title = $('
', {'class':'title text-break text-capitalize font-weight-bold text-primary'}); + var $Desc = $('
', {'class':'desc text-break'}); + + if(item.field=='title') { + $Title.append(fGetHighLightedContent(item.title, item.index, item.size)); + $Desc.text(item.label); + } + else { + $Title.text(item.title); + $Desc.append(fGetHighLightedContent(item.label, item.index, item.size)); + } + return $('
  • ', {'class':'list-group-item shadow'}) + .css('width', $('#search').outerWidth()) + .data("item.autocomplete", item) + .append($('
    ', {'class':'row'}) + .append($('
    ', {'class':'col'}) + .append($Title) + .append($Desc) + ) + .append($('
    ', {'class':'col-auto d-flex align-items-center'}).append($('