|
@@ -5,74 +5,30 @@
|
|
|
|
|
|
|
|
import AttributeElement from '../view/attributeelement';
|
|
import AttributeElement from '../view/attributeelement';
|
|
|
import buildModelConverter from './buildmodelconverter';
|
|
import buildModelConverter from './buildmodelconverter';
|
|
|
-import buildViewConverter from './buildviewconverter';
|
|
|
|
|
|
|
+import { defineConverter, parseDefinition } from './utils';
|
|
|
|
|
|
|
|
-export function viewToModelAttribute( attributeName, attributeValue, view, dispatchers ) {
|
|
|
|
|
- const viewDefinitions = view.from ? view.from : [ view ];
|
|
|
|
|
|
|
+export function modelAttributeToView( attributeName, definition, dispatchers ) {
|
|
|
|
|
+ const { model: attributeValue, viewDefinition } = parseDefinition( definition );
|
|
|
|
|
|
|
|
- for ( const viewDefinition of viewDefinitions ) {
|
|
|
|
|
- const element = viewDefinition.name;
|
|
|
|
|
- const classes = viewDefinition.class;
|
|
|
|
|
- const styles = viewDefinition.style;
|
|
|
|
|
-
|
|
|
|
|
- const pattern = { name: element };
|
|
|
|
|
-
|
|
|
|
|
- if ( classes ) {
|
|
|
|
|
- pattern.class = classes;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if ( styles ) {
|
|
|
|
|
- pattern.style = styles;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- buildViewConverter()
|
|
|
|
|
- .for( ...dispatchers )
|
|
|
|
|
- .from( pattern )
|
|
|
|
|
- .toAttribute( () => ( {
|
|
|
|
|
- key: attributeName,
|
|
|
|
|
- value: attributeValue
|
|
|
|
|
- } ) );
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-export function modelAttributeToView( attributeName, attributeValue, view, dispatchers ) {
|
|
|
|
|
buildModelConverter()
|
|
buildModelConverter()
|
|
|
.for( ...dispatchers )
|
|
.for( ...dispatchers )
|
|
|
.fromAttribute( attributeName )
|
|
.fromAttribute( attributeName )
|
|
|
.toElement( value => {
|
|
.toElement( value => {
|
|
|
- // TODO: string vs numeric values
|
|
|
|
|
if ( value != attributeValue ) {
|
|
if ( value != attributeValue ) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- const viewDefinition = view.to ? view.to : view;
|
|
|
|
|
- // TODO: AttributeElement.fromDefinition() ?
|
|
|
|
|
-
|
|
|
|
|
- const classes = viewDefinition.class;
|
|
|
|
|
- const styles = viewDefinition.style;
|
|
|
|
|
-
|
|
|
|
|
- const attributes = {};
|
|
|
|
|
-
|
|
|
|
|
- // TODO: AttributeElement does no accept Array
|
|
|
|
|
- if ( classes ) {
|
|
|
|
|
- attributes.class = Array.isArray( classes ) ? classes.join( ' ' ) : classes;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // TODO: Attribute element does not accept Object
|
|
|
|
|
- if ( styles ) {
|
|
|
|
|
- attributes.style = typeof styles === 'string' ? styles : toStylesString( styles );
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return new AttributeElement( viewDefinition.name, attributes );
|
|
|
|
|
|
|
+ return AttributeElement.fromViewDefinition( viewDefinition );
|
|
|
} );
|
|
} );
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function toStylesString( stylesObject ) {
|
|
|
|
|
- const styles = [];
|
|
|
|
|
|
|
+export function viewToModelAttribute( attributeName, definition, dispatchers ) {
|
|
|
|
|
+ const { model: attributeValue, viewDefinitions } = parseDefinition( definition );
|
|
|
|
|
|
|
|
- for ( const key in stylesObject ) {
|
|
|
|
|
- styles.push( key + ':' + stylesObject[ key ] );
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const converter = defineConverter( dispatchers, viewDefinitions );
|
|
|
|
|
|
|
|
- return styles.join( ';' );
|
|
|
|
|
|
|
+ converter.toAttribute( () => ( {
|
|
|
|
|
+ key: attributeName,
|
|
|
|
|
+ value: attributeValue
|
|
|
|
|
+ } ) );
|
|
|
}
|
|
}
|