1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/wizard/components/similar-topics-validator.js.es6

150 Zeilen
4,2 KiB
Text

import WizardFieldValidator from "../../wizard/components/validator";
import { deepMerge } from "discourse-common/lib/object";
import { observes } from "discourse-common/utils/decorators";
import { cancel, later } from "@ember/runloop";
import { A } from "@ember/array";
import EmberObject, { computed } from "@ember/object";
import { notEmpty, and, equal, empty } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators";
import { categoryBadgeHTML } from "discourse/helpers/category-link";
import { dasherize } from "@ember/string";
export default WizardFieldValidator.extend({
2021-02-24 08:43:35 +01:00
classNames: ["similar-topics-validator"],
similarTopics: null,
2021-02-24 08:43:35 +01:00
hasInput: notEmpty("field.value"),
hasSimilarTopics: notEmpty("similarTopics"),
hasNotSearched: equal("similarTopics", null),
noSimilarTopics: computed("similarTopics", function () {
return this.similarTopics !== null && this.similarTopics.length == 0;
}),
2021-02-24 08:43:35 +01:00
showDefault: computed("hasNotSearched", "hasInput", "typing", function () {
return this.hasInput && (this.hasNotSearched || this.typing);
}),
2021-02-24 08:43:35 +01:00
showSimilarTopics: computed("typing", "hasSimilarTopics", function () {
return this.hasSimilarTopics && !this.typing;
}),
2021-02-24 08:43:35 +01:00
showNoSimilarTopics: computed("typing", "noSimilarTopics", function () {
return this.noSimilarTopics && !this.typing;
}),
2021-02-24 08:43:35 +01:00
hasValidationCategories: notEmpty("validationCategories"),
showValidationCategories: and("showDefault", "hasValidationCategories"),
@discourseComputed("validation.categories")
validationCategories(categoryIds) {
2021-02-24 08:43:35 +01:00
if (categoryIds)
return categoryIds.map((id) => this.site.categoriesById[id]);
return A();
},
2021-02-24 08:43:35 +01:00
@discourseComputed("validationCategories")
catLinks(categories) {
2021-02-24 08:43:35 +01:00
return categories.map((category) => categoryBadgeHTML(category)).join("");
},
@discourseComputed(
2021-02-24 08:43:35 +01:00
"loading",
"showSimilarTopics",
"showNoSimilarTopics",
"showValidationCategories",
"showDefault"
)
currentState(
loading,
showSimilarTopics,
showNoSimilarTopics,
showValidationCategories,
showDefault
) {
switch (true) {
case loading:
2021-02-24 08:43:35 +01:00
return "loading";
case showSimilarTopics:
2021-02-24 08:43:35 +01:00
return "results";
case showNoSimilarTopics:
2021-02-24 08:43:35 +01:00
return "no_results";
case showValidationCategories:
2021-02-24 08:43:35 +01:00
return "default_categories";
case showDefault:
2021-02-24 08:43:35 +01:00
return "default";
default:
return false;
}
},
2021-02-24 08:43:35 +01:00
@discourseComputed("currentState")
currentStateClass(currentState) {
if (currentState) return `similar-topics-${dasherize(currentState)}`;
return "similar-topics";
},
2021-02-24 08:43:35 +01:00
@discourseComputed("currentState")
currentStateKey(currentState) {
if (currentState)
return `realtime_validations.similar_topics.${currentState}`;
return false;
},
validate() {},
2021-02-15 09:17:37 +01:00
@observes("field.value")
customValidate() {
const field = this.field;
2021-02-24 08:43:35 +01:00
if (!field.value) return;
const value = field.value;
2021-02-24 08:43:35 +01:00
this.set("typing", true);
2021-02-24 08:43:35 +01:00
if (value && value.length < 5) {
2021-02-24 08:43:35 +01:00
this.set("similarTopics", null);
return;
}
2021-02-24 08:43:35 +01:00
const lastKeyUp = new Date();
this._lastKeyUp = lastKeyUp;
// 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;
}
this.set("typing", false);
2021-02-24 08:43:35 +01:00
this.updateSimilarTopics();
}, 1000);
},
updateSimilarTopics() {
2021-02-24 08:43:35 +01:00
this.set("updating", true);
this.backendValidate({
title: this.get("field.value"),
categories: this.get("validation.categories"),
date_after: this.get("validation.date_after"),
2021-02-24 08:43:35 +01:00
})
.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:17:37 +01:00
actions: {
closeMessage() {
this.set("showMessage", false);
},
},
});