|
|
|
@ -59,6 +59,13 @@ define([
|
|
|
|
|
|
|
|
|
|
Messages.form_duplicates = "Duplicate entries have been removed";
|
|
|
|
|
|
|
|
|
|
Messages.form_reset = "Reset";
|
|
|
|
|
Messages.form_sent = "Sent";
|
|
|
|
|
|
|
|
|
|
// XXX to update our own answers, we need to store the server hash of the message
|
|
|
|
|
// and we'll be able to use getHistoryRange to fetch this message when we come back
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var makeFormSettings = function (framework) {
|
|
|
|
|
// XXX
|
|
|
|
|
// Button to set results as public
|
|
|
|
@ -66,45 +73,7 @@ define([
|
|
|
|
|
// Button to clear all answers?
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var TYPES = {
|
|
|
|
|
input: {
|
|
|
|
|
get: function () {
|
|
|
|
|
var tag = h('input');
|
|
|
|
|
var $tag = $(tag);
|
|
|
|
|
return {
|
|
|
|
|
tag: tag,
|
|
|
|
|
getValue: function () { return $tag.val(); },
|
|
|
|
|
//setValue: function (val) { $tag.val(val); }
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
icon: h('i.fa.fa-font')
|
|
|
|
|
},
|
|
|
|
|
radio: {
|
|
|
|
|
defaultOpts: {
|
|
|
|
|
values: ["Option 1", "Option 2"] // XXX?
|
|
|
|
|
},
|
|
|
|
|
get: function (opts) {
|
|
|
|
|
if (!opts) { opts = TYPES.radio.defaultOpts; }
|
|
|
|
|
var name = Util.uid();
|
|
|
|
|
var els = opts.values.map(function (data, i) {
|
|
|
|
|
return UI.createRadio(name, 'cp-form-'+name+'-'+i,
|
|
|
|
|
data, false, { mark: {tabindex:1} });
|
|
|
|
|
});
|
|
|
|
|
var tag = h('div.radio-group', els);
|
|
|
|
|
return {
|
|
|
|
|
tag: tag,
|
|
|
|
|
getValue: function () {
|
|
|
|
|
var res;
|
|
|
|
|
els.some(function (el, i) {
|
|
|
|
|
if (Util.isChecked($(el).find('input'))) {
|
|
|
|
|
res = opts.values[i];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
edit: function (cb) {
|
|
|
|
|
var v = opts.values.slice();
|
|
|
|
|
|
|
|
|
|
var editOptions = function (v, cb) {
|
|
|
|
|
var add = h('button.btn.btn-secondary', [
|
|
|
|
|
h('i.fa.fa-plus'),
|
|
|
|
|
h('span', Messages.tag_add)
|
|
|
|
@ -155,6 +124,49 @@ define([
|
|
|
|
|
container,
|
|
|
|
|
h('div', [cancelBlock, saveBlock])
|
|
|
|
|
];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var TYPES = {
|
|
|
|
|
input: {
|
|
|
|
|
get: function () {
|
|
|
|
|
var tag = h('input');
|
|
|
|
|
var $tag = $(tag);
|
|
|
|
|
return {
|
|
|
|
|
tag: tag,
|
|
|
|
|
getValue: function () { return $tag.val(); },
|
|
|
|
|
//setValue: function (val) { $tag.val(val); }
|
|
|
|
|
reset: function () { $tag.val(''); }
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
icon: h('i.fa.fa-font')
|
|
|
|
|
},
|
|
|
|
|
radio: {
|
|
|
|
|
defaultOpts: {
|
|
|
|
|
values: ["Option 1", "Option 2"] // XXX?
|
|
|
|
|
},
|
|
|
|
|
get: function (opts) {
|
|
|
|
|
if (!opts) { opts = TYPES.radio.defaultOpts; }
|
|
|
|
|
var name = Util.uid();
|
|
|
|
|
var els = opts.values.map(function (data, i) {
|
|
|
|
|
return UI.createRadio(name, 'cp-form-'+name+'-'+i,
|
|
|
|
|
data, false, { mark: {tabindex:1} });
|
|
|
|
|
});
|
|
|
|
|
var tag = h('div.radio-group', els);
|
|
|
|
|
return {
|
|
|
|
|
tag: tag,
|
|
|
|
|
getValue: function () {
|
|
|
|
|
var res;
|
|
|
|
|
els.some(function (el, i) {
|
|
|
|
|
if (Util.isChecked($(el).find('input'))) {
|
|
|
|
|
res = opts.values[i];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
reset: function () { $(tag).find('input').removeAttr('checked'); },
|
|
|
|
|
edit: function (cb) {
|
|
|
|
|
var v = opts.values.slice();
|
|
|
|
|
return editOptions(v, cb);
|
|
|
|
|
}
|
|
|
|
|
//setValue: function (val) {}
|
|
|
|
|
};
|
|
|
|
@ -164,23 +176,60 @@ define([
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var renderForm = function (content, editable) {
|
|
|
|
|
var makeFormControls = function (framework, content) {
|
|
|
|
|
var send = h('button.btn.btn-primary', Messages.poll_commit);
|
|
|
|
|
var reset = h('button.btn.btn-danger-alt', Messages.form_reset);
|
|
|
|
|
$(reset).click(function () {
|
|
|
|
|
if (!Array.isArray(APP.formBlocks)) { return; }
|
|
|
|
|
APP.formBlocks.forEach(function (data) {
|
|
|
|
|
if (typeof(data.reset) === "function") { data.reset(); }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
var $send = $(send).click(function () {
|
|
|
|
|
$send.attr('disabled', 'disabled');
|
|
|
|
|
if (!Array.isArray(APP.formBlocks)) { return; }
|
|
|
|
|
var results = {};
|
|
|
|
|
APP.formBlocks.forEach(function (data) {
|
|
|
|
|
results[data.uid] = data.getValue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var sframeChan = framework._.sfCommon.getSframeChannel();
|
|
|
|
|
sframeChan.query('Q_FORM_SUBMIT', {
|
|
|
|
|
mailbox: content.answers,
|
|
|
|
|
results: results
|
|
|
|
|
}, function (err, data) {
|
|
|
|
|
console.error(data);
|
|
|
|
|
if (err || (data && data.error)) {
|
|
|
|
|
console.error(err || data.error);
|
|
|
|
|
return void UI.warn(Messages.error);
|
|
|
|
|
}
|
|
|
|
|
UI.alert(Messages.form_sent);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return h('div.cp-form-send-container', [send, reset]);
|
|
|
|
|
};
|
|
|
|
|
var updateForm = function (framework, content, editable) {
|
|
|
|
|
var $container = $('div.cp-form-creator-content');
|
|
|
|
|
|
|
|
|
|
var form = content.form;
|
|
|
|
|
|
|
|
|
|
APP.formBlocks = [];
|
|
|
|
|
|
|
|
|
|
// XXX order array later
|
|
|
|
|
var elements = Object.keys(form).map(function (uid) {
|
|
|
|
|
var block = form[uid];
|
|
|
|
|
var type = block.type;
|
|
|
|
|
var model = TYPES[type];
|
|
|
|
|
if (!model) { return; }
|
|
|
|
|
|
|
|
|
|
var data = model.get(block.opts);
|
|
|
|
|
data.uid = uid;
|
|
|
|
|
|
|
|
|
|
var q = h('div.cp-form-block-question', block.q || Messages.form_default);
|
|
|
|
|
var edit, editContainer;
|
|
|
|
|
|
|
|
|
|
APP.formBlocks.push(data);
|
|
|
|
|
|
|
|
|
|
if (editable) {
|
|
|
|
|
// Question
|
|
|
|
|
|
|
|
|
@ -231,6 +280,7 @@ define([
|
|
|
|
|
}
|
|
|
|
|
$(editContainer).empty();
|
|
|
|
|
block.opts = newOpts;
|
|
|
|
|
framework.localChange();
|
|
|
|
|
var $oldTag = $(data.tag);
|
|
|
|
|
framework._.cpNfInner.chainpad.onSettle(function () {
|
|
|
|
|
$edit.show();
|
|
|
|
@ -257,15 +307,23 @@ define([
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$container.empty().append(elements);
|
|
|
|
|
$container.append(makeFormControls(framework, content));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var andThen = function (framework) {
|
|
|
|
|
framework.start();
|
|
|
|
|
var content = {};
|
|
|
|
|
|
|
|
|
|
var $container = $('#cp-app-form-container');
|
|
|
|
|
var sframeChan = framework._.sfCommon.getSframeChannel();
|
|
|
|
|
var metadataMgr = framework._.cpNfInner.metadataMgr;
|
|
|
|
|
|
|
|
|
|
var priv = metadataMgr.getPrivateData();
|
|
|
|
|
APP.isEditor = Boolean(priv.form_public);
|
|
|
|
|
|
|
|
|
|
var makeFormCreator = function () {
|
|
|
|
|
|
|
|
|
|
var controlContainer;
|
|
|
|
|
if (APP.isEditor) {
|
|
|
|
|
var controls = Object.keys(TYPES).map(function (type) {
|
|
|
|
|
|
|
|
|
|
var btn = h('button.btn', [
|
|
|
|
@ -284,7 +342,8 @@ define([
|
|
|
|
|
});
|
|
|
|
|
return btn;
|
|
|
|
|
});
|
|
|
|
|
var controlContainer = h('div.cp-form-creator-control', controls);
|
|
|
|
|
controlContainer = h('div.cp-form-creator-control', controls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var contentContainer = h('div.cp-form-creator-content');
|
|
|
|
|
var div = h('div.cp-form-creator-container', [
|
|
|
|
@ -294,12 +353,9 @@ define([
|
|
|
|
|
return div;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var $container = $('#cp-app-form-container');
|
|
|
|
|
$container.append(makeFormCreator());
|
|
|
|
|
|
|
|
|
|
var sframeChan = framework._.sfCommon.getSframeChannel();
|
|
|
|
|
var metadataMgr = framework._.cpNfInner.metadataMgr;
|
|
|
|
|
var priv = metadataMgr.getPrivateData();
|
|
|
|
|
APP.isEditor = Boolean(priv.form_public);
|
|
|
|
|
if (!APP.isEditor) { makeFormControls(); }
|
|
|
|
|
|
|
|
|
|
framework.onReady(function (isNew) {
|
|
|
|
|
var priv = metadataMgr.getPrivateData();
|
|
|
|
@ -340,6 +396,7 @@ define([
|
|
|
|
|
framework.onContentUpdate(function (newContent) {
|
|
|
|
|
console.log(newContent);
|
|
|
|
|
content = newContent;
|
|
|
|
|
updateForm(framework, content, APP.isEditor);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
framework.setContentGetter(function () {
|
|
|
|
|