2015-04-23 13:52:37 -04:00
|
|
|
/*global md5 */
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
(function() {
|
|
|
|
|
2015-05-25 12:32:24 -04:00
|
|
|
var DATA_PREFIX = "data-poll-";
|
|
|
|
var DEFAULT_POLL_NAME = "poll";
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2016-06-07 06:55:01 -04:00
|
|
|
var WHITELISTED_ATTRIBUTES = ["type", "name", "min", "max", "step", "order", "status", "public"];
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-05-25 12:32:24 -04:00
|
|
|
var ATTRIBUTES_REGEX = new RegExp("(" + WHITELISTED_ATTRIBUTES.join("|") + ")=['\"]?[^\\s\\]]+['\"]?", "g");
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
Discourse.Dialect.replaceBlock({
|
2015-05-13 11:50:25 -04:00
|
|
|
start: /\[poll((?:\s+\w+=[^\s\]]+)*)\]([\s\S]*)/igm,
|
2015-04-23 13:33:29 -04:00
|
|
|
stop: /\[\/poll\]/igm,
|
|
|
|
|
2015-04-23 13:52:37 -04:00
|
|
|
emitter: function(blockContents, matches) {
|
|
|
|
var o, contents = [];
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-04-23 13:52:37 -04:00
|
|
|
// post-process inside block contents
|
2015-04-23 13:33:29 -04:00
|
|
|
if (blockContents.length) {
|
|
|
|
var self = this, b;
|
2015-04-23 13:52:37 -04:00
|
|
|
|
|
|
|
var postProcess = function(bc) {
|
|
|
|
if (typeof bc === "string" || bc instanceof String) {
|
|
|
|
var processed = self.processInline(String(bc));
|
|
|
|
if (processed.length) {
|
|
|
|
contents.push(["p"].concat(processed));
|
2015-04-23 13:33:29 -04:00
|
|
|
}
|
2015-04-23 13:52:37 -04:00
|
|
|
} else {
|
|
|
|
contents.push(bc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((b = blockContents.shift()) !== undefined) {
|
|
|
|
this.processBlock(b, blockContents).forEach(postProcess);
|
2015-04-23 13:33:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 14:11:03 -04:00
|
|
|
// Disable dialect when poll plugin is disabled
|
|
|
|
if (!Discourse.SiteSettings.poll_enabled) { return ["div"].concat(contents); }
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
// default poll attributes
|
|
|
|
var attributes = { "class": "poll" };
|
|
|
|
attributes[DATA_PREFIX + "status"] = "open";
|
|
|
|
attributes[DATA_PREFIX + "name"] = DEFAULT_POLL_NAME;
|
|
|
|
|
|
|
|
// extract poll attributes
|
|
|
|
(matches[1].match(ATTRIBUTES_REGEX) || []).forEach(function(m) {
|
2015-05-07 10:40:14 -04:00
|
|
|
var attr = m.split("="), name = attr[0], value = attr[1];
|
2015-05-11 14:09:17 -04:00
|
|
|
value = Handlebars.Utils.escapeExpression(value.replace(/["']/g, ""));
|
2015-05-07 10:40:14 -04:00
|
|
|
attributes[DATA_PREFIX + name] = value;
|
2015-04-23 13:33:29 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// we might need these values later...
|
|
|
|
var min = parseInt(attributes[DATA_PREFIX + "min"], 10),
|
|
|
|
max = parseInt(attributes[DATA_PREFIX + "max"], 10),
|
|
|
|
step = parseInt(attributes[DATA_PREFIX + "step"], 10);
|
|
|
|
|
|
|
|
// generate the options when the type is "number"
|
|
|
|
if (attributes[DATA_PREFIX + "type"] === "number") {
|
|
|
|
// default values
|
|
|
|
if (isNaN(min)) { min = 1; }
|
2015-06-01 13:28:05 -04:00
|
|
|
if (isNaN(max)) { max = Discourse.SiteSettings.poll_maximum_options; }
|
2015-04-23 13:33:29 -04:00
|
|
|
if (isNaN(step)) { step = 1; }
|
|
|
|
// dynamically generate options
|
|
|
|
contents.push(["bulletlist"]);
|
2015-04-23 13:52:37 -04:00
|
|
|
for (o = min; o <= max; o += step) {
|
2015-04-23 13:33:29 -04:00
|
|
|
contents[0].push(["listitem", String(o)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:12:53 -04:00
|
|
|
// make sure there's only 1 child and it's a list with at least 1 option
|
|
|
|
if (contents.length !== 1 || contents[0].length <= 1 || (contents[0][0] !== "numberlist" && contents[0][0] !== "bulletlist")) {
|
2015-04-23 13:33:29 -04:00
|
|
|
return ["div"].concat(contents);
|
|
|
|
}
|
|
|
|
|
2015-05-13 17:12:53 -04:00
|
|
|
// make sure there's only options in the list
|
|
|
|
for (o = 1; o < contents[0].length; o++) {
|
|
|
|
if (contents[0][o][0] !== "listitem") {
|
|
|
|
return ["div"].concat(contents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
// TODO: remove non whitelisted content
|
|
|
|
|
2015-05-15 05:08:51 -04:00
|
|
|
// add option id (hash)
|
2015-04-23 13:52:37 -04:00
|
|
|
for (o = 1; o < contents[0].length; o++) {
|
2015-04-23 13:33:29 -04:00
|
|
|
var attr = {};
|
|
|
|
// compute md5 hash of the content of the option
|
|
|
|
attr[DATA_PREFIX + "option-id"] = md5(JSON.stringify(contents[0][o].slice(1)));
|
|
|
|
// store options attributes
|
|
|
|
contents[0][o].splice(1, 0, attr);
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:29:45 -04:00
|
|
|
var result = ["div", attributes],
|
|
|
|
poll = ["div"];
|
|
|
|
|
2015-05-07 13:49:06 -04:00
|
|
|
// 1 - POLL CONTAINER
|
|
|
|
var container = ["div", { "class": "poll-container" }].concat(contents);
|
|
|
|
poll.push(container);
|
|
|
|
|
|
|
|
// 2 - POLL INFO
|
2015-05-04 16:29:45 -04:00
|
|
|
var info = ["div", { "class": "poll-info" }];
|
|
|
|
|
|
|
|
// # of voters
|
|
|
|
info.push(["p",
|
|
|
|
["span", { "class": "info-number" }, "0"],
|
|
|
|
["span", { "class": "info-text"}, I18n.t("poll.voters", { count: 0 })]
|
|
|
|
]);
|
|
|
|
|
|
|
|
// multiple help text
|
2015-04-23 13:33:29 -04:00
|
|
|
if (attributes[DATA_PREFIX + "type"] === "multiple") {
|
|
|
|
var optionCount = contents[0].length - 1;
|
|
|
|
|
|
|
|
// default values
|
|
|
|
if (isNaN(min) || min < 1) { min = 1; }
|
|
|
|
if (isNaN(max) || max > optionCount) { max = optionCount; }
|
|
|
|
|
|
|
|
// add some help text
|
|
|
|
var help;
|
|
|
|
|
|
|
|
if (max > 0) {
|
|
|
|
if (min === max) {
|
|
|
|
if (min > 1) {
|
|
|
|
help = I18n.t("poll.multiple.help.x_options", { count: min });
|
|
|
|
}
|
|
|
|
} else if (min > 1) {
|
|
|
|
if (max < optionCount) {
|
|
|
|
help = I18n.t("poll.multiple.help.between_min_and_max_options", { min: min, max: max });
|
|
|
|
} else {
|
|
|
|
help = I18n.t("poll.multiple.help.at_least_min_options", { count: min });
|
|
|
|
}
|
|
|
|
} else if (max <= optionCount) {
|
|
|
|
help = I18n.t("poll.multiple.help.up_to_max_options", { count: max });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:29:45 -04:00
|
|
|
if (help) { info.push(["p", help]); }
|
|
|
|
}
|
|
|
|
|
2016-06-09 10:15:08 -04:00
|
|
|
if (attributes[DATA_PREFIX + "public"] === "true") {
|
|
|
|
info.push(["p", I18n.t("poll.public.title")]);
|
|
|
|
}
|
|
|
|
|
2015-05-04 16:29:45 -04:00
|
|
|
poll.push(info);
|
|
|
|
|
|
|
|
// 3 - BUTTONS
|
|
|
|
var buttons = ["div", { "class": "poll-buttons" }];
|
2015-04-23 13:33:29 -04:00
|
|
|
|
2015-05-04 16:29:45 -04:00
|
|
|
// add "cast-votes" button
|
|
|
|
if (attributes[DATA_PREFIX + "type"] === "multiple") {
|
|
|
|
buttons.push(["a", { "class": "button cast-votes", "title": I18n.t("poll.cast-votes.title") }, I18n.t("poll.cast-votes.label")]);
|
2015-04-23 13:33:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add "toggle-results" button
|
2015-05-04 16:29:45 -04:00
|
|
|
buttons.push(["a", { "class": "button toggle-results", "title": I18n.t("poll.show-results.title") }, I18n.t("poll.show-results.label")]);
|
|
|
|
|
|
|
|
// 4 - MIX IT ALL UP
|
|
|
|
result.push(poll);
|
2015-05-04 20:10:50 -04:00
|
|
|
result.push(buttons);
|
2015-04-23 13:33:29 -04:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Discourse.Markdown.whiteListTag("div", "class", "poll");
|
2015-05-04 16:29:45 -04:00
|
|
|
Discourse.Markdown.whiteListTag("div", "class", /^poll-(info|container|buttons)/);
|
2015-04-23 13:33:29 -04:00
|
|
|
Discourse.Markdown.whiteListTag("div", "data-*");
|
|
|
|
|
2015-05-04 16:29:45 -04:00
|
|
|
Discourse.Markdown.whiteListTag("span", "class", /^info-(number|text)/);
|
|
|
|
|
2015-04-23 13:33:29 -04:00
|
|
|
Discourse.Markdown.whiteListTag("a", "class", /^button (cast-votes|toggle-results)/);
|
|
|
|
|
|
|
|
Discourse.Markdown.whiteListTag("li", "data-*");
|
|
|
|
})();
|