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