@@ -667,6 +667,39 @@ function orEmptyArray(v) {
667
667
668
668
} ;
669
669
670
+ function applyIncludeExclude ( metadata , outputModel , observable , initialConstraints ) {
671
+ var path = metadata . constraints . excludePath || metadata . constraints . includePath ;
672
+
673
+ var currentValue = observable ( ) ;
674
+ var selectedSoFar = [ ] ;
675
+ outputModel . eachValueForPath ( path , function ( val ) {
676
+ if ( _ . isArray ( val ) ) {
677
+ selectedSoFar = selectedSoFar . concat ( val ) ;
678
+ } else if ( val != null ) {
679
+ selectedSoFar . push ( val ) ;
680
+ }
681
+ } ) ;
682
+
683
+ if ( metadata . constraints . excludePath ) {
684
+ return _ . filter ( initialConstraints , function ( value ) {
685
+ var isCurrentSelection = _ . isArray ( currentValue ) ? currentValue . indexOf ( value ) >= 0 : value == currentValue ;
686
+ return ( isCurrentSelection || selectedSoFar . indexOf ( value ) < 0 ) ;
687
+ } ) ;
688
+ } else {
689
+ var constraints = initialConstraints . concat ( selectedSoFar ) ;
690
+ var currentSelection = _ . isArray ( currentValue ) ? currentValue : [ currentValue ] ;
691
+ for ( var i = 0 ; i < currentSelection . length ; i ++ ) {
692
+
693
+ if ( currentSelection [ i ] != null && constraints . indexOf ( currentSelection [ i ] ) < 0 ) {
694
+ constraints . push ( currentSelection [ i ] ) ;
695
+ }
696
+ }
697
+
698
+ return constraints ;
699
+ }
700
+
701
+ }
702
+
670
703
/**
671
704
* Implements the constraints specified on a single data model item using the "constraints" attribute in the metadata.
672
705
* Also provides access to global configuration and context for components that need it.
@@ -743,6 +776,7 @@ function orEmptyArray(v) {
743
776
}
744
777
745
778
self . constraints = [ ] ;
779
+ var includeExcludeDefined = metadata . constraints . excludePath || metadata . constraints . includePath ;
746
780
// Support existing configuration style.
747
781
if ( _ . isArray ( metadata . constraints ) ) {
748
782
self . constraints = [ ] . concat ( metadata . constraints ) ;
@@ -754,63 +788,44 @@ function orEmptyArray(v) {
754
788
var rule = _ . find ( metadata . constraints . options , function ( option ) {
755
789
return ecodata . forms . expressionEvaluator . evaluateBoolean ( option . condition , context ) ;
756
790
} ) ;
757
- return rule ? rule . value : metadata . constraints . default ;
791
+ var evaluatedConstraints = rule ? rule . value : metadata . constraints . default ;
792
+ return ! includeExcludeDefined ? evaluatedConstraints : applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . default || [ ] ) ;
758
793
} ) ;
759
- }
760
- // This configuration takes a set of default constraints then either
761
- // adds values based on selections from other model items in the form or
762
- // removes selections based on other model items in the form. The main use
763
- // case this was developed for was to only allow each contraint to be selected once
764
- // in a form.
765
- else if ( metadata . constraints . excludePath || metadata . constraints . includePath ) {
766
- var defaultConstraints = metadata . constraints . default || [ ] ;
767
- var path = metadata . constraints . excludePath || metadata . constraints . includePath ;
768
- self . constraints = ko . computed ( function ( ) {
769
- var currentValue = self ( ) ;
770
- var selectedSoFar = [ ] ;
771
- context . outputModel . eachValueForPath ( path , function ( val ) {
772
- if ( _ . isArray ( val ) ) {
773
- selectedSoFar = selectedSoFar . concat ( val ) ;
774
- }
775
- else if ( val != null ) {
776
- selectedSoFar . push ( val ) ;
777
- }
778
- } ) ;
779
-
780
- if ( metadata . constraints . excludePath ) {
781
- return _ . filter ( defaultConstraints , function ( value ) {
782
- var isCurrentSelection = _ . isArray ( currentValue ) ? currentValue . indexOf ( value ) >= 0 : value == currentValue ;
783
- return ( isCurrentSelection || selectedSoFar . indexOf ( value ) < 0 ) ;
784
- } ) ;
785
- }
786
- else {
787
- var constraints = defaultConstraints . concat ( selectedSoFar ) ;
788
- var currentSelection = _ . isArray ( currentValue ) ? currentValue : [ currentValue ] ;
789
- for ( var i = 0 ; i < currentSelection . length ; i ++ ) {
790
-
791
- if ( currentSelection [ i ] != null && constraints . indexOf ( currentSelection [ i ] ) < 0 ) {
792
- constraints . push ( currentSelection [ i ] ) ;
793
- }
794
- }
795
-
796
- return constraints ;
797
- }
794
+ } else if ( includeExcludeDefined ) {
795
+ self . constraints = ko . computed ( function ( ) {
796
+ return applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . default || [ ] ) ;
798
797
} ) ;
799
798
}
800
799
}
801
800
else if ( metadata . constraints . type == 'pre-populated' ) {
802
801
var defaultConstraints = metadata . constraints . defaults || [ ] ;
803
- self . constraints = ko . observableArray ( defaultConstraints ) ;
802
+ var constraintsObservable = ko . observableArray ( defaultConstraints ) ;
803
+ if ( ! includeExcludeDefined ) {
804
+ self . constraints = constraintsObservable ;
805
+ }
806
+ else {
807
+ self . constraints = ko . computed ( function ( ) {
808
+ return applyIncludeExclude ( metadata , context . outputModel , self , constraintsObservable ( ) ) ;
809
+ } ) ;
810
+ }
804
811
805
812
constraintsInititaliser = $ . Deferred ( ) ;
806
813
var dataLoader = ecodata . forms . dataLoader ( context , config ) ;
807
814
dataLoader . prepop ( metadata . constraints . config ) . done ( function ( data ) {
808
- self . constraints ( data ) ;
815
+ constraintsObservable ( data ) ;
809
816
constraintsInititaliser . resolve ( ) ;
810
817
} ) ;
811
818
}
812
819
else if ( metadata . constraints . type == 'literal' || metadata . constraints . literal ) {
813
- self . constraints = [ ] . concat ( metadata . constraints . literal ) ;
820
+
821
+ if ( includeExcludeDefined ) {
822
+ self . constraints = ko . computed ( function ( ) {
823
+ return applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . literal || [ ] ) ;
824
+ } ) ;
825
+ }
826
+ else {
827
+ self . constraints = [ ] . concat ( metadata . constraints . literal ) ;
828
+ }
814
829
}
815
830
}
816
831
0 commit comments