2021-02-15 09:16:19 +01:00
|
|
|
import { A } from "@ember/array";
|
2021-02-16 01:43:00 +01:00
|
|
|
import EmberObject, { computed } from "@ember/object";
|
2021-04-12 08:26:22 +02:00
|
|
|
import { and, equal, notEmpty } from "@ember/object/computed";
|
2024-11-22 17:11:51 +01:00
|
|
|
import { cancel, later } from "@ember/runloop";
|
2021-02-22 11:55:47 +01:00
|
|
|
import { dasherize } from "@ember/string";
|
2024-11-22 17:11:51 +01:00
|
|
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
|
|
|
import { deepMerge } from "discourse-common/lib/object";
|
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
|
|
|
import WizardFieldValidator from "discourse/plugins/discourse-custom-wizard/discourse/components/validator";
|
2021-02-01 14:58:37 +01:00
|
|
|
|
|
|
|
export default WizardFieldValidator.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
classNames: ["similar-topics-validator"],
|
2021-02-16 01:43:00 +01:00
|
|
|
similarTopics: null,
|
2021-03-28 11:06:49 +02:00
|
|
|
hasInput: notEmpty("field.value"),
|
|
|
|
hasSimilarTopics: notEmpty("similarTopics"),
|
|
|
|
hasNotSearched: equal("similarTopics", null),
|
|
|
|
noSimilarTopics: computed("similarTopics", function () {
|
2021-04-12 08:26:22 +02:00
|
|
|
return this.similarTopics !== null && this.similarTopics.length === 0;
|
2021-02-16 01:43:00 +01:00
|
|
|
}),
|
2021-03-28 11:06:49 +02:00
|
|
|
showSimilarTopics: computed("typing", "hasSimilarTopics", function () {
|
2021-02-17 05:49:20 +01:00
|
|
|
return this.hasSimilarTopics && !this.typing;
|
|
|
|
}),
|
2021-03-28 11:06:49 +02:00
|
|
|
showNoSimilarTopics: computed("typing", "noSimilarTopics", function () {
|
2021-02-17 05:49:20 +01:00
|
|
|
return this.noSimilarTopics && !this.typing;
|
|
|
|
}),
|
2021-03-28 11:06:49 +02:00
|
|
|
hasValidationCategories: notEmpty("validationCategories"),
|
|
|
|
insufficientCharacters: computed("typing", "field.value", function () {
|
2021-03-25 07:03:12 +01:00
|
|
|
return this.hasInput && this.field.value.length < 5 && !this.typing;
|
|
|
|
}),
|
2021-03-28 11:06:49 +02:00
|
|
|
insufficientCharactersCategories: and(
|
|
|
|
"insufficientCharacters",
|
|
|
|
"hasValidationCategories"
|
|
|
|
),
|
|
|
|
|
|
|
|
@discourseComputed("validation.categories")
|
2021-02-18 04:40:59 +01:00
|
|
|
validationCategories(categoryIds) {
|
2021-04-12 08:26:22 +02:00
|
|
|
if (categoryIds) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return categoryIds.map((id) => this.site.categoriesById[id]);
|
2021-04-12 08:26:22 +02:00
|
|
|
}
|
2021-02-22 10:11:18 +01:00
|
|
|
|
|
|
|
return A();
|
2021-02-18 04:40:59 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
@discourseComputed("validationCategories")
|
2021-02-18 04:40:59 +01:00
|
|
|
catLinks(categories) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return categories.map((category) => categoryBadgeHTML(category)).join("");
|
2021-02-18 04:40:59 +01:00
|
|
|
},
|
2021-02-22 11:55:47 +01:00
|
|
|
|
|
|
|
@discourseComputed(
|
2021-03-28 11:06:49 +02:00
|
|
|
"loading",
|
|
|
|
"showSimilarTopics",
|
|
|
|
"showNoSimilarTopics",
|
|
|
|
"insufficientCharacters",
|
|
|
|
"insufficientCharactersCategories"
|
2021-02-22 11:55:47 +01:00
|
|
|
)
|
|
|
|
currentState(
|
|
|
|
loading,
|
|
|
|
showSimilarTopics,
|
|
|
|
showNoSimilarTopics,
|
2021-03-25 06:48:33 +01:00
|
|
|
insufficientCharacters,
|
|
|
|
insufficientCharactersCategories
|
2021-02-22 11:55:47 +01:00
|
|
|
) {
|
|
|
|
switch (true) {
|
|
|
|
case loading:
|
2021-03-28 11:06:49 +02:00
|
|
|
return "loading";
|
2021-02-22 11:55:47 +01:00
|
|
|
case showSimilarTopics:
|
2021-03-28 11:06:49 +02:00
|
|
|
return "results";
|
2021-02-22 11:55:47 +01:00
|
|
|
case showNoSimilarTopics:
|
2021-03-28 11:06:49 +02:00
|
|
|
return "no_results";
|
2021-03-25 06:48:33 +01:00
|
|
|
case insufficientCharactersCategories:
|
2021-03-28 11:06:49 +02:00
|
|
|
return "insufficient_characters_categories";
|
2021-03-25 06:48:33 +01:00
|
|
|
case insufficientCharacters:
|
2021-03-28 11:06:49 +02:00
|
|
|
return "insufficient_characters";
|
2021-02-22 11:55:47 +01:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
@discourseComputed("currentState")
|
|
|
|
currentStateClass(currentState) {
|
2021-04-12 08:26:22 +02:00
|
|
|
if (currentState) {
|
|
|
|
return `similar-topics-${dasherize(currentState)}`;
|
|
|
|
}
|
2021-02-22 11:55:47 +01:00
|
|
|
|
|
|
|
return "similar-topics";
|
|
|
|
},
|
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
@discourseComputed("currentState")
|
|
|
|
currentStateKey(currentState) {
|
2021-04-12 08:26:22 +02:00
|
|
|
if (currentState) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return `realtime_validations.similar_topics.${currentState}`;
|
2021-04-12 08:26:22 +02:00
|
|
|
}
|
2021-02-22 11:55:47 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
validate() {},
|
2021-02-15 09:17:37 +01:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
@observes("field.value")
|
|
|
|
customValidate() {
|
2021-02-16 01:43:00 +01:00
|
|
|
const field = this.field;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-03-25 12:01:26 +01:00
|
|
|
if (!field.value) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("similarTopics", null);
|
2021-03-25 12:01:26 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-02-16 01:43:00 +01:00
|
|
|
const value = field.value;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-02-17 05:49:20 +01:00
|
|
|
this.set("typing", true);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
const lastKeyUp = new Date();
|
|
|
|
this._lastKeyUp = lastKeyUp;
|
2021-02-05 13:59:30 +01:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
// One second from now, check to see if the last key was hit when
|
|
|
|
// we recorded it. If it was, the user paused typing.
|
|
|
|
cancel(this._lastKeyTimeout);
|
|
|
|
this._lastKeyTimeout = later(() => {
|
|
|
|
if (lastKeyUp !== this._lastKeyUp) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-17 05:49:20 +01:00
|
|
|
this.set("typing", false);
|
2021-03-25 06:48:33 +01:00
|
|
|
|
|
|
|
if (value && value.length < 5) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("similarTopics", null);
|
2021-03-25 06:48:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
this.updateSimilarTopics();
|
|
|
|
}, 1000);
|
|
|
|
},
|
2021-02-01 14:58:37 +01:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
updateSimilarTopics() {
|
2021-03-31 08:24:29 +02:00
|
|
|
this.set("similarTopics", null);
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("updating", true);
|
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
this.backendValidate({
|
|
|
|
title: this.get("field.value"),
|
|
|
|
categories: this.get("validation.categories"),
|
2021-02-25 11:06:43 +01:00
|
|
|
time_unit: this.get("validation.time_unit"),
|
2021-03-28 11:06:49 +02:00
|
|
|
time_n_value: this.get("validation.time_n_value"),
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
const similarTopics = A(
|
|
|
|
deepMerge(result["topics"], result["similar_topics"])
|
|
|
|
);
|
|
|
|
similarTopics.forEach(function (topic, index) {
|
|
|
|
similarTopics[index] = EmberObject.create(topic);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set("similarTopics", similarTopics);
|
|
|
|
})
|
|
|
|
.finally(() => this.set("updating", false));
|
2021-02-15 09:16:19 +01:00
|
|
|
},
|
2021-02-15 09:17:37 +01:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
actions: {
|
|
|
|
closeMessage() {
|
|
|
|
this.set("showMessage", false);
|
2021-02-05 13:59:30 +01:00
|
|
|
},
|
2021-02-15 09:16:19 +01:00
|
|
|
},
|
|
|
|
});
|