|
import * as is from './is'; |
|
import Style from './style'; |
|
import { dash2camel } from './util'; |
|
|
|
|
|
|
|
let Stylesheet = function(){ |
|
if( !(this instanceof Stylesheet) ){ |
|
return new Stylesheet(); |
|
} |
|
|
|
this.length = 0; |
|
}; |
|
|
|
let sheetfn = Stylesheet.prototype; |
|
|
|
sheetfn.instanceString = function(){ |
|
return 'stylesheet'; |
|
}; |
|
|
|
|
|
sheetfn.selector = function( selector ){ |
|
let i = this.length++; |
|
|
|
this[ i ] = { |
|
selector: selector, |
|
properties: [] |
|
}; |
|
|
|
return this; |
|
}; |
|
|
|
|
|
sheetfn.css = function( name, value ){ |
|
let i = this.length - 1; |
|
|
|
if( is.string( name ) ){ |
|
this[ i ].properties.push( { |
|
name: name, |
|
value: value |
|
} ); |
|
} else if( is.plainObject( name ) ){ |
|
let map = name; |
|
let propNames = Object.keys( map ); |
|
|
|
for( let j = 0; j < propNames.length; j++ ){ |
|
let key = propNames[ j ]; |
|
let mapVal = map[ key ]; |
|
|
|
if( mapVal == null ){ continue; } |
|
|
|
let prop = Style.properties[key] || Style.properties[dash2camel(key)]; |
|
|
|
if( prop == null ){ continue; } |
|
|
|
let name = prop.name; |
|
let value = mapVal; |
|
|
|
this[ i ].properties.push( { |
|
name: name, |
|
value: value |
|
} ); |
|
} |
|
} |
|
|
|
return this; |
|
}; |
|
|
|
sheetfn.style = sheetfn.css; |
|
|
|
|
|
sheetfn.generateStyle = function( cy ){ |
|
let style = new Style( cy ); |
|
|
|
return this.appendToStyle( style ); |
|
}; |
|
|
|
|
|
sheetfn.appendToStyle = function( style ){ |
|
for( let i = 0; i < this.length; i++ ){ |
|
let context = this[ i ]; |
|
let selector = context.selector; |
|
let props = context.properties; |
|
|
|
style.selector( selector ); |
|
|
|
for( let j = 0; j < props.length; j++ ){ |
|
let prop = props[ j ]; |
|
|
|
style.css( prop.name, prop.value ); |
|
} |
|
} |
|
|
|
return style; |
|
}; |
|
|
|
export default Stylesheet; |
|
|