This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/components/future-date-input.js.es6
Jarek Radosz fe588cc7f8
DEV: Fix function prototype deprecations (#8681)
* DEV: Fix the function prototype observers deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.observes('foo') to observer('foo', function() {}). [deprecation id: function-prototype-extensions.observes] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-observes for more details.

* DEV: Fix the function prototype event listeners deprecation

DEPRECATION: Function prototype extensions have been deprecated, please migrate from function(){}.on('foo') to on('foo', function() {}). [deprecation id: function-prototype-extensions.on] See https://deprecations.emberjs.com/v3.x/#toc_function-prototype-extensions-on for more details.

* DEV: Simplify `default as` imports

Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
2020-01-16 18:56:53 +01:00

137 lines
3.3 KiB
JavaScript

import { isEmpty } from "@ember/utils";
import { equal, and, empty } from "@ember/object/computed";
import Component from "@ember/component";
import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { FORMAT } from "select-kit/components/future-date-input-selector";
import { PUBLISH_TO_CATEGORY_STATUS_TYPE } from "discourse/controllers/edit-topic-timer";
export default Component.extend({
selection: null,
date: null,
time: null,
includeDateTime: true,
isCustom: equal("selection", "pick_date_and_time"),
isBasedOnLastPost: equal("selection", "set_based_on_last_post"),
displayDateAndTimePicker: and("includeDateTime", "isCustom"),
displayLabel: null,
init() {
this._super(...arguments);
if (this.input) {
if (this.basedOnLastPost) {
this.set("selection", "set_based_on_last_post");
} else {
const datetime = moment(this.input);
this.setProperties({
selection: "pick_date_and_time",
date: datetime.format("YYYY-MM-DD"),
time: datetime.format("HH:mm")
});
this._updateInput();
}
}
},
timeInputDisabled: empty("date"),
@observes("date", "time")
_updateInput() {
if (!this.date) {
this.set("time", null);
}
const time = this.time ? ` ${this.time}` : "";
const dateTime = moment(`${this.date}${time}`);
if (dateTime.isValid()) {
this.set("input", dateTime.format(FORMAT));
} else {
this.set("input", null);
}
},
@observes("isBasedOnLastPost")
_updateBasedOnLastPost() {
this.set("basedOnLastPost", this.isBasedOnLastPost);
},
@discourseComputed("input", "isBasedOnLastPost")
duration(input, isBasedOnLastPost) {
const now = moment();
if (isBasedOnLastPost) {
return parseFloat(input);
} else {
return moment(input) - now;
}
},
@discourseComputed("input", "isBasedOnLastPost")
executeAt(input, isBasedOnLastPost) {
if (isBasedOnLastPost) {
return moment()
.add(input, "hours")
.format(FORMAT);
} else {
return input;
}
},
didReceiveAttrs() {
this._super(...arguments);
if (this.label) this.set("displayLabel", I18n.t(this.label));
},
@discourseComputed(
"statusType",
"input",
"isCustom",
"date",
"time",
"willCloseImmediately",
"categoryId"
)
showTopicStatusInfo(
statusType,
input,
isCustom,
date,
time,
willCloseImmediately,
categoryId
) {
if (!statusType || willCloseImmediately) return false;
if (statusType === PUBLISH_TO_CATEGORY_STATUS_TYPE && isEmpty(categoryId)) {
return false;
}
if (isCustom) {
return date || time;
} else {
return input;
}
},
@discourseComputed("isBasedOnLastPost", "input", "lastPostedAt")
willCloseImmediately(isBasedOnLastPost, input, lastPostedAt) {
if (isBasedOnLastPost && input) {
let closeDate = moment(lastPostedAt);
closeDate = closeDate.add(input, "hours");
return closeDate < moment();
}
},
@discourseComputed("isBasedOnLastPost", "lastPostedAt")
willCloseI18n(isBasedOnLastPost, lastPostedAt) {
if (isBasedOnLastPost) {
const diff = Math.round(
(new Date() - new Date(lastPostedAt)) / (1000 * 60 * 60)
);
return I18n.t("topic.auto_close_immediate", { count: diff });
}
}
});