2017-11-21 05:53:09 -05:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2018-01-24 05:48:20 -05:00
|
|
|
import { default as computed, on } from 'ember-addons/ember-computed-decorators';
|
2018-03-22 06:29:55 -04:00
|
|
|
const { get, isNone, isEmpty, isPresent, run, makeArray } = Ember;
|
|
|
|
|
|
|
|
import {
|
|
|
|
applyOnSelectPluginApiCallbacks,
|
|
|
|
} from "select-kit/mixins/plugin-api";
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
export default SelectKitComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["single-select"],
|
2018-02-26 05:42:57 -05:00
|
|
|
layoutName: "select-kit/templates/components/single-select",
|
2017-11-21 05:53:09 -05:00
|
|
|
classNames: "single-select",
|
|
|
|
computedValue: null,
|
|
|
|
value: null,
|
2017-11-24 07:15:02 -05:00
|
|
|
allowInitialValueMutation: false,
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
@on("didReceiveAttrs")
|
|
|
|
_compute() {
|
2017-11-27 13:50:04 -05:00
|
|
|
run.scheduleOnce("afterRender", () => {
|
2017-11-21 05:53:09 -05:00
|
|
|
this.willComputeAttributes();
|
2018-02-26 05:42:57 -05:00
|
|
|
let content = this.get("content") || [];
|
|
|
|
let asyncContent = this.get("asyncContent") || [];
|
|
|
|
content = this.willComputeContent(content);
|
|
|
|
asyncContent = this.willComputeAsyncContent(asyncContent);
|
2017-11-21 05:53:09 -05:00
|
|
|
let value = this._beforeWillComputeValue(this.get("value"));
|
|
|
|
content = this.computeContent(content);
|
2018-02-26 05:42:57 -05:00
|
|
|
asyncContent = this.computeAsyncContent(asyncContent);
|
2017-11-21 05:53:09 -05:00
|
|
|
content = this._beforeDidComputeContent(content);
|
2018-02-26 05:42:57 -05:00
|
|
|
asyncContent = this._beforeDidComputeAsyncContent(asyncContent);
|
2017-11-21 05:53:09 -05:00
|
|
|
value = this.willComputeValue(value);
|
|
|
|
value = this.computeValue(value);
|
|
|
|
value = this._beforeDidComputeValue(value);
|
|
|
|
this.didComputeContent(content);
|
2018-02-26 05:42:57 -05:00
|
|
|
this.didComputeAsyncContent(asyncContent);
|
2017-11-21 05:53:09 -05:00
|
|
|
this.didComputeValue(value);
|
|
|
|
this.didComputeAttributes();
|
2017-11-23 08:39:26 -05:00
|
|
|
|
|
|
|
if (this.get("allowInitialValueMutation")) this.mutateAttributes();
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-22 04:34:12 -05:00
|
|
|
mutateAttributes() {
|
|
|
|
run.next(() => {
|
2018-03-22 06:29:55 -04:00
|
|
|
if (this.get("isDestroyed") || this.get("isDestroying")) return;
|
|
|
|
|
2017-11-22 04:34:12 -05:00
|
|
|
this.mutateContent(this.get("computedContent"));
|
|
|
|
this.mutateValue(this.get("computedValue"));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
mutateContent() {},
|
|
|
|
mutateValue(computedValue) {
|
|
|
|
this.set("value", computedValue);
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
_beforeWillComputeValue(value) {
|
2018-01-24 05:48:20 -05: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 07:15:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 05:53:09 -05: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 06:29:55 -04:00
|
|
|
computeHeaderContent() {
|
2018-03-29 07:42:00 -04:00
|
|
|
let content = {
|
2017-12-22 07:08:12 -05:00
|
|
|
title: this.get("title"),
|
2018-03-22 06:29:55 -04:00
|
|
|
icons: makeArray(this.getWithDefault("headerIcon", [])),
|
|
|
|
value: this.get("selection.value"),
|
|
|
|
name: this.get("selection.name") || this.get("noneRowComputedContent.name")
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
2018-03-29 07:42:00 -04:00
|
|
|
|
2018-04-05 10:45:19 -04:00
|
|
|
if (this.get("noneLabel") && !this.get("hasSelection")) {
|
2018-03-29 07:42:00 -04:00
|
|
|
content.title = content.name = I18n.t(this.get("noneLabel"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2018-02-26 05:42:57 -05: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 05:53:09 -05:00
|
|
|
@computed("computedContent.[]", "computedValue", "filter", "shouldFilter")
|
|
|
|
filteredComputedContent(computedContent, computedValue, filter, shouldFilter) {
|
2018-01-24 05:48:20 -05:00
|
|
|
if (shouldFilter) {
|
2017-11-21 05:53:09 -05:00
|
|
|
computedContent = this.filterComputedContent(computedContent, computedValue, filter);
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:54:39 -05:00
|
|
|
if (this.get("limitMatches")) {
|
|
|
|
return computedContent.slice(0, this.get("limitMatches"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return computedContent;
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("computedValue", "computedContent.[]")
|
2018-03-22 06:29:55 -04:00
|
|
|
selection(computedValue, computedContent) {
|
2017-11-21 05:53:09 -05:00
|
|
|
return computedContent.findBy("value", computedValue);
|
|
|
|
},
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
@computed("selection")
|
|
|
|
hasSelection(selection) {
|
|
|
|
return selection !== this.get("noneRowComputedContent") && !isNone(selection);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2018-04-05 10:45:19 -04:00
|
|
|
@computed("computedValue", "filter", "collectionComputedContent.[]", "hasReachedMaximum", "hasReachedMinimum")
|
2018-03-03 05:42:44 -05:00
|
|
|
shouldDisplayCreateRow(computedValue, filter) {
|
2017-11-21 05:53:09 -05:00
|
|
|
return this._super() && computedValue !== filter;
|
|
|
|
},
|
|
|
|
|
|
|
|
autoHighlight() {
|
2017-11-27 13:50:04 -05:00
|
|
|
run.schedule("afterRender", () => {
|
2018-03-22 06:29:55 -04:00
|
|
|
if (this.get("shouldDisplayCreateRow")) {
|
|
|
|
this.highlight(this.get("createRowComputedContent"));
|
2017-11-21 05:53:09 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
if (!isEmpty(this.get("filter")) && !isEmpty(this.get("collectionComputedContent"))) {
|
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
2017-11-21 05:53:09 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
if (!this.get("isAsync") && this.get("hasSelection") && isEmpty(this.get("filter"))) {
|
|
|
|
this.highlight(get(makeArray(this.get("selection")), "firstObject"));
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04: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 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
if (isPresent(this.get("noneRowComputedContent"))) {
|
|
|
|
this.highlight(this.get("noneRowComputedContent"));
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
select(computedContentItem) {
|
|
|
|
if (!computedContentItem || computedContentItem.__sk_row_type === "noneRow") {
|
|
|
|
this.clearSelection();
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
if (computedContentItem.__sk_row_type === "createRow") {
|
2018-02-13 11:23:12 -05:00
|
|
|
if (this.get("computedValue") !== computedContentItem.value &&
|
|
|
|
this.validateCreate(computedContentItem.value)) {
|
2018-03-22 06:29:55 -04:00
|
|
|
this.willCreate(computedContentItem);
|
|
|
|
computedContentItem.__sk_row_type = null;
|
2017-11-21 05:53:09 -05:00
|
|
|
this.get("computedContent").pushObject(computedContentItem);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didCreate(computedContentItem);
|
|
|
|
this._boundaryActionHandler("onCreate");
|
|
|
|
});
|
|
|
|
|
|
|
|
this.select(computedContentItem);
|
|
|
|
return;
|
2018-01-11 03:39:51 -05:00
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onCreateFailure");
|
2018-03-22 06:29:55 -04:00
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04: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 05:53:09 -05:00
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
this._boundaryActionHandler("onSelect", computedContentItem.value);
|
|
|
|
|
|
|
|
this.autoHighlight();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onSelectFailure");
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04: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 05:53:09 -05:00
|
|
|
}
|
|
|
|
});
|