Koha/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js
Owen Leonard 1bc45a470e Bug 18752 - Automatic item modifications by age should allow 'blank' values
This patch modifies the automatic item modification by age template to
correct errors in form validation:

- Age in days should not be required
- A value should not be required in substitutions.

To test, apply the patch and clear your browser cache if necessary.

- Go to Tools -> Automatic item modification by age.
- Confirm that when editing or creating a rule the only required field
  is the substitutions field name.

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2017-06-13 16:21:37 -03:00

124 lines
3.8 KiB
JavaScript

function clear_inputs(node, new_node) {
var selects = $(node).find("select");
$(selects).each(function(i) {
var select = this;
$(new_node).find("select").eq(i).val($(select).val());
});
var inputs = $(node).find("input");
$(inputs).each(function(i) {
var input = this;
$(new_node).find("input").eq(i).val($(input).val());
});
}
function remove_block_action( link ) {
var blocks = $(link).parent().parent();
if( $(blocks).find(".block").length > 2 ) {
$(blocks).find("a.remove_block").show();
} else {
$(blocks).find("a.remove_block").hide();
}
$(link).parent().remove();
}
function remove_rule_action( link ) {
if( $("#rules").find(".rule").length < 2 ) {
$("#rules").hide();
$("#norules").show();
}
$(link).parent().parent().remove();
update_rule_count();
}
function clone_block(block) {
var new_block = $(block).clone(1);
clear_inputs(block, new_block);
$(new_block).find('a.remove_block').show();
var blocks = $(block).parent();
$(blocks).append(new_block);
$(blocks).find('a.remove_block').click(function(e){
e.preventDefault();
remove_block_action($(this));
}).show();
}
function update_rule_count(){
rules = $(".rulecount");
rules.each( function( i ){
$(this).text( i + 1 );
});
}
$(document).ready(function() {
$("#new_rule .remove_rule").hide();
$("#new_rule a.remove_block").hide();
$("#rules a.remove_block").click(function(e){
e.preventDefault();
remove_block_action($(this));
});
$("#rules .remove_rule").click(function(e){
e.preventDefault();
remove_rule_action($(this));
});
var unique_id = $(".rule").length + 1;
$(".add_rule").click(function(e){
e.preventDefault();
var rule = $("#new_rule");
var rules = $("#rules");
var new_rule = rule.clone(1);
new_rule.removeAttr('id');
new_rule.attr('class', 'rule');
clear_inputs(rule, new_rule);
new_rule.find("select[name='condition_field']").attr('name', 'condition_field_' + unique_id);
new_rule.find("select[name='substitution_field']").attr('name', 'substitution_field_' + unique_id);
new_rule.find("input[name='condition_value']").attr('name', 'condition_value_' + unique_id);
new_rule.find("input[name='substitution_value']").attr('name', 'substitution_value_' + unique_id);
new_rule.find("input[name='age']").attr('name', 'age_' + unique_id);
new_rule.find("input[name='unique_id']").val(unique_id);
$("#rules").append(new_rule);
update_rule_count();
var scrollToPoint = new_rule.position();
window.scroll(0, scrollToPoint.top - $("#toolbar").height() );
if( $("#rules").find(".rule").length > 0 ) {
$("#rules").show();
$("#norules").hide();
}
if( $("#rules").find(".conditions > .condition").length > 1 ) {
}
if( $("#rules").find(".conditions > .condition").length > 1 ) {
}
new_rule.find('.remove_rule').click(function(e){
e.preventDefault();
remove_rule_action( $(this) );
}).show();
new_rule.find('.add_rule').remove();
unique_id++;
});
$("a.add_block").click(function(e){
e.preventDefault();
clone_block( $(this).parent() );
});
if( $("#rules").find(".rule").length < 1 ) {
$("#rules").hide();
$("#norules").show();
}
$("#rules .rule .blocks").each(function(){
if ( $(this).find(".block").length == 1 ) {
$(this).find("a.remove_block").hide();
}
});
jQuery.validator.addClassRules("age", {
digits: true
});
$("#rules_form").validate();
});