Commit fa276fe1 authored by Frédéric Escudié's avatar Frédéric Escudié
Browse files

Remove exclusion management.

parent a44aaaae
......@@ -29,25 +29,43 @@ var get_parent_with_class = function( $element, selected_class ) {
}
}
var get_parent_with_class_startswith = function( $element, selected_class_prefix ) {
if( $element.parent() == null || $element.parent().is("html") ){
return null ;
} else {
var has_class = false ;
if( $element.parent().attr('class') ){
var classes = $element.parent().attr('class').split(/\s/) ;
for( var i = 0 ; i < classes.length ; i++ ) {
if( classes[i].indexOf(selected_class_prefix) == 0 ){
has_class = true ;
}
/**
* Checks if the input or at least one input in the element is set.
* @param {jquery object} $element The DOM element checked.
*/
var _isSet = function( $element ) {
var elt_is_set = false ;
if( $element.is(":input") ){ // simple
if( !$element.is(":checkbox") ){
elt_is_set = ( $element.val() != "" );
} else {
if( $element.is(':checked') ){
elt_is_set = true ;
}
}
if( has_class ){
return $element.parent() ;
} else {
return get_parent_with_class_startswith( $element.parent(), selected_class_prefix );
} else {
if( !$element.has( "[id^=handsontable_]" ) ){ // multiple
$element.find(":input").each( function(){
if( !$(this).is(":button") ) {
if( _isSet($(this)) ){
elt_is_set = true ;
}
}
});
} else { // multiple append
$element.find("[id^=handsontable_]").each( function() {
var hot_data = $(this).handsontable('getData');
for( var idx = 0; idx < hot_data.length; idx++ ){
for( var sub_param in hot_data[idx] ){
if( hot_data[idx][sub_param] != null && hot_data[idx][sub_param] != "" ){
elt_is_set = true ;
}
}
}
});
}
}
return elt_is_set ;
}
$.validator.setDefaults({
......@@ -95,43 +113,17 @@ var get_parent_with_class_startswith = function( $element, selected_class_prefix
errorClass: "has-error"
});
jQuery.validator.addMethod("exclude", function(value, element, options) {
var validator = this,
selector = options;
var numberFilled = $(selector, element.form).filter(function() {
return validator.elementValue(this);
}).length;
var valid = false;
if (numberFilled === 1) {
valid = true;
} else if (numberFilled === 0) {
valid = true;
}
$(selector).each(function() {
if( !$(this).is(element) ){
if( valid ){
$( "#" + $(this).attr("id") + "-error" ).remove();
validator.settings.unhighlight( $(this), validator.settings.errorClass, validator.settings.validClass );
} else {
validator.settings.highlight( $(this), validator.settings.errorClass, validator.settings.validClass );
validator.showLabel( validator.findByName( $(this).attr("name") )[0], "Please fill only one of these fields." );
}
}
});
return valid;
}, jQuery.validator.format("Please fill only one of these fields."));
jQuery.validator.addMethod("exclude_required", function(value, element, options) {
var validator = this,
selector = options;
var numberFilled = $(selector, element.form).filter(function() {
return validator.elementValue(this);
return _isSet($(this));
}).length;
var valid = false;
if (numberFilled === 1) {
valid = true;
}
$(selector).each(function() {
/*$(selector).each(function() {
if( !$(this).is(element) ){
if( valid ){
$( "#" + $(this).attr("id") + "-error" ).remove();
......@@ -141,7 +133,7 @@ jQuery.validator.addMethod("exclude_required", function(value, element, options)
validator.showLabel( validator.findByName( $(this).attr("name") )[0], "Please fill one and only one of these fields." );
}
}
});
});*/
return valid;
}, jQuery.validator.format("Please fill one and only one of these fields."));
......@@ -315,220 +307,6 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
}
}
}
/**
* Checks if the input or at least one input in the element is set.
* @param {jquery object} $element The DOM element checked.
*/
var _isSet = function( $element ) {
var elt_is_set = false ;
if( $element.is(":input") ){ // simple
if( !$element.is(":checkbox") ){
elt_is_set = ( $element.val() != "" );
} else {
if( $element.is(':checked') ){
elt_is_set = true ;
}
}
} else {
if( !$element.has( "[id^=handsontable_]" ) ){ // multiple
$element.find(":input").each( function(){
if( !$(this).is(":button") ) {
if( _isSet($(this)) ){
elt_is_set = true ;
}
}
});
} else { // multiple append
$element.find("[id^=handsontable_]").each( function() {
var hot_data = $(this).handsontable('getData');
for( var idx = 0; idx < hot_data.length; idx++ ){
for( var sub_param in hot_data[idx] ){
if( hot_data[idx][sub_param] != null && hot_data[idx][sub_param] != "" ){
elt_is_set = true ;
}
}
}
});
}
}
return elt_is_set ;
}
/**
* Checks if the input or all inputs in the elements are read only.
* @param {jquery object} $element The DOM element checked.
*/
var _isLocked = function( $element ) {
var elt_is_locked = false ;
if( $element.is(":input") ){ // simple
if( $element.is("select") || $element.is(":checkbox") || $element.is(":button") ){
if( $element.attr('disabled') != undefined ){
elt_is_locked = true ;
}
} else {
if( $element.attr('readonly') != undefined ){
elt_is_locked = true ;
}
}
} else {
if( !$element.has( "[id^=handsontable_]" ) ){ // multiple
var sub_are_locked = new Array();
$element.find(":input").each( function(){
if( !$(this).is(":button") ) {
if( _isLocked($(this)) ){
sub_are_locked.push( true );
} else {
sub_are_locked.push( false );
}
}
});
if( sub_are_locked.indexOf(false) != -1 && sub_are_locked.length != 0 ){
elt_is_locked = false ;
} else {
elt_is_locked = true ;
}
} else { // multiple append
$element.find("[id^=handsontable_]").each( function() {
var handsontable = $(this).handsontable('getInstance');
elt_is_locked = handsontable.getSettings().readOnly ;
});
}
}
return elt_is_locked ;
}
/**
* All inputs in element become read only.
* @param {jquery object} $element The DOM element to lock.
*/
var _lockFields = function( $element ){
if( $element.is(":input") ){
if( $element.is("select") || $element.is(":checkbox") || $element.is(":button") ){
$element.attr('disabled', true);
} else {
$element.attr('readonly', true);
}
} else {
$element.find(":input").each( function() {
if( $(this).is("select") || $(this).is(":checkbox") || $(this).is(":button") ){
$(this).attr('disabled', true);
} else {
$(this).attr('readonly', true);
}
});
$element.find("[id^=handsontable_]").each( function() {
var handsontable = $(this).handsontable('getInstance');
handsontable.updateSettings({
readOnly: true
});
handsontable.render();
});
}
}
/**
* All inputs in element become writable.
* @param {jquery object} $element The DOM element to unlock.
*/
var _unlockFields = function( $element ){
if( $element.is(":input") ){
if( $element.is("select") || $element.is(":checkbox") || $element.is(":button") ){
$element.attr('disabled', false);
} else {
$element.prop('readonly', false);
}
} else {
$element.find(":input").each( function() {
if( $(this).is("select") || $(this).is(":checkbox") || $(this).is(":button") ){
$(this).attr('disabled', false);
} else {
$(this).attr('readonly', false);
}
});
$element.find("[id^=handsontable_]").each( function() {
var handsontable = $(this).handsontable('getInstance');
handsontable.updateSettings({
readOnly: false
});
handsontable.render();
});
}
}
/**
* Lock fields excluded by default values.
*/
var _initExclusions = function() {
// Find groups
var exclusion_groups = new Array();
$( "[class^='exclude-'],[class*=' exclude-']" ).each( function() {
// Select the exclusion group
var group = null ;
var classes = $(this).attr('class').split(/\s/) ;
for( var i = 0 ; i < classes.length ; i++ ) {
if( classes[i].indexOf("exclude-") == 0 ){
group = classes[i] ;
}
}
// Indicates if fields is empty
if( !exclusion_groups.hasOwnProperty(group) ){
exclusion_groups[group] = "empty" ;
}
if( _isSet($(this)) ){
exclusion_groups[group] = "value" ;
}
});
// Lock field if it is necessary
$( "[class^='exclude-'],[class*=' exclude-']" ).each( function() {
// Select the exclusion group
var group = null ;
var classes = $(this).attr('class').split(/\s/) ;
for( var i = 0 ; i < classes.length ; i++ ) {
if( classes[i].indexOf("exclude-") == 0 ){
group = classes[i] ;
}
}
if( exclusion_groups[group] != "empty" ){ // One element of group is setted
var current_is_set = _isSet( $(this) );
if( !current_is_set ) {
_lockFields( $(this) );
}
}
});
}
/**
* Lock/Unlock excluded fields after the element modification.
* @param {jquery object} $element The DOM element checked.
*/
var _updateExclusions = function( $elt ) {
if( !_isLocked($elt) ){
// Select the exclusion group
var exclusion_group = null ;
var classes = $elt.attr('class').split(/\s/) ;
for( var i = 0 ; i < classes.length ; i++ ) {
if( classes[i].indexOf("exclude-") == 0 ){
exclusion_group = classes[i] ;
}
}
// Lock/unlock fields
var $updated_elt = $elt ;
var updated_is_set = _isSet( $elt );
$('.' + exclusion_group).each(function( index ) {
var current_is_set = _isSet( $(this) );
if( updated_is_set ) {
if( $(this).is($updated_elt) ) {
_unlockFields( $(this) );
} else {
_lockFields( $(this) );
}
} else {
_unlockFields( $(this) );
}
});
}
}
var _uploadProgress = function(elt, justinit) {
var allUploaded = true,
......@@ -894,7 +672,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
// for multiple and append values, use the handsontable
$("[id^=handsontable_]").each(function(){
var id_parts = $(this).attr("id").split("handsontable_"),
var id_parts = $(this).attr("id").split("handsontable_"),
param_name = id_parts.slice(1, id_parts.length),
dataSchema = {},
colHeaders = new Array(),
......@@ -1096,28 +874,6 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
});
});
_initExclusions();
// Add exclusion listeners
$( "[class^='exclude-'],[class*=' exclude-']" ).on( "keyup change", function() {
_updateExclusions( $(this) );
});
$("[id^='handsontable_']").each(function(){
var $param = get_parent_with_class_startswith( $(this), "exclude-" );
if( $param != null ){
var hot = $(this).handsontable('getInstance');
hot.addHook( 'afterRemoveRow', function() {
_updateExclusions( $param );
});
hot.addHook( 'afterCreateRow', function() {
_updateExclusions( $param );
});
hot.addHook( 'afterChange', function() {
_updateExclusions( $param );
});
}
});
if ($this.options.displayRunButton) {
$("#wfform_run_btn").click(function() { $this.run(); })
}
......@@ -1176,10 +932,7 @@ Handsontable.cellTypes["bootdate"] = Handsontable.BootstrapDateCell;
if (excludereq.indexOf(param.group) != -1) {
crule["exclude_required"] = "."+param.group;
crule["required"] = false;
} else if (param.group.indexOf("exclude-") != -1) {
crule["exclude"] = "."+param.group;
crule["required"] = false;
} else {
} else if (param.group.indexOf("exclude-") == -1) {
crule["required"] = param.required;
}
return crule;
......
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