discourse/app/assets/javascripts/admin/addon/controllers/admin-watched-words.js
Roman Rizzi 8b426431a4
DEV: Wrap Ember.run.debounce. (#11352)
We want to wrap the `Ember.run.debounce` function and internally call `Ember.run` instead when running tests.

This commit changes discourseDebounce to work the same way as `Ember.run.debounce`.

Now that `discourseDebounce` works exactly like `Ember.run.debounce`, let's replace it and only use `DiscourseDebounce` from now on.

Move debounce to discourse-common to be able to reuse it in different bundles

Keep old debounce file for backwards-compatibility
2020-12-10 11:01:42 -03:00

72 lines
1.7 KiB
JavaScript

import Controller from "@ember/controller";
import EmberObject from "@ember/object";
import { INPUT_DELAY } from "discourse-common/config/environment";
import { alias } from "@ember/object/computed";
import discourseDebounce from "discourse-common/lib/debounce";
import { isEmpty } from "@ember/utils";
import { observes } from "discourse-common/utils/decorators";
export default Controller.extend({
filter: null,
filtered: false,
showWords: false,
disableShowWords: alias("filtered"),
regularExpressions: null,
filterContentNow() {
if (!!isEmpty(this.allWatchedWords)) {
return;
}
let filter;
if (this.filter) {
filter = this.filter.toLowerCase();
}
if (filter === undefined || filter.length < 1) {
this.set("model", this.allWatchedWords);
return;
}
const matchesByAction = [];
this.allWatchedWords.forEach((wordsForAction) => {
const wordRecords = wordsForAction.words.filter((wordRecord) => {
return wordRecord.word.indexOf(filter) > -1;
});
matchesByAction.pushObject(
EmberObject.create({
nameKey: wordsForAction.nameKey,
name: wordsForAction.name,
words: wordRecords,
count: wordRecords.length,
})
);
});
this.set("model", matchesByAction);
},
@observes("filter")
filterContent() {
discourseDebounce(
this,
function () {
this.filterContentNow();
this.set("filtered", !isEmpty(this.filter));
},
INPUT_DELAY
);
},
actions: {
clearFilter() {
this.setProperties({ filter: "" });
},
toggleMenu() {
$(".admin-detail").toggleClass("mobile-closed mobile-open");
},
},
});