Commit ec14cec3 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Merge remote-tracking branch 'origin/raos_work' into crose_work

parents 38c47c81 af27a2bc
......@@ -11,7 +11,32 @@ var QfqNS = QfqNS || {};
(function (n) {
'use strict';
/**
* @name addFields
* @function
* @global
* @deprecated use appendTemplate().
*
*/
n.addFields = function (templateSelector, targetSelector, maxLines) {
n.appendTemplate(templateSelector, targetSelector, maxLines);
};
/**
* Append a child created from a template to a target. All occurrences of `%d` in attribute values will be replaced
* by the number of children of target.
*
* @name appendTemplate
* @public
* @global
* @function
*
* @param {string} templateSelector jQuery selector uniquely identifying the template.
* @param {string} targetSelector jQuery selector uniquely identifying the target container.
* @param {number} maxChildren do not allow more than `maxChildren` of children in target.
*
*/
n.appendTemplate = function (templateSelector, targetSelector, maxChildren) {
var escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
var escapedTargetSelector = n.escapeJqueryIdSelector(targetSelector);
......@@ -19,9 +44,9 @@ var QfqNS = QfqNS || {};
var $target = $(escapedTargetSelector);
var lines = n.countLines(escapedTargetSelector);
var lines = n.countLines(escapedTargetSelector) + 1;
if (lines >= maxLines) {
if (lines >= maxChildren) {
return;
}
......@@ -31,7 +56,30 @@ var QfqNS = QfqNS || {};
$target.append(deserializedTemplate);
};
/**
* @name initializeFields
* @global
* @function
* @deprecated use initializeTemplateTarget()
*/
n.initializeFields = function (element, templateSelectorData) {
n.initializeTemplateTarget(element, templateSelectorData);
};
/**
* When the template target contains no children, it initializes the target element by appending the first child
* created from the template.
*
* @name initializeTemplateTarget
* @global
* @function
*
* @param {HTMLElement} element the target HTMLElement to be initialized
* @param {string} [templateSelectorData=qfq-line-template] name of the `data-` attribute containing the jQuery
* selector
* selecting the template
*/
n.initializeTemplateTarget = function (element, templateSelectorData) {
var $element = $(element);
var templateSelector, escapedTemplateSelector, $template, deserializedTemplate;
......@@ -46,14 +94,33 @@ var QfqNS = QfqNS || {};
$template = $(escapedTemplateSelector);
deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, 0);
n.expandRetainedPlaceholders(deserializedTemplate, 1);
deserializedTemplate.find('.qfq-delete-button').remove();
$element.append(deserializedTemplate);
};
/**
* @name removeFields
* @global
* @function
* @deprecated use removeThisChild()
*/
n.removeFields = function (target) {
var $line = $(target).closest('.qfq-line');
n.removeThisChild(target);
};
/**
* Remove the element having a class `qfq-line`. Uses `eventTarget` as start point for determining the closest
* element.
*
* @name removeFields
* @global
* @function
* @param {HTMLElement} eventTarget start point for determining the closest `.qfq-line`.
*/
n.removeThisChild = function (eventTarget) {
var $line = $(eventTarget).closest('.qfq-line');
var $container = $line.parent();
$line.remove();
......@@ -61,8 +128,11 @@ var QfqNS = QfqNS || {};
};
/**
* Takes a template as string and deserializes it into DOM. Any attributes having a value containing `%d` will be
*
* @private
* @param template
*
*/
n.deserializeTemplateAndRetainPlaceholders = function (template) {
var $deserializedTemplate = $(template);
......@@ -74,6 +144,10 @@ var QfqNS = QfqNS || {};
$element.data(this.name, this.value);
}
});
if (n.isTextRetainable($element)) {
$element.data('element-text', $element.text());
}
});
return $deserializedTemplate;
......@@ -92,9 +166,32 @@ var QfqNS = QfqNS || {};
this.value = n.replacePlaceholder(retainedPlaceholder, value);
}
});
if (n.hasRetainedText($element)) {
$element.text(n.replacePlaceholder($element.data('element-text'), value));
}
});
};
/**
* @private
* @param $element
* @returns {*}
*/
n.isTextRetainable = function ($element) {
return $element.is("label") && $element.text().indexOf('%d') !== -1 && $element.children().length === 0;
};
/**
* @private
* @param $element
* @returns {boolean}
*/
n.hasRetainedText = function ($element) {
return $element.data('element-text') !== undefined;
};
/**
* @private
* @param targetSelector
......@@ -119,7 +216,7 @@ var QfqNS = QfqNS || {};
n.reExpandLineByLine = function ($container) {
$container.children().each(function (index) {
var $element = $(this);
n.expandRetainedPlaceholders($element, index);
n.expandRetainedPlaceholders($element, index + 1);
});
};
})(QfqNS);
\ No newline at end of file
......@@ -29,7 +29,7 @@
</div>
<div class="form-group">
<div class="col-md-1">
<button type="button" onclick="QfqNS.addFields('#template', '#target', 10)">Add</button>
<button type="button" onclick="QfqNS.appendTemplate('#template', '#target', 10)">Add</button>
</div>
</div>
</form>
......@@ -37,11 +37,11 @@
<script id="template" type="text/template">
<div class="form-inline qfq-line">
<div class="form-group">
<label for="id1_%d" class="control-label">Zeile (A-K)</label>
<label for="id1_%d" class="control-label">Zeile (A-K) %d</label>
<input id="id1_%d" type="text" class="form-control" name="name1_%d">
</div>
<div class="form-group">
<label for="id2_%d" class="control-label">Zeile (1-10)</label>
<label for="id2_%d" class="control-label">Zeile (1-10) %d</label>
<input id="id2_%d" type="text" class="form-control" name="name2_%d">
</div>
......@@ -54,7 +54,7 @@
</div>
<div class="form-group qfq-delete-button">
<button type="button" onclick="QfqNS.removeFields(this)">Remove</button>
<button type="button" onclick="QfqNS.removeThisChild(this)">Remove</button>
</div>
</div>
</script>
......@@ -75,7 +75,7 @@
$(function () {
$(".qfq-fields").each(
function () {
QfqNS.initializeFields(this);
QfqNS.initializeTemplateTarget(this);
}
);
});
......
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