diff options
Diffstat (limited to 'js/panels/css-panel/style-declaration.reel/style-declaration.js')
-rw-r--r-- | js/panels/css-panel/style-declaration.reel/style-declaration.js | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/js/panels/css-panel/style-declaration.reel/style-declaration.js b/js/panels/css-panel/style-declaration.reel/style-declaration.js new file mode 100644 index 00000000..61a65099 --- /dev/null +++ b/js/panels/css-panel/style-declaration.reel/style-declaration.js | |||
@@ -0,0 +1,296 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | var Montage = require("montage/core/core").Montage, | ||
8 | Component = require("montage/ui/component").Component, | ||
9 | ShorthandProps = require("js/panels/CSSPanel/css-shorthand-map"); | ||
10 | |||
11 | exports.StyleDeclaration = Montage.create(Component, { | ||
12 | cssText : { value: null }, | ||
13 | focusDelegate : { value: null }, | ||
14 | needsSort : { value: null }, | ||
15 | |||
16 | includeEmptyStyle : { | ||
17 | value: true, | ||
18 | distinct: true | ||
19 | }, | ||
20 | styles : { | ||
21 | value: [], | ||
22 | distinct: true | ||
23 | }, | ||
24 | |||
25 | _styleSortFunction : { | ||
26 | value: function(styleA, styleB) { | ||
27 | ///// If the style is an empty style (with Add button) | ||
28 | ///// push to end of declaration | ||
29 | if(styleA.isEmpty) { | ||
30 | return 1; | ||
31 | } else if (styleB.isEmpty) { | ||
32 | return -1; | ||
33 | } | ||
34 | |||
35 | ///// Alphabetic sort based on property name | ||
36 | if (styleA.name < styleB.name) { | ||
37 | return -1; | ||
38 | } else if (styleA.name > styleB.name) { | ||
39 | return 1; | ||
40 | } else { | ||
41 | return 0; | ||
42 | } | ||
43 | } | ||
44 | }, | ||
45 | _styleFilterFunction: { | ||
46 | value: function(style, styleArray) { | ||
47 | var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[style.name]; | ||
48 | |||
49 | ///// No shorthands, return true to include style | ||
50 | if(!shorthands) { return true; } | ||
51 | |||
52 | var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]], | ||
53 | stylesArray = styleArray, | ||
54 | hasAll; | ||
55 | |||
56 | debugger; | ||
57 | hasAll = subProps.every(function(subProp) { | ||
58 | debugger; | ||
59 | return this.declaration[subProp]; | ||
60 | }, this); | ||
61 | |||
62 | if(hasAll) { | ||
63 | return false; | ||
64 | } | ||
65 | } | ||
66 | }, | ||
67 | |||
68 | _declaration: { | ||
69 | value: null | ||
70 | }, | ||
71 | declaration: { | ||
72 | get: function() { | ||
73 | return this._declaration; | ||
74 | }, | ||
75 | set: function(dec) { | ||
76 | var stylesArray; | ||
77 | |||
78 | if(this._declaration) { | ||
79 | this.styles = null; | ||
80 | this.styles = []; | ||
81 | } | ||
82 | |||
83 | ///// Take snapshot of declaration | ||
84 | this.cssText = dec.cssText; | ||
85 | |||
86 | stylesArray = Array.prototype.slice.call(dec); | ||
87 | |||
88 | if(this.includeEmptyStyle) { | ||
89 | this.styles.push({ | ||
90 | name : "property", | ||
91 | value : "value", | ||
92 | isEmpty : true | ||
93 | }); | ||
94 | } | ||
95 | |||
96 | stylesArray.forEach(function(prop, index) { | ||
97 | this.styles.push({ | ||
98 | name: prop, | ||
99 | value: dec.getPropertyValue(prop) | ||
100 | }); | ||
101 | }, this); | ||
102 | |||
103 | this._declaration = dec; | ||
104 | this.needsDraw = this.needsSort = true; | ||
105 | } | ||
106 | }, | ||
107 | |||
108 | styleShorthander : { | ||
109 | value: function(styles) { | ||
110 | var shorthandsToAdd = [], | ||
111 | subProps, hasAll; | ||
112 | |||
113 | styles.forEach(function(property, index, styleArray) { | ||
114 | var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[property]; | ||
115 | |||
116 | if(!shorthands) { return false; } | ||
117 | |||
118 | var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]], | ||
119 | stylesArray = styleArray; | ||
120 | |||
121 | hasAll = subProps.every(function(subProp) { | ||
122 | return stylesArray.indexOf(subProp) !== -1; | ||
123 | }); | ||
124 | |||
125 | if(hasAll) { | ||
126 | subProps.forEach(function(subProp) { | ||
127 | stylesArray.splice(stylesArray.indexOf(subProp), 1); | ||
128 | }, this); | ||
129 | shorthandsToAdd.push(shorthands[0]); | ||
130 | } | ||
131 | }, this); | ||
132 | |||
133 | return styles.concat(shorthandsToAdd); | ||
134 | } | ||
135 | }, | ||
136 | |||
137 | _getStyleToIndexMap : { | ||
138 | value: function() { | ||
139 | var map = {}; | ||
140 | |||
141 | for(var i = 0; i<this.styles.length; i++) { | ||
142 | map[this.styles[i].name] = i; | ||
143 | } | ||
144 | |||
145 | return map; | ||
146 | } | ||
147 | }, | ||
148 | |||
149 | update : { | ||
150 | value: function() { | ||
151 | if(this.declaration.cssText !== this.cssText) { | ||
152 | var usedIndices = [], | ||
153 | styleToIndexMap = this._getStyleToIndexMap(); | ||
154 | |||
155 | Array.prototype.slice.call(this.declaration).forEach(function(prop, index) { | ||
156 | var i = styleToIndexMap[prop]; | ||
157 | |||
158 | ///// Style component exists for property | ||
159 | ///// Update its value | ||
160 | if(i) { | ||
161 | this.styles[i].value = this.declaration.getPropertyValue(prop); | ||
162 | usedIndices.push(i); | ||
163 | } else { | ||
164 | //// styles doesn't exist, does shorthand? | ||
165 | var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[prop], | ||
166 | shorthandUpdated = false; | ||
167 | |||
168 | if(shorthands) { | ||
169 | shorthands.forEach(function(shorthand) { | ||
170 | var shorthandIndex = styleToIndexMap[shorthand]; | ||
171 | if(shorthandIndex) { | ||
172 | //// if shorthand exists in list of rendered styles | ||
173 | //// update it | ||
174 | this.styles[shorthandIndex].value = this.declaration.getPropertyValue(shorthand); | ||
175 | shorthandUpdated = true; | ||
176 | } | ||
177 | }, this); | ||
178 | } | ||
179 | |||
180 | if(!shorthandUpdated) { | ||
181 | this.addStyle(prop, this.declaration.getPropertyValue(prop)); | ||
182 | } | ||
183 | } | ||
184 | }, this); | ||
185 | |||
186 | ///// Keep copy of cssText to know when we need to | ||
187 | ///// update the view | ||
188 | this.cssText = this.declaration.cssText; | ||
189 | this.needsDraw = true; | ||
190 | } | ||
191 | } | ||
192 | }, | ||
193 | |||
194 | styleTree : { | ||
195 | value: { | ||
196 | "properties" : [] | ||
197 | }, | ||
198 | distinct: true | ||
199 | }, | ||
200 | |||
201 | addNewStyle : { | ||
202 | value: function() { | ||
203 | this.addStyle('property', 'value', { | ||
204 | isEmpty : true | ||
205 | }); | ||
206 | } | ||
207 | }, | ||
208 | addStyle : { | ||
209 | value: function(property, value, data) { | ||
210 | var styleDescriptor = { | ||