Skip to content

Instantly share code, notes, and snippets.

@Thanood
Created June 3, 2016 13:43
Show Gist options
  • Save Thanood/9215cb0cefe0e1701d9c095f828f0676 to your computer and use it in GitHub Desktop.
Save Thanood/9215cb0cefe0e1701d9c095f828f0676 to your computer and use it in GitHub Desktop.
Aurelia - Materialize disabling select
<template>
<require from="./md-x-select"></require>
<select md-x-select="disabled.bind: disabled" value.two-way="selectedValue">
<option value="" disabled selected>select an item</option>
<option value="item1">item 1</option>
<option value="item2">item 2</option>
<option value="item3">item 3</option>
<option value="item4">item 4</option>
</select>
<div>${selectedValue}</div>
<button click.delegate="toggle()">toggle</button>
</template>
export class App {
selectedValue = "";
disabled = false;
toggle() {
this.disabled = !this.disabled;
}
}
<!doctype html>
<html>
<head>
<title>Aurelia</title>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body aurelia-app="main">
<h1>Loading...</h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.6/system.js"></script>
<script src="https://rawgit.com/aurelia-ui-toolkits/aurelia-materialize-bundles/0.1.1/config2.js"></script>
<script>
System.import('aurelia-bootstrapper');
</script>
</body>
</html>
/*******************************************************************************
* The following two lines enable async/await without using babel's
* "runtime" transformer. Uncomment the lines if you intend to use async/await.
*
* More info here: https://github.com/jdanyow/aurelia-plunker/issues/2
*/
//import regeneratorRuntime from 'babel-runtime/regenerator';
//window.regeneratorRuntime = regeneratorRuntime;
/******************************************************************************/
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-materialize-bridge', bridge => bridge.useAll() );
aurelia.start().then(a => a.setRoot());
}
import { bindable, customAttribute } from 'aurelia-templating';
import { ObserverLocator } from 'aurelia-binding';
import { inject } from 'aurelia-dependency-injection';
import { TaskQueue } from 'aurelia-task-queue';
import * as LogManager from 'aurelia-logging';
function fireEvent(element: Element, name: string, data? = {}) {
let event = new CustomEvent(name, {
detail: data,
bubbles: true
});
element.dispatchEvent(event);
return event;
}
@inject(Element, LogManager, ObserverLocator, TaskQueue)
@customAttribute('md-x-select')
export class MdSelect {
@bindable() disabled = false;
_suspendUpdate = false;
constructor(element, logManager, observerLocator, taskQueue) {
this.element = element;
this.taskQueue = taskQueue;
this.handleChangeFromViewModel = this.handleChangeFromViewModel.bind(this);
this.handleChangeFromNativeSelect = this.handleChangeFromNativeSelect.bind(this);
this.log = LogManager.getLogger('md-select');
this.observerLocator = observerLocator;
this.valueObserver = this.observerLocator.getObserver(this.element, 'value');
}
attached() {
this.valueObserver.subscribe(this.handleChangeFromViewModel);
// $(this.element).material_select(() => {
// this.log.warn('materialize callback', $(this.element).val());
// this.handleChangeFromNativeSelect();
// });
$(this.element).material_select();
$(this.element).on('change', this.handleChangeFromNativeSelect);
}
detached() {
$(this.element).off('change', this.handleChangeFromNativeSelect);
$(this.element).material_select('destroy');
this.valueObserver.unsubscribe();
}
refresh() {
this.taskQueue.queueTask(() => {
$(this.element).material_select('destroy');
$(this.element).material_select();
});
}
disabledChanged(newValue) {
let $wrapper = $(this.element).parent('.select-wrapper');
if ($wrapper.length > 0) {
if (newValue) {
$('.caret', $wrapper).addClass('disabled');
$('input.select-dropdown', $wrapper).attr('disabled', 'disabled');
$wrapper.attr('disabled', 'disabled');
} else {
$('.caret', $wrapper).removeClass('disabled');
$('input.select-dropdown', $wrapper).attr('disabled', null);
$wrapper.attr('disabled', null);
$('.select-dropdown', $wrapper).dropdown({'hover': false, 'closeOnClick': false});
}
}
}
handleChangeFromNativeSelect() {
// Aurelia's select observer doesn't get noticed when something changes the
// select value directly (this.element.value = "something"). So we trigger
// the change here.
// this.valueObserver.value = $(this.element).val();
// this.valueObserver.synchronizeValue();
// this.valueObserver.synchronizeOptions();
// this._suspendUpdate = true;
// this.valueObserver.notify();
// this._suspendUpdate = false;
if (!this._suspendUpdate) {
this.log.debug('handleChangeFromNativeSelect', this.element.value, $(this.element).val());
this._suspendUpdate = true;
fireEvent(this.element, 'change');
this.log.debug('this.valueObserver.value', this.valueObserver.value);
// this.valueObserver.value = $(this.element).val();
// this.valueObserver.notify();
this._suspendUpdate = false;
}
}
handleChangeFromViewModel(newValue) {
this.log.debug('handleChangeFromViewModel', newValue, $(this.element).val());
if (!this._suspendUpdate) {
$(this.element).material_select();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment