aboutsummaryrefslogtreecommitdiff
path: root/js/panels/css-panel/style-declaration.reel/style-declaration.js
diff options
context:
space:
mode:
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.js321
1 files changed, 321 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..8e364d0d
--- /dev/null
+++ b/js/panels/css-panel/style-declaration.reel/style-declaration.js
@@ -0,0 +1,321 @@
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
7var Montage = require("montage/core/core").Montage,
8 Component = require("montage/ui/component").Component,
9 ShorthandProps = require("js/panels/CSSPanel/css-shorthand-map");
10
11exports.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 stylesArray.forEach(function(prop, index) {
89 this.styles.push({
90 name: prop,
91 value: dec.getPropertyValue(prop),
92 isEmpty: false
93 });
94 }, this);
95
96 if(this.includeEmptyStyle) {
97 this.styles.push({
98 name : "property",
99 value : "value",
100 isEmpty : true
101 });
102 }
103
104 this._declaration = dec;
105 this.needsDraw = this.needsSort = true;
106 }
107 },
108
109 styleShorthander : {
110 value: function(styles) {
111 var shorthandsToAdd = [],
112 subProps, hasAll;
113
114 styles.forEach(function(property, index, styleArray) {
115 var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[property];
116
117 if(!shorthands) { return false; }
118
119 var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]],
120 stylesArray = styleArray;
121
122 hasAll = subProps.every(function(subProp) {
123 return stylesArray.indexOf(subProp) !== -1;
124 });
125
126 if(hasAll) {
127 subProps.forEach(function(subProp) {
128 stylesArray.splice(stylesArray.indexOf(subProp), 1);
129 }, this);
130 shorthandsToAdd.push(shorthands[0]);
131 }
132 }, this);
133
134 return styles.concat(shorthandsToAdd);
135 }
136 },
137
138 _getStyleToIndexMap : {
139 value: function() {
140 var map = {};
141
142 for(var i = 0; i<this.styles.length; i++) {
143 map[this.styles[i].name] = i;
144 }
145
146 return map;
147 }
148 },
149
150 update : {
151 value: function() {
152 if(this.declaration.cssText !== this.cssText) {
153 var usedIndices = [],
154 styleToIndexMap = this._getStyleToIndexMap();
155
156 Array.prototype.slice.call(this.declaration).forEach(function(prop, index) {
157 var styleObjectIndex = styleToIndexMap[prop];
158
159 ///// Style component exists for property
160 ///// Update its value
161 if(styleObjectIndex !== undefined) {
162 this.styles[styleObjectIndex].value = this.declaration.getPropertyValue(prop);
163 usedIndices.push(styleObjectIndex);
164 } else {
165 //// styles doesn't exist, does shorthand?
166 var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[prop],
167 shorthandUpdated = false;
168
169 if(shorthands) {
170 shorthands.forEach(function(shorthand) {
171 var shorthandIndex = styleToIndexMap[shorthand];
172 if(shorthandIndex) {
173 //// if shorthand exists in list of rendered styles
174 //// update it
175 this.styles[shorthandIndex].value = this.declaration.getPropertyValue(shorthand);
176 shorthandUpdated = true;
177 }
178 }, this);
179 }
180
181 if(!shorthandUpdated) {
182 //// push to usedIndices so we don't remove styles we just added
183 usedIndices.push(this.styles.length);
184 this.addStyle(prop, this.declaration.getPropertyValue(prop));
185 }
186 }
187 }, this);
188
189 for(var i = this.styles.length-1; i>=0; i--) {
190 if(usedIndices.indexOf(i) === -1) {
191 if(!this.styles[i].isEmpty) {
192 ///// index not used, remove style
193 this.removeStyle(this.styles[i]);
194 }
195 }
196 }
197
198 ///// Keep copy of cssText to know when we need to
199 ///// update the view
200 this.cssText = this.declaration.cssText;
201 this.needsDraw = this.needsSort = true;
202 }
203 }
204 },
205
206 styleTree : {
207 value: {
208 "properties" : []
209 },
210