aboutsummaryrefslogtreecommitdiff
path: root/js/panels/css-panel/declaration.reel/declaration.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/panels/css-panel/declaration.reel/declaration.js')
-rw-r--r--js/panels/css-panel/declaration.reel/declaration.js218
1 files changed, 218 insertions, 0 deletions
diff --git a/js/panels/css-panel/declaration.reel/declaration.js b/js/panels/css-panel/declaration.reel/declaration.js
new file mode 100644
index 00000000..8ab19ad6
--- /dev/null
+++ b/js/panels/css-panel/declaration.reel/declaration.js
@@ -0,0 +1,218 @@
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.Declaration = Montage.create(Component, {
12 cssText : {
13 value: null
14 },
15 focusDelegate : {
16 value: null
17 },
18 includeEmptyStyle : {
19 value: true
20 },
21 templateDidLoad : {
22 value: function() {
23 console.log("declaration - template did load");
24
25 if(this.focusDelegate) {
26 this.treeController.delegate = this.focusDelegate;
27 }
28 }
29 },
30 prepareForDraw : {
31 value: function(e) {
32 console.log("Declaration :: prepare for draw");
33 this._element.addEventListener('drop', this, false);
34 this.element.addEventListener('dragenter', this, false);
35 this.element.addEventListener('dragleave', this, false);
36 }
37 },
38 _declaration: {
39 value: null
40 },
41 declaration: {
42 get: function() {
43 return this._declaration;
44 },
45 set: function(dec) {
46 this._declaration = dec;
47
48 this.cssText = dec.cssText;
49
50 ///// creates data structure to use with tree component
51 this.buildStyleTree();
52
53 if(this.includeEmptyStyle) {
54 this.styleTree.properties.push({
55 "name": "property",
56 "value" : "value",
57 "isEmpty": true
58 });
59 }
60
61 this.needsDraw = true;
62 }
63 },
64
65 update : {
66 value: function() {
67 if(this.declaration.cssText !== this.cssText) {
68 ///// Needs update
69 this.treeController.branchControllers[0].content.forEach(function(obj) {
70 this.treeController.branchControllers[0].removeObjects(obj);
71 }, this );
72
73 this.buildStyleTree();
74
75 if(this.includeEmptyStyle) {
76 this.styleTree.properties.push({
77 "name": "property",
78 "value" : "value",
79 "isEmpty": true
80 });
81 }
82//debugger;
83 this.needsDraw = true;
84 }
85 }
86 },
87
88 buildStyleTree : {
89 value: function() {
90 var styles = Array.prototype.slice.call(this._declaration).sort();
91 this.styleTree = {
92 properties : styles.map(this.styleTreeMapper, this)
93 };
94 }
95 },
96 styleTreeMapper : {
97 value: function arrayToTreeMapper(property, i, styleArray) {
98 var shorthands = ShorthandProps.CSS_SHORTHAND_MAP[property],
99 subProps, hasAll;
100
101 ///// Is this a sub property of a shorthand property?
102 if(shorthands) {
103 //debugger;
104 ///// Yes.
105 ///// Now, are all sub properties in the declaration?
106 subProps = ShorthandProps.CSS_SHORTHAND_TO_SUBPROP_MAP[shorthands[0]];
107 hasAll = subProps.every(function(subProp) {
108 return styleArray.indexOf(subProp) !== -1;
109 });
110
111 if(hasAll) {
112 ///// It has all sub properties
113 ///// Let's return a tree branch and remove the
114 ///// sub properties from the flat array
115
116 this._removeItemsFromArray(styleArray, subProps);
117
118 return {
119 name: shorthands[0],
120 value: this._declaration.getPropertyValue(shorthands[0]),
121 properties: subProps.map(function(p, i, array) {
122 return {
123 name: p,
124 value: this._declaration.getPropertyValue(p)
125 };
126 }, this)
127 };
128 }
129 }
130
131
132 return {
133 name: property,
134 value: this._declaration.getPropertyValue(property)
135 };
136 }
137 },
138 _removeItemsFromArray : {
139 value: function(array, items) {
140 items.forEach(function(item) {
141 var index = array.indexOf(item);
142 array.splice(index, 1);
143 }, this);
144 }
145 },
146 styleTree : {
147 value: {
148 "properties" : []
149 },
150 distinct: true
151 },
152
153 addNewStyleAfter : {
154 value: function(style) {
155 style.parentComponent.parentComponent.contentController.addObjects({
156 name: 'property',
157 value: 'value',
158 isEmpty: true,
159 treeNodeType: 'leaf'
160 });
161 style.parentComponent.parentComponent.needsDraw = true;
162 }
163 },
164
165 /* drag/drop events */
166 handleDrop : {
167 value: function(e) {
168 console.log('dropped');
169 }
170 },
171 handleDragenter : {
172 value: function(e) {
173 console.log("dec - drag enter");
174 this.element.classList.add("drag-over");
175 }
176 },
177 handleDragleave : {
178 value: function(e) {
179 if(this.element === e._event.toElement || this._containsElement(e._event.toElement)) {
180 //// Dragged-over element is inside of component element
181 //// I.e. it's not really a "drag leave"
182 e.stopPropagation();
183 e.preventDefault();
184 return false;
185 }
186
187 console.log("DECLARATION - ELEMENT NOT IN DEC", e._event.toElement);
188
189 //console.log("dec - drag leave");
190 this.element.classList.remove("drag-over");
191 }
192 },
193
194 draw: {
195 value: function() {
196 if(this._declaration) {
197
198 }
199 }
200 },
201
202 _containsElement : {
203 value: function(innerElement) {
204 var isInComponent = false,
205 parent = innerElement.parentNode;
206
207 while (parent !== document) {
208 if(parent === this.element) {
209 isInComponent = true;
210 break;
211 }
212 parent = parent.parentNode;
213 }
214
215 return isInComponent;
216 }
217 }
218}); \ No newline at end of file