From 6ad8bf8de1438eff10acdd203cc0d91271a34811 Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 06:50:53 -0400
Subject: [PATCH 1/7] use ajax cache to NOT ajax call if previous term yielded
nothing
---
public/dist/autocompeter.js | 18 ++++++++++++++++++
public/dist/autocompeter.min.js | 2 +-
src/autocompeter.js | 18 ++++++++++++++++++
3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/public/dist/autocompeter.js b/public/dist/autocompeter.js
index 7c38a2f..12bb8e9 100644
--- a/public/dist/autocompeter.js
+++ b/public/dist/autocompeter.js
@@ -177,6 +177,7 @@
}
} else {
r.style.display = 'none';
+ return;
}
results_ps = [];
var p, a;
@@ -355,6 +356,23 @@
} else {
return;
}
+
+ if (q.value.trim().length) {
+ // Suppose you have typed in "Pytho", then your
+ // previously typed in search is likely to be "Pyth".
+ // I.e. the same minus the last character.
+ // If that search is in the cache and had 0 results, then
+ // there's no point also searching for "Pytho"
+ var previous_value = q.value.trim().substring(
+ 0, q.value.trim().length - 1
+ );
+ if (cache[previous_value] && !cache[previous_value].results.length) {
+ // don't bother sending this search then
+ cache[q.value.trim()] = {results: []};
+ return;
+ }
+ }
+
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
diff --git a/public/dist/autocompeter.min.js b/public/dist/autocompeter.min.js
index 6b5e685..9e58293 100644
--- a/public/dist/autocompeter.min.js
+++ b/public/dist/autocompeter.min.js
@@ -1,2 +1,2 @@
/* Autocompeter.com 1.1.12 */
-!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=N.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,c())}function p(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 c(a){a=a||!1,!a&&k&&e.clearTimeout(k);var l,u;if(null!==M){if(a&&(M=p(M,N)),M.length){L.style.display="block";var i=L.getElementsByTagName("p");for(l=i.length-1;l>=0;l--)i[l].remove()}else L.style.display="none";b=[];var c,d,v=null,f=[];M.length||(E.value="");var m=N.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=M.length;u>l;l++){for(var y,x;null!==(y=g.exec(M[l][1]));)x=new RegExp("\\b"+o(y[0])+"\\b","gi"),v=y[y.length-1],void 0===v||x.test(r.value)||w===l&&f.push(v);c=n("p"),l===w&&c.classList.add("selected"),c.dataset.i=l,d=n("a",{innerHTML:s(M[l][1]),href:M[l][0]}),c.appendChild(d),h.appendChild(c),b.push(c)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(v=f[w%f.length],E.value=r.value+v):E.value=""}}function d(e){var t=e.parentNode;if("FORM"===t.nodeName)return t;if(null===t)throw"too deep. no parent form node to be found";return d(t)}function v(t){var n,a;if("tab"===t)E.value!==r.value&&(r.value=E.value+" "),r.value!==E.value&&g();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?b[n].classList.add("selected"):b[n].classList.remove("selected");c()}else if("enter"===t){if(!b.length)return d(r).submit(),!0;var l=b[w],u=l.getElementsByTagName("a")[0];r.value=E.value=u.textContent,L.style.display="none",e.location=u.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(),v(t[e.keyCode])):!1}function m(){L.style.display="none"}function g(){if(!r.value.trim())return E.value="",void(L.style.display="none");if(E.value.length&&(-1===E.value.indexOf(r.value.trim())&&(E.value=r.value),"block"===L.style.display&&(N=r.value.trim().split(/\s+/),k=e.setTimeout(function(){c(!0)},150))),w=0,X[r.value.trim()]){var t=X[r.value.trim()];N=t.terms,M=t.results,c()}else{if(T&&T.abort(),e.XMLHttpRequest)T=new e.XMLHttpRequest;else{if(!e.ActiveXObject)return;T=new e.ActiveXObject("Microsoft.XMLHTTP")}T.onreadystatechange=function(){if(4===T.readyState)if(200===T.status){if(R===r.value){var e=JSON.parse(T.responseText);X[r.value.trim()]=e,N=e.terms,M=e.results,c()}}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){E.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(j&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),j=!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;r.spellcheck=!1,r.autocomplete="off";var x=n("span",{className:"_ac-wrap"}),E=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),x.appendChild(E);var C=r.cloneNode(!0);x.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",i),x.appendChild(L),r.parentElement.insertBefore(x,r),r.parentNode.removeChild(r),r=C;var N,T,R,M=null,k=null,X={},j=!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
+!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 i(e){var t=N.map(o),n=new RegExp("\\b(("+t.join("|")+")[\\S']*)","gi");return e.replace(n,"$1")}function s(e){"P"===e.target.tagName&&w!==+e.target.dataset.i&&(w=+e.target.dataset.i,c())}function p(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 c(a){a=a||!1,!a&&k&&e.clearTimeout(k);var l,u;if(null!==M){if(a&&(M=p(M,N)),!M.length)return void(L.style.display="none");L.style.display="block";var s=L.getElementsByTagName("p");for(l=s.length-1;l>=0;l--)s[l].remove();b=[];var c,v,d=null,f=[];M.length||(E.value="");var m=N.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=M.length;u>l;l++){for(var y,x;null!==(y=g.exec(M[l][1]));)x=new RegExp("\\b"+o(y[0])+"\\b","gi"),d=y[y.length-1],void 0===d||x.test(r.value)||w===l&&f.push(d);c=n("p"),l===w&&c.classList.add("selected"),c.dataset.i=l,v=n("a",{innerHTML:i(M[l][1]),href:M[l][0]}),c.appendChild(v),h.appendChild(c),b.push(c)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(d=f[w%f.length],E.value=r.value+d):E.value=""}}function v(e){var t=e.parentNode;if("FORM"===t.nodeName)return t;if(null===t)throw"too deep. no parent form node to be found";return v(t)}function d(t){var n,a;if("tab"===t)E.value!==r.value&&(r.value=E.value+" "),r.value!==E.value&&g();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?b[n].classList.add("selected"):b[n].classList.remove("selected");c()}else if("enter"===t){if(!b.length)return v(r).submit(),!0;var l=b[w],u=l.getElementsByTagName("a")[0];r.value=E.value=u.textContent,L.style.display="none",e.location=u.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 E.value="",void(L.style.display="none");if(E.value.length&&(-1===E.value.indexOf(r.value.trim())&&(E.value=r.value),"block"===L.style.display&&(N=r.value.trim().split(/\s+/),k=e.setTimeout(function(){c(!0)},150))),w=0,X[r.value.trim()]){var t=X[r.value.trim()];N=t.terms,M=t.results,c()}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(X[n]&&!X[n].results.length)return void(X[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);X[r.value.trim()]=e,N=e.terms,M=e.results,c()}}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){E.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(j&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),j=!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;r.spellcheck=!1,r.autocomplete="off";var x=n("span",{className:"_ac-wrap"}),E=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),x.appendChild(E);var C=r.cloneNode(!0);x.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",s),x.appendChild(L),r.parentElement.insertBefore(x,r),r.parentNode.removeChild(r),r=C;var N,T,R,M=null,k=null,X={},j=!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 7c38a2f..12bb8e9 100644
--- a/src/autocompeter.js
+++ b/src/autocompeter.js
@@ -177,6 +177,7 @@
}
} else {
r.style.display = 'none';
+ return;
}
results_ps = [];
var p, a;
@@ -355,6 +356,23 @@
} else {
return;
}
+
+ if (q.value.trim().length) {
+ // Suppose you have typed in "Pytho", then your
+ // previously typed in search is likely to be "Pyth".
+ // I.e. the same minus the last character.
+ // If that search is in the cache and had 0 results, then
+ // there's no point also searching for "Pytho"
+ var previous_value = q.value.trim().substring(
+ 0, q.value.trim().length - 1
+ );
+ if (cache[previous_value] && !cache[previous_value].results.length) {
+ // don't bother sending this search then
+ cache[q.value.trim()] = {results: []};
+ return;
+ }
+ }
+
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status === 200) {
From f841523581fb051d8be739e03449b919b5cd826b Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 06:52:10 -0400
Subject: [PATCH 2/7] pinned gulp dependencies
---
package.json | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)
diff --git a/package.json b/package.json
index 7e8c9ec..63a2018 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "autocompeter",
- "version": "0.1.0",
+ "version": "0.1.1",
"description": "A really fast AJAX autocomplete service and widget",
"main": "index.js",
"scripts": {
@@ -17,14 +17,16 @@
},
"homepage": "https://github.com/peterbe/autocompeter",
"devDependencies": {
- "gulp": "^3.8.10",
- "gulp-concat": "^2.5.2",
- "gulp-header": "^1.2.2",
- "gulp-jshint": "^1.10.0",
- "gulp-less": "^2.0.1",
- "gulp-minify-css": "^0.4.5",
- "gulp-rename": "^1.2.0",
- "gulp-sass": "^1.3.3",
- "gulp-uglify": "^1.1.0"
+ "gulp": "3.9.1",
+ "gulp-concat": "2.6.0",
+ "gulp-header": "1.8.2",
+ "gulp-jshint": "2.0.1",
+ "gulp-less": "3.1.0",
+ "gulp-minify-css": "1.2.4",
+ "gulp-rename": "1.2.2",
+ "gulp-sass": "2.3.1",
+ "gulp-uglify": "1.5.3",
+ "jshint": "2.9.2",
+ "node-sass": "2.1.1"
}
}
From 20d5f8fe5fd5ddd6f704aab3da2a499ec6caa2db Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 06:52:21 -0400
Subject: [PATCH 3/7] update links to https://www.peterbe.com
---
templates/index.tmpl | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/templates/index.tmpl b/templates/index.tmpl
index 3703c42..4ebf025 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -179,7 +179,7 @@ Autocompeter(
- See the HTML bits here on the right.
- The database was populate by sucking up all ~1,000+
- blog post titles from www.peterbe.com.
- A little bit of CSS overrides was added because
this page's Boostrap CSS affected the CSS inside
@@ -210,7 +210,7 @@ Autocompeter(
widget on your own site.
- Original blog post about the launch here.
+ Original blog post about the launch here.
@@ -382,7 +382,7 @@ Autocompeter(
-
+
Peterbe.com (real)
@@ -441,7 +441,7 @@ Autocompeter(
{{end}}
- Copyright © Peter Bengtsson 2015.
+ Copyright © Peter Bengtsson 2015.
All Rights Reserved.
Design by Start Bootstrap
From 462d1d82910e491d2202846419ca8348953f5a30 Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 06:56:31 -0400
Subject: [PATCH 4/7] switch to gulp-clean-css
---
gulpfile.js | 4 ++--
package.json | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/gulpfile.js b/gulpfile.js
index 05c4183..b453ea1 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -7,7 +7,7 @@ var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
-var minifyCSS = require('gulp-minify-css');
+var cleanCSS = require('gulp-clean-css');
var header = require('gulp-header');
var bowerPkg = require('./bower.json');
@@ -27,7 +27,7 @@ gulp.task('sass', function() {
.pipe(sass())
.pipe(gulp.dest('public/dist'))
.pipe(rename('autocompeter.min.css'))
- .pipe(minifyCSS({keepBreaks:true}))
+ .pipe(cleanCSS({keepBreaks:true}))
.pipe(header(banner, {pkg: bowerPkg}))
.pipe(gulp.dest('public/dist'));
});
diff --git a/package.json b/package.json
index 63a2018..e2194bd 100644
--- a/package.json
+++ b/package.json
@@ -18,11 +18,11 @@
"homepage": "https://github.com/peterbe/autocompeter",
"devDependencies": {
"gulp": "3.9.1",
+ "gulp-clean-css": "2.0.10",
"gulp-concat": "2.6.0",
"gulp-header": "1.8.2",
"gulp-jshint": "2.0.1",
"gulp-less": "3.1.0",
- "gulp-minify-css": "1.2.4",
"gulp-rename": "1.2.2",
"gulp-sass": "2.3.1",
"gulp-uglify": "1.5.3",
From ff5a0814f361059f8513bdaed65d0495f6132cd9 Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 06:57:27 -0400
Subject: [PATCH 5/7] only test once
---
.travis.yml | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/.travis.yml b/.travis.yml
index ad04506..14d8d5b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -10,6 +10,10 @@ services:
env:
- REDISDATABASE=8
+branches:
+ only:
+ - master
+
script:
- go test -v ./...
- ./travis-e2e.sh
From 29745ca969b2a40b8d5807b5eb8a84445f86a453 Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 07:00:37 -0400
Subject: [PATCH 6/7] set version where it matters
---
bower.json | 2 +-
package.json | 2 +-
public/dist/autocompeter.min.css | 2 +-
public/dist/autocompeter.min.js | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/bower.json b/bower.json
index b9efb35..cf24cef 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "autocompeter",
- "version": "1.1.12",
+ "version": "1.1.13",
"homepage": "https://autocompeter.com",
"authors": [
"Peter Bengtsson "
diff --git a/package.json b/package.json
index e2194bd..1e64360 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "autocompeter",
- "version": "0.1.1",
+ "version": "0.1.0",
"description": "A really fast AJAX autocomplete service and widget",
"main": "index.js",
"scripts": {
diff --git a/public/dist/autocompeter.min.css b/public/dist/autocompeter.min.css
index f7d852b..a2ee0af 100644
--- a/public/dist/autocompeter.min.css
+++ b/public/dist/autocompeter.min.css
@@ -1,4 +1,4 @@
-/* Autocompeter.com 1.1.12 */
+/* Autocompeter.com 1.1.13 */
._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 9e58293..a912fbc 100644
--- a/public/dist/autocompeter.min.js
+++ b/public/dist/autocompeter.min.js
@@ -1,2 +1,2 @@
-/* Autocompeter.com 1.1.12 */
+/* Autocompeter.com 1.1.13 */
!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 i(e){var t=N.map(o),n=new RegExp("\\b(("+t.join("|")+")[\\S']*)","gi");return e.replace(n,"$1")}function s(e){"P"===e.target.tagName&&w!==+e.target.dataset.i&&(w=+e.target.dataset.i,c())}function p(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 c(a){a=a||!1,!a&&k&&e.clearTimeout(k);var l,u;if(null!==M){if(a&&(M=p(M,N)),!M.length)return void(L.style.display="none");L.style.display="block";var s=L.getElementsByTagName("p");for(l=s.length-1;l>=0;l--)s[l].remove();b=[];var c,v,d=null,f=[];M.length||(E.value="");var m=N.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=M.length;u>l;l++){for(var y,x;null!==(y=g.exec(M[l][1]));)x=new RegExp("\\b"+o(y[0])+"\\b","gi"),d=y[y.length-1],void 0===d||x.test(r.value)||w===l&&f.push(d);c=n("p"),l===w&&c.classList.add("selected"),c.dataset.i=l,v=n("a",{innerHTML:i(M[l][1]),href:M[l][0]}),c.appendChild(v),h.appendChild(c),b.push(c)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(d=f[w%f.length],E.value=r.value+d):E.value=""}}function v(e){var t=e.parentNode;if("FORM"===t.nodeName)return t;if(null===t)throw"too deep. no parent form node to be found";return v(t)}function d(t){var n,a;if("tab"===t)E.value!==r.value&&(r.value=E.value+" "),r.value!==E.value&&g();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?b[n].classList.add("selected"):b[n].classList.remove("selected");c()}else if("enter"===t){if(!b.length)return v(r).submit(),!0;var l=b[w],u=l.getElementsByTagName("a")[0];r.value=E.value=u.textContent,L.style.display="none",e.location=u.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 E.value="",void(L.style.display="none");if(E.value.length&&(-1===E.value.indexOf(r.value.trim())&&(E.value=r.value),"block"===L.style.display&&(N=r.value.trim().split(/\s+/),k=e.setTimeout(function(){c(!0)},150))),w=0,X[r.value.trim()]){var t=X[r.value.trim()];N=t.terms,M=t.results,c()}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(X[n]&&!X[n].results.length)return void(X[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);X[r.value.trim()]=e,N=e.terms,M=e.results,c()}}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){E.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(j&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),j=!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;r.spellcheck=!1,r.autocomplete="off";var x=n("span",{className:"_ac-wrap"}),E=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),x.appendChild(E);var C=r.cloneNode(!0);x.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",s),x.appendChild(L),r.parentElement.insertBefore(x,r),r.parentNode.removeChild(r),r=C;var N,T,R,M=null,k=null,X={},j=!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
From cc9742d57b82aac37754474d8b522439f51c500d Mon Sep 17 00:00:00 2001
From: Peter Bengtsson
Date: Thu, 9 Jun 2016 07:21:55 -0400
Subject: [PATCH 7/7] don't actually select until the user uses tab or down
arrow
---
bower.json | 2 +-
public/dist/autocompeter.js | 26 ++++++++++++++++++--------
public/dist/autocompeter.min.js | 2 +-
src/autocompeter.js | 26 ++++++++++++++++++--------
templates/index.tmpl | 4 +++-
5 files changed, 41 insertions(+), 19 deletions(-)
diff --git a/bower.json b/bower.json
index cf24cef..29c8934 100644
--- a/bower.json
+++ b/bower.json
@@ -1,6 +1,6 @@
{
"name": "autocompeter",
- "version": "1.1.13",
+ "version": "1.2.0",
"homepage": "https://autocompeter.com",
"authors": [
"Peter Bengtsson "
diff --git a/public/dist/autocompeter.js b/public/dist/autocompeter.js
index 12bb8e9..1e47701 100644
--- a/public/dist/autocompeter.js
+++ b/public/dist/autocompeter.js
@@ -90,6 +90,7 @@
var results_ps = [];
var selected_pointer = 0;
+ var actually_selected_pointer = false;
q.spellcheck = false;
q.autocomplete = 'off';
@@ -215,7 +216,7 @@
}
p = createDomElement('p');
- if (i === selected_pointer) {
+ if (i === selected_pointer && actually_selected_pointer) {
p.classList.add('selected');
}
p.dataset.i = i; // needed by the onmouseover event handler
@@ -244,12 +245,13 @@
function findParentForm(element) {
var parent = element.parentNode;
+ if (parent === null) {
+ console.warn("Form can not be found. Nothing to submit");
+ return;
+ }
if (parent.nodeName === 'FORM') {
return parent;
}
- if (parent === null) {
- throw "too deep. no parent form node to be found";
- }
return findParentForm(parent);
}
@@ -262,6 +264,7 @@
if (q.value !== hint.value) {
handler(); // this starts a new ajax request
}
+ actually_selected_pointer = true;
} else if (name === 'down' || name === 'up') {
if (name === 'down') {
selected_pointer = Math.min(results_ps.length - 1, ++selected_pointer);
@@ -269,15 +272,16 @@
selected_pointer = Math.max(0, --selected_pointer);
}
for (i=0, len=results_ps.length; i < len; i++) {
- if (i === selected_pointer) {
+ if (i === selected_pointer && actually_selected_pointer) {
results_ps[i].classList.add('selected');
} else {
results_ps[i].classList.remove('selected');
}
}
+ actually_selected_pointer = true;
displayResults();
} else if (name === 'enter') {
- if (results_ps.length) {
+ if (results_ps.length && actually_selected_pointer) {
var p = results_ps[selected_pointer];
var a = p.getElementsByTagName('a')[0];
q.value = hint.value = a.textContent;
@@ -286,7 +290,10 @@
} else {
// We need to submit the form but we can't simply `return true`
// because the event we're returning to isn't a form submission.
- findParentForm(q).submit();
+ var form = findParentForm(q);
+ if (form) {
+ form.submit();
+ }
return true;
}
} else if (name === 'esc') {
@@ -338,8 +345,11 @@
}
}
- // new character, let's reset the selected_pointer
+ // New character, let's reset the selected_pointer
selected_pointer = 0;
+ // Also, reset that none of the results have been explicitly
+ // selected yet.
+ actually_selected_pointer = false;
if (cache[q.value.trim()]) {
var response = cache[q.value.trim()];
terms = response.terms;
diff --git a/public/dist/autocompeter.min.js b/public/dist/autocompeter.min.js
index a912fbc..edbbc98 100644
--- a/public/dist/autocompeter.min.js
+++ b/public/dist/autocompeter.min.js
@@ -1,2 +1,2 @@
/* Autocompeter.com 1.1.13 */
-!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 i(e){var t=N.map(o),n=new RegExp("\\b(("+t.join("|")+")[\\S']*)","gi");return e.replace(n,"$1")}function s(e){"P"===e.target.tagName&&w!==+e.target.dataset.i&&(w=+e.target.dataset.i,c())}function p(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 c(a){a=a||!1,!a&&k&&e.clearTimeout(k);var l,u;if(null!==M){if(a&&(M=p(M,N)),!M.length)return void(L.style.display="none");L.style.display="block";var s=L.getElementsByTagName("p");for(l=s.length-1;l>=0;l--)s[l].remove();b=[];var c,v,d=null,f=[];M.length||(E.value="");var m=N.map(o),g=new RegExp("\\b("+m.join("|")+")(\\w+)\\b","gi"),h=t.createDocumentFragment();for(l=0,u=M.length;u>l;l++){for(var y,x;null!==(y=g.exec(M[l][1]));)x=new RegExp("\\b"+o(y[0])+"\\b","gi"),d=y[y.length-1],void 0===d||x.test(r.value)||w===l&&f.push(d);c=n("p"),l===w&&c.classList.add("selected"),c.dataset.i=l,v=n("a",{innerHTML:i(M[l][1]),href:M[l][0]}),c.appendChild(v),h.appendChild(c),b.push(c)}L.appendChild(h),f.length&&" "!==r.value.charAt(r.value.length-1)?(d=f[w%f.length],E.value=r.value+d):E.value=""}}function v(e){var t=e.parentNode;if("FORM"===t.nodeName)return t;if(null===t)throw"too deep. no parent form node to be found";return v(t)}function d(t){var n,a;if("tab"===t)E.value!==r.value&&(r.value=E.value+" "),r.value!==E.value&&g();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?b[n].classList.add("selected"):b[n].classList.remove("selected");c()}else if("enter"===t){if(!b.length)return v(r).submit(),!0;var l=b[w],u=l.getElementsByTagName("a")[0];r.value=E.value=u.textContent,L.style.display="none",e.location=u.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 E.value="",void(L.style.display="none");if(E.value.length&&(-1===E.value.indexOf(r.value.trim())&&(E.value=r.value),"block"===L.style.display&&(N=r.value.trim().split(/\s+/),k=e.setTimeout(function(){c(!0)},150))),w=0,X[r.value.trim()]){var t=X[r.value.trim()];N=t.terms,M=t.results,c()}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(X[n]&&!X[n].results.length)return void(X[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);X[r.value.trim()]=e,N=e.terms,M=e.results,c()}}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){E.value=r.value,setTimeout(function(){L.style.display="none"},200),e.preventDefault()}function y(){if(j&&u.ping&&e.XMLHttpRequest){var t=new e.XMLHttpRequest;t.open("GET",u.url.split("?")[0]+"/ping"),t.send(),j=!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;r.spellcheck=!1,r.autocomplete="off";var x=n("span",{className:"_ac-wrap"}),E=n("input",{tabindex:-1,spellcheck:!1,autocomplete:"off",readonly:"readonly",type:r.type||"text",className:r.className+" _ac-hint"});r.classList.add("_ac-foreground"),x.appendChild(E);var C=r.cloneNode(!0);x.appendChild(C);var L=n("div",{className:"_ac-results"});a(L,"mouseover",s),x.appendChild(L),r.parentElement.insertBefore(x,r),r.parentNode.removeChild(r),r=C;var N,T,R,M=null,k=null,X={},j=!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
+!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
diff --git a/src/autocompeter.js b/src/autocompeter.js
index 12bb8e9..1e47701 100644
--- a/src/autocompeter.js
+++ b/src/autocompeter.js
@@ -90,6 +90,7 @@
var results_ps = [];
var selected_pointer = 0;
+ var actually_selected_pointer = false;
q.spellcheck = false;
q.autocomplete = 'off';
@@ -215,7 +216,7 @@
}
p = createDomElement('p');
- if (i === selected_pointer) {
+ if (i === selected_pointer && actually_selected_pointer) {
p.classList.add('selected');
}
p.dataset.i = i; // needed by the onmouseover event handler
@@ -244,12 +245,13 @@
function findParentForm(element) {
var parent = element.parentNode;
+ if (parent === null) {
+ console.warn("Form can not be found. Nothing to submit");
+ return;
+ }
if (parent.nodeName === 'FORM') {
return parent;
}
- if (parent === null) {
- throw "too deep. no parent form node to be found";
- }
return findParentForm(parent);
}
@@ -262,6 +264,7 @@
if (q.value !== hint.value) {
handler(); // this starts a new ajax request
}
+ actually_selected_pointer = true;
} else if (name === 'down' || name === 'up') {
if (name === 'down') {
selected_pointer = Math.min(results_ps.length - 1, ++selected_pointer);
@@ -269,15 +272,16 @@
selected_pointer = Math.max(0, --selected_pointer);
}
for (i=0, len=results_ps.length; i < len; i++) {
- if (i === selected_pointer) {
+ if (i === selected_pointer && actually_selected_pointer) {
results_ps[i].classList.add('selected');
} else {
results_ps[i].classList.remove('selected');
}
}
+ actually_selected_pointer = true;
displayResults();
} else if (name === 'enter') {
- if (results_ps.length) {
+ if (results_ps.length && actually_selected_pointer) {
var p = results_ps[selected_pointer];
var a = p.getElementsByTagName('a')[0];
q.value = hint.value = a.textContent;
@@ -286,7 +290,10 @@
} else {
// We need to submit the form but we can't simply `return true`
// because the event we're returning to isn't a form submission.
- findParentForm(q).submit();
+ var form = findParentForm(q);
+ if (form) {
+ form.submit();
+ }
return true;
}
} else if (name === 'esc') {
@@ -338,8 +345,11 @@
}
}
- // new character, let's reset the selected_pointer
+ // New character, let's reset the selected_pointer
selected_pointer = 0;
+ // Also, reset that none of the results have been explicitly
+ // selected yet.
+ actually_selected_pointer = false;
if (cache[q.value.trim()]) {
var response = cache[q.value.trim()];
terms = response.terms;
diff --git a/templates/index.tmpl b/templates/index.tmpl
index 4ebf025..3d62723 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -102,8 +102,10 @@
Autocompeter
A really fast AJAX autocomplete service and widget
-
+
+