Lines 42-54
Link Here
|
42 |
package org.netbeans.spi.knockout; |
42 |
package org.netbeans.spi.knockout; |
43 |
|
43 |
|
44 |
import java.util.ArrayList; |
44 |
import java.util.ArrayList; |
|
|
45 |
import java.util.Collection; |
46 |
import java.util.Collections; |
47 |
import java.util.HashMap; |
45 |
import java.util.HashSet; |
48 |
import java.util.HashSet; |
46 |
import java.util.LinkedHashSet; |
49 |
import java.util.LinkedHashSet; |
47 |
import java.util.List; |
50 |
import java.util.List; |
|
|
51 |
import java.util.Map; |
48 |
import java.util.Set; |
52 |
import java.util.Set; |
49 |
import org.netbeans.spi.knockout.BindingsProvider.Response; |
53 |
import org.netbeans.spi.knockout.BindingsProvider.Response; |
50 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileObject; |
|
|
55 |
import org.openide.util.BaseUtilities; |
51 |
import org.openide.util.Lookup; |
56 |
import org.openide.util.Lookup; |
|
|
57 |
import org.openide.util.TopologicalSortException; |
52 |
|
58 |
|
53 |
/** |
59 |
/** |
54 |
* Allows structural description of a <code>ko.applyBindings</code> parameter. |
60 |
* Allows structural description of a <code>ko.applyBindings</code> parameter. |
Lines 63-69
Link Here
|
63 |
|
69 |
|
64 |
private final String name; |
70 |
private final String name; |
65 |
private final List<Bindings> subBindings = new ArrayList<>(); |
71 |
private final List<Bindings> subBindings = new ArrayList<>(); |
66 |
private final List<String> props = new ArrayList<>(); |
72 |
private final List<Object> props = new ArrayList<>(); |
67 |
|
73 |
|
68 |
private Bindings(String name) { |
74 |
private Bindings(String name) { |
69 |
this.name = name; |
75 |
this.name = name; |
Lines 144-150
Link Here
|
144 |
*/ |
150 |
*/ |
145 |
public final Bindings modelProperty(String name, Bindings binding, boolean array) { |
151 |
public final Bindings modelProperty(String name, Bindings binding, boolean array) { |
146 |
subBindings.add(binding); |
152 |
subBindings.add(binding); |
147 |
addProp(name, array, binding.name); |
153 |
addProp(name, array, binding); |
148 |
return this; |
154 |
return this; |
149 |
} |
155 |
} |
150 |
|
156 |
|
Lines 152-195
Link Here
|
152 |
StringBuilder sb = new StringBuilder(); |
158 |
StringBuilder sb = new StringBuilder(); |
153 |
//sb.append("(function() {\n"); |
159 |
//sb.append("(function() {\n"); |
154 |
HashSet<Bindings> visited = new HashSet<>(); |
160 |
HashSet<Bindings> visited = new HashSet<>(); |
155 |
LinkedHashSet<Bindings> lhs = new LinkedHashSet<>(); |
161 |
Collection<Bindings> lhs = new LinkedHashSet<>(); |
156 |
walkBindings(visited, lhs); |
162 |
StringBuilder delayedInit = new StringBuilder(); |
|
|
163 |
if (!walkBindings(visited, lhs)) { |
164 |
Map<Bindings, List<Bindings>> edges = new HashMap<>(); |
165 |
for (Bindings b : lhs) { |
166 |
edges.put(b, b.subBindings); |
167 |
} |
168 |
try { |
169 |
BaseUtilities.topologicalSort(lhs, edges); |
170 |
// There is a cycle, so the resolution should fail |
171 |
throw new IllegalStateException(); |
172 |
} catch (TopologicalSortException ex) { |
173 |
lhs = Collections.checkedList(ex.partialSort(), Bindings.class); |
174 |
} |
175 |
} |
157 |
for (Bindings b : lhs) { |
176 |
for (Bindings b : lhs) { |
158 |
b.generate(sb); |
177 |
b.generate(sb, delayedInit, visited); |
|
|
178 |
visited.remove(b); |
159 |
} |
179 |
} |
160 |
sb.append(" ko.applyBindings(").append(name).append(");\n"); |
180 |
sb.append(delayedInit); |
|
|
181 |
sb.append("\nko.applyBindings(").append(name).append(");\n"); |
161 |
//sb.append("}());"); |
182 |
//sb.append("}());"); |
162 |
return sb.toString(); |
183 |
return sb.toString(); |
163 |
} |
184 |
} |
164 |
|
185 |
|
165 |
private void generate(StringBuilder sb) { |
186 |
private void generate(StringBuilder sb, StringBuilder delayedInit, Set<Bindings> notYetProcessed) { |
166 |
sb.append("var ").append(name).append(" = {"); |
187 |
sb.append("var ").append(name).append(" = {"); |
167 |
String sep = "\n"; |
188 |
String sep = "\n"; |
168 |
for (String s : props) { |
189 |
for (int i = 0; i < props.size(); i += 3) { |
169 |
sb.append(sep).append(" ").append(s); |
190 |
String propName = (String)props.get(i); |
|
|
191 |
Boolean array = (Boolean)props.get(i + 1); |
192 |
Object value = props.get(i + 2); |
193 |
|
194 |
if (value instanceof Bindings) { |
195 |
Bindings b = (Bindings) value; |
196 |
if (notYetProcessed.contains(b)) { |
197 |
delayedInit.append("\n").append(this.name).append("[\""). |
198 |
append(propName).append("\"] = ").append(b.name). |
199 |
append(";"); |
200 |
continue; |
201 |
} |
202 |
value = b.name; |
203 |
} |
204 |
|
205 |
if (array) { |
206 |
value = "[ " + value + " ]"; |
207 |
} |
208 |
sb.append(sep).append(" ").append('\"').append(propName).append("\" : ").append(value); |
170 |
sep = ",\n"; |
209 |
sep = ",\n"; |
171 |
} |
210 |
} |
172 |
sb.append("\n};\n"); |
211 |
sb.append("\n};\n"); |
173 |
} |
212 |
} |
174 |
|
213 |
|
175 |
private void addProp(String name, boolean array, String value) { |
214 |
private void addProp(String name, boolean array, Object value) { |
176 |
if (array) { |
|
|
177 |
value = "[ " + value + " ]"; |
178 |
} |
179 |
if (name.contains("\"")) { |
215 |
if (name.contains("\"")) { |
180 |
throw new IllegalStateException("Wrong name " + name); |
216 |
throw new IllegalStateException("Wrong name " + name); |
181 |
} |
217 |
} |
182 |
props.add('\"' + name + "\" : " + value); |
218 |
props.add(name); |
|
|
219 |
props.add(array); |
220 |
props.add(value); |
183 |
} |
221 |
} |
184 |
|
222 |
|
185 |
private void walkBindings(Set<Bindings> visited, Set<Bindings> collect) { |
223 |
private boolean walkBindings(Set<Bindings> visited, Collection<Bindings> collect) { |
186 |
if (!visited.add(this)) { |
224 |
if (!visited.add(this)) { |
187 |
return; |
225 |
return false; |
188 |
} |
226 |
} |
|
|
227 |
boolean ok = true; |
189 |
for (Bindings b : subBindings) { |
228 |
for (Bindings b : subBindings) { |
190 |
b.walkBindings(visited, collect); |
229 |
ok &= b.walkBindings(visited, collect); |
191 |
} |
230 |
} |
192 |
collect.add(this); |
231 |
collect.add(this); |
|
|
232 |
return ok; |
193 |
} |
233 |
} |
194 |
|
234 |
|
195 |
/** |
235 |
/** |