From 81da30efec96e6d33a380304b160dd86060afbba Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Sun, 8 Jan 2017 21:36:07 -0500 Subject: [PATCH] version 1.2.3 --- bower.json | 2 +- package.json | 2 +- public/dist/autocompeter.js | 13 ++++++------- public/dist/autocompeter.min.css | 2 +- public/dist/autocompeter.min.js | 4 ++-- src/autocompeter.js | 13 ++++++------- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/bower.json b/bower.json index e39dd80..31f9695 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "autocompeter", - "version": "1.2.2", + "version": "1.2.3", "homepage": "https://autocompeter.com", "authors": [ "Peter Bengtsson " diff --git a/package.json b/package.json index 3a46589..1c2613b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "autocompeter", - "version": "1.2.2", + "version": "1.2.3", "description": "A really fast AJAX autocomplete service and widget", "main": "index.js", "scripts": { diff --git a/public/dist/autocompeter.js b/public/dist/autocompeter.js index 1e47701..8130160 100644 --- a/public/dist/autocompeter.js +++ b/public/dist/autocompeter.js @@ -1,6 +1,6 @@ /*! * -* Copyright (c) 2015, Peter Bengtsson +* Copyright (c) 2015-2017, Peter Bengtsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -89,7 +89,7 @@ options.url += 'd=' + options.domain + '&q='; var results_ps = []; - var selected_pointer = 0; + var selected_pointer = -1; var actually_selected_pointer = false; q.spellcheck = false; q.autocomplete = 'off'; @@ -208,8 +208,7 @@ hint_candidate = found[found.length - 1]; if (hint_candidate !== undefined && !matched.test(q.value)) { - - if (selected_pointer === i) { + if (selected_pointer === i || (selected_pointer === -1 && i === 0)) { hint_candidates.push(hint_candidate); } } @@ -231,7 +230,7 @@ } r.appendChild(p_fragments); if (hint_candidates.length && q.value.charAt(q.value.length - 1) !== ' ') { - hint_candidate = hint_candidates[selected_pointer % hint_candidates.length]; + hint_candidate = hint_candidates[Math.max(0, selected_pointer) % hint_candidates.length]; hint.value = q.value + hint_candidate; } else { // If there are no candidates there's no point putting the @@ -282,7 +281,7 @@ displayResults(); } else if (name === 'enter') { if (results_ps.length && actually_selected_pointer) { - var p = results_ps[selected_pointer]; + var p = results_ps[Math.max(0, selected_pointer)]; var a = p.getElementsByTagName('a')[0]; q.value = hint.value = a.textContent; r.style.display = 'none'; @@ -346,7 +345,7 @@ } } // New character, let's reset the selected_pointer - selected_pointer = 0; + selected_pointer = -1; // Also, reset that none of the results have been explicitly // selected yet. actually_selected_pointer = false; diff --git a/public/dist/autocompeter.min.css b/public/dist/autocompeter.min.css index 0e03403..52fbf9e 100644 --- a/public/dist/autocompeter.min.css +++ b/public/dist/autocompeter.min.css @@ -1,4 +1,4 @@ -/* Autocompeter.com 1.2.2 */ +/* Autocompeter.com 1.2.3 */ ._ac-wrap{position:relative;display:inline-block} ._ac-wrap ._ac-hint{position:absolute;top:0;left:0;border-color:transparent;box-shadow:none;opacity:1;color:#b4b4b4;background:#fff} ._ac-wrap ._ac-foreground{background-color:transparent;position:relative;vertical-align:top} diff --git a/public/dist/autocompeter.min.js b/public/dist/autocompeter.min.js index be20c98..ff672a9 100644 --- a/public/dist/autocompeter.min.js +++ b/public/dist/autocompeter.min.js @@ -1,2 +1,2 @@ -/* Autocompeter.com 1.2.2 */ -!function(e,t){"use strict";function n(e,n){var a=t.createElement(e);for(var l in n)a[l]=n[l];return a}function a(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent(t,n)}function l(){var e,t,n,a,l,r,u,o=[].slice;for(a=arguments[0],n=2<=arguments.length?o.call(arguments,1):[],r=0,u=n.length;u>r;r++){t=n[r];for(e in t)l=t[e],a[e]=l}return a}function r(r,u){function o(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function s(e){var t=T.map(o),n=new RegExp("\\b(("+t.join("|")+")[\\S']*)","gi");return e.replace(n,"$1")}function i(e){"P"===e.target.tagName&&w!==+e.target.dataset.i&&(w=+e.target.dataset.i,p())}function c(e,t){for(var n=[],a=t.map(o),l=new RegExp("\\b("+a.join("|")+")","gi"),r=0,u=e.length;u>r;r++)e[r][1].search(l)>-1&&n.push(e[r]);return n}function p(a){a=a||!1,!a&&X&&e.clearTimeout(X);var l,u;if(null!==k){if(a&&(k=c(k,T)),!k.length)return void(L.style.display="none");L.style.display="block";var i=L.getElementsByTagName("p");for(l=i.length-1;l>=0;l--)i[l].remove();b=[];var p,v,d=null,f=[];k.length||(N.value="");var m=T.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=k.length;u>l;l++){for(var y,E;null!==(y=g.exec(k[l][1]));)E=new RegExp("\\b"+o(y[0])+"\\b","gi"),d=y[y.length-1],void 0===d||E.test(r.value)||w===l&&f.push(d);p=n("p"),l===w&&x&&p.classList.add("selected"),p.dataset.i=l,v=n("a",{innerHTML:s(k[l][1]),href:k[l][0]}),p.appendChild(v),h.appendChild(p),b.push(p)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(d=f[w%f.length],N.value=r.value+d):N.value=""}}function v(e){var t=e.parentNode;return null===t?void console.warn("Form can not be found. Nothing to submit"):"FORM"===t.nodeName?t:v(t)}function d(t){var n,a;if("tab"===t)N.value!==r.value&&(r.value=N.value+" "),r.value!==N.value&&g(),x=!0;else if("down"===t||"up"===t){for("down"===t?w=Math.min(b.length-1,++w):"up"===t&&(w=Math.max(0,--w)),n=0,a=b.length;a>n;n++)n===w&&x?b[n].classList.add("selected"):b[n].classList.remove("selected");x=!0,p()}else if("enter"===t){if(!b.length||!x){var l=v(r);return l&&l.submit(),!0}var u=b[w],o=u.getElementsByTagName("a")[0];r.value=N.value=o.textContent,L.style.display="none",e.location=o.href}else"esc"===t&&(L.style.display="none");return!1}function f(e){var t={13:"enter",9:"tab",38:"up",40:"down",27:"esc"};return t[e.keyCode]?(e.preventDefault(),d(t[e.keyCode])):!1}function m(){L.style.display="none"}function g(){if(!r.value.trim())return N.value="",void(L.style.display="none");if(N.value.length&&(-1===N.value.indexOf(r.value.trim())&&(N.value=r.value),"block"===L.style.display&&(T=r.value.trim().split(/\s+/),X=e.setTimeout(function(){p(!0)},150))),w=0,x=!1,j[r.value.trim()]){var t=j[r.value.trim()];T=t.terms,k=t.results,p()}else{if(R&&R.abort(),e.XMLHttpRequest)R=new e.XMLHttpRequest;else{if(!e.ActiveXObject)return;R=new e.ActiveXObject("Microsoft.XMLHTTP")}if(r.value.trim().length){var n=r.value.trim().substring(0,r.value.trim().length-1);if(j[n]&&!j[n].results.length)return void(j[r.value.trim()]={results:[]})}R.onreadystatechange=function(){if(4===R.readyState)if(200===R.status){if(M===r.value){var e=JSON.parse(R.responseText);j[r.value.trim()]=e,T=e.terms,k=e.results,p()}}else 0!==R.status&&(console.warn(R.status,R.responseText),m())},R.open("GET",u.url+encodeURIComponent(r.value.trim()),!0),M=r.value,R.send()}}function h(e){N.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(A&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),A=!1}r.value.length&&b.length&&(L.style.display="block")}u=l({url:"https://autocompeter.com/v1",domain:location.host,groups:null,ping:!0},u||{}),u.url+=u.url.indexOf("?")>-1?"&":"?",u.number&&(u.url+="n="+u.number+"&"),u.groups&&(Array.isArray(u.groups)&&(u.groups=u.groups.join(",")),u.url+="g="+encodeURIComponent(u.groups)+"&"),u.url+="d="+u.domain+"&q=";var b=[],w=0,x=!1;r.spellcheck=!1,r.autocomplete="off";var E=n("span",{className:"_ac-wrap"}),N=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),E.appendChild(N);var C=r.cloneNode(!0);E.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",i),E.appendChild(L),r.parentElement.insertBefore(E,r),r.parentNode.removeChild(r),r=C;var T,R,M,k=null,X=null,j={},A=!0;a(r,"input",g),a(r,"keydown",f),a(r,"blur",h),a(r,"focus",y)}e.Autocompeter=r}(window,document); \ No newline at end of file +/* Autocompeter.com 1.2.3 */ +!function(e,t){"use strict";function n(e,n){var a=t.createElement(e);for(var l in n)a[l]=n[l];return a}function a(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent(t,n)}function l(){var e,t,n,a,l,r,u,o=[].slice;for(a=arguments[0],n=2<=arguments.length?o.call(arguments,1):[],r=0,u=n.length;u>r;r++){t=n[r];for(e in t)l=t[e],a[e]=l}return a}function r(r,u){function o(e){return e.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}function s(e){var t=M.map(o),n=new RegExp("\\b(("+t.join("|")+")[\\S']*)","gi");return e.replace(n,"$1")}function i(e){"P"===e.target.tagName&&w!==+e.target.dataset.i&&(w=+e.target.dataset.i,p())}function c(e,t){for(var n=[],a=t.map(o),l=new RegExp("\\b("+a.join("|")+")","gi"),r=0,u=e.length;u>r;r++)e[r][1].search(l)>-1&&n.push(e[r]);return n}function p(a){a=a||!1,!a&&X&&e.clearTimeout(X);var l,u;if(null!==k){if(a&&(k=c(k,M)),!k.length)return void(L.style.display="none");L.style.display="block";var i=L.getElementsByTagName("p");for(l=i.length-1;l>=0;l--)i[l].remove();b=[];var p,v,d=null,f=[];k.length||(N.value="");var m=M.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=k.length;u>l;l++){for(var y,E;null!==(y=g.exec(k[l][1]));)E=new RegExp("\\b"+o(y[0])+"\\b","gi"),d=y[y.length-1],void 0===d||E.test(r.value)||(w===l||-1===w&&0===l)&&f.push(d);p=n("p"),l===w&&x&&p.classList.add("selected"),p.dataset.i=l,v=n("a",{innerHTML:s(k[l][1]),href:k[l][0]}),p.appendChild(v),h.appendChild(p),b.push(p)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(d=f[Math.max(0,w)%f.length],N.value=r.value+d):N.value=""}}function v(e){var t=e.parentNode;return null===t?void console.warn("Form can not be found. Nothing to submit"):"FORM"===t.nodeName?t:v(t)}function d(t){var n,a;if("tab"===t)N.value!==r.value&&(r.value=N.value+" "),r.value!==N.value&&g(),x=!0;else if("down"===t||"up"===t){for("down"===t?w=Math.min(b.length-1,++w):"up"===t&&(w=Math.max(0,--w)),n=0,a=b.length;a>n;n++)n===w&&x?b[n].classList.add("selected"):b[n].classList.remove("selected");x=!0,p()}else if("enter"===t){if(!b.length||!x){var l=v(r);return l&&l.submit(),!0}var u=b[Math.max(0,w)],o=u.getElementsByTagName("a")[0];r.value=N.value=o.textContent,L.style.display="none",e.location=o.href}else"esc"===t&&(L.style.display="none");return!1}function f(e){var t={13:"enter",9:"tab",38:"up",40:"down",27:"esc"};return t[e.keyCode]?(e.preventDefault(),d(t[e.keyCode])):!1}function m(){L.style.display="none"}function g(){if(!r.value.trim())return N.value="",void(L.style.display="none");if(N.value.length&&(-1===N.value.indexOf(r.value.trim())&&(N.value=r.value),"block"===L.style.display&&(M=r.value.trim().split(/\s+/),X=e.setTimeout(function(){p(!0)},150))),w=-1,x=!1,j[r.value.trim()]){var t=j[r.value.trim()];M=t.terms,k=t.results,p()}else{if(T&&T.abort(),e.XMLHttpRequest)T=new e.XMLHttpRequest;else{if(!e.ActiveXObject)return;T=new e.ActiveXObject("Microsoft.XMLHTTP")}if(r.value.trim().length){var n=r.value.trim().substring(0,r.value.trim().length-1);if(j[n]&&!j[n].results.length)return void(j[r.value.trim()]={results:[]})}T.onreadystatechange=function(){if(4===T.readyState)if(200===T.status){if(R===r.value){var e=JSON.parse(T.responseText);j[r.value.trim()]=e,M=e.terms,k=e.results,p()}}else 0!==T.status&&(console.warn(T.status,T.responseText),m())},T.open("GET",u.url+encodeURIComponent(r.value.trim()),!0),R=r.value,T.send()}}function h(e){N.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(A&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),A=!1}r.value.length&&b.length&&(L.style.display="block")}u=l({url:"https://autocompeter.com/v1",domain:location.host,groups:null,ping:!0},u||{}),u.url+=u.url.indexOf("?")>-1?"&":"?",u.number&&(u.url+="n="+u.number+"&"),u.groups&&(Array.isArray(u.groups)&&(u.groups=u.groups.join(",")),u.url+="g="+encodeURIComponent(u.groups)+"&"),u.url+="d="+u.domain+"&q=";var b=[],w=-1,x=!1;r.spellcheck=!1,r.autocomplete="off";var E=n("span",{className:"_ac-wrap"}),N=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),E.appendChild(N);var C=r.cloneNode(!0);E.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",i),E.appendChild(L),r.parentElement.insertBefore(E,r),r.parentNode.removeChild(r),r=C;var M,T,R,k=null,X=null,j={},A=!0;a(r,"input",g),a(r,"keydown",f),a(r,"blur",h),a(r,"focus",y)}e.Autocompeter=r}(window,document); \ No newline at end of file diff --git a/src/autocompeter.js b/src/autocompeter.js index 1e47701..8130160 100644 --- a/src/autocompeter.js +++ b/src/autocompeter.js @@ -1,6 +1,6 @@ /*! * -* Copyright (c) 2015, Peter Bengtsson +* Copyright (c) 2015-2017, Peter Bengtsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -89,7 +89,7 @@ options.url += 'd=' + options.domain + '&q='; var results_ps = []; - var selected_pointer = 0; + var selected_pointer = -1; var actually_selected_pointer = false; q.spellcheck = false; q.autocomplete = 'off'; @@ -208,8 +208,7 @@ hint_candidate = found[found.length - 1]; if (hint_candidate !== undefined && !matched.test(q.value)) { - - if (selected_pointer === i) { + if (selected_pointer === i || (selected_pointer === -1 && i === 0)) { hint_candidates.push(hint_candidate); } } @@ -231,7 +230,7 @@ } r.appendChild(p_fragments); if (hint_candidates.length && q.value.charAt(q.value.length - 1) !== ' ') { - hint_candidate = hint_candidates[selected_pointer % hint_candidates.length]; + hint_candidate = hint_candidates[Math.max(0, selected_pointer) % hint_candidates.length]; hint.value = q.value + hint_candidate; } else { // If there are no candidates there's no point putting the @@ -282,7 +281,7 @@ displayResults(); } else if (name === 'enter') { if (results_ps.length && actually_selected_pointer) { - var p = results_ps[selected_pointer]; + var p = results_ps[Math.max(0, selected_pointer)]; var a = p.getElementsByTagName('a')[0]; q.value = hint.value = a.textContent; r.style.display = 'none'; @@ -346,7 +345,7 @@ } } // New character, let's reset the selected_pointer - selected_pointer = 0; + selected_pointer = -1; // Also, reset that none of the results have been explicitly // selected yet. actually_selected_pointer = false;