Skip to content

Commit 26988e5

Browse files
authored
code cleanup of template (#21)
1 parent c0e30ef commit 26988e5

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.html

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
<section class="ngx-json-treeview">
22
@if (segments().length === 0) {
3-
@let sectionClass =
4-
['object', 'array'].includes(rootType())
5-
? 'punctuation'
6-
: 'segment-type-' + rootType();
7-
<section [class]="sectionClass">
8-
<span class="segment-primative">
3+
<div [class]="primativeSegmentClass()">
4+
<span class="segment-primitive">
95
{{ asString() }}
106
</span>
11-
</section>
7+
</div>
128
} @else {
139
@if (_currentDepth() === 0) {
1410
<span class="punctuation">{{ openingBrace() }}</span>
@@ -19,13 +15,14 @@
1915
let i = $index;
2016
let len = $count
2117
) {
22-
<section [class]="'segment segment-type-' + segment.type">
23-
@let expandable = isExpandable(segment);
24-
@let empty = isEmpty(segment);
25-
@let openingBrace = openingBraceForSegment(segment);
26-
@let closingBrace = closingBraceForSegment(segment);
27-
@let clickableValue = isClickable(segment);
28-
<section class="segment-main">
18+
@let needsComma = i < len - 1;
19+
@let expandable = isExpandable(segment);
20+
@let empty = isEmpty(segment);
21+
@let openingBrace = openingBraceForSegment(segment);
22+
@let closingBrace = closingBraceForSegment(segment);
23+
@let clickableValue = isClickable(segment);
24+
<div [class]="'segment segment-type-' + segment.type">
25+
<div class="segment-main">
2926
<span
3027
[tabindex]="expandable ? 0 : -1"
3128
[class.expandable]="expandable"
@@ -39,9 +36,9 @@
3936
</span>
4037
<span class="punctuation">: </span>
4138
@if (empty) {
42-
<span
43-
class="punctuation"
44-
>{{ `${openingBrace}${closingBrace}${i < len - 1 ? ',' : ''}` }}</span
39+
<span class="punctuation"
40+
>{{ openingBrace }}{{ closingBrace
41+
}}{{ needsComma ? ',' : '' }}</span
4542
>
4643
} @else if (!expandable || !segment.expanded) {
4744
<span
@@ -53,17 +50,15 @@
5350
(keydown.enter)="onValueClickHandler(segment)"
5451
>{{ segment.description }}</span
5552
>
56-
<span class="punctuation">{{ i < len - 1 ? ',' : '' }}</span>
53+
<span class="punctuation">{{ needsComma ? ',' : '' }}</span>
5754
} @else {
58-
@if (openingBrace) {
59-
<span class="punctuation">
60-
{{ openingBrace }}
61-
</span>
62-
}
55+
<span class="punctuation">
56+
{{ openingBrace }}
57+
</span>
6358
}
64-
</section>
59+
</div>
6560
@if (expandable && segment.expanded) {
66-
<section class="children">
61+
<div class="children">
6762
<ngx-json-treeview
6863
[json]="segment.value"
6964
[expanded]="expanded()"
@@ -73,14 +68,12 @@
7368
[_parent]="segment"
7469
[_currentDepth]="_currentDepth() + 1"
7570
(onValueClick)="onValueClickHandler($event)" />
76-
@if (closingBrace) {
77-
<span class="punctuation"
78-
>{{ closingBrace }}{{ i < len - 1 ? ',' : '' }}</span
79-
>
80-
}
81-
</section>
71+
<span class="punctuation">
72+
{{ closingBrace }}{{ needsComma ? ',' : '' }}
73+
</span>
74+
</div>
8275
}
83-
</section>
76+
</div>
8477
}
8578
@if (_currentDepth() === 0) {
8679
<span class="punctuation">{{ closingBrace() }}</span>

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ $type-colors: (
105105
}
106106

107107
@each $type, $color in $type-colors {
108-
.segment-type-#{$type} > .segment-primative {
108+
.segment-type-#{$type} > .segment-primitive {
109109
color: #{$color};
110110
}
111111
}
112-
.segment-primative {
113-
margin: 0px;
114-
padding: 0px;
112+
.segment-primitive {
113+
margin: 0;
114+
padding: 0;
115115
}
116116
// special cases that need highlighting
117-
.segment-type-null > .segment-primative {
117+
.segment-type-null > .segment-primitive {
118118
background-color: var(--ngx-json-null-bg, red);
119119
}
120120
}

projects/ngx-json-treeview/src/lib/ngx-json-treeview.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ export class NgxJsonTreeviewComponent {
145145
asString = computed<string>(() =>
146146
JSON.stringify(this.json(), null, 2).trim()
147147
);
148+
primativeSegmentClass = computed<string>(() => {
149+
const type = this.rootType();
150+
if (['object', 'array'].includes(type)) {
151+
return 'punctuation';
152+
}
153+
return 'segment-type-' + type;
154+
});
148155

149156
isExpandable(segment: Segment) {
150157
return (

0 commit comments

Comments
 (0)