@@ -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.
@@ -744,6 +777,7 @@ function orEmptyArray(v) {
744
777
}
745
778
746
779
self . constraints = [ ] ;
780
+ var includeExcludeDefined = metadata . constraints . excludePath || metadata . constraints . includePath ;
747
781
// Support existing configuration style.
748
782
if ( _ . isArray ( metadata . constraints ) ) {
749
783
self . constraints = [ ] . concat ( metadata . constraints ) ;
@@ -755,63 +789,44 @@ function orEmptyArray(v) {
755
789
var rule = _ . find ( metadata . constraints . options , function ( option ) {
756
790
return ecodata . forms . expressionEvaluator . evaluateBoolean ( option . condition , context ) ;
757
791
} ) ;
758
- return rule ? rule . value : metadata . constraints . default ;
792
+ var evaluatedConstraints = rule ? rule . value : metadata . constraints . default ;
793
+ return ! includeExcludeDefined ? evaluatedConstraints : applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . default || [ ] ) ;
759
794
} ) ;
760
- }
761
- // This configuration takes a set of default constraints then either
762
- // adds values based on selections from other model items in the form or
763
- // removes selections based on other model items in the form. The main use
764
- // case this was developed for was to only allow each contraint to be selected once
765
- // in a form.
766
- else if ( metadata . constraints . excludePath || metadata . constraints . includePath ) {
767
- var defaultConstraints = metadata . constraints . default || [ ] ;
768
- var path = metadata . constraints . excludePath || metadata . constraints . includePath ;
769
- self . constraints = ko . computed ( function ( ) {
770
- var currentValue = self ( ) ;
771
- var selectedSoFar = [ ] ;
772
- context . outputModel . eachValueForPath ( path , function ( val ) {
773
- if ( _ . isArray ( val ) ) {
774
- selectedSoFar = selectedSoFar . concat ( val ) ;
775
- }
776
- else if ( val != null ) {
777
- selectedSoFar . push ( val ) ;
778
- }
779
- } ) ;
780
-
781
- if ( metadata . constraints . excludePath ) {
782
- return _ . filter ( defaultConstraints , function ( value ) {
783
- var isCurrentSelection = _ . isArray ( currentValue ) ? currentValue . indexOf ( value ) >= 0 : value == currentValue ;
784
- return ( isCurrentSelection || selectedSoFar . indexOf ( value ) < 0 ) ;
785
- } ) ;
786
- }
787
- else {
788
- var constraints = defaultConstraints . concat ( selectedSoFar ) ;
789
- var currentSelection = _ . isArray ( currentValue ) ? currentValue : [ currentValue ] ;
790
- for ( var i = 0 ; i < currentSelection . length ; i ++ ) {
791
-
792
- if ( currentSelection [ i ] != null && constraints . indexOf ( currentSelection [ i ] ) < 0 ) {
793
- constraints . push ( currentSelection [ i ] ) ;
794
- }
795
- }
796
-
797
- return constraints ;
798
- }
795
+ } else if ( includeExcludeDefined ) {
796
+ self . constraints = ko . computed ( function ( ) {
797
+ return applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . default || [ ] ) ;
799
798
} ) ;
800
799
}
801
800
}
802
801
else if ( metadata . constraints . type == 'pre-populated' ) {
803
802
var defaultConstraints = metadata . constraints . defaults || [ ] ;
804
- self . constraints = ko . observableArray ( defaultConstraints ) ;
803
+ var constraintsObservable = ko . observableArray ( defaultConstraints ) ;
804
+ if ( ! includeExcludeDefined ) {
805
+ self . constraints = constraintsObservable ;
806
+ }
807
+ else {
808
+ self . constraints = ko . computed ( function ( ) {
809
+ return applyIncludeExclude ( metadata , context . outputModel , self , constraintsObservable ( ) ) ;
810
+ } ) ;
811
+ }
805
812
806
813
constraintsInititaliser = $ . Deferred ( ) ;
807
814
var dataLoader = ecodata . forms . dataLoader ( context , config ) ;
808
815
dataLoader . prepop ( metadata . constraints . config ) . done ( function ( data ) {
809
- self . constraints ( data ) ;
816
+ constraintsObservable ( data ) ;
810
817
constraintsInititaliser . resolve ( ) ;
811
818
} ) ;
812
819
}
813
820
else if ( metadata . constraints . type == 'literal' || metadata . constraints . literal ) {
814
- self . constraints = [ ] . concat ( metadata . constraints . literal ) ;
821
+
822
+ if ( includeExcludeDefined ) {
823
+ self . constraints = ko . computed ( function ( ) {
824
+ return applyIncludeExclude ( metadata , context . outputModel , self , metadata . constraints . literal || [ ] ) ;
825
+ } ) ;
826
+ }
827
+ else {
828
+ self . constraints = [ ] . concat ( metadata . constraints . literal ) ;
829
+ }
815
830
}
816
831
}
817
832
0 commit comments