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

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

parents 01c0161e e0422f49
/**
* @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
*/
/* global $ */
/* @depend Utils.js */
var QfqNS = QfqNS || {};
(function (n) {
'use strict';
n.addFields = function (templateSelector, targetSelector, maxLines) {
var escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
var escapedTargetSelector = n.escapeJqueryIdSelector(targetSelector);
var $template = $(escapedTemplateSelector);
var $target = $(escapedTargetSelector);
var lines = n.countLines(escapedTargetSelector);
if (lines >= maxLines) {
return;
}
var deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, lines);
$target.append(deserializedTemplate);
};
n.initializeFields = function (element, templateSelectorData) {
var $element = $(element);
var templateSelector, escapedTemplateSelector, $template, deserializedTemplate;
if ($element.children().length > 0) {
// already initialized
return;
}
templateSelector = $element.data(templateSelectorData || 'qfq-line-template');
escapedTemplateSelector = n.escapeJqueryIdSelector(templateSelector);
$template = $(escapedTemplateSelector);
deserializedTemplate = n.deserializeTemplateAndRetainPlaceholders($template.text());
n.expandRetainedPlaceholders(deserializedTemplate, 0);
deserializedTemplate.find('.qfq-delete-button').remove();
$element.append(deserializedTemplate);
};
n.removeFields = function (target) {
var $line = $(target).closest('.qfq-line');
var $container = $line.parent();
$line.remove();
n.reExpandLineByLine($container);
};
/**
* @private
* @param template
*/
n.deserializeTemplateAndRetainPlaceholders = function (template) {
var $deserializedTemplate = $(template);
$deserializedTemplate.find("*").each(function () {
var $element = $(this);
$.each(this.attributes, function () {
if (this.value.indexOf('%d') !== -1) {
$element.data(this.name, this.value);
}
});
});
return $deserializedTemplate;
};
/**
* @private
* @param
*/
n.expandRetainedPlaceholders = function ($elements, value) {
$elements.find('*').each(function () {
var $element = $(this);
$.each(this.attributes, function () {
var retainedPlaceholder = $element.data(this.name);
if (retainedPlaceholder) {
this.value = n.replacePlaceholder(retainedPlaceholder, value);
}
});
});
};
/**
* @private
* @param targetSelector
* @returns {jQuery}
*/
n.countLines = function (targetSelector) {
var escapedTargetSelector = n.escapeJqueryIdSelector(targetSelector);
return $(targetSelector).children().length;
};
/**
* @private
*/
n.replacePlaceholder = function (haystack, by) {
return haystack.replace(/%d/g, by);
};
/**
* @private
* @param $container
*/
n.reExpandLineByLine = function ($container) {
$container.children().each(function (index) {
var $element = $(this);
n.expandRetainedPlaceholders($element, index);
});
};
})(QfqNS);
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.min.css">
<link rel="stylesheet" href="../css/jqx.base.css">
<link rel="stylesheet" href="../css/jqx.bootstrap.css">
<link rel="stylesheet" href="../css/qfq-bs.css">
<title>Field Templates</title>
</head>
<body>
<form id="myForm" 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="target"
data-qfq-line-template="#template">
</div>
</div>
<div class="form-group">
<div class="col-md-1">
<button type="button" onclick="QfqNS.addFields('#template', '#target', 10)">Add</button>
</div>
</div>
</form>
<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>
<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>
<input id="id2_%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.removeFields(this)">Remove</button>
</div>
</div>
</script>
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/validator.min.js"></script>
<script src="../js/jqx-all.js"></script>
<script src="../js/globalize.js"></script>
<script src="../js/tinymce.min.js"></script>
<script src="../js/EventEmitter.min.js"></script>
<script src="../js/qfq.debug.js"></script>
<script type="text/javascript">
$(function () {
$(".qfq-fields").each(
function () {
QfqNS.initializeFields(this);
}
);
});
</script>
</body>
</html>
\ No newline at end of file
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