Commit ec26ad07 authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

Set template data on existing lines in order to enable renumbering.

parent 91981ca3
......@@ -83,15 +83,17 @@ var QfqNS = QfqNS || {};
var $element = $(element);
var templateSelector, escapedTemplateSelector, $template, deserializedTemplate;
templateSelector = $element.data(templateSelectorData || 'qfq-line-template');
escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
$template = $(escapedTemplateSelector);
if ($element.children().length > 0) {
// already initialized
n.setPlaceholderRetainers($template.text(), element);
return;
}
templateSelector = $element.data(templateSelectorData || 'qfq-line-template');
escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
$template = $(escapedTemplateSelector);
deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, 1);
......@@ -153,9 +155,56 @@ var QfqNS = QfqNS || {};
return $deserializedTemplate;
};
/**
* Set placeholder retainer on existing lines
* @private
* @param template
* @param element container element
*/
n.setPlaceholderRetainers = function (template, element) {
var $deserializedTemplate = $(template);
var $flatTemplate = $deserializedTemplate.find('*');
var $element = $(element);
var $childrenOfElement = $element.children();
$childrenOfElement.each(function () {
// We use .find() to increase chances of $flatTemplate and $childrenOfChild having the same ordering.
var $childrenOfChild = $(this).find('*');
$childrenOfChild.each(function (childIndex) {
var correspondingTemplateElement, $correspondingTemplateElement, $child;
if (childIndex >= ($flatTemplate.length - 1)) {
// the current child element has no corresponding element in the template, so no use of trying to
// copy over retaining information.
return;
}
$child = $(this);
correspondingTemplateElement = $flatTemplate[childIndex];
$correspondingTemplateElement = $(correspondingTemplateElement);
// Create the retainers on the child for each child
$.each(correspondingTemplateElement.attributes, function () {
if (this.value.indexOf('%d') !== -1) {
$child.data(this.name, this.value);
}
});
if (n.isTextRetainable($correspondingTemplateElement)) {
$child.data('element-text', $correspondingTemplateElement.text());
}
});
});
};
/**
* @private
* @param
*/
n.expandRetainedPlaceholders = function ($elements, value) {
$elements.find('*').each(function () {
......
......@@ -34,6 +34,88 @@
</div>
</form>
<form id="myForm2" class="form-horizontal" data-toggle="validator">
<input type="hidden" name="s" value="badcaffee1234">
<div class="form-group">
<div class="col-md-2">
<b class="control-label">Fotowand</b>
</div>
<div class="col-md-6 qfq-fields" id="target2"
data-qfq-line-template="#template">
<div class="form-inline qfq-line">
<div class="form-group">
<label for="id21_1" class="control-label">Zeile (A-K) 1</label>
<input id="id21_1" type="text" class="form-control" name="name1_1" value="value 1">
</div>
<div class="form-group">
<label for="id22_1" class="control-label">Zeile (1-10) 1</label>
<input id="id22_1" type="text" class="form-control" name="name2_1" value="value 1">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="name3_1">Zweizeilig
</label>
</div>
</div>
</div>
<div class="form-inline qfq-line">
<div class="form-group">
<label for="id21_2" class="control-label">Zeile (A-K) 2</label>
<input id="id21_2" type="text" class="form-control" name="name1_2" value="value 2">
</div>
<div class="form-group">
<label for="id22_2" class="control-label">Zeile (1-10) 2</label>
<input id="id22_2" type="text" class="form-control" name="name2_2" value="value 2">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="name3_2">Zweizeilig
</label>
</div>
</div>
<div class="form-group qfq-delete-button">
<button type="button" onclick="QfqNS.removeThisChild(this)">Remove</button>
</div>
</div>
<div class="form-inline qfq-line">
<div class="form-group">
<label for="id21_3" class="control-label">Zeile (A-K) 3</label>
<input id="id21_3" type="text" class="form-control" name="name1_3" value="value 3">
</div>
<div class="form-group">
<label for="id22_3" class="control-label">Zeile (1-10) 3</label>
<input id="id22_3" type="text" class="form-control" name="name2_3" value="value 3">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="name3_3">Zweizeilig
</label>
</div>
</div>
<div class="form-group qfq-delete-button">
<button type="button" onclick="QfqNS.removeThisChild(this)">Remove</button>
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-1">
<button type="button" onclick="QfqNS.appendTemplate('#template2', '#target2', 10)">Add</button>
</div>
</div>
</form>
<script id="template" type="text/template">
<div class="form-inline qfq-line">
<div class="form-group">
......@@ -59,6 +141,31 @@
</div>
</script>
<script id="template2" type="text/template">
<div class="form-inline qfq-line">
<div class="form-group">
<label for="id21_%d" class="control-label">Zeile (A-K) %d</label>
<input id="id21_%d" type="text" class="form-control" name="name1_%d">
</div>
<div class="form-group">
<label for="id22_%d" class="control-label">Zeile (1-10) %d</label>
<input id="id22_%d" type="text" class="form-control" name="name2_%d">
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" name="name3_%d">Zweizeilig
</label>
</div>
</div>
<div class="form-group qfq-delete-button">
<button type="button" onclick="QfqNS.removeThisChild(this)">Remove</button>
</div>
</div>
</script>
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
......
Supports Markdown
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