Skip to content
This repository has been archived by the owner on Apr 20, 2019. It is now read-only.

Option to not select first one by default #13

Merged
merged 7 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ services:
env:
- REDISDATABASE=8

branches:
only:
- master

script:
- go test -v ./...
- ./travis-e2e.sh
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "autocompeter",
"version": "1.1.12",
"version": "1.2.0",
"homepage": "https://autocompeter.com",
"authors": [
"Peter Bengtsson <mail@peterbe.com>"
Expand Down
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'));
});
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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-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-rename": "1.2.2",
"gulp-sass": "2.3.1",
"gulp-uglify": "1.5.3",
"jshint": "2.9.2",
"node-sass": "2.1.1"
}
}
44 changes: 36 additions & 8 deletions public/dist/autocompeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

var results_ps = [];
var selected_pointer = 0;
var actually_selected_pointer = false;
q.spellcheck = false;
q.autocomplete = 'off';

Expand Down Expand Up @@ -177,6 +178,7 @@
}
} else {
r.style.display = 'none';
return;
}
results_ps = [];
var p, a;
Expand Down Expand Up @@ -214,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
Expand Down Expand Up @@ -243,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);
}

Expand All @@ -261,22 +264,24 @@
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);
} else if (name === 'up') {
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;
Expand All @@ -285,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') {
Expand Down Expand Up @@ -337,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;
Expand All @@ -355,6 +366,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) {
Expand Down
2 changes: 1 addition & 1 deletion public/dist/autocompeter.min.css
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
4 changes: 2 additions & 2 deletions public/dist/autocompeter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 36 additions & 8 deletions src/autocompeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

var results_ps = [];
var selected_pointer = 0;
var actually_selected_pointer = false;
q.spellcheck = false;
q.autocomplete = 'off';

Expand Down Expand Up @@ -177,6 +178,7 @@
}
} else {
r.style.display = 'none';
return;
}
results_ps = [];
var p, a;
Expand Down Expand Up @@ -214,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
Expand Down Expand Up @@ -243,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);
}

Expand All @@ -261,22 +264,24 @@
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);
} else if (name === 'up') {
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;
Expand All @@ -285,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') {
Expand Down Expand Up @@ -337,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;
Expand All @@ -355,6 +366,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) {
Expand Down
Loading