UNPKG

4.4 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 let _p = this._private;
44 let cy = _p.cy;
45 let eles = cy.elements();
46
47 for( let i = 0; i < this.length; i++ ){
48 this[ i ] = undefined;
49 }
50 this.length = 0;
51
52 _p.contextStyles = {};
53 _p.propDiffs = {};
54
55 this.cleanElements( eles, true );
56
57 eles.forEach(ele => {
58 let ele_p = ele[0]._private;
59
60 ele_p.styleDirty = true;
61 ele_p.appliedInitStyle = false;
62 });
63
64 return this; // chaining
65};
66
67styfn.resetToDefault = function(){
68 this.clear();
69 this.addDefaultStylesheet();
70
71 return this;
72};
73
74// builds a style object for the 'core' selector
75styfn.core = function( propName ){
76 return this._private.coreStyle[ propName ] || this.getDefaultProperty( propName );
77};
78
79// create a new context from the specified selector string and switch to that context
80styfn.selector = function( selectorStr ){
81 // 'core' is a special case and does not need a selector
82 let selector = selectorStr === 'core' ? null : new Selector( selectorStr );
83
84 let i = this.length++; // new context means new index
85 this[ i ] = {
86 selector: selector,
87 properties: [],
88 mappedProperties: [],
89 index: i
90 };
91
92 return this; // chaining
93};
94
95// add one or many css rules to the current context
96styfn.css = function(){
97 let self = this;
98 let args = arguments;
99
100 if( args.length === 1 ){
101 let map = args[0];
102
103 for( let i = 0; i < self.properties.length; i++ ){
104 let prop = self.properties[ i ];
105 let mapVal = map[ prop.name ];
106
107 if( mapVal === undefined ){
108 mapVal = map[ util.dash2camel( prop.name ) ];
109 }
110
111 if( mapVal !== undefined ){
112 this.cssRule( prop.name, mapVal );
113 }
114 }
115
116 } else if( args.length === 2 ){
117 this.cssRule( args[0], args[1] );
118 }
119
120 // do nothing if args are invalid
121
122 return this; // chaining
123};
124styfn.style = styfn.css;
125
126// add a single css rule to the current context
127styfn.cssRule = function( name, value ){
128 // name-value pair
129 let property = this.parse( name, value );
130
131 // add property to current context if valid
132 if( property ){
133 let i = this.length - 1;
134 this[ i ].properties.push( property );
135 this[ i ].properties[ property.name ] = property; // allow access by name as well
136
137 if( property.name.match( /pie-(\d+)-background-size/ ) && property.value ){
138 this._private.hasPie = true;
139 }
140
141 if( property.mapped ){
142 this[ i ].mappedProperties.push( property );
143 }
144
145 // add to core style if necessary
146 let currentSelectorIsCore = !this[ i ].selector;
147 if( currentSelectorIsCore ){
148 this._private.coreStyle[ property.name ] = property;
149 }
150 }
151
152 return this; // chaining
153};
154
155styfn.append = function( style ){
156 if( is.stylesheet( style ) ){
157 style.appendToStyle( this );
158 } else if( is.array( style ) ){
159 this.appendFromJson( style );
160 } else if( is.string( style ) ){
161 this.appendFromString( style );
162 } // you probably wouldn't want to append a Style, since you'd duplicate the default parts
163
164 return this;
165};
166
167// static function
168Style.fromJson = function( cy, json ){
169 let style = new Style( cy );
170
171 style.fromJson( json );
172
173 return style;
174};
175
176Style.fromString = function( cy, string ){
177 return new Style( cy ).fromString( string );
178};
179
180[
181 apply,
182 bypass,
183 container,
184 getForEle,
185 json,
186 stringSheet,
187 properties,
188 parse
189].forEach( function( props ){
190 util.extend( styfn, props );
191} );
192
193
194Style.types = styfn.types;
195Style.properties = styfn.properties;
196Style.propertyGroups = styfn.propertyGroups;
197Style.propertyGroupNames = styfn.propertyGroupNames;
198Style.propertyGroupKeys = styfn.propertyGroupKeys;
199
200export default Style;