FIX: makes state of select-box more resilient to model changes

This commit is contained in:
Joffrey JAFFEUX 2017-09-13 18:44:40 +02:00 committed by GitHub
parent eb61ad0114
commit 7a4ddc2bb1
7 changed files with 79 additions and 60 deletions

View File

@ -1,7 +1,6 @@
import DropdownSelectBoxComponent from "discourse/components/dropdown-select-box"; import DropdownSelectBoxComponent from "discourse/components/dropdown-select-box";
import { iconHTML } from "discourse-common/lib/icon-library"; import { iconHTML } from "discourse-common/lib/icon-library";
import computed from "ember-addons/ember-computed-decorators"; import computed from "ember-addons/ember-computed-decorators";
import { observes } from "ember-addons/ember-computed-decorators";
export default DropdownSelectBoxComponent.extend({ export default DropdownSelectBoxComponent.extend({
classNames: ["categories-admin-dropdown"], classNames: ["categories-admin-dropdown"],
@ -39,9 +38,12 @@ export default DropdownSelectBoxComponent.extend({
reorder: "reorderCategories" reorder: "reorderCategories"
}, },
@observes("value") actions: {
_didSelectRow() { onSelectRow(content) {
this.sendAction(`actionNames.${this.get("value")}`); this._super(content);
this.set("value", null);
this.sendAction(`actionNames.${this.get("value")}`);
this.set("value", null);
}
} }
}); });

View File

@ -1,5 +1,4 @@
import NotificationOptionsComponent from "discourse/components/notifications-button"; import NotificationOptionsComponent from "discourse/components/notifications-button";
import { observes } from "ember-addons/ember-computed-decorators";
import computed from "ember-addons/ember-computed-decorators"; import computed from "ember-addons/ember-computed-decorators";
import { iconHTML } from "discourse-common/lib/icon-library"; import { iconHTML } from "discourse-common/lib/icon-library";
@ -19,8 +18,11 @@ export default NotificationOptionsComponent.extend({
generatedHeadertext: null, generatedHeadertext: null,
@observes("value") actions: {
_notificationLevelChanged() { onSelectRow(content) {
this.get("category").setNotification(this.get("value")); this._super(content);
},
this.get("category").setNotification(this.get("value"));
}
}
}); });

View File

@ -1,5 +1,4 @@
import NotificationOptionsComponent from "discourse/components/notifications-button"; import NotificationOptionsComponent from "discourse/components/notifications-button";
import { observes } from "ember-addons/ember-computed-decorators";
export default NotificationOptionsComponent.extend({ export default NotificationOptionsComponent.extend({
classNames: ["group-notifications-button"], classNames: ["group-notifications-button"],
@ -8,8 +7,11 @@ export default NotificationOptionsComponent.extend({
i18nPrefix: "groups.notifications", i18nPrefix: "groups.notifications",
@observes("value") actions: {
_notificationLevelChanged() { onSelectRow(content) {
this.get("group").setNotification(this.get("value"), this.get("user.id")); this._super(content);
this.get("group").setNotification(this.get("value"), this.get("user.id"));
}
} }
}); });

View File

@ -57,14 +57,17 @@ export default DropdownSelectBoxComponent.extend({
return `${title}${iconHTML("caret-down")}`.htmlSafe(); return `${title}${iconHTML("caret-down")}`.htmlSafe();
}, },
@observes("value") actions: {
_didSelectRow() { onSelectRow(content) {
const topic = this.get("topic"); this._super(content);
if (this.get("value") === "unpinned") { const topic = this.get("topic");
topic.clearPin();
} else { if (this.get("value") === "unpinned") {
topic.rePin(); topic.clearPin();
} else {
topic.rePin();
}
} }
} }
}); });

View File

@ -215,10 +215,7 @@ export default Ember.Component.extend({
if (this.get("expanded")) { if (this.get("expanded")) {
if ((keyCode === 13 || keyCode === 9) && Ember.isPresent(this.get("highlightedValue"))) { if ((keyCode === 13 || keyCode === 9) && Ember.isPresent(this.get("highlightedValue"))) {
event.preventDefault(); event.preventDefault();
this.setProperties({ this.send("onSelectRow", this.get("highlightedContent"));
value: this._castInteger(this.get("highlightedValue")),
expanded: false
});
} }
if (keyCode === 9) { if (keyCode === 9) {
@ -301,14 +298,25 @@ export default Ember.Component.extend({
}; };
}, },
@computed("value", "content.[]") @computed("value", "content.[]", "idKey")
selectedContent(value, content) { selectedContent(value, content, idKey) {
if (Ember.isNone(value)) { if (Ember.isNone(value)) {
return null; return null;
} }
return content.find((c) => { return content.find((c) => {
return this._castInteger(c[this.get("idKey")]) === value; return this._castInteger(Ember.get(c, idKey)) === value;
});
},
@computed("highlightedValue", "content.[]", "idKey")
highlightedContent(highlightedValue, content, idKey) {
if (Ember.isNone(highlightedValue)) {
return null;
}
return content.find((c) => {
return this._castInteger(Ember.get(c, idKey)) === highlightedValue;
}); });
}, },
@ -366,13 +374,13 @@ export default Ember.Component.extend({
}, },
onHoverRow(content) { onHoverRow(content) {
const id = this._castInteger(content[this.get("idKey")]); const id = this._castInteger(Ember.get(content, this.get("idKey")));
this.set("highlightedValue", id); this.set("highlightedValue", id);
}, },
onSelectRow(content) { onSelectRow(content) {
this.setProperties({ this.setProperties({
value: this._castInteger(content[this.get("idKey")]), value: this._castInteger(Ember.get(content, this.get("idKey"))),
expanded: false expanded: false
}); });
}, },
@ -444,15 +452,15 @@ export default Ember.Component.extend({
if (direction === "down") { if (direction === "down") {
if (currentIndex < 0) { if (currentIndex < 0) {
this.set("highlightedValue", this._castInteger(content[0][idKey])); this.set("highlightedValue", this._castInteger(Ember.get(content[0], idKey)));
} else if(currentIndex + 1 < content.length) { } else if(currentIndex + 1 < content.length) {
this.set("highlightedValue", this._castInteger(content[currentIndex + 1][idKey])); this.set("highlightedValue", this._castInteger(Ember.get(content[currentIndex + 1], idKey)));
} }
} else { } else {
if (currentIndex <= 0) { if (currentIndex <= 0) {
this.set("highlightedValue", this._castInteger(content[0][idKey])); this.set("highlightedValue", this._castInteger(Ember.get(content[0], idKey)));
} else if(currentIndex - 1 < content.length) { } else if(currentIndex - 1 < content.length) {
this.set("highlightedValue", this._castInteger(content[currentIndex - 1][idKey])); this.set("highlightedValue", this._castInteger(Ember.get(content[currentIndex - 1], idKey)));
} }
} }

View File

@ -1,5 +1,4 @@
import NotificationOptionsComponent from "discourse/components/notifications-button"; import NotificationOptionsComponent from "discourse/components/notifications-button";
import { observes } from "ember-addons/ember-computed-decorators";
import computed from "ember-addons/ember-computed-decorators"; import computed from "ember-addons/ember-computed-decorators";
import { iconHTML } from "discourse-common/lib/icon-library"; import { iconHTML } from "discourse-common/lib/icon-library";
@ -8,15 +7,18 @@ export default NotificationOptionsComponent.extend({
i18nPrefix: "tagging.notifications", i18nPrefix: "tagging.notifications",
@observes("value")
_notificationLevelChanged() {
this.sendAction("action", this.get("value"));
},
@computed("value") @computed("value")
icon() { icon() {
return `${this._super()}${iconHTML("caret-down")}`.htmlSafe(); return `${this._super()}${iconHTML("caret-down")}`.htmlSafe();
}, },
generatedHeadertext: null generatedHeadertext: null,
actions: {
onSelectRow(content) {
this._super(content);
this.sendAction("action", this.get("value"));
}
}
}); });

View File

@ -1,5 +1,5 @@
import NotificationOptionsComponent from "discourse/components/notifications-button"; import NotificationOptionsComponent from "discourse/components/notifications-button";
import { observes, on } from "ember-addons/ember-computed-decorators"; import { on } from "ember-addons/ember-computed-decorators";
import computed from "ember-addons/ember-computed-decorators"; import computed from "ember-addons/ember-computed-decorators";
import { topicLevels, buttonDetails } from "discourse/lib/notification-levels"; import { topicLevels, buttonDetails } from "discourse/lib/notification-levels";
@ -7,18 +7,16 @@ export default NotificationOptionsComponent.extend({
classNames: ["topic-notifications-options"], classNames: ["topic-notifications-options"],
content: topicLevels, content: topicLevels,
i18nPrefix: "topic.notifications", i18nPrefix: "topic.notifications",
@on("init") value: Ember.computed.alias("topic.details.notification_level"),
_setInitialNotificationLevel() {
this.set("value", this.get("topic.details.notification_level"));
},
@on("didInsertElement") @on("didInsertElement")
_bindGlobalLevelChanged() { _bindGlobalLevelChanged() {
this.appEvents.on("topic-notifications-button:changed", (msg) => { this.appEvents.on("topic-notifications-button:changed", (msg) => {
if (msg.type === "notification") { if (msg.type === "notification") {
if (this.get("topic.details.notification_level") !== msg.id) { if (this.get("value") !== msg.id) {
this.get("topic.details").updateNotifications(msg.id); this.get("topic.details").updateNotifications(msg.id);
} }
} }
@ -30,23 +28,25 @@ export default NotificationOptionsComponent.extend({
this.appEvents.off("topic-notifications-button:changed"); this.appEvents.off("topic-notifications-button:changed");
}, },
@observes("value") @computed("value", "showFullTitle")
_notificationLevelChanged() { generatedHeadertext(value, showFullTitle) {
this.appEvents.trigger("topic-notifications-button:changed", {type: "notification", id: this.get("value")});
},
@observes("topic.details.notification_level")
_content() {
this.set("value", this.get("topic.details.notification_level"));
},
@computed("topic.details.notification_level", "showFullTitle")
generatedHeadertext(notificationLevel, showFullTitle) {
if (showFullTitle) { if (showFullTitle) {
const details = buttonDetails(notificationLevel); const details = buttonDetails(value);
return I18n.t(`topic.notifications.${details.key}.title`); return I18n.t(`topic.notifications.${details.key}.title`);
} else { } else {
return null; return null;
} }
},
actions: {
onSelectRow(content) {
const notificationLevelId = Ember.get(content, this.get("idKey"));
if (notificationLevelId !== this.get("value")) {
this.get("topic.details").updateNotifications(notificationLevelId);
}
this._super(content);
}
} }
}); });