1 | import * as is from '../is';
|
2 | import * as util from '../util';
|
3 | import Selector from '../selector';
|
4 |
|
5 | import apply from './apply';
|
6 | import bypass from './bypass';
|
7 | import container from './container';
|
8 | import getForEle from './get-for-ele';
|
9 | import json from './json';
|
10 | import stringSheet from './string-sheet';
|
11 | import properties from './properties';
|
12 | import parse from './parse';
|
13 |
|
14 | let 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 |
|
35 | let styfn = Style.prototype;
|
36 |
|
37 | styfn.instanceString = function(){
|
38 | return 'style';
|
39 | };
|
40 |
|
41 |
|
42 | styfn.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;
|
65 | };
|
66 |
|
67 | styfn.resetToDefault = function(){
|
68 | this.clear();
|
69 | this.addDefaultStylesheet();
|
70 |
|
71 | return this;
|
72 | };
|
73 |
|
74 |
|
75 | styfn.core = function( propName ){
|
76 | return this._private.coreStyle[ propName ] || this.getDefaultProperty( propName );
|
77 | };
|
78 |
|
79 |
|
80 | styfn.selector = function( selectorStr ){
|
81 |
|
82 | let selector = selectorStr === 'core' ? null : new Selector( selectorStr );
|
83 |
|
84 | let i = this.length++;
|
85 | this[ i ] = {
|
86 | selector: selector,
|
87 | properties: [],
|
88 | mappedProperties: [],
|
89 | index: i
|
90 | };
|
91 |
|
92 | return this;
|
93 | };
|
94 |
|
95 |
|
96 | styfn.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 |
|
121 |
|
122 | return this;
|
123 | };
|
124 | styfn.style = styfn.css;
|
125 |
|
126 |
|
127 | styfn.cssRule = function( name, value ){
|
128 |
|
129 | let property = this.parse( name, value );
|
130 |
|
131 |
|
132 | if( property ){
|
133 | let i = this.length - 1;
|
134 | this[ i ].properties.push( property );
|
135 | this[ i ].properties[ property.name ] = property;
|
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 |
|
146 | let currentSelectorIsCore = !this[ i ].selector;
|
147 | if( currentSelectorIsCore ){
|
148 | this._private.coreStyle[ property.name ] = property;
|
149 | }
|
150 | }
|
151 |
|
152 | return this;
|
153 | };
|
154 |
|
155 | styfn.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 | }
|
163 |
|
164 | return this;
|
165 | };
|
166 |
|
167 |
|
168 | Style.fromJson = function( cy, json ){
|
169 | let style = new Style( cy );
|
170 |
|
171 | style.fromJson( json );
|
172 |
|
173 | return style;
|
174 | };
|
175 |
|
176 | Style.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 |
|
194 | Style.types = styfn.types;
|
195 | Style.properties = styfn.properties;
|
196 | Style.propertyGroups = styfn.propertyGroups;
|
197 | Style.propertyGroupNames = styfn.propertyGroupNames;
|
198 | Style.propertyGroupKeys = styfn.propertyGroupKeys;
|
199 |
|
200 | export default Style;
|