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-12-15 14:09:34 +03:00
|
|
|
const {computed, isArray, isBlank, get, run} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
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-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');
|
2016-01-06 15:20:50 +03:00
|
|
|
let reorderedSelection = emberA([]);
|
2015-10-26 19:02:28 +03:00
|
|
|
|
2016-01-06 15:20:50 +03:00
|
|
|
if (!args || !selection || !isArray(selection) || args.length !== get(selection, 'length')) {
|
2015-10-26 19:02:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:20:50 +03:00
|
|
|
// exit if we're not dealing with the same objects as the selection
|
|
|
|
let objectsHaveChanged = selection.any(function (obj) {
|
|
|
|
return args.indexOf(get(obj, valuePath)) === -1;
|
2015-10-26 19:02:28 +03:00
|
|
|
});
|
|
|
|
|
2016-01-06 15:20:50 +03:00
|
|
|
if (objectsHaveChanged) {
|
2015-10-26 19:02:28 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:20:50 +03:00
|
|
|
// exit if the order is still the same
|
|
|
|
let orderIsSame = selection.every(function (obj, idx) {
|
|
|
|
return get(obj, valuePath) === args[idx];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (orderIsSame) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-26 19:02:28 +03:00
|
|
|
|
2016-01-06 15:20:50 +03:00
|
|
|
// we have a re-order, update the selection
|
2015-10-28 14:36:45 +03:00
|
|
|
args.forEach((value) => {
|
|
|
|
let obj = selection.find(function (item) {
|
2015-12-15 14:09:34 +03:00
|
|
|
return `${get(item, valuePath)}` === value;
|
2015-10-26 19:02:28 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (obj) {
|
|
|
|
reorderedSelection.addObject(obj);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set('selection', reorderedSelection);
|
2015-12-15 14:09:34 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_preventOpeningWhenBlank() {
|
|
|
|
let openOnFocus = this.get('openOnFocus');
|
|
|
|
|
|
|
|
if (!openOnFocus) {
|
|
|
|
run.schedule('afterRender', this, function () {
|
|
|
|
let selectize = this._selectize;
|
|
|
|
if (selectize) {
|
|
|
|
selectize.on('dropdown_open', function () {
|
|
|
|
if (isBlank(selectize.$control_input.val())) {
|
|
|
|
selectize.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
selectize.on('type', function (filter) {
|
|
|
|
if (isBlank(filter)) {
|
|
|
|
selectize.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this._preventOpeningWhenBlank();
|
2015-08-26 23:19:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|