2018-06-15 12:42:20 -04:00
|
|
|
import {
|
|
|
|
default as computed,
|
|
|
|
observes
|
|
|
|
} from "ember-addons/ember-computed-decorators";
|
|
|
|
import InputValidation from "discourse/models/input-validation";
|
2016-06-13 06:21:14 -04:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2018-06-15 12:42:20 -04:00
|
|
|
regularPollType: "regular",
|
|
|
|
numberPollType: "number",
|
|
|
|
multiplePollType: "multiple",
|
2016-06-13 06:21:14 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
alwaysPollResult: "always",
|
|
|
|
votePollResult: "on_vote",
|
|
|
|
closedPollResult: "on_close",
|
|
|
|
|
2016-06-13 06:21:14 -04:00
|
|
|
init() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2016-06-13 06:21:14 -04:00
|
|
|
this._setupPoll();
|
|
|
|
},
|
|
|
|
|
2017-04-04 23:15:39 -04:00
|
|
|
@computed("regularPollType", "numberPollType", "multiplePollType")
|
|
|
|
pollTypes(regularPollType, numberPollType, multiplePollType) {
|
2018-05-02 20:12:19 -04:00
|
|
|
return [
|
2018-06-15 12:42:20 -04:00
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.regular"),
|
|
|
|
value: regularPollType
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.number"),
|
|
|
|
value: numberPollType
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_type.multiple"),
|
|
|
|
value: multiplePollType
|
|
|
|
}
|
2018-05-02 20:12:19 -04:00
|
|
|
];
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
@computed("alwaysPollResult", "votePollResult", "closedPollResult")
|
|
|
|
pollResults(alwaysPollResult, votePollResult, closedPollResult) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.always"),
|
|
|
|
value: alwaysPollResult
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.vote"),
|
|
|
|
value: votePollResult
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: I18n.t("poll.ui_builder.poll_result.closed"),
|
|
|
|
value: closedPollResult
|
|
|
|
}
|
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2017-04-04 23:15:39 -04:00
|
|
|
@computed("pollType", "regularPollType")
|
|
|
|
isRegular(pollType, regularPollType) {
|
|
|
|
return pollType === regularPollType;
|
|
|
|
},
|
|
|
|
|
2016-06-20 00:40:24 -04:00
|
|
|
@computed("pollType", "pollOptionsCount", "multiplePollType")
|
|
|
|
isMultiple(pollType, count, multiplePollType) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return pollType === multiplePollType && count > 0;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2016-06-20 00:40:24 -04:00
|
|
|
@computed("pollType", "numberPollType")
|
|
|
|
isNumber(pollType, numberPollType) {
|
|
|
|
return pollType === numberPollType;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2017-04-04 23:15:39 -04:00
|
|
|
@computed("isRegular")
|
|
|
|
showMinMax(isRegular) {
|
|
|
|
return !isRegular;
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("pollOptions")
|
|
|
|
pollOptionsCount(pollOptions) {
|
|
|
|
if (pollOptions.length === 0) return 0;
|
|
|
|
|
|
|
|
let length = 0;
|
|
|
|
|
|
|
|
pollOptions.split("\n").forEach(option => {
|
|
|
|
if (option.length !== 0) length += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
return length;
|
|
|
|
},
|
|
|
|
|
|
|
|
@observes("isMultiple", "isNumber", "pollOptionsCount")
|
|
|
|
_setPollMax() {
|
|
|
|
const isMultiple = this.get("isMultiple");
|
|
|
|
const isNumber = this.get("isNumber");
|
|
|
|
if (!isMultiple && !isNumber) return;
|
|
|
|
|
|
|
|
if (isMultiple) {
|
|
|
|
this.set("pollMax", this.get("pollOptionsCount"));
|
|
|
|
} else if (isNumber) {
|
|
|
|
this.set("pollMax", this.siteSettings.poll_maximum_options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-04-04 23:15:39 -04:00
|
|
|
@computed("isRegular", "isMultiple", "isNumber", "pollOptionsCount")
|
|
|
|
pollMinOptions(isRegular, isMultiple, isNumber, count) {
|
|
|
|
if (isRegular) return;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
|
|
|
if (isMultiple) {
|
|
|
|
return this._comboboxOptions(1, count + 1);
|
|
|
|
} else if (isNumber) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return this._comboboxOptions(
|
|
|
|
1,
|
|
|
|
this.siteSettings.poll_maximum_options + 1
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
@computed(
|
|
|
|
"isRegular",
|
|
|
|
"isMultiple",
|
|
|
|
"isNumber",
|
|
|
|
"pollOptionsCount",
|
|
|
|
"pollMin",
|
|
|
|
"pollStep"
|
|
|
|
)
|
2017-04-04 23:15:39 -04:00
|
|
|
pollMaxOptions(isRegular, isMultiple, isNumber, count, pollMin, pollStep) {
|
|
|
|
if (isRegular) return;
|
2016-06-15 00:54:52 -04:00
|
|
|
const pollMinInt = parseInt(pollMin) || 1;
|
2016-06-13 06:21:14 -04:00
|
|
|
|
|
|
|
if (isMultiple) {
|
|
|
|
return this._comboboxOptions(pollMinInt + 1, count + 1);
|
|
|
|
} else if (isNumber) {
|
2017-11-30 11:04:41 -05:00
|
|
|
let pollStepInt = parseInt(pollStep, 10);
|
|
|
|
if (pollStepInt < 1) {
|
|
|
|
pollStepInt = 1;
|
|
|
|
}
|
2018-06-15 12:42:20 -04:00
|
|
|
return this._comboboxOptions(
|
|
|
|
pollMinInt + 1,
|
|
|
|
pollMinInt + this.siteSettings.poll_maximum_options * pollStepInt
|
|
|
|
);
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("isNumber", "pollMax")
|
|
|
|
pollStepOptions(isNumber, pollMax) {
|
|
|
|
if (!isNumber) return;
|
2016-06-15 00:54:52 -04:00
|
|
|
return this._comboboxOptions(1, (parseInt(pollMax) || 1) + 1);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
@computed(
|
|
|
|
"isNumber",
|
|
|
|
"showMinMax",
|
|
|
|
"pollType",
|
2018-11-19 08:50:00 -05:00
|
|
|
"pollResult",
|
2018-06-15 12:42:20 -04:00
|
|
|
"publicPoll",
|
|
|
|
"pollOptions",
|
|
|
|
"pollMin",
|
|
|
|
"pollMax",
|
|
|
|
"pollStep",
|
|
|
|
"autoClose",
|
|
|
|
"date",
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
pollOutput(
|
|
|
|
isNumber,
|
|
|
|
showMinMax,
|
|
|
|
pollType,
|
2018-11-19 08:50:00 -05:00
|
|
|
pollResult,
|
2018-06-15 12:42:20 -04:00
|
|
|
publicPoll,
|
|
|
|
pollOptions,
|
|
|
|
pollMin,
|
|
|
|
pollMax,
|
|
|
|
pollStep,
|
|
|
|
autoClose,
|
|
|
|
date,
|
|
|
|
time
|
|
|
|
) {
|
|
|
|
let pollHeader = "[poll";
|
|
|
|
let output = "";
|
|
|
|
|
|
|
|
const match = this.get("toolbarEvent")
|
|
|
|
.getText()
|
|
|
|
.match(/\[poll(\s+name=[^\s\]]+)*.*\]/gim);
|
2016-07-05 10:14:59 -04:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
pollHeader += ` name=poll${match.length + 1}`;
|
2018-06-15 12:42:20 -04:00
|
|
|
}
|
2016-07-05 10:14:59 -04:00
|
|
|
|
2017-11-30 11:04:41 -05:00
|
|
|
let step = pollStep;
|
2018-06-15 12:42:20 -04:00
|
|
|
if (step < 1) {
|
|
|
|
step = 1;
|
|
|
|
}
|
2017-11-30 11:04:41 -05:00
|
|
|
|
2016-06-13 06:21:14 -04:00
|
|
|
if (pollType) pollHeader += ` type=${pollType}`;
|
2018-11-19 08:50:00 -05:00
|
|
|
if (pollResult) pollHeader += ` results=${pollResult}`;
|
2016-06-13 06:21:14 -04:00
|
|
|
if (pollMin && showMinMax) pollHeader += ` min=${pollMin}`;
|
|
|
|
if (pollMax) pollHeader += ` max=${pollMax}`;
|
2017-11-30 11:04:41 -05:00
|
|
|
if (isNumber) pollHeader += ` step=${step}`;
|
2018-05-02 20:12:19 -04:00
|
|
|
if (publicPoll) pollHeader += ` public=true`;
|
2018-06-12 09:31:09 -04:00
|
|
|
if (autoClose) {
|
2018-06-15 12:42:20 -04:00
|
|
|
let closeDate = moment(
|
|
|
|
date + " " + time,
|
|
|
|
"YYYY-MM-DD HH:mm"
|
|
|
|
).toISOString();
|
2018-06-12 09:31:09 -04:00
|
|
|
if (closeDate) pollHeader += ` close=${closeDate}`;
|
|
|
|
}
|
2018-05-02 20:12:19 -04:00
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
pollHeader += "]";
|
2016-06-13 06:21:14 -04:00
|
|
|
output += `${pollHeader}\n`;
|
|
|
|
|
|
|
|
if (pollOptions.length > 0 && !isNumber) {
|
2016-07-14 03:10:31 -04:00
|
|
|
pollOptions.split("\n").forEach(option => {
|
|
|
|
if (option.length !== 0) output += `* ${option}\n`;
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
output += "[/poll]";
|
2016-06-13 06:21:14 -04:00
|
|
|
return output;
|
|
|
|
},
|
|
|
|
|
2018-06-15 12:42:20 -04:00
|
|
|
@computed(
|
|
|
|
"pollOptionsCount",
|
|
|
|
"isRegular",
|
|
|
|
"isMultiple",
|
|
|
|
"isNumber",
|
|
|
|
"pollMin",
|
|
|
|
"pollMax"
|
|
|
|
)
|
2017-04-04 23:15:39 -04:00
|
|
|
disableInsert(count, isRegular, isMultiple, isNumber, pollMin, pollMax) {
|
2018-06-15 12:42:20 -04:00
|
|
|
return (
|
|
|
|
(isRegular && count < 2) ||
|
|
|
|
(isMultiple && count < pollMin && pollMin >= pollMax) ||
|
|
|
|
(isNumber ? false : count < 2)
|
|
|
|
);
|
2017-03-14 12:34:30 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
@computed("pollMin", "pollMax")
|
|
|
|
minMaxValueValidation(pollMin, pollMax) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (pollMin >= pollMax) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.invalid_values")
|
|
|
|
};
|
2017-03-14 12:34:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return InputValidation.create(options);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
2017-11-30 11:04:41 -05:00
|
|
|
@computed("pollStep")
|
|
|
|
minStepValueValidation(pollStep) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (pollStep < 1) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.min_step_value")
|
|
|
|
};
|
2017-11-30 11:04:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return InputValidation.create(options);
|
|
|
|
},
|
|
|
|
|
2016-06-13 06:21:14 -04:00
|
|
|
@computed("disableInsert")
|
|
|
|
minNumOfOptionsValidation(disableInsert) {
|
|
|
|
let options = { ok: true };
|
|
|
|
|
|
|
|
if (disableInsert) {
|
2018-06-15 12:42:20 -04:00
|
|
|
options = {
|
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("poll.ui_builder.help.options_count")
|
|
|
|
};
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
|
2016-06-30 12:26:49 -04:00
|
|
|
return InputValidation.create(options);
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_comboboxOptions(start_index, end_index) {
|
|
|
|
return _.range(start_index, end_index).map(number => {
|
2016-06-14 04:11:45 -04:00
|
|
|
return { value: number, name: number };
|
|
|
|
});
|
2016-06-13 06:21:14 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
_setupPoll() {
|
|
|
|
this.setProperties({
|
|
|
|
pollType: null,
|
|
|
|
publicPoll: false,
|
2018-06-15 12:42:20 -04:00
|
|
|
pollOptions: "",
|
2016-06-13 06:21:14 -04:00
|
|
|
pollMin: 1,
|
|
|
|
pollMax: null,
|
2018-05-02 20:12:19 -04:00
|
|
|
pollStep: 1,
|
|
|
|
autoClose: false,
|
2018-06-15 12:42:20 -04:00
|
|
|
date: moment()
|
|
|
|
.add(1, "day")
|
|
|
|
.format("YYYY-MM-DD"),
|
|
|
|
time: moment()
|
|
|
|
.add(1, "hour")
|
|
|
|
.format("HH:mm")
|
2016-06-13 06:21:14 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
insertPoll() {
|
|
|
|
this.get("toolbarEvent").addText(this.get("pollOutput"));
|
|
|
|
this.send("closeModal");
|
2016-06-20 06:13:18 -04:00
|
|
|
this._setupPoll();
|
2016-06-13 06:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|