2015-10-28 14:36:45 +03:00
|
|
|
/* jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
|
2015-08-26 23:19:33 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
import EmberSelectizeComponent from 'ember-cli-selectize/components/ember-selectize';
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {computed, isBlank, get, on, run} = Ember;
|
|
|
|
const emberA = Ember.A;
|
|
|
|
|
2015-08-26 23:19:33 +03:00
|
|
|
export default EmberSelectizeComponent.extend({
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
selectizeOptions: computed(function () {
|
|
|
|
let options = this._super(...arguments);
|
2015-10-26 19:02:28 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
options.onChange = run.bind(this, '_onChange');
|
2015-10-26 19:02:28 +03:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}),
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_dontOpenWhenBlank: on('didInsertElement', function () {
|
|
|
|
let openOnFocus = this.get('openOnFocus');
|
2015-09-01 11:53:10 +03:00
|
|
|
|
|
|
|
if (!openOnFocus) {
|
2015-10-28 14:36:45 +03:00
|
|
|
run.schedule('afterRender', this, function () {
|
|
|
|
let selectize = this._selectize;
|
2015-11-04 18:20:11 +03:00
|
|
|
if (selectize) {
|
|
|
|
selectize.on('dropdown_open', function () {
|
2015-10-28 14:36:45 +03:00
|
|
|
if (isBlank(selectize.$control_input.val())) {
|
2015-11-04 18:20:11 +03:00
|
|
|
selectize.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectize.on('type', function (filter) {
|
2015-10-28 14:36:45 +03:00
|
|
|
if (isBlank(filter)) {
|
2015-11-04 18:20:11 +03:00
|
|
|
selectize.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-09-01 11:53:10 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2015-08-26 23:19:33 +03:00
|
|
|
/**
|
|
|
|
* Event callback that is triggered when user creates a tag
|
|
|
|
* - modified to pass the caret position to the action
|
|
|
|
*/
|
2015-10-28 14:36:45 +03:00
|
|
|
_create(input, callback) {
|
|
|
|
let caret = this._selectize.caretPos;
|
2015-08-26 23:19:33 +03:00
|
|
|
|
|
|
|
// Delete user entered text
|
|
|
|
this._selectize.setTextboxValue('');
|
|
|
|
// Send create action
|
|
|
|
|
|
|
|
// allow the observers and computed properties to run first
|
2015-10-28 14:36:45 +03:00
|
|
|
run.schedule('actions', this, function () {
|
2015-08-26 23:19:33 +03:00
|
|
|
this.sendAction('create-item', input, caret);
|
|
|
|
});
|
|
|
|
// We cancel the creation here, so it's up to you to include the created element
|
|
|
|
// in the content and selection property
|
|
|
|
callback(null);
|
2015-09-01 19:34:31 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_addSelection(obj) {
|
|
|
|
let _valuePath = this.get('_valuePath');
|
|
|
|
let val = get(obj, _valuePath);
|
|
|
|
let caret = this._selectize.caretPos;
|
2015-09-01 19:34:31 +03:00
|
|
|
|
|
|
|
// caret position is always 1 more than the desired index as this method
|
|
|
|
// is called after selectize has inserted the item and the caret has moved
|
|
|
|
// to the right
|
|
|
|
caret = caret - 1;
|
|
|
|
|
|
|
|
this.get('selection').insertAt(caret, obj);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
run.schedule('actions', this, function () {
|
2015-09-01 19:34:31 +03:00
|
|
|
this.sendAction('add-item', obj);
|
|
|
|
this.sendAction('add-value', val);
|
|
|
|
});
|
2015-10-26 19:02:28 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
_onChange(args) {
|
|
|
|
let selection = Ember.get(this, 'selection');
|
|
|
|
let valuePath = Ember.get(this, '_valuePath');
|
2015-10-26 19:02:28 +03:00
|
|
|
|
|
|
|
if (!args || !selection || !Ember.isArray(selection) || args.length !== selection.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hasNoChanges = selection.every(function (obj, idx) {
|
|
|
|
return Ember.get(obj, valuePath) === args[idx];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hasNoChanges) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
let reorderedSelection = emberA([]);
|
2015-10-26 19:02:28 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
args.forEach((value) => {
|
|
|
|
let obj = selection.find(function (item) {
|
|
|
|
// jscs:disable
|
|
|
|
return (get(item, valuePath) + '') === value;
|
|
|
|
// jscs:enable
|
2015-10-26 19:02:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
reorderedSelection.addObject(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('selection', reorderedSelection);
|
2015-08-26 23:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|