Created
January 12, 2016 09:26
-
-
Save Thanood/69bc22bb3fd80193caa0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<autocomplete data.two-way="filterValues" selected.bind="selectedFilter> | |
</autocomplete> | |
This file contains hidden or 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
<template> | |
<label for.bind="inputId">${label}</label> | |
<input name.bind="inputId" | |
id.bind="inputId" | |
selected.two-way="selectedFilter"> | |
</template> |
This file contains hidden or 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
import {bindable, inject} from 'aurelia-framework'; | |
import _ from 'lodash'; | |
import * as Awe from 'awesomplete'; //eslint-disable-line | |
const SELECT_COMPLETE = 'awesomplete-selectcomplete'; | |
export class AwesompleteFactory { | |
create(input) { | |
const awe = new Awesomplete(input); | |
// monkey patch Awesomplete to add toggle function | |
awe.toggle = function () { | |
if (this.ul.childNodes.length === 0) { | |
this.minChars = 0; | |
this.evaluate(); | |
} else if (this.ul.hasAttribute('hidden')) { | |
this.evaluate(); | |
this.open(); | |
} else { | |
this.evaluate(); | |
this.close(); | |
} | |
}; | |
return awe; | |
} | |
} | |
@inject(AwesompleteFactory) | |
export class Autocomplete { | |
@bindable data; | |
input; | |
inputId; | |
awe; | |
constructor(awesompleteFactory) { | |
this.inputId = _.uniqueId('input-'); | |
this.awesompleteFactory = awesompleteFactory; | |
} | |
listener(e) { | |
this.selected = e.target.value; | |
e.target.value = null; | |
} | |
attached() { | |
this.input = document.getElementById(this.inputId); | |
this.input.placeholder = 'Select filter...'; | |
this.awe = this.awesompleteFactory.create(this.input); | |
this.awe.list = this.data; | |
this.input.addEventListener(SELECT_COMPLETE, this.listener); | |
} | |
detached() { | |
this.input.removeEventListener(SELECT_COMPLETE, this.listener); | |
} | |
toggle() { | |
this.input.value = ''; | |
this.awe.toggle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment