Skip to content

A platform for community discussion. Free, open, simple.

REFACTOR: user-selector (#7529)

Joffrey JAFFEUX9a56df89f033
diff --git a/app/assets/javascripts/discourse/components/user-selector.js.es6 b/app/assets/javascripts/discourse/components/user-selector.js.es6
index 1bb77b9ff16..a4f2b76966f 100644
--- a/app/assets/javascripts/discourse/components/user-selector.js.es6
+++ b/app/assets/javascripts/discourse/components/user-selector.js.es6
@@ -1,4 +1,4 @@
-import { observes } from "ember-addons/ember-computed-decorators";
+import { on, observes } from "ember-addons/ember-computed-decorators";
import TextField from "discourse/components/text-field";
import userSearch from "discourse/lib/user-search";
import { findRawTemplate } from "discourse/lib/raw-templates";
@@ -10,20 +10,24 @@ export default TextField.extend({
@observes("usernames")
_update() {
- if (this.get("canReceiveUpdates") === "true")
- this.didInsertElement({ updateData: true });
+ if (this.canReceiveUpdates === "true") {
+ this._createAutocompleteInstance({ updateData: true });
+ }
},
- didInsertElement(opts) {
- this._super(...arguments);
+ @on("willDestroyElement")
+ _destroyAutocompleteInstance() {
+ $(this.element).autocomplete("destroy");
+ },
+ @on("didInsertElement")
+ _createAutocompleteInstance(opts) {
const bool = n => {
- const val = this.get(n);
+ const val = this[n];
return val === true || val === "true";
};
- var self = this,
- selected = [],
+ let selected = [],
groups = [],
currentUser = this.currentUser,
includeMentionableGroups = bool("includeMentionableGroups"),
@@ -37,39 +41,38 @@ export default TextField.extend({
allowEmails = bool("allowEmails"),
fullWidthWrap = bool("fullWidthWrap");
- function excludedUsernames() {
+ const excludedUsernames = () => {
// hack works around some issues with allowAny eventing
const usernames = single ? [] : selected;
if (currentUser && excludeCurrentUser) {
- return usernames.concat([currentUser.get("username")]);
+ return usernames.concat([currentUser.username]);
}
return usernames;
- }
+ };
- this.$()
- .val(this.get("usernames"))
+ $(this.element)
+ .val(this.usernames)
.autocomplete({
template: findRawTemplate("user-selector-autocomplete"),
- disabled: disabled,
- single: single,
- allowAny: allowAny,
+ disabled,
+ single,
+ allowAny,
updateData: opts && opts.updateData ? opts.updateData : false,
fullWidthWrap,
dataSource(term) {
- var results = userSearch({
+ return userSearch({
term,
- topicId: self.get("topicId"),
+ topicId: this.topicId,
exclude: excludedUsernames(),
includeGroups,
allowedUsers,
includeMentionableGroups,
includeMessageableGroups,
- group: self.get("group"),
+ group: this.group,
allowEmails
});
- return results;
},
transformComplete(v) {
@@ -79,16 +82,14 @@ export default TextField.extend({
}
return v.username || v.name;
} else {
- var excludes = excludedUsernames();
- return v.usernames.filter(function(item) {
- return excludes.indexOf(item) === -1;
- });
+ const excludes = excludedUsernames();
+ return v.usernames.filter(item => excludes.indexOf(item) === -1);
}
},
onChangeItems(items) {
- var hasGroups = false;
- items = items.map(function(i) {
+ let hasGroups = false;
+ items = items.map(i => {
if (groups.indexOf(i) > -1) {
hasGroups = true;
}
@@ -96,18 +97,20 @@ export default TextField.extend({
});
let previouslySelected = [];
- if (Array.isArray(self.get("usernames"))) {
- previouslySelected = self.get("usernames");
+ if (Array.isArray(this.usernames)) {
+ previouslySelected = this.usernames;
} else {
- if (self.get("usernames")) {
- previouslySelected = self.get("usernames").split(",");
+ if (this.usernames) {
+ previouslySelected = this.usernames.split(",");
}
}
- self.set("usernames", items.join(","));
- self.set("hasGroups", hasGroups);
+
+ this.setProperties({ usernames: items.join(","), hasGroups });
selected = items;
- if (self.get("onChangeCallback"))
- self.onChangeCallback(previouslySelected, selected);
+
+ if (this.onChangeCallback) {
+ this.onChangeCallback(previouslySelected, selected);
+ }
},
reverseTransform(i) {
@@ -116,21 +119,14 @@ export default TextField.extend({
});
},
- willDestroyElement() {
- this._super(...arguments);
- this.$().autocomplete("destroy");
- },
-
// THIS IS A HUGE HACK TO SUPPORT CLEARING THE INPUT
@observes("usernames")
- _clearInput: function() {
- if (arguments.length > 1) {
- if (Ember.isEmpty(this.get("usernames"))) {
- this.$()
- .parent()
- .find("a")
- .click();
- }
+ _clearInput() {
+ if (arguments.length > 1 && Ember.isEmpty(this.usernames)) {
+ $(this.element)
+ .parent()
+ .find("a")
+ .click();
}
}
});