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 | 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;
|
53 | };
|
54 |
|
55 | styfn.resetToDefault = function(){
|
56 | this.clear();
|
57 | this.addDefaultStylesheet();
|
58 |
|
59 | return this;
|
60 | };
|
61 |
|
62 |
|
63 | styfn.core = function( propName ){
|
64 | return this._private.coreStyle[ propName ] || this.getDefaultProperty( propName );
|
65 | };
|
66 |
|
67 |
|
68 | styfn.selector = function( selectorStr ){
|
69 |
|
70 | let selector = selectorStr === 'core' ? null : new Selector( selectorStr );
|
71 |
|
72 | let i = this.length++;
|
73 | this[ i ] = {
|
74 | selector: selector,
|
75 | properties: [],
|
76 | mappedProperties: [],
|
77 | index: i
|
78 | };
|
79 |
|
80 | return this;
|
81 | };
|
82 |
|
83 |
|
84 | styfn.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 |
|
109 |
|
110 | return this;
|
111 | };
|
112 | styfn.style = styfn.css;
|
113 |
|
114 |
|
115 | styfn.cssRule = function( name, value ){
|
116 |
|
117 | let property = this.parse( name, value );
|
118 |
|
119 |
|
120 | if( property ){
|
121 | let i = this.length - 1;
|
122 | this[ i ].properties.push( property );
|
123 | this[ i ].properties[ property.name ] = property;
|
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 |
|
134 | let currentSelectorIsCore = !this[ i ].selector;
|
135 | if( currentSelectorIsCore ){
|
136 | this._private.coreStyle[ property.name ] = property;
|
137 | }
|
138 | }
|
139 |
|
140 | return this;
|
141 | };
|
142 |
|
143 | styfn.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 | }
|
151 |
|
152 | return this;
|
153 | };
|
154 |
|
155 |
|
156 | Style.fromJson = function( cy, json ){
|
157 | let style = new Style( cy );
|
158 |
|
159 | style.fromJson( json );
|
160 |
|
161 | return style;
|
162 | };
|
163 |
|
164 | Style.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 |
|
182 | Style.types = styfn.types;
|
183 | Style.properties = styfn.properties;
|
184 | Style.propertyGroups = styfn.propertyGroups;
|
185 | Style.propertyGroupNames = styfn.propertyGroupNames;
|
186 | Style.propertyGroupKeys = styfn.propertyGroupKeys;
|
187 |
|
188 | export default Style;
|