aboutsummaryrefslogtreecommitdiff
path: root/js/panels/css-panel/rule-list.reel/rule-list.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/panels/css-panel/rule-list.reel/rule-list.js')
-rw-r--r--js/panels/css-panel/rule-list.reel/rule-list.js213
1 files changed, 213 insertions, 0 deletions
diff --git a/js/panels/css-panel/rule-list.reel/rule-list.js b/js/panels/css-panel/rule-list.reel/rule-list.js
new file mode 100644
index 00000000..3e18c3bf
--- /dev/null
+++ b/js/panels/css-panel/rule-list.reel/rule-list.js
@@ -0,0 +1,213 @@
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
10exports.RuleList = Montage.create(Component, {
11 focusDelegate : { value: null },
12 ruleNodeName : { value: 'li' },
13 _needsScrollToBottom: { value: null },
14
15 _hide : {
16 value: null
17 },
18 hide : {
19 get: function() {
20 return this._hide;
21 },
22 set: function(value) {
23 if(value === this._hide) { return; }
24
25 this._hide = value;
26 this.needsDraw = true;
27 }
28 },
29
30 childComponents : {
31 value: [],
32 distinct: true
33 },
34
35 rulesToDraw : {
36 value: [],
37 distinct: true
38 },
39 rulesToRemove : {
40 value: [],
41 distinct: true
42 },
43
44 update : {
45 value: function(rules) {
46 this.childComponents.forEach(function(component) {
47 component.update();
48 }, this);
49 }
50 },
51
52 _rules: { value: null },
53 rules: {
54 get: function() {
55 return this._rules;
56 },
57 set: function(list) {
58 if(!list) {
59 return null;
60 }
61
62 var foundIndices = [];
63
64 //// Iterate existing rules, update those which rule exists in new
65 //// rule list
66 this.childComponents.forEach(function(ruleComponent, i, drawnRules) {
67 //// If rule exists in new list, update it
68 var index = list.indexOf(ruleComponent.rule);
69
70 if(ruleComponent.rule.type === 'inline') {
71 //// Let's emulate finding the line rule at the first index
72 index = 0;
73 }
74
75 if(index !== -1) {
76 // found rule in our component list, or it's the inline rule
77 ruleComponent.update();
78 foundIndices.push(index);
79 } else if(!rule.applied) { /// remove rule (unless unapplied)
80 this.rulesToRemove.push(ruleComponent);
81 }
82 }, this);
83
84 //// Find rules to add
85 list.forEach(function(rule, index) {
86 //// If we haven't updated the rule already,
87 //// we're dealing with a new rule to add
88 if(foundIndices.indexOf(index) === -1) {
89 this.addRule(rule, index);
90 }
91 }, this);
92
93 this._rules = list;
94
95 this.needsDraw = true;
96
97 }
98 },
99
100 addRule: {
101 value: function(rule, atIndex, applies, drawCallback) {
102 var insertIndex = atIndex || this.childComponents.length;
103
104 this.rulesToDraw.push({
105 rule: rule,
106 index: insertIndex,
107 instance : null,
108 applies : applies,
109 callback : drawCallback
110 });
111
112 this.needsDraw = true;
113 }
114 },
115
116 removeRule: {
117 value: function(ruleComponent) {
118 this.rulesToRemove.push(ruleComponent);
119 this.needsDraw = true;
120 }
121 },
122
123 willDraw : {
124 value: function() {
125 this.rulesToDraw.forEach(function(ruleObj) {
126 var el = document.createElement(this.ruleNodeName);
127
128 var componentBase = this.supportedRules[ruleObj.rule.type],
129 instance;
130
131 ///// Draw the rule if we have a template for the rule type
132 if(!componentBase) { return false; }
133
134 instance = Montage.create(componentBase);
135 instance.element = document.createElement(this.ruleNodeName);
136 instance.rule = ruleObj.rule;
137 instance.applied = ruleObj.applies;
138
139 if(this.focusDelegate) {
140 instance.focusDelegate = this.focusDelegate;
141 }
142
143 ruleObj.instance = instance;
144
145 }, this);
146
147 }
148 },
149
150 draw : {
151 value: function() {
152 ///// If needed, scroll to bottom
153 if(this._needsScrollToBottom) {
154 ///// Make sure the appended rule item is visible (scrolled-to)
155 this.element.scrollTop = this.element.offsetHeight;
156 this._needsScrollToBottom = false;
157 }
158
159 //// Iterate through all rules needing removal
160 this.rulesToRemove.forEach(function(ruleComponent) {
161 var componentIndex = this.childComponents.indexOf(ruleComponent);
162 this.childComponents.splice(componentIndex, 1);
163 this.element.removeChild(ruleComponent.element);
164 }, this);
165
166 //// Iterate through all rules that need draw and append them
167 this.rulesToDraw.forEach(function(ruleObj) {
168 var ruleAtIndex = this.childComponents[ruleObj.index];
169
170 if(ruleAtIndex) {
171 //// Insert rule at appropriate index
172 this.element.insertBefore(ruleObj.instance.element, ruleAtIndex.element);
173 } else {
174 this.element.appendChild(ruleObj.instance.element);
175 }
176
177 this._needsScrollToBottom = this.needsDraw = true;
178 this.childComponents.push(ruleObj.instance);
179 ruleObj.instance.needsDraw = true;
180 }, this);
181
182 if(this.hide) {
183 this.element.classList.add('hidden-rule-list');
184 } else {
185 this.element.classList.remove('hidden-rule-list');
186 }
187
188 }
189 },
190
191 didDraw : {
192 value: function() {
193 this.rulesToDraw.forEach(function(ruleObj) {
194 if(typeof ruleObj.callback === 'function') {
195 ruleObj.callback(ruleObj.instance);
196 }
197 });
198
199 this.rulesToRemove.forEach(function(ruleObj) {
200 ruleObj.instance = null;
201 }, this);
202
203 ///// Null out any rules that were just drawn
204 this.rulesToDraw.length = 0;
205 this.rulesToRemove.length = 0;
206
207 if(!this.parentComponent.ruleListDrawn) {
208 this.parentComponent.ruleListDrawn = true;
209 this.parentComponent.needsDraw = true;
210 }
211 }
212 }
213});