Skip to content

Commit 7232734

Browse files
committed
Fix buttons in JsTree output
Buttons expand all and colapse all did nothing when using multiple modules. JIRA:LIGHTY-230 Signed-off-by: tobias.pobocik <tobias.pobocik@pantheon.tech>
1 parent 5d7dd17 commit 7232734

File tree

8 files changed

+45
-21
lines changed

8 files changed

+45
-21
lines changed

src/main/java/io/lighty/yang/validator/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static void runLywForeachYangFile(final List<String> yangFiles, final Co
184184
for (final Module module : lyvContext.testedModules()) {
185185
format.emit(module);
186186
}
187-
format.close();
187+
format.close(lyvContext.testedModules());
188188
}
189189

190190
private static void generateHtmlAnalyzeOutput(final List<String> yangFiles, final Configuration config)

src/main/java/io/lighty/yang/validator/formats/Emitter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.lighty.yang.validator.config.Configuration;
1111
import io.lighty.yang.validator.simplify.SchemaTree;
12+
import java.util.Collection;
1213
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
1314
import org.opendaylight.yangtools.yang.model.api.Module;
1415

@@ -33,5 +34,5 @@ public interface Emitter {
3334
/**
3435
* Close after printing out the module.
3536
*/
36-
void close();
37+
void close(Collection<Module> modules);
3738
}

src/main/java/io/lighty/yang/validator/formats/Format.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.lighty.yang.validator.config.Configuration;
1313
import io.lighty.yang.validator.simplify.SchemaTree;
1414
import java.util.ArrayList;
15+
import java.util.Collection;
1516
import java.util.List;
1617
import java.util.Optional;
1718
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
@@ -56,7 +57,7 @@ public void emit(final Module module) {
5657
}
5758

5859
@Override
59-
public void close() {
60-
this.usedFormat.close();
60+
public void close(Collection<Module> modules) {
61+
this.usedFormat.close(modules);
6162
}
6263
}

src/main/java/io/lighty/yang/validator/formats/FormatPlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.lighty.yang.validator.simplify.SchemaTree;
1414
import java.nio.file.Path;
1515
import java.nio.file.Paths;
16+
import java.util.Collection;
1617
import java.util.Optional;
1718
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
1819
import org.opendaylight.yangtools.yang.model.api.Module;
@@ -48,7 +49,7 @@ void init(final EffectiveModelContext context, final SchemaTree tree, final Conf
4849
/**
4950
* Close after printing out the module.
5051
*/
51-
protected void close() {
52+
protected void close(Collection<Module> modules) {
5253
// no-op by default
5354
}
5455

src/main/java/io/lighty/yang/validator/formats/JsTree.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,39 @@ private static RpcInputOutput getRpcInputOutput(final List<QName> qnames,
236236
return inputOutputOther;
237237
}
238238

239-
private static String loadJS() {
239+
private static String loadJS(Collection<Module> modules) {
240240
final URL url = Resources.getResource("js");
241241
String text = "";
242242
try {
243243
text = Resources.toString(url, StandardCharsets.UTF_8);
244+
245+
StringBuilder moduleCode = new StringBuilder();
246+
247+
for (Module module : modules) {
248+
249+
// Define the multi-line string to add
250+
String multiLineStringToAdd =
251+
" $('#basic-" + module.getName()
252+
+ "').simpleTreeTable({\n"
253+
+ "expander: $('#expander-" + module.getName() + "'),\n"
254+
+ "collapser: $('#collapser-" + module.getName() + "')\n});\n";
255+
moduleCode.append(multiLineStringToAdd);
256+
}
257+
258+
// Encapsulate module code within $(document).ready(function () {...});
259+
String encapsulatedCode = "\n$(document).ready(function () {\n" + moduleCode + "});\n";
260+
261+
// Find the index of the placeholder comment
262+
String placeholderComment = "//AddTableLogic";
263+
int commentIndex = text.indexOf(placeholderComment);
264+
265+
if (commentIndex != -1) {
266+
// Insert the encapsulated code after the comment
267+
StringBuilder modifiedText = new StringBuilder(text);
268+
modifiedText.insert(commentIndex + placeholderComment.length(), encapsulatedCode);
269+
text = modifiedText.toString();
270+
}
271+
244272
} catch (final IOException e) {
245273
LOG.error("Can not load text from js file");
246274
}
@@ -267,6 +295,9 @@ private static String prepareModule(final Module module) {
267295
String text = "";
268296
try {
269297
text = Resources.toString(url, StandardCharsets.UTF_8);
298+
text = text.replace("expander", "expander-" + module.getName());
299+
text = text.replace("collapser", "collapser-" + module.getName());
300+
text = text.replace("basic", "basic-" + module.getName());
270301
text = text.replace("<NAME_REVISION>", nameRevision);
271302
text = text.replace("<NAMESPACE>", module.getNamespace().toString());
272303
text = text.replace("<PREFIX>", module.getPrefix());
@@ -403,8 +434,8 @@ public Optional<GroupArguments> getGroupArguments() {
403434
@Override
404435
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
405436
justification = "Valid output from LYV is dependent on Logback output")
406-
public void close() {
407-
LOG.info("{}", loadJS());
437+
public void close(Collection<Module> modules) {
438+
LOG.info("{}", loadJS(modules));
408439
LOG.info("</body>");
409440
LOG.info("</html>");
410441
}

src/main/java/io/lighty/yang/validator/formats/JsonTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void emitFormat(final Module module) {
146146
@SuppressFBWarnings(value = "SLF4J_SIGN_ONLY_FORMAT",
147147
justification = "Valid output from LYV is dependent on Logback output")
148148
@Override
149-
public void close() {
149+
public void close(Collection<Module> modules) {
150150
LOG.info("{}", new JSONObject().put("parsed-models", parsedModels).toString(4));
151151
parsedModels.clear();
152152
}

src/main/resources/js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,5 @@ $.fn.simpleTreeTable = function(options) {
325325

326326
$.SimpleTreeTable = SimpleTreeTable;
327327

328-
$('.table.table-bordered.table-striped.simple-tree-table').simpleTreeTable({
329-
expander: $('#expander'),
330-
collapser: $('#collapser')
331-
});
332-
333-
$('#collapsed').simpleTreeTable({
334-
opened: 'none',
335-
});
336-
$('#collapsed').simpleTreeTable({
337-
margin: 25
338-
});
328+
//AddTableLogic
339329
</script>

src/test/java/io/lighty/yang/validator/MainTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testSimplifyWithYangFormat() throws Exception {
7676
for (final Module module : effectiveModelContext.getModules()) {
7777
format.emit(module);
7878
}
79-
format.close();
79+
format.close((Collection<Module>) effectiveModelContext.getModules());
8080
contextFactory =
8181
new YangContextFactory(ImmutableList.of(outPath), ImmutableList.of(), Collections.emptySet(), false);
8282
effectiveModelContext = contextFactory.createContext(true);

0 commit comments

Comments
 (0)