UNPKG

4.16 kBJavaScriptView Raw
1import * as is from '../is';
2import * as util from '../util';
3import Selector from '../selector';
4
5import apply from './apply';
6import bypass from './bypass';
7import container from './container';
8import getForEle from './get-for-ele';
9import json from './json';
10import stringSheet from './string-sheet';
11import properties from './properties';
12import parse from './parse';
13
14let Style = function( cy ){
15
16 if( !(this instanceof Style) ){
17 return new Style( cy );
18 }
19
20 if( !is.core( cy ) ){
21 util.error( 'A style must have a core reference' );
22 return;
23 }
24
25 this._private = {
26 cy: cy,
27 coreStyle: {}
28 };
29
30 this.length = 0;
31
32 this.resetToDefault();
33};
34
35let styfn = Style.prototype;
36
37styfn.instanceString = function(){
38 return 'style';
39};
40
41// remove all contexts
42styfn.clear = function(){
43 for( let i = 0; i < this.length; i++ ){
44 this[ i ] = undefined;
45 }
46 this.length = 0;
47
48 let _p = this._private;
49
50 _p.newStyle = true;
51
52 return this; // chaining
53};
54
55styfn.resetToDefault = function(){
56 this.clear();
57 this.addDefaultStylesheet();
58
59 return this;
60};
61
62// builds a style object for the 'core' selector
63styfn.core = function( propName ){
64 return this._private.coreStyle[ propName ] || this.getDefaultProperty( propName );
65};
66
67// create a new context from the specified selector string and switch to that context
68styfn.selector = function( selectorStr ){
69 // 'core' is a special case and does not need a selector
70 let selector = selectorStr === 'core' ? null : new Selector( selectorStr );
71
72 let i = this.length++; // new context means new index
73 this[ i ] = {
74 selector: selector,
75 properties: [],
76 mappedProperties: [],
77 index: i
78 };
79
80 return this; // chaining
81};
82
83// add one or many css rules to the current context
84styfn.css = function(){
85 let self = this;
86 let args = arguments;
87
88 if( args.length === 1 ){
89 let map = args[0];
90
91 for( let i = 0; i < self.properties.length; i++ ){
92 let prop = self.properties[ i ];
93 let mapVal = map[ prop.name ];
94
95 if( mapVal === undefined ){
96 mapVal = map[ util.dash2camel( prop.name ) ];
97 }
98
99 if( mapVal !== undefined ){
100 this.cssRule( prop.name, mapVal );
101 }
102 }
103
104 } else if( args.length === 2 ){
105 this.cssRule( args[0], args[1] );
106 }
107
108 // do nothing if args are invalid
109
110 return this; // chaining
111};
112styfn.style = styfn.css;
113
114// add a single css rule to the current context
115styfn.cssRule = function( name, value ){
116 // name-value pair
117 let property = this.parse( name, value );
118
119 // add property to current context if valid
120 if( property ){
121 let i = this.length - 1;
122 this[ i ].properties.push( property );
123 this[ i ].properties[ property.name ] = property; // allow access by name as well
124
125 if( property.name.match( /pie-(\d+)-background-size/ ) && property.value ){
126 this._private.hasPie = true;
127 }
128
129 if( property.mapped ){
130 this[ i ].mappedProperties.push( property );
131 }
132
133 // add to core style if necessary
134 let currentSelectorIsCore = !this[ i ].selector;
135 if( currentSelectorIsCore ){
136 this._private.coreStyle[ property.name ] = property;
137 }
138 }
139
140 return this; // chaining
141};
142
143styfn.append = function( style ){
144 if( is.stylesheet( style ) ){
145 style.appendToStyle( this );
146 } else if( is.array( style ) ){
147 this.appendFromJson( style );
148 } else if( is.string( style ) ){
149 this.appendFromString( style );
150 } // you probably wouldn't want to append a Style, since you'd duplicate the default parts
151
152 return this;
153};
154
155// static function
156Style.fromJson = function( cy, json ){
157 let style = new Style( cy );
158
159 style.fromJson( json );
160
161 return style;
162};
163
164Style.fromString = function( cy, string ){
165 return new Style( cy ).fromString( string );
166};
167
168[
169 apply,
170 bypass,
171 container,
172 getForEle,
173 json,
174 stringSheet,
175 properties,
176 parse
177].forEach( function( props ){
178 util.extend( styfn, props );
179} );
180
181
182Style.types = styfn.types;
183Style.properties = styfn.properties;
184Style.propertyGroups = styfn.propertyGroups;
185Style.propertyGroupNames = styfn.propertyGroupNames;
186Style.propertyGroupKeys = styfn.propertyGroupKeys;
187
188export default Style;