2019-11-02 00:06:25 +01:00
|
|
|
const render_widgets_zform_choices = require('../templates/widgets/zform_choices.hbs');
|
2019-07-09 21:24:00 +02:00
|
|
|
|
2018-02-23 15:37:19 +01:00
|
|
|
exports.validate_extra_data = function (data) {
|
|
|
|
function check(data) {
|
|
|
|
function check_choice_data(data) {
|
|
|
|
function check_choice_item(field_name, val) {
|
|
|
|
return schema.check_record(field_name, val, {
|
|
|
|
short_name: schema.check_string,
|
|
|
|
long_name: schema.check_string,
|
|
|
|
reply: schema.check_string,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_choices(field_name, val) {
|
|
|
|
return schema.check_array(
|
|
|
|
field_name,
|
|
|
|
val,
|
|
|
|
check_choice_item
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema.check_record('zform data', data, {
|
|
|
|
heading: schema.check_string,
|
|
|
|
choices: check_choices,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === 'choices') {
|
|
|
|
return check_choice_data(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'unknown zform type: ' + data.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const msg = check(data);
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
blueslip.warn(msg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.activate = function (opts) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const self = {};
|
2018-02-23 15:37:19 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const outer_elem = opts.elem;
|
|
|
|
const data = opts.extra_data;
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
if (!exports.validate_extra_data(data)) {
|
|
|
|
// callee will log reason we fail
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function make_choices(data) {
|
|
|
|
// Assign idx values to each of our choices so that
|
|
|
|
// our template can create data-idx values for our
|
|
|
|
// JS code to use later.
|
|
|
|
_.each(data.choices, function (choice, idx) {
|
|
|
|
choice.idx = idx;
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const html = render_widgets_zform_choices(data);
|
|
|
|
const elem = $(html);
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
elem.find('button').on('click', function (e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
// Grab our index from the markup.
|
2019-11-02 00:06:25 +01:00
|
|
|
const idx = $(e.target).attr('data-idx');
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
// Use the index from the markup to dereference our
|
|
|
|
// data structure.
|
2019-11-02 00:06:25 +01:00
|
|
|
const reply_content = data.choices[idx].reply;
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
transmit.reply_message({
|
|
|
|
message: opts.message,
|
|
|
|
content: reply_content,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
2019-11-02 00:06:25 +01:00
|
|
|
let rendered_widget;
|
2018-02-23 15:37:19 +01:00
|
|
|
|
|
|
|
if (data.type === 'choices') {
|
|
|
|
rendered_widget = make_choices(data);
|
|
|
|
outer_elem.html(rendered_widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.handle_events = function (events) {
|
|
|
|
if (events) {
|
|
|
|
blueslip.info('unexpected');
|
|
|
|
}
|
|
|
|
render();
|
|
|
|
};
|
|
|
|
|
|
|
|
render();
|
|
|
|
|
|
|
|
return self;
|
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.zform = exports;
|