REFACTOR: user-selector (#7529)
This commit is contained in:
parent
2b28abdc21
commit
9a56df89f0
|
@ -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 TextField from "discourse/components/text-field";
|
||||||
import userSearch from "discourse/lib/user-search";
|
import userSearch from "discourse/lib/user-search";
|
||||||
import { findRawTemplate } from "discourse/lib/raw-templates";
|
import { findRawTemplate } from "discourse/lib/raw-templates";
|
||||||
|
@ -10,20 +10,24 @@ export default TextField.extend({
|
||||||
|
|
||||||
@observes("usernames")
|
@observes("usernames")
|
||||||
_update() {
|
_update() {
|
||||||
if (this.get("canReceiveUpdates") === "true")
|
if (this.canReceiveUpdates === "true") {
|
||||||
this.didInsertElement({ updateData: true });
|
this._createAutocompleteInstance({ updateData: true });
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
didInsertElement(opts) {
|
@on("willDestroyElement")
|
||||||
this._super(...arguments);
|
_destroyAutocompleteInstance() {
|
||||||
|
$(this.element).autocomplete("destroy");
|
||||||
|
},
|
||||||
|
|
||||||
|
@on("didInsertElement")
|
||||||
|
_createAutocompleteInstance(opts) {
|
||||||
const bool = n => {
|
const bool = n => {
|
||||||
const val = this.get(n);
|
const val = this[n];
|
||||||
return val === true || val === "true";
|
return val === true || val === "true";
|
||||||
};
|
};
|
||||||
|
|
||||||
var self = this,
|
let selected = [],
|
||||||
selected = [],
|
|
||||||
groups = [],
|
groups = [],
|
||||||
currentUser = this.currentUser,
|
currentUser = this.currentUser,
|
||||||
includeMentionableGroups = bool("includeMentionableGroups"),
|
includeMentionableGroups = bool("includeMentionableGroups"),
|
||||||
|
@ -37,39 +41,38 @@ export default TextField.extend({
|
||||||
allowEmails = bool("allowEmails"),
|
allowEmails = bool("allowEmails"),
|
||||||
fullWidthWrap = bool("fullWidthWrap");
|
fullWidthWrap = bool("fullWidthWrap");
|
||||||
|
|
||||||
function excludedUsernames() {
|
const excludedUsernames = () => {
|
||||||
// hack works around some issues with allowAny eventing
|
// hack works around some issues with allowAny eventing
|
||||||
const usernames = single ? [] : selected;
|
const usernames = single ? [] : selected;
|
||||||
|
|
||||||
if (currentUser && excludeCurrentUser) {
|
if (currentUser && excludeCurrentUser) {
|
||||||
return usernames.concat([currentUser.get("username")]);
|
return usernames.concat([currentUser.username]);
|
||||||
}
|
}
|
||||||
return usernames;
|
return usernames;
|
||||||
}
|
};
|
||||||
|
|
||||||
this.$()
|
$(this.element)
|
||||||
.val(this.get("usernames"))
|
.val(this.usernames)
|
||||||
.autocomplete({
|
.autocomplete({
|
||||||
template: findRawTemplate("user-selector-autocomplete"),
|
template: findRawTemplate("user-selector-autocomplete"),
|
||||||
disabled: disabled,
|
disabled,
|
||||||
single: single,
|
single,
|
||||||
allowAny: allowAny,
|
allowAny,
|
||||||
updateData: opts && opts.updateData ? opts.updateData : false,
|
updateData: opts && opts.updateData ? opts.updateData : false,
|
||||||
fullWidthWrap,
|
fullWidthWrap,
|
||||||
|
|
||||||
dataSource(term) {
|
dataSource(term) {
|
||||||
var results = userSearch({
|
return userSearch({
|
||||||
term,
|
term,
|
||||||
topicId: self.get("topicId"),
|
topicId: this.topicId,
|
||||||
exclude: excludedUsernames(),
|
exclude: excludedUsernames(),
|
||||||
includeGroups,
|
includeGroups,
|
||||||
allowedUsers,
|
allowedUsers,
|
||||||
includeMentionableGroups,
|
includeMentionableGroups,
|
||||||
includeMessageableGroups,
|
includeMessageableGroups,
|
||||||
group: self.get("group"),
|
group: this.group,
|
||||||
allowEmails
|
allowEmails
|
||||||
});
|
});
|
||||||
return results;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
transformComplete(v) {
|
transformComplete(v) {
|
||||||
|
@ -79,16 +82,14 @@ export default TextField.extend({
|
||||||
}
|
}
|
||||||
return v.username || v.name;
|
return v.username || v.name;
|
||||||
} else {
|
} else {
|
||||||
var excludes = excludedUsernames();
|
const excludes = excludedUsernames();
|
||||||
return v.usernames.filter(function(item) {
|
return v.usernames.filter(item => excludes.indexOf(item) === -1);
|
||||||
return excludes.indexOf(item) === -1;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onChangeItems(items) {
|
onChangeItems(items) {
|
||||||
var hasGroups = false;
|
let hasGroups = false;
|
||||||
items = items.map(function(i) {
|
items = items.map(i => {
|
||||||
if (groups.indexOf(i) > -1) {
|
if (groups.indexOf(i) > -1) {
|
||||||
hasGroups = true;
|
hasGroups = true;
|
||||||
}
|
}
|
||||||
|
@ -96,18 +97,20 @@ export default TextField.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
let previouslySelected = [];
|
let previouslySelected = [];
|
||||||
if (Array.isArray(self.get("usernames"))) {
|
if (Array.isArray(this.usernames)) {
|
||||||
previouslySelected = self.get("usernames");
|
previouslySelected = this.usernames;
|
||||||
} else {
|
} else {
|
||||||
if (self.get("usernames")) {
|
if (this.usernames) {
|
||||||
previouslySelected = self.get("usernames").split(",");
|
previouslySelected = this.usernames.split(",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.set("usernames", items.join(","));
|
|
||||||
self.set("hasGroups", hasGroups);
|
this.setProperties({ usernames: items.join(","), hasGroups });
|
||||||
selected = items;
|
selected = items;
|
||||||
if (self.get("onChangeCallback"))
|
|
||||||
self.onChangeCallback(previouslySelected, selected);
|
if (this.onChangeCallback) {
|
||||||
|
this.onChangeCallback(previouslySelected, selected);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
reverseTransform(i) {
|
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
|
// THIS IS A HUGE HACK TO SUPPORT CLEARING THE INPUT
|
||||||
@observes("usernames")
|
@observes("usernames")
|
||||||
_clearInput: function() {
|
_clearInput() {
|
||||||
if (arguments.length > 1) {
|
if (arguments.length > 1 && Ember.isEmpty(this.usernames)) {
|
||||||
if (Ember.isEmpty(this.get("usernames"))) {
|
$(this.element)
|
||||||
this.$()
|
.parent()
|
||||||
.parent()
|
.find("a")
|
||||||
.find("a")
|
.click();
|
||||||
.click();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue