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 | 252 |
1 files changed, 252 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..33e77297 --- /dev/null +++ b/js/panels/css-panel/style-declaration.reel/style-declaration.js | |||
@@ -0,0 +1,252 @@ | |||
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 | |||
15 | includeEmptyStyle : { | ||
16 | value: true, | ||
17 | distinct: true | ||
18 | }, | ||
19 | styles : { | ||
20 | value: [], | ||
21 | distinct: true | ||
22 | }, | ||
23 | |||
24 | _styleSortFunction : { | ||
25 | value: function(styleA, styleB) { | ||
26 | ///// If the style is an empty style (with Add button) | ||
27 | ///// push to end of declaration | ||
28 | if(styleA.isEmpty) { | ||
29 | return 1; | ||
30 | } else if (styleB.isEmpty) { | ||
31 | return -1; | ||
32 | } | ||
33 | |||
34 | ///// Alphabetic sort based on property name | ||
35 | if (styleA.name < styleB.name) { | ||
36 | return -1; | ||
37 | } else if (styleA.name > styleB.name) { | ||
38 | return 1; | ||
39 | } else { | ||
40 | return 0; | ||
41 | } | ||
42 | } | ||
43 | }, | ||
44 | _styleFilterFunction: { | ||
45 | value: function(style, styleArray) { | ||
46 | var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[style.name]; | ||
47 | |||
48 | ///// No shorthands, return true to include style | ||
49 | if(!shorthands) { return true; } | ||
50 | |||
51 | var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]], | ||
52 | stylesArray = styleArray, | ||
53 | hasAll; | ||
54 | |||
55 | debugger; | ||
56 | hasAll = subProps.every(function(subProp) { | ||
57 | debugger; | ||
58 | return this.declaration[subProp]; | ||
59 | }, this); | ||
60 | |||
61 | if(hasAll) { | ||
62 | return false; | ||
63 | } | ||
64 | } | ||
65 | }, | ||
66 | |||
67 | _declaration: { | ||
68 | value: null | ||
69 | }, | ||
70 | declaration: { | ||
71 | get: function() { | ||
72 | return this._declaration; | ||
73 | }, | ||
74 | set: function(dec) { | ||
75 | var stylesArray; | ||
76 | |||
77 | if(this._declaration) { | ||
78 | this.styles = null; | ||
79 | this.styles = []; | ||
80 | } | ||
81 | |||
82 | ///// Take snapshot of declaration | ||
83 | this.cssText = dec.cssText; | ||
84 | |||
85 | stylesArray = Array.prototype.slice.call(dec); | ||
86 | |||
87 | if(this.includeEmptyStyle) { | ||
88 | this.styles.push({ | ||
89 | "name": "property", | ||
90 | "value" : "value", | ||
91 | "isEmpty": true | ||
92 | }); | ||
93 | } | ||
94 | |||
95 | stylesArray.forEach(function(prop, index) { | ||
96 | this.styles.push({ | ||
97 | name: prop, | ||
98 | value: dec.getPropertyValue(prop) | ||
99 | }); | ||
100 | }, this); | ||
101 | |||
102 | this._declaration = dec; | ||
103 | this.needsDraw = true; | ||
104 | } | ||
105 | }, | ||
106 | |||
107 | styleShorthander : { | ||
108 | value: function(styles) { | ||
109 | var shorthandsToAdd = [], | ||
110 | subProps, hasAll; | ||
111 | |||
112 | styles.forEach(function(property, index, styleArray) { | ||
113 | var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[property]; | ||
114 | |||
115 | if(!shorthands) { return false; } | ||
116 | |||
117 | var subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]], | ||
118 | stylesArray = styleArray; | ||
119 | |||
120 | hasAll = subProps.every(function(subProp) { | ||
121 | return stylesArray.indexOf(subProp) !== -1; | ||
122 | }); | ||
123 | |||
124 | if(hasAll) { | ||
125 | subProps.forEach(function(subProp) { | ||
126 | stylesArray.splice(stylesArray.indexOf(subProp), 1); | ||
127 | }, this); | ||
128 | shorthandsToAdd.push(shorthands[0]); | ||
129 | } | ||
130 | }, this); | ||
131 | |||
132 | return styles.concat(shorthandsToAdd); | ||
133 | } | ||
134 | }, | ||
135 | |||
136 | _getStyleToIndexMap : { | ||
137 | value: function() { | ||
138 | var map = {}; | ||
139 | |||
140 | for(var i = 0; i<this.styles.length; i++) { | ||
141 | map[this.styles[i].name] = i; | ||
142 | } | ||
143 | |||
144 | return map; | ||
145 | } | ||
146 | }, | ||
147 | |||
148 | update : { | ||
149 | value: function() { | ||
150 | if(this.declaration.cssText !== this.cssText) { | ||
151 | var usedIndices = [], | ||
152 | styleToIndexMap = this._getStyleToIndexMap(); | ||
153 | |||
154 | Array.prototype.slice.call(this.declaration).forEach(function(prop, index) { | ||
155 | var i = styleToIndexMap[prop]; | ||
156 | |||
157 | ///// Style component exists for property | ||
158 | ///// Update its value | ||
159 | if(i) { | ||
160 | this.styles[i].value = this.declaration.getPropertyValue(prop); | ||
161 | usedIndices.push(i); | ||
162 | } | ||
163 | }, this); | ||
164 | |||
165 | ///// Keep copy of cssText to know when we need to | ||
166 | ///// update the view | ||
167 | this.cssText = this.declaration.cssText; | ||
168 | this.needsDraw = true; | ||
169 | } | ||
170 | } | ||
171 | }, | ||
172 | |||
173 | styleTree : { | ||
174 | value: { | ||
175 | "properties" : [] | ||
176 | }, | ||
177 | distinct: true | ||
178 | }, | ||
179 | |||
180 | addNewStyle : { | ||
181 | value: function() { | ||
182 | this.styles.push({ | ||
183 | "name": "property", | ||
184 | "value" : "value", | ||
185 | "isEmpty": true | ||
186 | }); | ||
187 | } | ||
188 | }, | ||
189 | |||
190 | /* drag/drop events */ | ||
191 | handleDrop : { | ||
192 | value: function(e) { | ||
193 | console.log('dropped'); | ||
194 | } | ||
195 | }, | ||
196 | handleDragenter : { | ||
197 | value: function(e) { | ||
198 | console.log("dec - drag enter"); | ||
199 | this.element.classList.add("drag-over"); | ||
200 | } | ||
201 | }, | ||
202 | handleDragleave : { | ||
203 | value: function(e) { | ||
204 | if(this.element === e._event.toElement || this._containsElement(e._event.toElement)) { | ||
205 | //// Dragged-over element is inside of component element | ||
206 | //// I.e. it's not really a "drag leave" | ||
207 | e.stopPropagation(); | ||
208 | e.preventDefault(); | ||
209 | return false; | ||
210 | } | ||
211 | |||
212 | console.log("DECLARATION - ELEMENT NOT IN DEC", e._event.toElement); |