2017-11-21 05:53:09 -05:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2018-06-15 11:03:24 -04: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;
|
|
|
|
|
2018-09-05 11:18:52 -04:00
|
|
|
import {
|
|
|
|
applyOnSelectPluginApiCallbacks,
|
|
|
|
applyOnSelectNonePluginApiCallbacks
|
|
|
|
} 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
|
|
|
|
2019-05-10 11:12:10 -04:00
|
|
|
@on("didUpdateAttrs", "init")
|
2017-11-21 05:53:09 -05:00
|
|
|
_compute() {
|
2017-11-27 13:50:04 -05:00
|
|
|
run.scheduleOnce("afterRender", () => {
|
2017-11-21 05:53:09 -05:00
|
|
|
this.willComputeAttributes();
|
2019-05-27 04:15:39 -04:00
|
|
|
let content = this.content || [];
|
|
|
|
let asyncContent = this.asyncContent || [];
|
2018-02-26 05:42:57 -05:00
|
|
|
content = this.willComputeContent(content);
|
|
|
|
asyncContent = this.willComputeAsyncContent(asyncContent);
|
2019-05-27 04:15:39 -04:00
|
|
|
let value = this._beforeWillComputeValue(this.value);
|
2017-11-21 05:53:09 -05:00
|
|
|
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
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.allowInitialValueMutation) this.mutateAttributes();
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-22 04:34:12 -05:00
|
|
|
mutateAttributes() {
|
|
|
|
run.next(() => {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.isDestroyed || this.isDestroying) return;
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
this.mutateContent(this.computedContent);
|
|
|
|
this.mutateValue(this.computedValue);
|
2017-11-22 04:34:12 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
mutateContent() {},
|
|
|
|
mutateValue(computedValue) {
|
|
|
|
this.set("value", computedValue);
|
|
|
|
},
|
|
|
|
|
2018-12-10 08:05:00 -05:00
|
|
|
forceValue(value) {
|
|
|
|
this.mutateValue(value);
|
|
|
|
this._compute();
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
_beforeWillComputeValue(value) {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
!isEmpty(this.content) &&
|
2018-01-24 05:48:20 -05:00
|
|
|
isEmpty(value) &&
|
2019-05-27 04:15:39 -04:00
|
|
|
isNone(this.none) &&
|
|
|
|
this.allowAutoSelectFirst
|
2018-06-15 11:03:24 -04:00
|
|
|
) {
|
2019-05-27 04:15:39 -04:00
|
|
|
value = this.valueForContentItem(get(this.content, "firstObject"));
|
2017-11-24 07:15:02 -05:00
|
|
|
}
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
switch (typeof value) {
|
2018-06-15 11:03:24 -04:00
|
|
|
case "string":
|
|
|
|
case "number":
|
|
|
|
return this._cast(value === "" ? null : value);
|
|
|
|
default:
|
|
|
|
return value;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
},
|
2018-06-15 11:03:24 -04:00
|
|
|
willComputeValue(value) {
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
computeValue(value) {
|
|
|
|
return value;
|
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
_beforeDidComputeValue(value) {
|
|
|
|
this.setProperties({ computedValue: value });
|
|
|
|
return value;
|
|
|
|
},
|
2018-06-15 11:03:24 -04:00
|
|
|
didComputeValue(value) {
|
|
|
|
return value;
|
|
|
|
},
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
filterComputedContent(computedContent, computedValue, filter) {
|
|
|
|
return computedContent.filter(c => {
|
2018-06-26 06:19:14 -04:00
|
|
|
return this._normalize(get(c, "name")).indexOf(filter) > -1;
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
computeHeaderContent() {
|
2018-03-29 07:42:00 -04:00
|
|
|
let content = {
|
2019-05-27 04:15:39 -04:00
|
|
|
title: this.title,
|
2018-03-22 06:29:55 -04:00
|
|
|
icons: makeArray(this.getWithDefault("headerIcon", [])),
|
|
|
|
value: this.get("selection.value"),
|
2018-06-15 11:03:24 -04:00
|
|
|
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
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.noneLabel && !this.hasSelection) {
|
|
|
|
content.title = content.name = I18n.t(this.noneLabel);
|
2018-03-29 07:42:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2018-02-26 05:42:57 -05:00
|
|
|
@computed("computedAsyncContent.[]", "computedValue")
|
|
|
|
filteredAsyncComputedContent(computedAsyncContent, computedValue) {
|
2019-02-07 08:43:33 -05:00
|
|
|
computedAsyncContent = (computedAsyncContent || []).filter(c => {
|
2018-02-26 05:42:57 -05:00
|
|
|
return computedValue !== get(c, "value");
|
|
|
|
});
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.limitMatches) {
|
|
|
|
return computedAsyncContent.slice(0, this.limitMatches);
|
2018-02-26 05:42:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return computedAsyncContent;
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
@computed("computedContent.[]", "computedValue", "filter", "shouldFilter")
|
2018-06-15 11:03:24 -04:00
|
|
|
filteredComputedContent(
|
|
|
|
computedContent,
|
|
|
|
computedValue,
|
|
|
|
filter,
|
|
|
|
shouldFilter
|
|
|
|
) {
|
2018-01-24 05:48:20 -05:00
|
|
|
if (shouldFilter) {
|
2018-06-15 11:03:24 -04:00
|
|
|
computedContent = this.filterComputedContent(
|
|
|
|
computedContent,
|
|
|
|
computedValue,
|
2018-06-26 06:19:14 -04:00
|
|
|
this._normalize(filter)
|
2018-06-15 11:03:24 -04:00
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.limitMatches) {
|
|
|
|
return computedContent.slice(0, this.limitMatches);
|
2018-01-11 03:54:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-05-27 04:42:53 -04:00
|
|
|
return selection !== this.noneRowComputedContent && !isNone(selection);
|
2017-11-21 05:53:09 -05:00
|
|
|
},
|
|
|
|
|
2018-06-15 11:03:24 -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", () => {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.shouldDisplayCreateRow) {
|
|
|
|
this.highlight(this.createRowComputedContent);
|
2017-11-21 05:53:09 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:42:53 -04:00
|
|
|
if (!isEmpty(this.filter) && !isEmpty(this.collectionComputedContent)) {
|
2018-03-22 06:29:55 -04:00
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
2017-11-21 05:53:09 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:42:53 -04:00
|
|
|
if (!this.isAsync && this.hasSelection && isEmpty(this.filter)) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.highlight(get(makeArray(this.selection), "firstObject"));
|
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
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
!this.isAsync &&
|
|
|
|
!this.hasSelection &&
|
|
|
|
isEmpty(this.filter) &&
|
|
|
|
!isEmpty(this.collectionComputedContent)
|
2018-06-15 11:03:24 -04:00
|
|
|
) {
|
2018-03-22 06:29:55 -04:00
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
|
|
|
return;
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (isPresent(this.noneRowComputedContent)) {
|
|
|
|
this.highlight(this.noneRowComputedContent);
|
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
|
|
|
select(computedContentItem) {
|
2018-11-15 09:21:40 -05:00
|
|
|
if (computedContentItem.__sk_row_type === "noopRow") {
|
2018-11-29 09:56:19 -05:00
|
|
|
applyOnSelectPluginApiCallbacks(
|
2019-05-27 04:15:39 -04:00
|
|
|
this.pluginApiIdentifiers,
|
2018-11-29 09:56:19 -05:00
|
|
|
computedContentItem.value,
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
2018-11-15 09:21:40 -05:00
|
|
|
this._boundaryActionHandler("onSelect", computedContentItem.value);
|
2019-05-24 07:11:33 -04:00
|
|
|
this._boundaryActionHandler("onSelectAny", computedContentItem);
|
2018-11-15 09:21:40 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.hasSelection) {
|
2018-09-18 05:31:23 -04:00
|
|
|
this.deselect(this.get("selection.value"));
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
|
|
|
!computedContentItem ||
|
|
|
|
computedContentItem.__sk_row_type === "noneRow"
|
|
|
|
) {
|
2019-05-27 04:42:53 -04:00
|
|
|
applyOnSelectNonePluginApiCallbacks(this.pluginApiIdentifiers, this);
|
2018-09-05 11:18:52 -04:00
|
|
|
this._boundaryActionHandler("onSelectNone");
|
2019-05-24 07:11:33 -04:00
|
|
|
this._boundaryActionHandler("onSelectAny", computedContentItem);
|
2018-03-22 06:29:55 -04:00
|
|
|
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-06-15 11:03:24 -04:00
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
this.computedValue !== computedContentItem.value &&
|
2018-06-15 11:03:24 -04:00
|
|
|
this.validateCreate(computedContentItem.value)
|
|
|
|
) {
|
2018-03-22 06:29:55 -04:00
|
|
|
this.willCreate(computedContentItem);
|
|
|
|
computedContentItem.__sk_row_type = null;
|
2019-05-27 04:15:39 -04:00
|
|
|
this.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();
|
|
|
|
|
2019-02-07 08:43:33 -05:00
|
|
|
const action = computedContentItem.originalContent.action;
|
|
|
|
if (action) {
|
|
|
|
action();
|
|
|
|
} else {
|
|
|
|
this.setProperties({
|
|
|
|
highlighted: null,
|
|
|
|
computedValue: computedContentItem.value
|
|
|
|
});
|
|
|
|
|
|
|
|
run.next(() => this.mutateAttributes());
|
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didSelect(computedContentItem);
|
|
|
|
|
|
|
|
applyOnSelectPluginApiCallbacks(
|
2019-05-27 04:15:39 -04:00
|
|
|
this.pluginApiIdentifiers,
|
2018-03-22 06:29:55 -04:00
|
|
|
computedContentItem.value,
|
|
|
|
this
|
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-03-22 06:29:55 -04:00
|
|
|
this._boundaryActionHandler("onSelect", computedContentItem.value);
|
2019-05-24 07:11:33 -04:00
|
|
|
this._boundaryActionHandler("onSelectAny", computedContentItem);
|
2018-03-22 06:29:55 -04:00
|
|
|
|
|
|
|
this.autoHighlight();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onSelectFailure");
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
2018-03-22 06:29:55 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
deselect(computedContentItem) {
|
2018-06-15 11:03:24 -04:00
|
|
|
makeArray(computedContentItem).forEach(item => {
|
2018-03-22 06:29:55 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|