-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #111 from italia/feature/105-autocomplete
Feature/105 autocomplete
- Loading branch information
Showing
16 changed files
with
380 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
|
||
$.fn.autocomplete = function (options) { | ||
|
||
// Default options | ||
var defaults = { | ||
data: {} | ||
}; | ||
|
||
var ENTER_CHAR_CODE = 13; | ||
|
||
// Get options | ||
options = $.extend(defaults, options); | ||
|
||
return this.each(function () { | ||
|
||
// text input | ||
var $input = $(this); | ||
var $autocomplete = void 0; | ||
|
||
// assign data from options | ||
var data = eval($(this).attr("data-db")); | ||
|
||
if (Object.keys(data).length) { | ||
|
||
$autocomplete = $('<ul class="autocomplete-wrap"></ul>'); | ||
|
||
$autocomplete.insertAfter($(this)); | ||
}; | ||
|
||
// add button clear | ||
$input.after('<button class="autocomplete-clear"><svg fill="#000000" height="24" viewBox="0 0 24 24" width="24" xmlns="https://www.w3.org/2000/svg"><path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" /><path d="M0 0h24v24H0z" fill="none" /></svg></button>'); | ||
|
||
// Listen if key was pressed | ||
$input.on('keyup', function (e) { | ||
|
||
// get value from input | ||
var q = $input.val(); | ||
|
||
$autocomplete.empty(); | ||
|
||
// check if input isn't empty | ||
if (q.length) { | ||
|
||
for (var item in data) { | ||
|
||
// check if item contains value that we're looking for | ||
if (data[item].toLowerCase().indexOf(q.toLowerCase()) !== -1) { | ||
var option = $('<li>' + data[item] + '</li>'); | ||
|
||
$autocomplete.append(option); | ||
} | ||
} | ||
} | ||
|
||
if (e.which === ENTER_CHAR_CODE) { | ||
$autocomplete.children(":first").trigger('click'); | ||
$autocomplete.empty(); | ||
} | ||
|
||
if (q.length === 0) { | ||
$('.autocomplete-clear').css('visibility', 'hidden'); | ||
} else { | ||
$('.autocomplete-clear').css('visibility', 'visible'); | ||
} | ||
}); | ||
|
||
$autocomplete.on('click', 'li', function () { | ||
|
||
// Set input value after click | ||
$input.val($(this).text()); | ||
|
||
// Clear autocomplete | ||
$autocomplete.empty(); | ||
}); | ||
|
||
$('.autocomplete-clear').on('click', function (e) { | ||
e.preventDefault(); | ||
$input.val(''); | ||
$(this).css('visibility', 'hidden'); | ||
$autocomplete.empty(); | ||
$(this).parent().find('label').removeClass('active'); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.