Skip to content

Instantly share code, notes, and snippets.

@jameshoward
Created August 16, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jameshoward/237bcae7e8929d4b7e0e418792d5f06e to your computer and use it in GitHub Desktop.
Save jameshoward/237bcae7e8929d4b7e0e418792d5f06e to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
import XSelectComponent from './x-select';
var isArray = Ember.isArray;
/**
* Used to wrap a native `<option>` tag and associate an object with
* it that can be bound. It can only be used in conjuction with a
* containing `x-select` component
*
* @class Ember.XOptionComponent
* @extends Ember.Component
*/
export default Ember.Component.extend({
tagName: 'option',
attributeBindings: ['selected', 'name', 'disabled', 'value', 'title'],
classNameBindings: [':x-option'],
/**
* The value associated with this option. When this option is
* selected, the `x-select` will fire its action with this
* value.
*
* @property value
* @type Object
* @default null
*/
value: null,
/**
* Property bound to the `selected` attribute of the native
* `<option>` element. It is aware of the containing `x-select`'s
* value and will mark itself if it is the same.
*
* @private
* @property selected
* @type Boolean
*/
selected: Ember.computed('value', 'select.value', 'select.multiple', function() {
if (this.get('select.multiple') && isArray(this.get('select.value'))) {
let selectValue = Ember.A(this.get('select.value'));
return selectValue.contains(this.get('value'));
} else {
return this.get('value') === this.get('select.value');
}
}),
/**
* Register this x-option with the containing `x-select`
*
* @override
*/
didInsertElement() {
this._super.apply(this, arguments);
Ember.run.scheduleOnce('afterRender', this, 'registerWithXSelect');
},
select: Ember.computed(function() {
return this.nearestOfType(XSelectComponent);
}),
registerWithXSelect() {
const select = this.get('select');
Ember.assert("x-option component declared without enclosing x-select", !!select);
select.registerOption(this);
},
/**
* Unregister this x-option with its containing x-select.
*
* @override
*/
willDestroyElement: function() {
this._super.apply(this, arguments);
let select = this.get('select');
if(select) {
select.unregisterOption(this);
}
}
});
import Ember from 'ember';
var isArray = Ember.isArray;
/**
* Wraps a native <select> element so that it can be object and
* binding aware. It is used in conjuction with the
* `x-option` component to construct select boxes. E.g.
*
* {{#x-select value="bob" action="selectPerson"}}
* {{x-option value="fred"}}Fred Flintstone{{/x-option}}
* {{x-option value="bob"}}Bob Newhart{{/x-option}}
* {{/x-select}}
*
* the options are always up to date, so that when the object bound to
* `value` changes, the corresponding option becomes selected.
*
* Whenever the select tag receives a change event, it will fire
* `action`
*
* @class Ember.XSelectComponent
* @extends Ember.Component
*/
export default Ember.Component.extend({
tagName: "select",
classNameBindings: [":x-select"],
attributeBindings: ['disabled', 'tabindex', 'multiple', 'form', 'name', 'autofocus', 'required', 'size', 'title'],
/**
* Bound to the `disabled` attribute on the native <select> tag.
*
* @property disabled
* @type Boolean
* @default null
*/
disabled: null,
/**
* Bound to the `multiple` attribute on the native <select> tag.
*
* @property multiple
* @type Boolean
* @default null
*/
multiple: null,
/**
* Bound to the `tabindex` attribute on the native <select> tag.
*
* @property tabindex
* @type Integer
* @ default 0
*/
tabindex: 0,
/**
* The collection of options for this select box. When options are
* inserted into the dom, they will register themselves with their
* containing `x-select`. This is for internal book-keeping only and should
* not be changed from outside.
*
* @private
* @property options
*/
options: Ember.computed(function() {
return Ember.A();
}),
/**
* When the select DOM event fires on the element, trigger the
* component's action with the current value.
*/
change(event) {
this._updateValue();
this.sendAction('action', this.get('value'), this);
this.sendAction('onchange', this, this.get('value'), event);
},
/**
* When the click DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
click(event) {
this.sendAction('onclick', this, this.get('value'), event);
},
/**
* When the blur DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
blur(event) {
this.sendAction('onblur', this, this.get('value'), event);
},
/**
* When the focusOut DOM event fires on the element, trigger the
* component's action with the component, x-select value, and the jQuery event.
*/
focusOut(event) {
this.sendAction('onfocusout', this, this.get('value'), event);
},
/**
* Updates `value` with the object associated with the selected option tag
*
* @private
*/
_updateValueSingle: function(){
var option = this.get('options').find(function(option) {
return option.$().is(':selected');
});
if (option) {
this.set('value', option.get('value'));
} else {
this.set('value', null);
}
},
/**
* Updates `value` with an array of objects associated with the selected option tags
*
* @private
*/
_updateValueMultiple: function() {
var options = this.get('options').filter(function(option) {
return option.$().is(':selected');
});
this.set('value', Ember.A(options).mapBy('value'));
},
/**
* A utility method to determine if the select is multiple or single and call
* its respective method to update the value.
*
* @private
* @utility
*/
_updateValue: function() {
if (this.isDestroying || this.isDestroyed) {
return;
}
if (this.get('multiple')) {
this._updateValueMultiple();
} else {
this._updateValueSingle();
}
},
/**
* If no explicit value is set, apply default values based on selected=true in
* the template.
*
* @private
*/
_setDefaultValues: function() {
if (this.get('value') == null) {
this._updateValue();
}
},
/**
* @override
*/
didInsertElement() {
this._super.apply(this, arguments);
this.$().on('blur', (event) => {
this.blur(event);
});
},
/**
* @override
*/
willDestroyElement: function() {
this._super.apply(this, arguments);
// might be overkill, but make sure options can get gc'd
this.get('options').clear();
this.$().off('blur');
},
/**
* If this is a multi-select, and the value is not an array, that
* probably indicates a misconfiguration somewhere, so we error out.
*
* @private
*/
ensureProperType: Ember.on('init', Ember.observer('value', function() {
let value = this.get('value');
if (value != null && this.get('multiple') && !isArray(value)) {
throw new Error(`x-select multiple=true was set, but value ${value} is not enumerable.`);
}
})),
/**
* @private
*/
registerOption: function(option) {
this.get('options').addObject(option);
this._setDefaultValues();
},
/**
* @private
*/
unregisterOption: function(option) {
this.get('options').removeObject(option);
// We don't want to update the value if we're tearing the component down.
if (!this.get('isDestroying')) {
this._updateValue();
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
options: [
{ value: 'A', label: 'Option A', disabled: true },
{ value: 'B', label: 'Option B' },
{ value: 'C', label: 'Option C', disabled: true },
{ value: 'D', label: 'Option D' }
],
actions: {
change() {
this.set('options.1.disabled', true);
}
}
});
{{#x-select as |xs|}}
{{#each options as |option|}}
{{#xs.option value=option.value disabled=option.disabled}}{{option.label}}{{/xs.option}}
{{/each}}
{{/x-select}}
<button {{action 'change'}}>Disable Option B</button>
{{yield
(hash
option=(component "x-option" select=this)
)
}}
{
"version": "0.10.4",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.7.0",
"ember-data": "2.7.0",
"ember-template-compiler": "2.7.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment