2017-11-21 18:53:09 +08:00
|
|
|
import SelectKitComponent from "select-kit/components/select-kit";
|
2019-11-08 05:38:28 +08:00
|
|
|
import {
|
|
|
|
default as discourseComputed,
|
|
|
|
on
|
|
|
|
} from "discourse-common/utils/decorators";
|
2018-03-22 18:29:55 +08:00
|
|
|
const { get, isNone, isEmpty, makeArray, run } = 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: ["multi-select"],
|
2018-02-26 18:42:57 +08:00
|
|
|
layoutName: "select-kit/templates/components/multi-select",
|
2017-11-21 18:53:09 +08:00
|
|
|
classNames: "multi-select",
|
|
|
|
headerComponent: "multi-select/multi-select-header",
|
|
|
|
headerText: "select_kit.default_header_text",
|
|
|
|
allowAny: true,
|
|
|
|
allowInitialValueMutation: false,
|
|
|
|
autoFilterable: true,
|
|
|
|
selectedNameComponent: "multi-select/selected-name",
|
2018-02-26 18:42:57 +08:00
|
|
|
filterIcon: null,
|
2018-03-22 18:29:55 +08:00
|
|
|
filterComponent: "multi-select/multi-select-filter",
|
|
|
|
computedValues: null,
|
|
|
|
values: null,
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
init() {
|
2019-01-19 17:05:51 +08:00
|
|
|
this._super(...arguments);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
this.set("computedValues", []);
|
2018-03-22 18:29:55 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (isNone(this.values)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("values", []);
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
this.headerComponentOptions.setProperties({
|
|
|
|
selectedNameComponent: this.selectedNameComponent
|
2018-09-12 18:19:04 +08:00
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
@on("didRender")
|
|
|
|
_setChoicesMaxWidth() {
|
|
|
|
const width = this.$body().outerWidth(false);
|
2018-11-06 18:30:12 +08:00
|
|
|
if (width > 0) {
|
2019-07-16 18:45:15 +08:00
|
|
|
this.element.querySelector(".choices").style.maxWidth = `${width}px`;
|
2018-11-06 18:30:12 +08:00
|
|
|
}
|
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() {
|
2018-03-22 18:29:55 +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 values = this._beforeWillComputeValues(this.values);
|
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
|
|
|
values = this.willComputeValues(values);
|
|
|
|
values = this.computeValues(values);
|
|
|
|
values = this._beforeDidComputeValues(values);
|
|
|
|
this.didComputeContent(content);
|
2018-02-26 18:42:57 +08:00
|
|
|
this.didComputeAsyncContent(asyncContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
this.didComputeValues(values);
|
|
|
|
this.didComputeAttributes();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("filter", "shouldDisplayCreateRow")
|
2017-11-21 18:53:09 +08:00
|
|
|
createRowComputedContent(filter, shouldDisplayCreateRow) {
|
2018-03-22 18:29:55 +08:00
|
|
|
if (shouldDisplayCreateRow) {
|
2017-11-21 18:53:09 +08:00
|
|
|
let content = this.createContentFromInput(filter);
|
|
|
|
return this.computeContentItem(content, { created: true });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("filter", "computedValues")
|
2017-11-21 18:53:09 +08:00
|
|
|
shouldDisplayCreateRow(filter, computedValues) {
|
|
|
|
return this._super() && !computedValues.includes(filter);
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed
|
2018-06-15 23:03:24 +08:00
|
|
|
shouldDisplayFilter() {
|
|
|
|
return true;
|
|
|
|
},
|
2017-11-22 01:07:10 +08:00
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
_beforeWillComputeValues(values) {
|
2018-05-25 05:41:39 +08:00
|
|
|
return values.map(v => this._cast(v === "" ? null : v));
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
willComputeValues(values) {
|
|
|
|
return values;
|
|
|
|
},
|
|
|
|
computeValues(values) {
|
|
|
|
return values;
|
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
_beforeDidComputeValues(values) {
|
|
|
|
this.setProperties({ computedValues: values });
|
|
|
|
return values;
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
didComputeValues(values) {
|
|
|
|
return values;
|
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
mutateAttributes() {
|
2018-03-22 18:29:55 +08:00
|
|
|
run.next(() => {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.isDestroyed || this.isDestroying) return;
|
2017-11-28 02:50:04 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
this.mutateContent(this.computedContent);
|
|
|
|
this.mutateValues(this.computedValues);
|
2017-11-21 18:53:09 +08:00
|
|
|
});
|
|
|
|
},
|
2017-11-22 17:34:12 +08:00
|
|
|
mutateValues(computedValues) {
|
|
|
|
this.set("values", computedValues);
|
|
|
|
},
|
2018-06-15 23:03:24 +08:00
|
|
|
mutateContent() {},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2018-12-10 21:05:00 +08:00
|
|
|
forceValues(values) {
|
|
|
|
this.mutateValues(values);
|
|
|
|
this._compute();
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
filterComputedContent(computedContent, computedValues, 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
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("computedAsyncContent.[]", "computedValues.[]")
|
2018-02-26 18:42:57 +08:00
|
|
|
filteredAsyncComputedContent(computedAsyncContent, computedValues) {
|
|
|
|
computedAsyncContent = computedAsyncContent.filter(c => {
|
|
|
|
return !computedValues.includes(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.[]", "computedValues.[]", "filter")
|
2017-11-21 18:53:09 +08:00
|
|
|
filteredComputedContent(computedContent, computedValues, filter) {
|
|
|
|
computedContent = computedContent.filter(c => {
|
|
|
|
return !computedValues.includes(get(c, "value"));
|
|
|
|
});
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.shouldFilter) {
|
2018-06-15 23:03:24 +08:00
|
|
|
computedContent = this.filterComputedContent(
|
|
|
|
computedContent,
|
|
|
|
computedValues,
|
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
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
computeHeaderContent() {
|
2018-04-05 22:45:19 +08:00
|
|
|
let content = {
|
2019-05-27 16:15:39 +08:00
|
|
|
title: this.title,
|
|
|
|
selection: this.selection
|
2017-11-21 18:53:09 +08:00
|
|
|
};
|
2018-04-05 22:45:19 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.noneLabel) {
|
|
|
|
if (!this.hasSelection) {
|
2019-05-27 16:42:53 +08:00
|
|
|
content.title = content.name = content.label = I18n.t(this.noneLabel);
|
2018-04-05 22:45:19 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.hasReachedMinimum) {
|
2019-05-27 16:42:53 +08:00
|
|
|
const key = this.minimumLabel || "select_kit.min_content_not_reached";
|
2018-06-15 23:03:24 +08:00
|
|
|
content.title = content.name = content.label = I18n.t(key, {
|
2019-05-27 16:15:39 +08:00
|
|
|
count: this.minimum
|
2018-06-15 23:03:24 +08:00
|
|
|
});
|
2018-04-05 22:45:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("filter")
|
2017-11-21 18:53:09 +08:00
|
|
|
templateForCreateRow() {
|
2018-06-15 23:03:24 +08:00
|
|
|
return rowComponent => {
|
|
|
|
return I18n.t("select_kit.create", {
|
|
|
|
content: rowComponent.get("computedContent.name")
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
validateSelect() {
|
2019-05-27 16:15:39 +08:00
|
|
|
return this._super() && !this.hasReachedMaximum;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("computedValues.[]", "computedContent.[]")
|
2019-05-25 21:50:41 +08:00
|
|
|
selection(computedValues, computedContent) {
|
2017-11-21 18:53:09 +08:00
|
|
|
const selected = [];
|
2017-12-15 15:45:50 +08:00
|
|
|
|
|
|
|
computedValues.forEach(v => {
|
2019-05-25 21:50:41 +08:00
|
|
|
const value = computedContent.findBy("value", v);
|
2017-12-15 15:45:50 +08:00
|
|
|
if (value) selected.push(value);
|
|
|
|
});
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
return selected;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("selection.[]")
|
2018-06-15 23:03:24 +08:00
|
|
|
hasSelection(selection) {
|
|
|
|
return !isEmpty(selection);
|
|
|
|
},
|
2018-03-22 18:29:55 +08:00
|
|
|
|
|
|
|
didPressTab(event) {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (isEmpty(this.filter) && !this.highlighted) {
|
2018-03-22 18:29:55 +08:00
|
|
|
this.$header().focus();
|
|
|
|
this.close(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (this.highlighted && this.isExpanded) {
|
2018-03-22 18:29:55 +08:00
|
|
|
this._destroyEvent(event);
|
|
|
|
this.focus();
|
2019-05-27 16:15:39 +08:00
|
|
|
this.select(this.highlighted);
|
2018-03-22 18:29:55 +08:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.close(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
autoHighlight() {
|
2018-03-22 18:29:55 +08:00
|
|
|
run.schedule("afterRender", () => {
|
2019-05-27 16:15:39 +08:00
|
|
|
if (!this.isExpanded) return;
|
|
|
|
if (!this.renderedBodyOnce) return;
|
|
|
|
if (this.highlighted) return;
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
if (isEmpty(this.collectionComputedContent)) {
|
|
|
|
if (this.createRowComputedContent) {
|
|
|
|
this.highlight(this.createRowComputedContent);
|
2019-05-27 16:42:53 +08:00
|
|
|
} else if (this.noneRowComputedContent && this.hasSelection) {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.highlight(this.noneRowComputedContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-22 18:29:55 +08:00
|
|
|
this.highlight(this.get("collectionComputedContent.firstObject"));
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
select(computedContentItem) {
|
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");
|
2018-03-22 18:29:55 +08:00
|
|
|
this.clearSelection();
|
|
|
|
return;
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.computedValues.includes(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
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
if (this.validateSelect(computedContentItem)) {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.willSelect(computedContentItem);
|
2018-03-22 18:29:55 +08:00
|
|
|
this.clearFilter();
|
|
|
|
this.setProperties({ highlighted: null });
|
2019-05-27 16:15:39 +08:00
|
|
|
this.computedValues.pushObject(computedContentItem.value);
|
2018-02-14 00:23:12 +08:00
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
run.next(() => this.mutateAttributes());
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
|
|
|
this.autoHighlight();
|
|
|
|
|
|
|
|
this._boundaryActionHandler("onSelect", computedContentItem.value);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onSelectFailure");
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2018-03-22 18:29:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
deselect(rowComputedContentItems) {
|
|
|
|
this.willDeselect(rowComputedContentItems);
|
2019-03-04 17:30:22 +08:00
|
|
|
|
2018-03-22 18:29:55 +08:00
|
|
|
rowComputedContentItems = makeArray(rowComputedContentItems);
|
2018-06-15 23:03:24 +08:00
|
|
|
const generatedComputedContents = this._filterRemovableComputedContents(
|
|
|
|
makeArray(rowComputedContentItems)
|
|
|
|
);
|
2019-03-04 17:30:22 +08:00
|
|
|
this.setProperties({ highlighted: null, highlightedSelection: [] });
|
2019-05-27 16:15:39 +08:00
|
|
|
this.computedValues.removeObjects(
|
2018-06-15 23:03:24 +08:00
|
|
|
rowComputedContentItems.map(r => r.value)
|
|
|
|
);
|
2019-05-27 16:15:39 +08:00
|
|
|
this.computedContent.removeObjects([
|
2019-03-04 17:30:22 +08:00
|
|
|
...rowComputedContentItems,
|
|
|
|
...generatedComputedContents
|
|
|
|
]);
|
|
|
|
|
|
|
|
run.next(() => {
|
|
|
|
this.mutateAttributes();
|
|
|
|
|
|
|
|
run.schedule("afterRender", () => {
|
|
|
|
this.didDeselect(rowComputedContentItems);
|
|
|
|
this.autoHighlight();
|
|
|
|
|
|
|
|
if (!this.isDestroying && !this.isDestroyed) {
|
|
|
|
this._positionWrapper();
|
|
|
|
}
|
|
|
|
});
|
2018-03-22 18:29:55 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
close(event) {
|
|
|
|
this.clearHighlightSelection();
|
|
|
|
|
|
|
|
this._super(event);
|
|
|
|
},
|
|
|
|
|
|
|
|
unfocus(event) {
|
|
|
|
this.clearHighlightSelection();
|
|
|
|
|
|
|
|
this._super(event);
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
});
|