@@ -1727,277 +1727,280 @@ angular.module('schemaForm').directive('sfField',
1727
1727
transclude : false ,
1728
1728
scope : true ,
1729
1729
require : '?^sfSchema' ,
1730
- link : function ( scope , element , attrs , sfSchema ) {
1731
-
1732
- //The ngModelController is used in some templates and
1733
- //is needed for error messages,
1734
- scope . $on ( 'schemaFormPropagateNgModelController' , function ( event , ngModel ) {
1735
- event . stopPropagation ( ) ;
1736
- event . preventDefault ( ) ;
1737
- scope . ngModel = ngModel ;
1738
- } ) ;
1730
+ link : {
1731
+ pre : function ( scope , element , attrs , sfSchema ) {
1732
+ //The ngModelController is used in some templates and
1733
+ //is needed for error messages,
1734
+ scope . $on ( 'schemaFormPropagateNgModelController' , function ( event , ngModel ) {
1735
+ event . stopPropagation ( ) ;
1736
+ event . preventDefault ( ) ;
1737
+ scope . ngModel = ngModel ;
1738
+ } ) ;
1739
+ } ,
1740
+ post : function ( scope , element , attrs , sfSchema ) {
1739
1741
1740
- //Keep error prone logic from the template
1741
- scope . showTitle = function ( ) {
1742
- return scope . form && scope . form . notitle !== true && scope . form . title ;
1743
- } ;
1742
+ //Keep error prone logic from the template
1743
+ scope . showTitle = function ( ) {
1744
+ return scope . form && scope . form . notitle !== true && scope . form . title ;
1745
+ } ;
1744
1746
1745
- scope . listToCheckboxValues = function ( list ) {
1746
- var values = { } ;
1747
- angular . forEach ( list , function ( v ) {
1748
- values [ v ] = true ;
1749
- } ) ;
1750
- return values ;
1751
- } ;
1747
+ scope . listToCheckboxValues = function ( list ) {
1748
+ var values = { } ;
1749
+ angular . forEach ( list , function ( v ) {
1750
+ values [ v ] = true ;
1751
+ } ) ;
1752
+ return values ;
1753
+ } ;
1752
1754
1753
- scope . checkboxValuesToList = function ( values ) {
1754
- var lst = [ ] ;
1755
- angular . forEach ( values , function ( v , k ) {
1756
- if ( v ) {
1757
- lst . push ( k ) ;
1755
+ scope . checkboxValuesToList = function ( values ) {
1756
+ var lst = [ ] ;
1757
+ angular . forEach ( values , function ( v , k ) {
1758
+ if ( v ) {
1759
+ lst . push ( k ) ;
1760
+ }
1761
+ } ) ;
1762
+ return lst ;
1763
+ } ;
1764
+
1765
+ scope . buttonClick = function ( $event , form ) {
1766
+ if ( angular . isFunction ( form . onClick ) ) {
1767
+ form . onClick ( $event , form ) ;
1768
+ } else if ( angular . isString ( form . onClick ) ) {
1769
+ if ( sfSchema ) {
1770
+ //evaluating in scope outside of sfSchemas isolated scope
1771
+ sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form } ) ;
1772
+ } else {
1773
+ scope . $eval ( form . onClick , { '$event' : $event , form : form } ) ;
1774
+ }
1758
1775
}
1759
- } ) ;
1760
- return lst ;
1761
- } ;
1776
+ } ;
1762
1777
1763
- scope . buttonClick = function ( $event , form ) {
1764
- if ( angular . isFunction ( form . onClick ) ) {
1765
- form . onClick ( $event , form ) ;
1766
- } else if ( angular . isString ( form . onClick ) ) {
1778
+ /**
1779
+ * Evaluate an expression, i.e. scope.$eval
1780
+ * but do it in sfSchemas parent scope sf-schema directive is used
1781
+ * @param {string } expression
1782
+ * @param {Object } locals (optional)
1783
+ * @return {Any } the result of the expression
1784
+ */
1785
+ scope . evalExpr = function ( expression , locals ) {
1767
1786
if ( sfSchema ) {
1768
1787
//evaluating in scope outside of sfSchemas isolated scope
1769
- sfSchema . evalInParentScope ( form . onClick , { '$event' : $event , form : form } ) ;
1770
- } else {
1771
- scope . $eval ( form . onClick , { '$event' : $event , form : form } ) ;
1788
+ return sfSchema . evalInParentScope ( expression , locals ) ;
1772
1789
}
1773
- }
1774
- } ;
1775
1790
1776
- /**
1777
- * Evaluate an expression, i.e. scope.$eval
1778
- * but do it in sfSchemas parent scope sf-schema directive is used
1779
- * @param {string } expression
1780
- * @param {Object } locals (optional)
1781
- * @return {Any } the result of the expression
1782
- */
1783
- scope . evalExpr = function ( expression , locals ) {
1784
- if ( sfSchema ) {
1785
- //evaluating in scope outside of sfSchemas isolated scope
1786
- return sfSchema . evalInParentScope ( expression , locals ) ;
1787
- }
1791
+ return scope . $eval ( expression , locals ) ;
1792
+ } ;
1788
1793
1789
- return scope . $eval ( expression , locals ) ;
1790
- } ;
1794
+ /**
1795
+ * Evaluate an expression, i.e. scope.$eval
1796
+ * in this decorators scope
1797
+ * @param {string } expression
1798
+ * @param {Object } locals (optional)
1799
+ * @return {Any } the result of the expression
1800
+ */
1801
+ scope . evalInScope = function ( expression , locals ) {
1802
+ if ( expression ) {
1803
+ return scope . $eval ( expression , locals ) ;
1804
+ }
1805
+ } ;
1791
1806
1792
- /**
1793
- * Evaluate an expression, i.e. scope.$eval
1794
- * in this decorators scope
1795
- * @param {string } expression
1796
- * @param {Object } locals (optional)
1797
- * @return {Any } the result of the expression
1798
- */
1799
- scope . evalInScope = function ( expression , locals ) {
1800
- if ( expression ) {
1801
- return scope . $eval ( expression , locals ) ;
1802
- }
1803
- } ;
1807
+ /**
1808
+ * Interpolate the expression.
1809
+ * Similar to `evalExpr()` and `evalInScope()`
1810
+ * but will not fail if the expression is
1811
+ * text that contains spaces.
1812
+ *
1813
+ * Use the Angular `{{ interpolation }}`
1814
+ * braces to access properties on `locals`.
1815
+ *
1816
+ * @param {string } content The string to interpolate.
1817
+ * @param {Object } locals (optional) Properties that may be accessed in the
1818
+ * `expression` string.
1819
+ * @return {Any } The result of the expression or `undefined`.
1820
+ */
1821
+ scope . interp = function ( expression , locals ) {
1822
+ return ( expression && $interpolate ( expression ) ( locals ) ) ;
1823
+ } ;
1804
1824
1805
- /**
1806
- * Interpolate the expression.
1807
- * Similar to `evalExpr()` and `evalInScope()`
1808
- * but will not fail if the expression is
1809
- * text that contains spaces.
1810
- *
1811
- * Use the Angular `{{ interpolation }}`
1812
- * braces to access properties on `locals`.
1813
- *
1814
- * @param {string } content The string to interpolate.
1815
- * @param {Object } locals (optional) Properties that may be accessed in the
1816
- * `expression` string.
1817
- * @return {Any } The result of the expression or `undefined`.
1818
- */
1819
- scope . interp = function ( expression , locals ) {
1820
- return ( expression && $interpolate ( expression ) ( locals ) ) ;
1821
- } ;
1825
+ //This works since we ot the ngModel from the array or the schema-validate directive.
1826
+ scope . hasSuccess = function ( ) {
1827
+ if ( ! scope . ngModel ) {
1828
+ return false ;
1829
+ }
1830
+ return scope . ngModel . $valid &&
1831
+ ( ! scope . ngModel . $pristine || ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ) ;
1832
+ } ;
1822
1833
1823
- //This works since we ot the ngModel from the array or the schema-validate directive.
1824
- scope . hasSuccess = function ( ) {
1825
- if ( ! scope . ngModel ) {
1826
- return false ;
1827
- }
1828
- return scope . ngModel . $valid &&
1829
- ( ! scope . ngModel . $pristine || ! scope . ngModel . $isEmpty ( scope . ngModel . $modelValue ) ) ;
1830
- } ;
1834
+ scope . hasError = function ( ) {
1835
+ if ( ! scope . ngModel ) {
1836
+ return false ;
1837
+ }
1838
+ return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
1839
+ } ;
1831
1840
1832
- scope . hasError = function ( ) {
1833
- if ( ! scope . ngModel ) {
1834
- return false ;
1835
- }
1836
- return scope . ngModel . $invalid && ! scope . ngModel . $pristine ;
1837
- } ;
1841
+ /**
1842
+ * DEPRECATED: use sf-messages instead.
1843
+ * Error message handler
1844
+ * An error can either be a schema validation message or a angular js validtion
1845
+ * error (i.e. required)
1846
+ */
1847
+ scope . errorMessage = function ( schemaError ) {
1848
+ return sfErrorMessage . interpolate (
1849
+ ( schemaError && schemaError . code + '' ) || 'default' ,
1850
+ ( scope . ngModel && scope . ngModel . $modelValue ) || '' ,
1851
+ ( scope . ngModel && scope . ngModel . $viewValue ) || '' ,
1852
+ scope . form ,
1853
+ scope . options && scope . options . validationMessage
1854
+ ) ;
1855
+ } ;
1838
1856
1839
- /**
1840
- * DEPRECATED: use sf-messages instead.
1841
- * Error message handler
1842
- * An error can either be a schema validation message or a angular js validtion
1843
- * error (i.e. required)
1844
- */
1845
- scope . errorMessage = function ( schemaError ) {
1846
- return sfErrorMessage . interpolate (
1847
- ( schemaError && schemaError . code + '' ) || 'default' ,
1848
- ( scope . ngModel && scope . ngModel . $modelValue ) || '' ,
1849
- ( scope . ngModel && scope . ngModel . $viewValue ) || '' ,
1850
- scope . form ,
1851
- scope . options && scope . options . validationMessage
1852
- ) ;
1853
- } ;
1857
+ // Rebind our part of the form to the scope.
1858
+ var once = scope . $watch ( attrs . sfField , function ( form ) {
1859
+ if ( form ) {
1860
+ console . warn ( 'got form!!!!' , form )
1861
+ // Workaround for 'updateOn' error from ngModelOptions
1862
+ // see https://github.com/Textalk/angular-schema-form/issues/255
1863
+ // and https://github.com/Textalk/angular-schema-form/issues/206
1864
+ form . ngModelOptions = form . ngModelOptions || { } ;
1865
+ scope . form = form ;
1854
1866
1855
- // Rebind our part of the form to the scope.
1856
- var once = scope . $watch ( attrs . sfField , function ( form ) {
1857
- if ( form ) {
1858
- console . warn ( 'got form!!!!' , form )
1859
- // Workaround for 'updateOn' error from ngModelOptions
1860
- // see https://github.com/Textalk/angular-schema-form/issues/255
1861
- // and https://github.com/Textalk/angular-schema-form/issues/206
1862
- form . ngModelOptions = form . ngModelOptions || { } ;
1863
- scope . form = form ;
1867
+ /*
1868
+ //ok let's replace that template!
1869
+ //We do this manually since we need to bind ng-model properly and also
1870
+ //for fieldsets to recurse properly.
1871
+ var templatePromise;
1864
1872
1865
- /*
1866
- //ok let's replace that template!
1867
- //We do this manually since we need to bind ng-model properly and also
1868
- //for fieldsets to recurse properly.
1869
- var templatePromise;
1870
-
1871
- // type: "template" is a special case. It can contain a template inline or an url.
1872
- // otherwise we find out the url to the template and load them.
1873
- if (form.type === 'template' && form.template) {
1874
- templatePromise = $q.when(form.template);
1875
- } else {
1876
- var url = form.type === 'template' ? form.templateUrl : templateUrl(name, form);
1877
- templatePromise = $http.get(url, {cache: $templateCache}).then(function(res) {
1878
- return res.data;
1879
- });
1880
- }
1881
- */
1882
- /*
1883
- templatePromise.then(function(template) {
1884
- if (form.key) {
1885
- var key = form.key ?
1886
- sfPathProvider.stringify(form.key).replace(/"/g, '"') : '';
1887
- template = template.replace(
1888
- /\$\$value\$\$/g,
1889
- 'model' + (key[0] !== '[' ? '.' : '') + key
1890
- );
1873
+ // type: "template" is a special case. It can contain a template inline or an url.
1874
+ // otherwise we find out the url to the template and load them.
1875
+ if (form.type === 'template' && form.template) {
1876
+ templatePromise = $q.when(form.template);
1877
+ } else {
1878
+ var url = form.type === 'template' ? form.templateUrl : templateUrl(name, form);
1879
+ templatePromise = $http.get(url, {cache: $templateCache}).then(function(res) {
1880
+ return res.data;
1881
+ });
1891
1882
}
1892
- element.html(template);
1893
- */
1894
- // Do we have a condition? Then we slap on an ng-if on all children,
1895
- // but be nice to existing ng-if.
1896
- /*if (form.condition) {
1897
-
1898
- var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})';
1883
+ */
1884
+ /*
1885
+ templatePromise.then(function(template) {
1899
1886
if (form.key) {
1900
- evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath.stringify(form.key) + '})';
1887
+ var key = form.key ?
1888
+ sfPathProvider.stringify(form.key).replace(/"/g, '"') : '';
1889
+ template = template.replace(
1890
+ /\$\$value\$\$/g,
1891
+ 'model' + (key[0] !== '[' ? '.' : '') + key
1892
+ );
1901
1893
}
1894
+ element.html(template);
1895
+ */
1896
+ // Do we have a condition? Then we slap on an ng-if on all children,
1897
+ // but be nice to existing ng-if.
1898
+ /*if (form.condition) {
1902
1899
1903
- angular.forEach(element.children(), function(child) {
1904
- var ngIf = child.getAttribute('ng-if');
1905
- child.setAttribute(
1906
- 'ng-if',
1907
- ngIf ?
1908
- '(' + ngIf +
1909
- ') || (' + evalExpr +')'
1910
- : evalExpr
1911
- );
1912
- });
1913
- }*/
1914
- //$compile(element.contents())(scope);
1915
- //});
1916
-
1917
- // Where there is a key there is probably a ngModel
1918
- if ( form . key ) {
1919
- // It looks better with dot notation.
1920
- scope . $on (
1921
- 'schemaForm.error.' + form . key . join ( '.' ) ,
1922
- function ( event , error , validationMessage , validity ) {
1923
- if ( validationMessage === true || validationMessage === false ) {
1924
- validity = validationMessage ;
1925
- validationMessage = undefined ;
1900
+ var evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex})';
1901
+ if (form.key) {
1902
+ evalExpr = 'evalExpr(form.condition,{ model: model, "arrayIndex": arrayIndex, "modelValue": model' + sfPath.stringify(form.key) + '})';
1926
1903
}
1927
1904
1928
- if ( scope . ngModel && error ) {
1929
- if ( scope . ngModel . $setDirty ( ) ) {
1930
- scope . ngModel . $setDirty ( ) ;
1931
- } else {
1932
- // FIXME: Check that this actually works on 1.2
1933
- scope . ngModel . $dirty = true ;
1934
- scope . ngModel . $pristine = false ;
1905
+ angular.forEach(element.children(), function(child) {
1906
+ var ngIf = child.getAttribute('ng-if');
1907
+ child.setAttribute(
1908
+ 'ng-if',
1909
+ ngIf ?
1910
+ '(' + ngIf +
1911
+ ') || (' + evalExpr +')'
1912
+ : evalExpr
1913
+ );
1914
+ });
1915
+ }*/
1916
+ //$compile(element.contents())(scope);
1917
+ //});
1918
+
1919
+ // Where there is a key there is probably a ngModel
1920
+ if ( form . key ) {
1921
+ // It looks better with dot notation.
1922
+ scope . $on (
1923
+ 'schemaForm.error.' + form . key . join ( '.' ) ,
1924
+ function ( event , error , validationMessage , validity ) {
1925
+ if ( validationMessage === true || validationMessage === false ) {
1926
+ validity = validationMessage ;
1927
+ validationMessage = undefined ;
1935
1928
}
1936
1929
1937
- // Set the new validation message if one is supplied
1938
- // Does not work when validationMessage is just a string.
1939
- if ( validationMessage ) {
1940
- if ( ! form . validationMessage ) {
1941
- form . validationMessage = { } ;
1930
+ if ( scope . ngModel && error ) {
1931
+ if ( scope . ngModel . $setDirty ( ) ) {
1932
+ scope . ngModel . $setDirty ( ) ;
1933
+ } else {
1934
+ // FIXME: Check that this actually works on 1.2
1935
+ scope . ngModel . $dirty = true ;
1936
+ scope . ngModel . $pristine = false ;
1937
+ }
1938
+
1939
+ // Set the new validation message if one is supplied
1940
+ // Does not work when validationMessage is just a string.
1941
+ if ( validationMessage ) {
1942
+ if ( ! form . validationMessage ) {
1943
+ form . validationMessage = { } ;
1944
+ }
1945
+ form . validationMessage [ error ] = validationMessage ;
1942
1946
}
1943
- form . validationMessage [ error ] = validationMessage ;
1944
- }
1945
1947
1946
- scope . ngModel . $setValidity ( error , validity === true ) ;
1948
+ scope . ngModel . $setValidity ( error , validity === true ) ;
1947
1949
1948
- if ( validity === true ) {
1949
- // Setting or removing a validity can change the field to believe its valid
1950
- // but its not. So lets trigger its validation as well.
1951
- scope . $broadcast ( 'schemaFormValidate' ) ;
1950
+ if ( validity === true ) {
1951
+ // Setting or removing a validity can change the field to believe its valid
1952
+ // but its not. So lets trigger its validation as well.
1953
+ scope . $broadcast ( 'schemaFormValidate' ) ;
1954
+ }
1952
1955
}
1953
- }
1954
- } ) ;
1956
+ } ) ;
1955
1957
1956
- // Clean up the model when the corresponding form field is $destroy-ed.
1957
- // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
1958
- scope . $on ( '$destroy' , function ( ) {
1959
- // If the entire schema form is destroyed we don't touch the model
1960
- if ( ! scope . externalDestructionInProgress ) {
1961
- var destroyStrategy = form . destroyStrategy ||
1962
- ( scope . options && scope . options . destroyStrategy ) || 'remove' ;
1963
- // No key no model, and we might have strategy 'retain'
1964
- if ( form . key && destroyStrategy !== 'retain' ) {
1965
-
1966
- // Get the object that has the property we wan't to clear.
1967
- var obj = scope . model ;
1968
- if ( form . key . length > 1 ) {
1969
- obj = sfSelect ( form . key . slice ( 0 , form . key . length - 1 ) , obj ) ;
1970
- }
1958
+ // Clean up the model when the corresponding form field is $destroy-ed.
1959
+ // Default behavior can be supplied as a globalOption, and behavior can be overridden in the form definition.
1960
+ scope . $on ( '$destroy' , function ( ) {
1961
+ // If the entire schema form is destroyed we don't touch the model
1962
+ if ( ! scope . externalDestructionInProgress ) {
1963
+ var destroyStrategy = form . destroyStrategy ||
1964
+ ( scope . options && scope . options . destroyStrategy ) || 'remove' ;
1965
+ // No key no model, and we might have strategy 'retain'
1966
+ if ( form . key && destroyStrategy !== 'retain' ) {
1971
1967
1972
- // We can get undefined here if the form hasn't been filled out entirely
1973
- if ( obj === undefined ) {
1974
- return ;
1975
- }
1968
+ // Get the object that has the property we wan't to clear.
1969
+ var obj = scope . model ;
1970
+ if ( form . key . length > 1 ) {
1971
+ obj = sfSelect ( form . key . slice ( 0 , form . key . length - 1 ) , obj ) ;
1972
+ }
1973
+
1974
+ // We can get undefined here if the form hasn't been filled out entirely
1975
+ if ( obj === undefined ) {
1976
+ return ;
1977
+ }
1978
+
1979
+ // Type can also be a list in JSON Schema
1980
+ var type = ( form . schema && form . schema . type ) || '' ;
1976
1981
1977
- // Type can also be a list in JSON Schema
1978
- var type = ( form . schema && form . schema . type ) || '' ;
1979
-
1980
- // Empty means '',{} and [] for appropriate types and undefined for the rest
1981
- //console.log('destroy', destroyStrategy, form.key, type, obj);
1982
- if ( destroyStrategy === 'empty' && type . indexOf ( 'string' ) !== - 1 ) {
1983
- obj [ form . key . slice ( - 1 ) ] = '' ;
1984
- } else if ( destroyStrategy === 'empty' && type . indexOf ( 'object' ) !== - 1 ) {
1985
- obj [ form . key . slice ( - 1 ) ] = { } ;
1986
- } else if ( destroyStrategy === 'empty' && type . indexOf ( 'array' ) !== - 1 ) {
1987
- obj [ form . key . slice ( - 1 ) ] = [ ] ;
1988
- } else if ( destroyStrategy === 'null' ) {
1989
- obj [ form . key . slice ( - 1 ) ] = null ;
1990
- } else {
1991
- delete obj [ form . key . slice ( - 1 ) ] ;
1982
+ // Empty means '',{} and [] for appropriate types and undefined for the rest
1983
+ //console.log('destroy', destroyStrategy, form.key, type, obj);
1984
+ if ( destroyStrategy === 'empty' && type . indexOf ( 'string' ) !== - 1 ) {
1985
+ obj [ form . key . slice ( - 1 ) ] = '' ;
1986
+ } else if ( destroyStrategy === 'empty' && type . indexOf ( 'object' ) !== - 1 ) {
1987
+ obj [ form . key . slice ( - 1 ) ] = { } ;
1988
+ } else if ( destroyStrategy === 'empty' && type . indexOf ( 'array' ) !== - 1 ) {
1989
+ obj [ form . key . slice ( - 1 ) ] = [ ] ;
1990
+ } else if ( destroyStrategy === 'null' ) {
1991
+ obj [ form . key . slice ( - 1 ) ] = null ;
1992
+ } else {
1993
+ delete obj [ form . key . slice ( - 1 ) ] ;
1994
+ }
1992
1995
}
1993
1996
}
1994
- }
1995
- } ) ;
1996
- }
1997
+ } ) ;
1998
+ }
1997
1999
1998
- once ( ) ;
1999
- }
2000
- } ) ;
2000
+ once ( ) ;
2001
+ }
2002
+ } ) ;
2003
+ }
2001
2004
}
2002
2005
} ;
2003
2006
}
0 commit comments