2017-11-21 18:53:09 +08:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2018-01-24 18:48:20 +08:00
|
|
|
import { default as computed, on } from 'ember-addons/ember-computed-decorators';
|
2018-03-22 18:29:55 +08:00
|
|
|
const { get, isNone, isEmpty, isPresent, run, makeArray } = Ember;
|
|
|
|
|
|
|
|
import {
|
|
|
|
applyOnSelectPluginApiCallbacks,
|
|
|
|
} from "select-kit/mixins/plugin-api";
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
export default SelectKitComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["single-select"],
|
2018-02-26 18:42:57 +08:00
|
|
|
layoutName: "select-kit/templates/components/single-select",
|
2017-11-21 18:53:09 +08:00
|
|
|
classNames: "single-select",
|
|
|
|
computedValue: null,
|
|
|
|
value: null,
|
2017-11-24 20:15:02 +08:00
|
|
|
allowInitialValueMutation: false,
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_compute() {
|
2017-11-28 02:50:04 +08:00
|
|
|
run.scheduleOnce("afterRender", () => {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.willComputeAttributes();
|
2018-02-26 18:42:57 +08:00
|
|
|
let content = this.get("content") || [];
|
|
|
|
let asyncContent = this.get("asyncContent") || [];
|
|
|
|
content = this.willComputeContent(content);
|
|
|
|
asyncContent = this.willComputeAsyncContent(asyncContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
let value = this._beforeWillComputeValue(this.get("value"));
|
|
|
|
content = this.computeContent(content);
|
2018-02-26 18:42:57 +08:00
|
|
|
asyncContent = this.computeAsyncContent(asyncContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
content = this._beforeDidComputeContent(content);
|
2018-02-26 18:42:57 +08:00
|
|
|
asyncContent = this._beforeDidComputeAsyncContent(asyncContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
value = this.willComputeValue(value);
|
|
|
|
value = this.computeValue(value);
|
|
|
|
value = this._beforeDidComputeValue(value);
|
|
|
|
this.didComputeContent(content);
|
2018-02-26 18:42:57 +08:00
|
|
|
this.didComputeAsyncContent(asyncContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
this.didComputeValue(value);
|
|
|
|
this.didComputeAttributes();
|
2017-11-23 21:39:26 +08:00
|
|
|
|
|
|
|
if (this.get("allowInitialValueMutation")) this.mutateAttributes();
|
2017-11-21 18:53:09 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-22 17:34:12 +08:00
|
|
|
mutateAttributes() {
|
|
|
|
run.next(() => {
|
2018-03-22 18:29:55 +08:00
|
|
|
if (this.get("isDestroyed") || this.get("isDestroying")) return;
|
|
|
|
|
2017-11-22 17:34:12 +08:00
|
|
|
this.mutateContent(this.get("computedContent"));
|
|
|
|
this.mutateValue(this.get("computedValue"));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
mutateContent() {},
|
|
|
|
mutateValue(computedValue) {
|
|
|
|
this.set("value", computedValue);
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
_beforeWillComputeValue(value) {
|
2018-01-24 18:48:20 +08:00
|
|
|
if (!isEmpty(this.get("content")) &&
|
|
|
|
isEmpty(value) &&
|
|
|
|
isNone(this.get("none")) &&
|
|
|
|
this.get("allowAutoSelectFirst")) {
|
|
|
|
value = this.valueForContentItem(get(this.get("content"), "firstObject"));
|
2017-11-24 20:15:02 +08:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
switch (typeof value) {
|
|
|
|
case "string":
|
|
|
|
case "number":
|
|
|
|
return this._castInteger(value === "" ? null : value);
|
|
|
|
default:
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
willComputeValue(value) { return value; },
|
|
|
|
computeValue(value) { return value; },
|
|
|
|
_beforeDidComputeValue(value) {
|
|
|
|
this.setProperties({ computedValue: value });
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
didComputeValue(value) { return value; },
|
|
|
|
|
|
|
|
filterComputedContent(computedContent, computedValue, filter) {
|
|
|
|
const lowerFilter = filter.toLowerCase();
|
|
|
|
return computedContent.filter(c => {
|
|
|
|
return get(c, "name").toLowerCase().indexOf(lowerFilter) > -1;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
computeHeaderContent() {
|
2017-11-21 18:53:09 +08:00
|
|
|
return {
|
2017-12-22 20:08:12 +08:00
|
|
|
title: this.get("title"),
|
2018-03-22 18:29:55 +08:00
|
|
|
icons: makeArray(this.getWithDefault("headerIcon", [])),
|
|
|
|
value: this.get("selection.value"),
|
|
|
|
name: this.get("selection.name") || this.get("noneRowComputedContent.name")
|
2017-11-21 18:53:09 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
@computed("computedAsyncContent.[]", "computedValue")
|
|
|
|
filteredAsyncComputedContent(computedAsyncContent, computedValue) {
|
|
|
|
computedAsyncContent = computedAsyncContent.filter(c => {
|
|
|
|
return computedValue !== get(c, "value");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.get("limitMatches")) {
|
|
|
|
return computedAsyncContent.slice(0, this.get("limitMatches"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return computedAsyncContent;
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
@computed("computedContent.[]", "computedValue", "filter", "shouldFilter")
|
|
|
|
filteredComputedContent(computedContent, computedValue, filter, shouldFilter) {
|
2018-01-24 18:48:20 +08:00
|
|
|
if (shouldFilter) {
|
2017-11-21 18:53:09 +08:00
|
|
|
computedContent = this.filterComputedContent(computedContent, computedValue, filter);
|
|
|
|
}
|
|
|
|
|
2018-01-11 16:54:39 +08:00
|
|
|
if (this.get("limitMatches")) {
|
|
|
|
return computedContent.slice(0, this.get("limitMatches"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return computedContent;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("computedValue", "computedContent.[]")
|
2018-03-22 18:29:55 +08:00
|
|
|
selection(computedValue, computedContent) {
|
2017-11-21 18:53:09 +08:00
|
|
|
return computedContent.findBy("value", computedValue);
|
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
@computed("selection")
|
|
|
|
hasSelection(selection) {
|
|
|
|
return selection !== this.get("noneRowComputedContent") && !isNone(selection);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
@computed("computedValue", "filter", "collectionComputedContent.[]", "hasReachedLimit")
|
2018-03-03 18:42:44 +08:00
|
|
|
shouldDisplayCreateRow(computedValue, filter) {
|
2017-11-21 18:53:09 +08:00
|
|
|
return this._super() && computedValue !== filter;
|
|
|
|
},
|
|
|
|
|
|
|
|
autoHighlight() {
|
2017-11-28 02:50:04 +08:00
|
|
|
run.schedule("afterRender", () => {
|
2018-03-22 18:29:55 +08:00
|
|
|
if (this.get("shouldDisplayCreateRow")) {
|
|
|
|
this.highlight(this.get("createRowComputedContent"));
|
2017-11-21 18:53:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
if (!isEmpty(this.get("filter")) && !isEmpty(this.get("collectionComputedContent"))) {
|
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
2017-11-21 18:53:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
if (!this.get("isAsync") && this.get("hasSelection") && isEmpty(this.get("filter"))) {
|
|
|
|
this.highlight(get(makeArray(this.get("selection")), "firstObject"));
|
|
|
|
return;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
|
|
|
|
if (!this.get("isAsync") && !this.get("hasSelection") && isEmpty(this.get("filter")) && !isEmpty(this.get("collectionComputedContent"))) {
|
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
|
|
|
return;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
|
|
|
|
if (isPresent(this.get("noneRowComputedContent"))) {
|
|
|
|
this.highlight(this.get("noneRowComputedContent"));
|
|
|
|
return;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
select(computedContentItem) {
|
|
|
|
if (!computedContentItem || computedContentItem.__sk_row_type === "noneRow") {
|
|
|
|
this.clearSelection();
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
if (computedContentItem.__sk_row_type === "createRow") {
|
2018-02-14 00:23:12 +08:00
|
|
|
if (this.get("computedValue") !== computedContentItem.value &&
|
|
|
|
this.validateCreate(computedContentItem.value)) {
|
2018-03-22 18:29:55 +08:00
|
|
|
this.willCreate(computedContentItem);
|
|
|
|
computedContentItem.__sk_row_type = null;
|
2017-11-21 18:53:09 +08:00
|
|
|
this.get("computedContent").pushObject(computedContentItem);
|
2018-03-22 18:29:55 +08:00
|
|
|
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didCreate(computedContentItem);
|
|
|
|
this._boundaryActionHandler("onCreate");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.select(computedContentItem);
|
|
|
|
return;
|
2018-01-11 16:39:51 +08:00
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onCreateFailure");
|
2018-03-22 18:29:55 +08:00
|
|
|
return;
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.validateSelect(computedContentItem)) {
|
|
|
|
this.willSelect(computedContentItem);
|
|
|
|
this.clearFilter();
|
|
|
|
this.setProperties({ highlighted: null, computedValue: computedContentItem.value });
|
|
|
|
|
|
|
|
run.next(() => this.mutateAttributes());
|
|
|
|
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didSelect(computedContentItem);
|
|
|
|
|
|
|
|
applyOnSelectPluginApiCallbacks(
|
|
|
|
this.get("pluginApiIdentifiers"),
|
|
|
|
computedContentItem.value,
|
|
|
|
this
|
|
|
|
);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
this._boundaryActionHandler("onSelect", computedContentItem.value);
|
|
|
|
|
|
|
|
this.autoHighlight();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onSelectFailure");
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
deselect(computedContentItem) {
|
|
|
|
makeArray(computedContentItem).forEach((item) => {
|
|
|
|
this.willDeselect(item);
|
|
|
|
|
|
|
|
this.clearFilter();
|
|
|
|
|
|
|
|
this.setProperties({
|
|
|
|
computedValue: null,
|
|
|
|
highlighted: null,
|
|
|
|
highlightedSelection: []
|
|
|
|
});
|
|
|
|
|
|
|
|
run.next(() => this.mutateAttributes());
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didDeselect(item);
|
|
|
|
this._boundaryActionHandler("onDeselect", item);
|
|
|
|
this.autoHighlight();
|
|
|
|
});
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
});
|