2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2020-02-03 21:22:14 +08:00
|
|
|
import { computed } from "@ember/object";
|
2019-11-09 00:32:20 +08:00
|
|
|
import { equal } from "@ember/object/computed";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2017-11-21 18:53:09 +08:00
|
|
|
import ComboBoxComponent from "select-kit/components/combo-box";
|
2017-10-20 03:51:08 +08:00
|
|
|
import { CLOSE_STATUS_TYPE } from "discourse/controllers/edit-topic-timer";
|
2017-11-21 18:53:09 +08:00
|
|
|
import DatetimeMixin from "select-kit/components/future-date-input-selector/mixin";
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-10-27 01:50:31 +08:00
|
|
|
const TIMEFRAME_BASE = {
|
|
|
|
enabled: () => true,
|
|
|
|
when: () => null,
|
|
|
|
icon: "briefcase",
|
|
|
|
displayWhen: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
function buildTimeframe(opts) {
|
|
|
|
return jQuery.extend({}, TIMEFRAME_BASE, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TIMEFRAMES = [
|
2020-06-15 12:06:03 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "now",
|
|
|
|
format: "h:mm a",
|
|
|
|
enabled: (opts) => opts.canScheduleNow,
|
|
|
|
when: (time) => time.add(1, "minute"),
|
|
|
|
icon: "magic",
|
|
|
|
}),
|
2017-10-27 01:50:31 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "later_today",
|
|
|
|
format: "h a",
|
|
|
|
enabled: (opts) => opts.canScheduleToday,
|
|
|
|
when: (time) => time.hour(18).minute(0),
|
2019-01-22 19:02:02 +08:00
|
|
|
icon: "far-moon",
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "tomorrow",
|
|
|
|
format: "ddd, h a",
|
|
|
|
when: (time, timeOfDay) => time.add(1, "day").hour(timeOfDay).minute(0),
|
2019-01-22 19:02:02 +08:00
|
|
|
icon: "far-sun",
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "later_this_week",
|
|
|
|
format: "ddd, h a",
|
|
|
|
enabled: (opts) => !opts.canScheduleToday && opts.day < 4,
|
|
|
|
when: (time, timeOfDay) => time.add(2, "day").hour(timeOfDay).minute(0),
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "this_weekend",
|
|
|
|
format: "ddd, h a",
|
|
|
|
enabled: (opts) => opts.day < 5 && opts.includeWeekend,
|
|
|
|
when: (time, timeOfDay) => time.day(6).hour(timeOfDay).minute(0),
|
|
|
|
icon: "bed",
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "next_week",
|
|
|
|
format: "ddd, h a",
|
|
|
|
enabled: (opts) => opts.day !== 7,
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(1, "week").day(1).hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "two_weeks",
|
|
|
|
format: "MMM D",
|
|
|
|
when: (time, timeOfDay) => time.add(2, "week").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "next_month",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: (opts) => opts.now.date() !== moment().endOf("month").date(),
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(1, "month").startOf("month").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
2019-03-29 18:14:53 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "two_months",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: (opts) => opts.includeMidFuture,
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(2, "month").startOf("month").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
2017-10-27 01:50:31 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "three_months",
|
|
|
|
format: "MMM D",
|
2019-03-29 18:14:53 +08:00
|
|
|
enabled: (opts) => opts.includeMidFuture,
|
2017-10-27 01:50:31 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(3, "month").startOf("month").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
2019-03-29 18:14:53 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "four_months",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: (opts) => opts.includeMidFuture,
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(4, "month").startOf("month").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
2017-10-27 01:50:31 +08:00
|
|
|
buildTimeframe({
|
|
|
|
id: "six_months",
|
|
|
|
format: "MMM D",
|
2020-04-17 21:27:11 +08:00
|
|
|
enabled: (opts) => opts.includeMidFuture,
|
2017-10-27 01:50:31 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(6, "month").startOf("month").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "one_year",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: (opts) => opts.includeFarFuture,
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time.add(1, "year").startOf("day").hour(timeOfDay).minute(0),
|
|
|
|
icon: "briefcase",
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "forever",
|
|
|
|
enabled: (opts) => opts.includeFarFuture,
|
|
|
|
when: (time, timeOfDay) => time.add(1000, "year").hour(timeOfDay).minute(0),
|
|
|
|
icon: "gavel",
|
|
|
|
displayWhen: false,
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "pick_date_and_time",
|
2019-03-29 18:14:53 +08:00
|
|
|
enabled: (opts) => opts.includeDateTime,
|
2019-01-22 19:02:02 +08:00
|
|
|
icon: "far-calendar-plus",
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "set_based_on_last_post",
|
|
|
|
enabled: (opts) => opts.includeBasedOnLastPost,
|
2019-01-22 19:02:02 +08:00
|
|
|
icon: "far-clock",
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
let _timeframeById = null;
|
|
|
|
export function timeframeDetails(id) {
|
|
|
|
if (!_timeframeById) {
|
|
|
|
_timeframeById = {};
|
|
|
|
TIMEFRAMES.forEach((t) => (_timeframeById[t.id] = t));
|
|
|
|
}
|
|
|
|
return _timeframeById[id];
|
|
|
|
}
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-11-29 03:16:13 +08:00
|
|
|
export const FORMAT = "YYYY-MM-DD HH:mmZ";
|
2017-10-20 03:51:08 +08:00
|
|
|
|
|
|
|
export default ComboBoxComponent.extend(DatetimeMixin, {
|
2017-11-21 18:53:09 +08:00
|
|
|
pluginApiIdentifiers: ["future-date-input-selector"],
|
2017-10-20 03:51:08 +08:00
|
|
|
classNames: ["future-date-input-selector"],
|
2019-11-09 00:32:20 +08:00
|
|
|
isCustom: equal("value", "pick_date_and_time"),
|
|
|
|
isBasedOnLastPost: equal("value", "set_based_on_last_post"),
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
selectKitOptions: {
|
|
|
|
autoInsertNoneItem: false,
|
|
|
|
headerComponent:
|
|
|
|
"future-date-input-selector/future-date-input-selector-header",
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
modifyComponentForRow() {
|
|
|
|
return "future-date-input-selector/future-date-input-selector-row";
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
2020-03-19 23:36:31 +08:00
|
|
|
content: computed("statusType", function () {
|
2020-02-03 21:22:14 +08:00
|
|
|
const now = moment();
|
|
|
|
const opts = {
|
2017-10-27 01:50:31 +08:00
|
|
|
now,
|
|
|
|
day: now.day(),
|
2019-05-27 16:15:39 +08:00
|
|
|
includeWeekend: this.includeWeekend,
|
|
|
|
includeMidFuture: this.includeMidFuture || true,
|
|
|
|
includeFarFuture: this.includeFarFuture,
|
|
|
|
includeDateTime: this.includeDateTime,
|
|
|
|
includeBasedOnLastPost: this.statusType === CLOSE_STATUS_TYPE,
|
2020-06-15 12:06:03 +08:00
|
|
|
canScheduleNow: this.includeNow || false,
|
2017-10-27 01:50:31 +08:00
|
|
|
canScheduleToday: 24 - now.hour() > 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
return TIMEFRAMES.filter((tf) => tf.enabled(opts)).map((tf) => {
|
|
|
|
return {
|
|
|
|
id: tf.id,
|
2020-02-03 21:22:14 +08:00
|
|
|
name: I18n.t(`topic.auto_update_input.${tf.id}`),
|
|
|
|
datetime: this._computeDatetimeForValue(tf.id),
|
|
|
|
icons: this._computeIconsForValue(tf.id),
|
2017-10-27 01:50:31 +08:00
|
|
|
};
|
2017-10-20 03:51:08 +08:00
|
|
|
});
|
2020-02-03 21:22:14 +08:00
|
|
|
}),
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
actions: {
|
|
|
|
onChange(value) {
|
|
|
|
if (value !== "pick_date_and_time" || !this.isBasedOnLastPost) {
|
|
|
|
const { time } = this._updateAt(value);
|
|
|
|
if (time && !isEmpty(value)) {
|
|
|
|
this.attrs.onChangeInput &&
|
|
|
|
this.attrs.onChangeInput(time.locale("en").format(FORMAT));
|
|
|
|
}
|
2017-11-23 22:18:27 +08:00
|
|
|
}
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2020-02-03 21:22:14 +08:00
|
|
|
this.attrs.onChange && this.attrs.onChange(value);
|
2017-11-23 22:18:27 +08:00
|
|
|
},
|
2017-10-20 03:51:08 +08:00
|
|
|
},
|
|
|
|
});
|