2021-02-01 14:58:37 +01:00
|
|
|
import WizardFieldValidator from "../../wizard/components/validator";
|
2021-02-05 13:59:30 +01:00
|
|
|
import { deepMerge } from "discourse-common/lib/object";
|
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
|
|
|
import { cancel, later } from "@ember/runloop";
|
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";
|
|
|
|
import { notEmpty, and, equal, empty } from "@ember/object/computed";
|
2021-02-18 04:40:59 +01:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
|
|
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
2021-02-01 14:58:37 +01:00
|
|
|
|
|
|
|
export default WizardFieldValidator.extend({
|
2021-02-16 01:43:00 +01:00
|
|
|
classNames: ['similar-topics-validator'],
|
|
|
|
similarTopics: null,
|
|
|
|
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-17 05:49:20 +01:00
|
|
|
showDefault: computed('hasNotSearched', 'hasInput', 'typing', function() {
|
|
|
|
return this.hasInput && (this.hasNotSearched || this.typing);
|
|
|
|
}),
|
|
|
|
showSimilarTopics: computed('typing', 'hasSimilarTopics', function() {
|
|
|
|
return this.hasSimilarTopics && !this.typing;
|
|
|
|
}),
|
|
|
|
showNoSimilarTopics: computed('typing', 'noSimilarTopics', function() {
|
|
|
|
return this.noSimilarTopics && !this.typing;
|
|
|
|
}),
|
2021-02-18 04:40:59 +01:00
|
|
|
hasValidationCategories: notEmpty('validationCategories'),
|
|
|
|
|
|
|
|
@discourseComputed('validation.categories')
|
|
|
|
validationCategories(categoryIds) {
|
|
|
|
return categoryIds.map(id => this.site.categoriesById[id]);
|
|
|
|
},
|
|
|
|
|
|
|
|
@discourseComputed('validationCategories')
|
|
|
|
catLinks(categories) {
|
|
|
|
return categories.map(category => categoryBadgeHTML(category)).join("");
|
|
|
|
},
|
2021-02-17 05:49:20 +01:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (!field.value) return;
|
|
|
|
const value = field.value;
|
|
|
|
|
2021-02-17 05:49:20 +01:00
|
|
|
this.set("typing", true);
|
|
|
|
|
2021-02-16 01:43:00 +01:00
|
|
|
if (value && value.length < 5) {
|
|
|
|
this.set('similarTopics', null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-02-16 01:43:00 +01:00
|
|
|
|
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-02-16 01:43:00 +01: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"),
|
|
|
|
date_after: this.get("validation.date_after"),
|
|
|
|
}).then((result) => {
|
|
|
|
const similarTopics = A(
|
|
|
|
deepMerge(result["topics"], result["similar_topics"])
|
|
|
|
);
|
|
|
|
similarTopics.forEach(function (topic, index) {
|
|
|
|
similarTopics[index] = EmberObject.create(topic);
|
|
|
|
});
|
2021-02-05 13:59:30 +01:00
|
|
|
|
2021-02-15 09:16:19 +01:00
|
|
|
this.set("similarTopics", similarTopics);
|
2021-02-16 01:43:00 +01:00
|
|
|
}).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
|
|
|
},
|
|
|
|
});
|