Skip to content

Instantly share code, notes, and snippets.

@cb-fred
Forked from jdanyow/app.html
Created February 23, 2017 10:32
Show Gist options
  • Save cb-fred/5ff5322b7558a3a8e252db2bc43b0b10 to your computer and use it in GitHub Desktop.
Save cb-fred/5ff5322b7558a3a8e252db2bc43b0b10 to your computer and use it in GitHub Desktop.
Aurelia Gist
<template>
<h1>${message}</h1>
</template>
export class App {
message = 'Hello World!';
}
<template bindable='model, view'>
<div class='cb-search-box' class.bind='input ? "not-empty": ""'>
<input class='form-control'
type='text'
value.bind='input'
focus.bind='hasFocus'
t='[placeholder]lbl-search-fidu'
placeholder='Fiduciary'>
<span class='cb-search-box-icon'>
<i if.bind='!view.reloading' class='fa fa-search'></i>
<i if.bind='view.reloading' class='fa fa-spin fa-gear'></i>
</span>
<div if.bind='!selected && hasFocusDelayed' class='cb-search-autocomplete'>
<template if.bind='results.length'>
${results.length}
<template repeat.for='result of results'>
<div class='cb-search-auto-item'
click.delegate='selectOption(result)'>
${result}
</div>
</template>
</template>
<template if.bind='!results.length'>
<div class='cb-search-auto-item' t='lbl-no-results'>lbl-no-results</div>
</template>
</div>
</div>
</template>
import {inject, customElement, containerless, bindable, bindingMode} from 'aurelia-framework';
import * as utils from '../utils';
import {ObserverLocator} from 'aurelia-binding';
@customElement('autosearch')
@containerless()
@inject(ObserverLocator)
export class Autosearch {
/* The parent search viewmodel */
@bindable view;
/*
* The search filter model, watched by the parent search view,
* modified by this view.
* {
* options: ['foo','bar','xyz'], <- available values
* selected: null <- user selected value
* }
*/
@bindable({ defaultBindingMode: bindingMode.twoWay }) model;
constructor(obsLoc, ...args) {
this.input = '';
this.results = [];
this.obsLoc = obsLoc;
this.selected = true; // has the user picked his desired value ?
this.hasFocus = false; // raw input focus
this.hasFocusDelayed = false; // input focus with a delay before blur.
obsLoc.getObserver(this,'input')
.subscribe(() => {
this.makeResults();
});
obsLoc.getObserver(this,'hasFocus')
.subscribe(() => {
if (this.hasFocus) {
this.hasFocusDelayed = true;
} else {
setTimeout(() => {
this.hasFocusDelayed = false;
},150);
}
});
}
attached() {
this.input = this.model.selected || '';
}
makeResults() { // search input in options.
if (this.input == this.model.selected) {
return;
}
this.selected = false;
var results = [];
var pattern = this.input.toLowerCase();
for (var option of this.model.options) {
var value = option.toLowerCase();
if (value.indexOf(pattern) >= 0) {
results.push(option);
}
}
utils.setArray(this.results, results);
}
selectOption(value) {
this.model.selected = value;
this.input = value;
this.selected = true;
utils.setArray(this.results, []); // Necessary to prevent a crash in aurelia templating. No idea why.
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app>
<h1>Loading...</h1>
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script>
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script>
<script>
require(['aurelia-bootstrapper']);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment