2018-01-15 18:55:16 +08:00
|
|
|
const { isNone, run } = Ember;
|
2017-11-21 18:53:09 +08:00
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
import UtilsMixin from "select-kit/mixins/utils";
|
|
|
|
import DomHelpersMixin from "select-kit/mixins/dom-helpers";
|
|
|
|
import EventsMixin from "select-kit/mixins/events";
|
|
|
|
import PluginApiMixin from "select-kit/mixins/plugin-api";
|
2017-11-22 17:34:12 +08:00
|
|
|
import {
|
2018-01-09 17:52:32 +08:00
|
|
|
applyContentPluginApiCallbacks,
|
2018-01-11 16:39:51 +08:00
|
|
|
applyHeaderContentPluginApiCallbacks,
|
2018-01-26 21:42:24 +08:00
|
|
|
applyOnSelectPluginApiCallbacks,
|
|
|
|
applyCollectionHeaderCallbacks
|
2017-11-22 17:34:12 +08:00
|
|
|
} from "select-kit/mixins/plugin-api";
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
export default Ember.Component.extend(UtilsMixin, PluginApiMixin, DomHelpersMixin, EventsMixin, {
|
|
|
|
pluginApiIdentifiers: ["select-kit"],
|
|
|
|
layoutName: "select-kit/templates/components/select-kit",
|
2018-02-06 01:25:39 +08:00
|
|
|
classNames: ["select-kit"],
|
2017-11-21 18:53:09 +08:00
|
|
|
classNameBindings: [
|
2018-02-14 00:23:12 +08:00
|
|
|
"isLoading",
|
2017-11-21 18:53:09 +08:00
|
|
|
"isFocused",
|
|
|
|
"isExpanded",
|
|
|
|
"isDisabled",
|
|
|
|
"isHidden",
|
|
|
|
"isAbove",
|
|
|
|
"isBelow",
|
|
|
|
"isLeftAligned",
|
2018-01-24 18:48:20 +08:00
|
|
|
"isRightAligned",
|
|
|
|
"hasSelection",
|
2017-11-21 18:53:09 +08:00
|
|
|
],
|
|
|
|
isDisabled: false,
|
|
|
|
isExpanded: false,
|
|
|
|
isFocused: false,
|
|
|
|
isHidden: false,
|
2018-02-14 00:23:12 +08:00
|
|
|
isLoading: false,
|
2018-02-26 18:42:57 +08:00
|
|
|
isAsync: false,
|
2017-11-21 18:53:09 +08:00
|
|
|
renderedBodyOnce: false,
|
|
|
|
renderedFilterOnce: false,
|
|
|
|
tabindex: 0,
|
|
|
|
none: null,
|
|
|
|
highlightedValue: null,
|
|
|
|
valueAttribute: "id",
|
|
|
|
nameProperty: "name",
|
|
|
|
autoFilterable: false,
|
|
|
|
filterable: false,
|
|
|
|
filter: "",
|
2018-02-14 00:23:12 +08:00
|
|
|
previousFilter: null,
|
2017-11-21 18:53:09 +08:00
|
|
|
filterPlaceholder: "select_kit.filter_placeholder",
|
|
|
|
filterIcon: "search",
|
|
|
|
headerIcon: null,
|
|
|
|
rowComponent: "select-kit/select-kit-row",
|
|
|
|
rowComponentOptions: null,
|
|
|
|
noneRowComponent: "select-kit/select-kit-none-row",
|
|
|
|
createRowComponent: "select-kit/select-kit-create-row",
|
|
|
|
filterComponent: "select-kit/select-kit-filter",
|
|
|
|
headerComponent: "select-kit/select-kit-header",
|
|
|
|
headerComponentOptions: null,
|
|
|
|
headerComputedContent: null,
|
2018-01-26 21:42:24 +08:00
|
|
|
collectionHeaderComputedContent: null,
|
2017-11-21 18:53:09 +08:00
|
|
|
collectionComponent: "select-kit/select-kit-collection",
|
|
|
|
verticalOffset: 0,
|
|
|
|
horizontalOffset: 0,
|
|
|
|
fullWidthOnMobile: false,
|
|
|
|
castInteger: false,
|
|
|
|
allowAny: false,
|
|
|
|
allowInitialValueMutation: false,
|
|
|
|
content: null,
|
|
|
|
computedContent: null,
|
2018-01-11 16:54:39 +08:00
|
|
|
limitMatches: null,
|
2017-11-22 20:49:45 +08:00
|
|
|
nameChanges: false,
|
2017-12-22 20:08:12 +08:00
|
|
|
allowContentReplacement: false,
|
2017-12-13 17:49:32 +08:00
|
|
|
collectionHeader: null,
|
2018-01-24 18:48:20 +08:00
|
|
|
allowAutoSelectFirst: true,
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
init() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
this.noneValue = "__none__";
|
|
|
|
this.set("headerComponentOptions", Ember.Object.create());
|
|
|
|
this.set("rowComponentOptions", Ember.Object.create());
|
|
|
|
this.set("computedContent", []);
|
|
|
|
|
2017-11-27 00:21:39 +08:00
|
|
|
if (this.site && this.site.isMobileDevice) {
|
2018-03-04 20:47:25 +08:00
|
|
|
this.setProperties({
|
|
|
|
filterable: isNone(this.get("filterable")) ? false : this.get("filterable"),
|
|
|
|
autoFilterable: isNone(this.get("autoFilterable")) ? false : this.get("filterable")
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
2017-11-22 20:49:45 +08:00
|
|
|
|
|
|
|
if (this.get("nameChanges")) {
|
|
|
|
this.addObserver(`content.@each.${this.get("nameProperty")}`, this, this._compute);
|
|
|
|
}
|
2017-11-28 02:50:04 +08:00
|
|
|
|
2017-12-22 20:08:12 +08:00
|
|
|
if (this.get("allowContentReplacement")) {
|
2017-11-28 02:50:04 +08:00
|
|
|
this.addObserver(`content.[]`, this, this._compute);
|
|
|
|
}
|
2018-02-26 18:42:57 +08:00
|
|
|
|
|
|
|
if (this.get("isAsync")) {
|
|
|
|
this.addObserver(`asyncContent.[]`, this, this._compute);
|
|
|
|
}
|
2017-11-22 20:49:45 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
willDestroyElement() {
|
|
|
|
this.removeObserver(`content.@each.${this.get("nameProperty")}`, this, this._compute);
|
2017-11-28 02:50:04 +08:00
|
|
|
this.removeObserver(`content.[]`, this, this._compute);
|
2018-02-26 18:42:57 +08:00
|
|
|
this.removeObserver(`asyncContent.[]`, this, this._compute);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
willComputeAttributes() {},
|
|
|
|
didComputeAttributes() {},
|
|
|
|
|
|
|
|
willComputeContent(content) { return content; },
|
|
|
|
computeContent(content) { return content; },
|
|
|
|
_beforeDidComputeContent(content) {
|
2017-11-22 17:34:12 +08:00
|
|
|
content = applyContentPluginApiCallbacks(this.get("pluginApiIdentifiers"), content, this);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
const existingCreatedComputedContent = this.get("computedContent").filterBy("created", true);
|
|
|
|
this.setProperties({
|
|
|
|
computedContent: content.map(c => this.computeContentItem(c)).concat(existingCreatedComputedContent)
|
|
|
|
});
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
didComputeContent() {},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
willComputeAsyncContent(content) { return content; },
|
|
|
|
computeAsyncContent(content) { return content; },
|
|
|
|
_beforeDidComputeAsyncContent(content) {
|
|
|
|
content = applyContentPluginApiCallbacks(this.get("pluginApiIdentifiers"), content, this);
|
|
|
|
this.setProperties({
|
|
|
|
computedAsyncContent: content.map(c => this.computeAsyncContentItem(c))
|
|
|
|
});
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
didComputeAsyncContent() {},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
computeHeaderContent() {
|
|
|
|
return this.baseHeaderComputedContent();
|
|
|
|
},
|
|
|
|
|
|
|
|
computeContentItem(contentItem, options) {
|
|
|
|
return this.baseComputedContentItem(contentItem, options);
|
|
|
|
},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
computeAsyncContentItem(contentItem, options) {
|
|
|
|
return this.computeContentItem(contentItem, options);
|
|
|
|
},
|
|
|
|
|
2018-03-03 18:42:44 +08:00
|
|
|
@computed("isAsync", "filteredAsyncComputedContent.[]", "filteredComputedContent.[]")
|
2018-02-26 18:42:57 +08:00
|
|
|
collectionComputedContent(isAsync, filteredAsyncComputedContent, filteredComputedContent) {
|
|
|
|
return isAsync ? filteredAsyncComputedContent : filteredComputedContent;
|
|
|
|
},
|
|
|
|
|
2018-02-14 00:23:12 +08:00
|
|
|
validateCreate() { return true; },
|
|
|
|
|
|
|
|
validateSelect() { return true; },
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
baseComputedContentItem(contentItem, options) {
|
|
|
|
let originalContent;
|
|
|
|
options = options || {};
|
|
|
|
const name = options.name;
|
|
|
|
|
|
|
|
if (typeof contentItem === "string" || typeof contentItem === "number") {
|
|
|
|
originalContent = {};
|
|
|
|
originalContent[this.get("valueAttribute")] = contentItem;
|
|
|
|
originalContent[this.get("nameProperty")] = name || contentItem;
|
|
|
|
} else {
|
|
|
|
originalContent = contentItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
value: this._castInteger(this.valueForContentItem(contentItem)),
|
|
|
|
name: name || this._nameForContent(contentItem),
|
|
|
|
locked: false,
|
|
|
|
created: options.created || false,
|
|
|
|
originalContent
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("shouldFilter", "allowAny", "filter")
|
|
|
|
shouldDisplayFilter(shouldFilter, allowAny, filter) {
|
2018-01-11 16:39:51 +08:00
|
|
|
if (shouldFilter) return true;
|
|
|
|
if (allowAny && filter.length > 0) return true;
|
2017-11-21 18:53:09 +08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
@computed("filter", "collectionComputedContent.[]")
|
|
|
|
noContentRow(filter, collectionComputedContent) {
|
|
|
|
if (filter.length > 0 && collectionComputedContent.length === 0) {
|
2018-02-14 07:30:09 +08:00
|
|
|
return I18n.t("select_kit.no_content");
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2018-02-26 18:42:57 +08:00
|
|
|
@computed("limitReached", "limit")
|
|
|
|
maxContentRow(limitReached, limit) {
|
|
|
|
if (limitReached) {
|
|
|
|
return I18n.t("select_kit.max_content_reached", { count: limit });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
@computed("filter", "filterable", "autoFilterable", "renderedFilterOnce")
|
|
|
|
shouldFilter(filter, filterable, autoFilterable, renderedFilterOnce) {
|
2018-01-11 16:39:51 +08:00
|
|
|
if (renderedFilterOnce && filterable) return true;
|
|
|
|
if (filterable) return true;
|
|
|
|
if (autoFilterable && filter.length > 0) return true;
|
2017-11-21 18:53:09 +08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2018-03-03 18:42:44 +08:00
|
|
|
@computed("computedValue", "filter", "collectionComputedContent.[]", "limitReached")
|
|
|
|
shouldDisplayCreateRow(computedValue, filter, collectionComputedContent, limitReached) {
|
2018-02-26 18:42:57 +08:00
|
|
|
if (limitReached) return false;
|
2018-03-03 18:42:44 +08:00
|
|
|
if (collectionComputedContent.map(c => c.value).includes(filter)) return false;
|
2018-02-14 00:23:12 +08:00
|
|
|
if (this.get("allowAny") && filter.length > 0 && this.validateCreate(filter)) return true;
|
2017-11-21 18:53:09 +08:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("filter", "shouldDisplayCreateRow")
|
|
|
|
createRowComputedContent(filter, shouldDisplayCreateRow) {
|
2018-01-11 16:39:51 +08:00
|
|
|
if (shouldDisplayCreateRow) {
|
2017-11-21 18:53:09 +08:00
|
|
|
let content = this.createContentFromInput(filter);
|
|
|
|
return this.computeContentItem(content, { created: true });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed
|
|
|
|
templateForRow() { return () => null; },
|
|
|
|
|
|
|
|
@computed
|
|
|
|
templateForNoneRow() { return () => null; },
|
|
|
|
|
|
|
|
@computed("filter")
|
|
|
|
templateForCreateRow() {
|
|
|
|
return (rowComponent) => {
|
2018-02-14 00:23:12 +08:00
|
|
|
return I18n.t("select_kit.create", {
|
2017-11-21 18:53:09 +08:00
|
|
|
content: rowComponent.get("computedContent.name")
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("none")
|
|
|
|
noneRowComputedContent(none) {
|
|
|
|
if (isNone(none)) { return null; }
|
|
|
|
|
|
|
|
switch (typeof none) {
|
|
|
|
case "string":
|
2017-12-06 08:09:12 +08:00
|
|
|
return this.computeContentItem(this.noneValue, {
|
2018-01-05 03:08:21 +08:00
|
|
|
name: (I18n.t(none) || "").htmlSafe()
|
2017-12-06 08:09:12 +08:00
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
default:
|
|
|
|
return this.computeContentItem(none);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
createContentFromInput(input) { return input; },
|
|
|
|
|
|
|
|
willSelect() {
|
|
|
|
this.clearFilter();
|
|
|
|
this.set("highlightedValue", null);
|
|
|
|
},
|
|
|
|
didSelect() {
|
|
|
|
this.collapse();
|
|
|
|
this.focus();
|
2018-01-11 16:39:51 +08:00
|
|
|
|
|
|
|
applyOnSelectPluginApiCallbacks(
|
|
|
|
this.get("pluginApiIdentifiers"),
|
|
|
|
this.get("computedValue"),
|
|
|
|
this
|
|
|
|
);
|
|
|
|
|
|
|
|
this._boundaryActionHandler("onSelect", this.get("computedValue"));
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
willDeselect() {
|
|
|
|
this.clearFilter();
|
|
|
|
this.set("highlightedValue", null);
|
|
|
|
},
|
2018-01-11 16:39:51 +08:00
|
|
|
didDeselect(rowComputedContentItem) {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.collapse();
|
|
|
|
this.focus();
|
2018-01-11 16:39:51 +08:00
|
|
|
this._boundaryActionHandler("onDeselect", rowComputedContentItem);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
clearFilter() {
|
|
|
|
this.$filterInput().val("");
|
|
|
|
this.setProperties({ filter: "" });
|
|
|
|
},
|
|
|
|
|
2018-02-14 00:23:12 +08:00
|
|
|
startLoading() {
|
|
|
|
this.set("isLoading", true);
|
|
|
|
this._boundaryActionHandler("onStartLoading");
|
|
|
|
},
|
|
|
|
|
|
|
|
stopLoading() {
|
|
|
|
this.set("isLoading", false);
|
|
|
|
this._boundaryActionHandler("onStopLoading");
|
|
|
|
},
|
|
|
|
|
2018-01-26 21:42:24 +08:00
|
|
|
_setCollectionHeaderComputedContent() {
|
|
|
|
const collectionHeaderComputedContent = applyCollectionHeaderCallbacks(
|
|
|
|
this.get("pluginApiIdentifiers"),
|
|
|
|
this.get("collectionHeader"),
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.set("collectionHeaderComputedContent", collectionHeaderComputedContent);
|
|
|
|
},
|
|
|
|
|
2018-01-09 17:52:32 +08:00
|
|
|
_setHeaderComputedContent() {
|
|
|
|
const headerComputedContent = applyHeaderContentPluginApiCallbacks(
|
|
|
|
this.get("pluginApiIdentifiers"),
|
|
|
|
this.computeHeaderContent(),
|
|
|
|
this
|
|
|
|
);
|
|
|
|
this.set("headerComputedContent", headerComputedContent);
|
|
|
|
},
|
|
|
|
|
2018-01-11 16:39:51 +08:00
|
|
|
_boundaryActionHandler(actionName, ...params) {
|
|
|
|
if (Ember.get(this.actions, actionName)) {
|
2018-01-15 18:55:16 +08:00
|
|
|
run.next(() => this.send(actionName, ...params));
|
2018-01-11 16:39:51 +08:00
|
|
|
} else if (this.get(actionName)) {
|
2018-01-15 18:55:16 +08:00
|
|
|
run.next(() => this.get(actionName)());
|
2018-01-11 16:39:51 +08:00
|
|
|
}
|
|
|
|
},
|
2018-01-09 17:52:32 +08:00
|
|
|
|
2018-01-11 16:39:51 +08:00
|
|
|
actions: {
|
|
|
|
toggle() {
|
|
|
|
this._boundaryActionHandler("onToggle", this);
|
|
|
|
|
|
|
|
if (this.get("isExpanded")) {
|
|
|
|
this._boundaryActionHandler("onCollapse", this);
|
|
|
|
this.collapse();
|
|
|
|
} else {
|
|
|
|
this._boundaryActionHandler("onExpand", this);
|
|
|
|
this.expand();
|
|
|
|
}
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2018-01-11 16:39:51 +08:00
|
|
|
highlight(rowComputedContent) {
|
2017-11-21 18:53:09 +08:00
|
|
|
this.set("highlightedValue", rowComputedContent.value);
|
2018-01-11 16:39:51 +08:00
|
|
|
this._boundaryActionHandler("onHighlight", rowComputedContent);
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2018-01-11 16:39:51 +08:00
|
|
|
filterComputedContent(filter) {
|
2018-02-14 00:23:12 +08:00
|
|
|
if (filter === this.get("previousFilter")) return;
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
this.setProperties({
|
|
|
|
highlightedValue: null,
|
|
|
|
renderedFilterOnce: true,
|
2018-02-14 00:23:12 +08:00
|
|
|
previousFilter: filter,
|
2017-11-21 18:53:09 +08:00
|
|
|
filter
|
|
|
|
});
|
|
|
|
this.autoHighlight();
|
2018-01-11 16:39:51 +08:00
|
|
|
this._boundaryActionHandler("onFilter", filter);
|
2017-11-21 18:53:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|