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,
|
2018-06-15 23:03:24 +08:00
|
|
|
icon: "briefcase",
|
|
|
|
displayWhen: true
|
2017-10-27 01:50:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function buildTimeframe(opts) {
|
|
|
|
return jQuery.extend({}, TIMEFRAME_BASE, opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const TIMEFRAMES = [
|
|
|
|
buildTimeframe({
|
2018-06-15 23:03:24 +08:00
|
|
|
id: "later_today",
|
2017-10-27 01:50:31 +08:00
|
|
|
format: "h a",
|
|
|
|
enabled: opts => opts.canScheduleToday,
|
2018-06-15 23:03:24 +08:00
|
|
|
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",
|
2018-06-15 23:03:24 +08:00
|
|
|
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,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(2, "day")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0)
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "this_weekend",
|
|
|
|
format: "ddd, h a",
|
|
|
|
enabled: opts => opts.day < 5 && opts.includeWeekend,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.day(6)
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "bed"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "next_week",
|
|
|
|
format: "ddd, h a",
|
|
|
|
enabled: opts => opts.day !== 7,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(1, "week")
|
|
|
|
.day(1)
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "two_weeks",
|
|
|
|
format: "MMM D",
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(2, "week")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "next_month",
|
|
|
|
format: "MMM D",
|
2018-06-15 23:03:24 +08:00
|
|
|
enabled: opts =>
|
|
|
|
opts.now.date() !==
|
|
|
|
moment()
|
|
|
|
.endOf("month")
|
|
|
|
.date(),
|
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(1, "month")
|
|
|
|
.startOf("month")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "three_months",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: opts => opts.includeFarFuture,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(3, "month")
|
|
|
|
.startOf("month")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "six_months",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: opts => opts.includeFarFuture,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(6, "month")
|
|
|
|
.startOf("month")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "one_year",
|
|
|
|
format: "MMM D",
|
|
|
|
enabled: opts => opts.includeFarFuture,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(1, "year")
|
|
|
|
.startOf("day")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "briefcase"
|
2017-10-27 01:50:31 +08:00
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "forever",
|
|
|
|
enabled: opts => opts.includeFarFuture,
|
2018-06-15 23:03:24 +08:00
|
|
|
when: (time, timeOfDay) =>
|
|
|
|
time
|
|
|
|
.add(1000, "year")
|
|
|
|
.hour(timeOfDay)
|
|
|
|
.minute(0),
|
|
|
|
icon: "gavel",
|
2017-10-27 01:50:31 +08:00
|
|
|
displayWhen: false
|
|
|
|
}),
|
|
|
|
buildTimeframe({
|
|
|
|
id: "pick_date_and_time",
|
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"
|
2018-06-15 23:03:24 +08:00
|
|
|
})
|
2017-10-27 01:50:31 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
let _timeframeById = null;
|
|
|
|
export function timeframeDetails(id) {
|
|
|
|
if (!_timeframeById) {
|
|
|
|
_timeframeById = {};
|
2018-06-15 23:03:24 +08:00
|
|
|
TIMEFRAMES.forEach(t => (_timeframeById[t.id] = t));
|
2017-10-27 01:50:31 +08:00
|
|
|
}
|
|
|
|
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"],
|
2017-10-27 01:50:31 +08:00
|
|
|
isCustom: Ember.computed.equal("value", "pick_date_and_time"),
|
2017-11-24 09:48:45 +08:00
|
|
|
isBasedOnLastPost: Ember.computed.equal("value", "set_based_on_last_post"),
|
2017-10-20 03:51:08 +08:00
|
|
|
rowComponent: "future-date-input-selector/future-date-input-selector-row",
|
2018-06-15 23:03:24 +08:00
|
|
|
headerComponent:
|
|
|
|
"future-date-input-selector/future-date-input-selector-header",
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
computeHeaderContent() {
|
2019-01-19 17:05:51 +08:00
|
|
|
let content = this._super(...arguments);
|
2017-11-21 18:53:09 +08:00
|
|
|
content.datetime = this._computeDatetimeForValue(this.get("computedValue"));
|
2018-03-22 18:29:55 +08:00
|
|
|
content.name = this.get("selection.name") || content.name;
|
2017-11-21 18:53:09 +08:00
|
|
|
content.hasSelection = this.get("hasSelection");
|
|
|
|
content.icons = this._computeIconsForValue(this.get("computedValue"));
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
|
|
|
|
computeContentItem(contentItem, name) {
|
2018-03-22 18:29:55 +08:00
|
|
|
let computedContentItem = this._super(contentItem, name);
|
2018-06-15 23:03:24 +08:00
|
|
|
computedContentItem.datetime = this._computeDatetimeForValue(
|
|
|
|
contentItem.id
|
|
|
|
);
|
2018-03-22 18:29:55 +08:00
|
|
|
computedContentItem.icons = this._computeIconsForValue(contentItem.id);
|
|
|
|
return computedContentItem;
|
2017-11-21 18:53:09 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
computeContent() {
|
2017-10-27 01:50:31 +08:00
|
|
|
let now = moment();
|
|
|
|
let opts = {
|
|
|
|
now,
|
|
|
|
day: now.day(),
|
2018-06-15 23:03:24 +08:00
|
|
|
includeWeekend: this.get("includeWeekend"),
|
|
|
|
includeFarFuture: this.get("includeFarFuture"),
|
2017-10-27 01:50:31 +08:00
|
|
|
includeBasedOnLastPost: this.get("statusType") === CLOSE_STATUS_TYPE,
|
2018-06-15 23:03:24 +08:00
|
|
|
canScheduleToday: 24 - now.hour() > 6
|
2017-10-27 01:50:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return TIMEFRAMES.filter(tf => tf.enabled(opts)).map(tf => {
|
|
|
|
return {
|
|
|
|
id: tf.id,
|
|
|
|
name: I18n.t(`topic.auto_update_input.${tf.id}`)
|
|
|
|
};
|
2017-10-20 03:51:08 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
mutateValue(value) {
|
2018-06-15 23:03:24 +08:00
|
|
|
if (value === "pick_date_and_time" || this.get("isBasedOnLastPost")) {
|
2017-11-23 22:18:27 +08:00
|
|
|
this.set("value", value);
|
|
|
|
} else {
|
|
|
|
let input = null;
|
|
|
|
const { time } = this._updateAt(value);
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-11-23 22:18:27 +08:00
|
|
|
if (time && !Ember.isEmpty(value)) {
|
2018-06-15 23:03:24 +08:00
|
|
|
input = time.locale("en").format(FORMAT);
|
2017-11-23 22:18:27 +08:00
|
|
|
}
|
2017-10-20 03:51:08 +08:00
|
|
|
|
2017-11-23 22:18:27 +08:00
|
|
|
this.setProperties({ input, value });
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2017-10-20 03:51:08 +08:00
|
|
|
});
|