2020-12-09 22:32:31 +03:00
|
|
|
import Component from '@glimmer/component';
|
|
|
|
import {action} from '@ember/object';
|
2020-02-14 12:34:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-12-09 22:32:31 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
export default class GhMemberLabelInput extends Component {
|
2021-07-15 17:27:29 +03:00
|
|
|
@service store;
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2021-07-15 17:27:29 +03:00
|
|
|
@tracked selectedLabels = [];
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
get availableLabels() {
|
|
|
|
return this._availableLabels.toArray().sort((labelA, labelB) => {
|
|
|
|
return labelA.name.localeCompare(labelB.name, undefined, {ignorePunctuation: true});
|
|
|
|
});
|
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2021-07-15 17:27:29 +03:00
|
|
|
get availableLabelNames() {
|
|
|
|
return this.availableLabels.map(label => label.name.toLowerCase());
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2020-02-14 12:34:01 +03:00
|
|
|
// perform a background query to fetch all users and set `availableLabels`
|
|
|
|
// to a live-query that will be immediately populated with what's in the
|
|
|
|
// store and be updated when the above query returns
|
|
|
|
this.store.query('label', {limit: 'all'});
|
2020-12-09 22:32:31 +03:00
|
|
|
this._availableLabels = this.store.peekAll('label');
|
|
|
|
this.selectedLabels = this.args.labels || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
willDestroy() {
|
2021-07-15 17:27:29 +03:00
|
|
|
super.willDestroy?.(...arguments);
|
2020-12-09 22:32:31 +03:00
|
|
|
this._availableLabels.forEach((label) => {
|
2020-06-09 08:42:55 +03:00
|
|
|
if (label.get('isNew')) {
|
|
|
|
this.store.deleteRecord(label);
|
|
|
|
}
|
|
|
|
});
|
2020-12-09 22:32:31 +03:00
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
@action
|
|
|
|
hideCreateOptionOnMatchingLabel(term) {
|
|
|
|
return !this.availableLabelNames.includes(term.toLowerCase());
|
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
@action
|
|
|
|
updateLabels(newLabels) {
|
|
|
|
let currentLabels = this.selectedLabels;
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// destroy new+unsaved labels that are no longer selected
|
|
|
|
currentLabels.forEach(function (label) {
|
|
|
|
if (!newLabels.includes(label) && label.get('isNew')) {
|
|
|
|
label.destroyRecord();
|
|
|
|
}
|
|
|
|
});
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// update labels
|
|
|
|
this.selectedLabels = newLabels;
|
|
|
|
this.args.onChange(newLabels);
|
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
@action
|
|
|
|
createLabel(labelName) {
|
|
|
|
let currentLabels = this.selectedLabels;
|
|
|
|
let currentLabelNames = currentLabels.map(label => label.get('name').toLowerCase());
|
|
|
|
let labelToAdd;
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
labelName = labelName.trim();
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// abort if label is already selected
|
|
|
|
if (currentLabelNames.includes(labelName.toLowerCase())) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// find existing label if there is one
|
|
|
|
labelToAdd = this._findLabelByName(labelName);
|
2020-02-14 12:34:01 +03:00
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// create new label if no match
|
|
|
|
if (!labelToAdd) {
|
|
|
|
labelToAdd = this.store.createRecord('label', {
|
|
|
|
name: labelName
|
|
|
|
});
|
2020-02-14 12:34:01 +03:00
|
|
|
}
|
|
|
|
|
2020-12-09 22:32:31 +03:00
|
|
|
// push label onto member relationship
|
|
|
|
currentLabels.pushObject(labelToAdd);
|
|
|
|
this.args.onChange(currentLabels);
|
|
|
|
}
|
2020-02-14 12:34:01 +03:00
|
|
|
|
|
|
|
_findLabelByName(name) {
|
|
|
|
let withMatchingName = function (label) {
|
|
|
|
return label.name.toLowerCase() === name.toLowerCase();
|
|
|
|
};
|
|
|
|
return this.availableLabels.find(withMatchingName);
|
|
|
|
}
|
2020-12-09 22:32:31 +03:00
|
|
|
}
|