Commit 59791a93 authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files
parent 62f84882
......@@ -18,8 +18,8 @@ var QfqNS = QfqNS || {};
* @deprecated use appendTemplate().
*
*/
n.addFields = function (templateSelector, targetSelector, maxLines) {
n.appendTemplate(templateSelector, targetSelector, maxLines);
n.addFields = function (templateSelector, targetSelector, maxLines, button) {
n.appendTemplate(templateSelector, targetSelector, maxLines, button);
};
/**
......@@ -36,7 +36,7 @@ var QfqNS = QfqNS || {};
* @param {number} maxChildren do not allow more than `maxChildren` of children in target.
*
*/
n.appendTemplate = function (templateSelector, targetSelector, maxChildren) {
n.appendTemplate = function (templateSelector, targetSelector, maxChildren, button) {
var escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
var escapedTargetSelector = n.escapeJqueryIdSelector(targetSelector);
......@@ -53,8 +53,24 @@ var QfqNS = QfqNS || {};
var deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, lines);
// Store the button object, so we can easily access it when this `line` is removed by the user
deserializedTemplate.data('field.template.addButton', button);
$target.append(deserializedTemplate);
n.informFormOfChange($target);
lines = n.countLines(escapedTargetSelector);
if (lines >= maxChildren && button) {
n.disableButton(button);
}
};
n.disableButton = function (button) {
$(button).attr('disabled', 'disabled');
};
n.enableButton = function (button) {
$(button).removeAttr('disabled');
};
n.informFormOfChange = function ($sourceOfChange) {
......@@ -99,8 +115,6 @@ var QfqNS = QfqNS || {};
return;
}
deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, 1);
......@@ -130,10 +144,15 @@ var QfqNS = QfqNS || {};
n.removeThisChild = function (eventTarget) {
var $line = $(eventTarget).closest('.qfq-line');
var $container = $line.parent();
var buttonToEnable = $line.data('field.template.addButton');
$line.remove();
n.reExpandLineByLine($container);
n.informFormOfChange($container);
if (buttonToEnable) {
n.enableButton(buttonToEnable);
}
};
/**
......
......@@ -99,7 +99,7 @@
</div>
<div class="form-group">
<div class="col-md-1">
<button type="button" onclick="QfqNS.appendTemplate('#template', '#target', 10)">Add</button>
<button type="button" onclick="QfqNS.appendTemplate('#template', '#target', 10, this)">Add</button>
</div>
</div>
</form>
......@@ -181,7 +181,7 @@
</div>
<div class="form-group">
<div class="col-md-1">
<button type="button" onclick="QfqNS.appendTemplate('#template2', '#target2', 10)">Add</button>
<button type="button" onclick="QfqNS.appendTemplate('#template2', '#target2', 10, this)">Add</button>
</div>
</div>
</form>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment