|
import * as is from '../is'; |
|
import * as util from '../util'; |
|
import Selector from '../selector'; |
|
|
|
import apply from './apply'; |
|
import bypass from './bypass'; |
|
import container from './container'; |
|
import getForEle from './get-for-ele'; |
|
import json from './json'; |
|
import stringSheet from './string-sheet'; |
|
import properties from './properties'; |
|
import parse from './parse'; |
|
|
|
let Style = function( cy ){ |
|
|
|
if( !(this instanceof Style) ){ |
|
return new Style( cy ); |
|
} |
|
|
|
if( !is.core( cy ) ){ |
|
util.error( 'A style must have a core reference' ); |
|
return; |
|
} |
|
|
|
this._private = { |
|
cy: cy, |
|
coreStyle: {} |
|
}; |
|
|
|
this.length = 0; |
|
|
|
this.resetToDefault(); |
|
}; |
|
|
|
let styfn = Style.prototype; |
|
|
|
styfn.instanceString = function(){ |
|
return 'style'; |
|
}; |
|
|
|
|
|
styfn.clear = function(){ |
|
let _p = this._private; |
|
let cy = _p.cy; |
|
let eles = cy.elements(); |
|
|
|
for( let i = 0; i < this.length; i++ ){ |
|
this[ i ] = undefined; |
|
} |
|
this.length = 0; |
|
|
|
_p.contextStyles = {}; |
|
_p.propDiffs = {}; |
|
|
|
this.cleanElements( eles, true ); |
|
|
|
eles.forEach(ele => { |
|
let ele_p = ele[0]._private; |
|
|
|
ele_p.styleDirty = true; |
|
ele_p.appliedInitStyle = false; |
|
}); |
|
|
|
return this; |
|
}; |
|
|
|
styfn.resetToDefault = function(){ |
|
this.clear(); |
|
this.addDefaultStylesheet(); |
|
|
|
return this; |
|
}; |
|
|
|
|
|
styfn.core = function( propName ){ |
|
return this._private.coreStyle[ propName ] || this.getDefaultProperty( propName ); |
|
}; |
|
|
|
|
|
styfn.selector = function( selectorStr ){ |
|
|
|
let selector = selectorStr === 'core' ? null : new Selector( selectorStr ); |
|
|
|
let i = this.length++; |
|
this[ i ] = { |
|
selector: selector, |
|
properties: [], |
|
mappedProperties: [], |
|
index: i |
|
}; |
|
|
|
return this; |
|
}; |
|
|
|
|
|
styfn.css = function(){ |
|
let self = this; |
|
let args = arguments; |
|
|
|
if( args.length === 1 ){ |
|
let map = args[0]; |
|
|
|
for( let i = 0; i < self.properties.length; i++ ){ |
|
let prop = self.properties[ i ]; |
|
let mapVal = map[ prop.name ]; |
|
|
|
if( mapVal === undefined ){ |
|
mapVal = map[ util.dash2camel( prop.name ) ]; |
|
} |
|
|
|
if( mapVal !== undefined ){ |
|
this.cssRule( prop.name, mapVal ); |
|
} |
|
} |
|
|
|
} else if( args.length === 2 ){ |
|
this.cssRule( args[0], args[1] ); |
|
} |
|
|
|
|
|
|
|
return this; |
|
}; |
|
styfn.style = styfn.css; |
|
|
|
|
|
styfn.cssRule = function( name, value ){ |
|
|
|
let property = this.parse( name, value ); |
|
|
|
|
|
if( property ){ |
|
let i = this.length - 1; |
|
this[ i ].properties.push( property ); |
|
this[ i ].properties[ property.name ] = property; |
|
|
|
if( property.name.match( /pie-(\d+)-background-size/ ) && property.value ){ |
|
this._private.hasPie = true; |
|
} |
|
|
|
if( property.mapped ){ |
|
this[ i ].mappedProperties.push( property ); |
|
} |
|
|
|
|
|
let currentSelectorIsCore = !this[ i ].selector; |
|
if( currentSelectorIsCore ){ |
|
this._private.coreStyle[ property.name ] = property; |
|
} |
|
} |
|
|
|
return this; |
|
}; |
|
|
|
styfn.append = function( style ){ |
|
if( is.stylesheet( style ) ){ |
|
style.appendToStyle( this ); |
|
} else if( is.array( style ) ){ |
|
this.appendFromJson( style ); |
|
} else if( is.string( style ) ){ |
|
this.appendFromString( style ); |
|
} |
|
|
|
return this; |
|
}; |
|
|
|
|
|
Style.fromJson = function( cy, json ){ |
|
let style = new Style( cy ); |
|
|
|
style.fromJson( json ); |
|
|
|
return style; |
|
}; |
|
|
|
Style.fromString = function( cy, string ){ |
|
return new Style( cy ).fromString( string ); |
|
}; |
|
|
|
[ |
|
apply, |
|
bypass, |
|
container, |
|
getForEle, |
|
json, |
|
stringSheet, |
|
properties, |
|
parse |
|
].forEach( function( props ){ |
|
util.extend( styfn, props ); |
|
} ); |
|
|
|
|
|
Style.types = styfn.types; |
|
Style.properties = styfn.properties; |
|
Style.propertyGroups = styfn.propertyGroups; |
|
Style.propertyGroupNames = styfn.propertyGroupNames; |
|
Style.propertyGroupKeys = styfn.propertyGroupKeys; |
|
|
|
export default Style; |
|
|