Parent componentn to diplay different trend diagrams
This commit is contained in:
+249
@@ -0,0 +1,249 @@
|
||||
<div class="trend-layout">
|
||||
<!-- Left Sidebar: Controls -->
|
||||
<aside class="trend-sidebar">
|
||||
<div class="control-card">
|
||||
<h3>1. Select Topics</h3>
|
||||
<div class="checkbox-group">
|
||||
@if (keywords().length === 0) {
|
||||
<div class="empty-list-info">No keywords selected.</div>
|
||||
} @else {
|
||||
@for (topic of keywords(); track topic) {
|
||||
<label class="checkbox-label">
|
||||
<input
|
||||
type="checkbox"
|
||||
[checked]="selectedTrendTopics().includes(topic)"
|
||||
(change)="onTrendTopicToggle(topic, $event)" />
|
||||
<span>{{ topic }}</span>
|
||||
</label>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Year selectors (replacing timeline slider) -->
|
||||
<div class="control-card">
|
||||
<h3>2. Year Range</h3>
|
||||
<div class="year-selectors">
|
||||
<select
|
||||
[ngModel]="startYear()"
|
||||
(ngModelChange)="startYear.set(+$event); updateUrlParams()">
|
||||
@for (y of availableYears; track y) {
|
||||
<option [value]="y" [disabled]="y > endYear()">{{ y }}</option>
|
||||
}
|
||||
</select>
|
||||
<span>to</span>
|
||||
<select
|
||||
[ngModel]="endYear()"
|
||||
(ngModelChange)="endYear.set(+$event); updateUrlParams()">
|
||||
@for (y of availableYears; track y) {
|
||||
<option [value]="y" [disabled]="y < startYear()">{{ y }}</option>
|
||||
}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Compare Countries/Continents -->
|
||||
<div class="control-card">
|
||||
<h3>3. Compare Countries/Continents</h3>
|
||||
|
||||
<div class="comparison-list">
|
||||
@if (
|
||||
comparisonCountries().length > 0 || comparisonRegions().length > 0
|
||||
) {
|
||||
@for (c of comparisonCountries(); track c.id) {
|
||||
<div class="comparison-item location-item font-country">
|
||||
<span class="item-name" [title]="c.name">🏳️ {{ c.name }}</span>
|
||||
<button
|
||||
class="remove-btn"
|
||||
(click)="removeLocation(c.id, 'country')"
|
||||
aria-label="Remove country">
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
@for (r of comparisonRegions(); track r.id) {
|
||||
<div class="comparison-item location-item font-region">
|
||||
<span class="item-name" [title]="r.name">🌍 {{ r.name }}</span>
|
||||
<button
|
||||
class="remove-btn"
|
||||
(click)="removeLocation(r.id, 'continent')"
|
||||
aria-label="Remove continent">
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
} @else {
|
||||
<div class="empty-list-info">
|
||||
No regions/countries selected. Search to add.
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<!-- Autocomplete Search for Countries/Continents -->
|
||||
<div class="autocomplete-search">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search country or continent..."
|
||||
[value]="locationQuery()"
|
||||
(input)="onLocationSearchChange($any($event.target).value)" />
|
||||
@if (locationSearchResults().length > 0) {
|
||||
<ul class="autocomplete-results">
|
||||
@for (loc of locationSearchResults(); track loc.id) {
|
||||
<li>
|
||||
<button (click)="addLocation(loc)">
|
||||
<span class="name">{{ loc.display_name }}</span>
|
||||
<span class="hint">{{
|
||||
loc.type === 'country' ? 'Country' : 'Continent'
|
||||
}}</span>
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Compare Institutions -->
|
||||
<div class="control-card">
|
||||
<h3>4. Compare Institutions</h3>
|
||||
|
||||
@if (primaryInstitutionName()) {
|
||||
<div class="primary-inst-badge">
|
||||
<span class="label">Assigned:</span>
|
||||
<strong class="name">{{ primaryInstitutionName() }}</strong>
|
||||
</div>
|
||||
}
|
||||
|
||||
<div class="comparison-list">
|
||||
@if (comparisonInstitutions().length > 0) {
|
||||
@for (inst of comparisonInstitutions(); track inst.id) {
|
||||
<div class="comparison-item">
|
||||
<span class="item-name" [title]="inst.name"
|
||||
>🏫 {{ inst.name }}</span
|
||||
>
|
||||
<button
|
||||
class="remove-btn"
|
||||
(click)="removeInstitution(inst.id)"
|
||||
aria-label="Remove comparison institution">
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
}
|
||||
} @else {
|
||||
<div class="empty-list-info">
|
||||
No additional institutions selected. Search to add.
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="autocomplete-search">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search and add institution..."
|
||||
[value]="searchQuery()"
|
||||
(input)="onSearchChange($any($event.target).value)" />
|
||||
@if (searchResults().length > 0) {
|
||||
<ul class="autocomplete-results">
|
||||
@for (result of searchResults(); track result.id) {
|
||||
<li>
|
||||
<button
|
||||
(click)="addInstitution(result.id, result.display_name)">
|
||||
<span class="name">{{ result.display_name }}</span>
|
||||
<span class="hint">{{ result.hint || result.type }}</span>
|
||||
</button>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Suggestions panel -->
|
||||
<div class="control-card suggestions-card">
|
||||
<h3>Suggested Top Institutions</h3>
|
||||
<div class="suggestions-list">
|
||||
@if (isSuggestionsLoading()) {
|
||||
<div class="suggestions-loader">Loading suggestions...</div>
|
||||
} @else {
|
||||
@for (item of topSuggestions(); track item.id || '') {
|
||||
<button
|
||||
class="suggestion-item"
|
||||
(click)="addInstitution(item.id || '', item.display_name || '')"
|
||||
[disabled]="
|
||||
(item.id
|
||||
? comparisonInstitutions().some((i) => i.id === item.id)
|
||||
: false) || primaryInstitution() === item.id
|
||||
">
|
||||
<div class="info">
|
||||
<strong class="name">{{ item.display_name }}</strong>
|
||||
<span class="meta"
|
||||
>{{ item.country_code }} •
|
||||
{{ item.exact_topic_count?.toLocaleString() }} works</span
|
||||
>
|
||||
</div>
|
||||
<span class="add-icon">+</span>
|
||||
</button>
|
||||
}
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Clean, Separate Search Trend Button -->
|
||||
<div class="search-trend-btn-container">
|
||||
<button
|
||||
class="search-trend-btn"
|
||||
[class.dirty]="isDirty()"
|
||||
[disabled]="isTrendLoading() || selectedTrendTopics().length === 0"
|
||||
(click)="fetchTrendData()">
|
||||
🔍 {{ isDirty() ? 'Apply Changes' : 'Search Trend' }}
|
||||
</button>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<!-- Right Main Panel: Display Toggles & Chart -->
|
||||
<main class="trend-main">
|
||||
<div class="trend-toggles-bar">
|
||||
<span class="toggle-label">Visualization Scale:</span>
|
||||
<div class="toggle-buttons">
|
||||
<button
|
||||
[class.active]="chartMode() === 'normalized'"
|
||||
(click)="setChartMode('normalized')"
|
||||
title="Normalizes publications output to [0,1] to compare growth trend shapes.">
|
||||
📉 Normalized Scale
|
||||
</button>
|
||||
<button
|
||||
[class.active]="chartMode() === 'total'"
|
||||
(click)="setChartMode('total')"
|
||||
title="Shows total absolute publication counts.">
|
||||
📊 Total Volume
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="chart-holder-card">
|
||||
@if (isTrendLoading()) {
|
||||
<div class="loader-overlay">
|
||||
<div class="spinner"></div>
|
||||
<p>Retrieving trend data from works index...</p>
|
||||
</div>
|
||||
}
|
||||
@if (selectedTrendTopics().length > 1) {
|
||||
<div class="chart-topic-tabs">
|
||||
@for (topic of selectedTrendTopics(); track topic) {
|
||||
<button
|
||||
[class.active]="activeChartTopic() === topic"
|
||||
(click)="updateActiveChartTopic(topic)">
|
||||
📚 {{ topic }}
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
<app-uni-trend-chart
|
||||
[data]="trendResponse()"
|
||||
[mode]="chartMode()"
|
||||
[activeTopic]="activeChartTopic()"
|
||||
[startYear]="startYear()"
|
||||
[endYear]="endYear()"></app-uni-trend-chart>
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
+449
@@ -0,0 +1,449 @@
|
||||
.trend-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 300px 1fr;
|
||||
gap: 1.5rem;
|
||||
margin-top: 1.5rem;
|
||||
font-family: 'Inter', sans-serif;
|
||||
|
||||
@media (max-width: 1024px) {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.trend-sidebar {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.control-card {
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
padding: 1.25rem;
|
||||
border: 1px solid #e5e7eb;
|
||||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
|
||||
|
||||
h3 {
|
||||
font-size: 0.8rem;
|
||||
font-weight: 600;
|
||||
text-transform: uppercase;
|
||||
color: #6b7280;
|
||||
margin: 0 0 0.75rem 0;
|
||||
letter-spacing: 0.05em;
|
||||
}
|
||||
|
||||
&.suggestions-card {
|
||||
background: #fafafa;
|
||||
}
|
||||
}
|
||||
|
||||
.checkbox-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.checkbox-label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 0.875rem;
|
||||
color: #374151;
|
||||
cursor: pointer;
|
||||
|
||||
input[type='checkbox'] {
|
||||
accent-color: #3b82f6;
|
||||
width: 15px;
|
||||
height: 15px;
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
.comparison-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
max-height: 140px;
|
||||
overflow-y: auto;
|
||||
margin-bottom: 0.75rem;
|
||||
|
||||
.empty-list-info {
|
||||
font-size: 0.75rem;
|
||||
color: #9ca3af;
|
||||
text-align: center;
|
||||
padding: 8px 0;
|
||||
border: 1px dashed #e5e7eb;
|
||||
border-radius: 5px;
|
||||
}
|
||||
}
|
||||
|
||||
.comparison-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background: #f3f4f6;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 5px;
|
||||
padding: 0.3rem 0.5rem;
|
||||
|
||||
.item-name {
|
||||
font-size: 0.75rem;
|
||||
color: #374151;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
max-width: 210px;
|
||||
}
|
||||
|
||||
.remove-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
color: #9ca3af;
|
||||
cursor: pointer;
|
||||
font-size: 0.7rem;
|
||||
padding: 1px 4px;
|
||||
border-radius: 3px;
|
||||
&:hover {
|
||||
color: #ef4444;
|
||||
background: #fee2e2;
|
||||
}
|
||||
}
|
||||
|
||||
&.location-item {
|
||||
&.font-country {
|
||||
background: #f0fdf4 !important;
|
||||
border-color: #bbf7d0 !important;
|
||||
.item-name {
|
||||
color: #166534 !important;
|
||||
}
|
||||
}
|
||||
&.font-region {
|
||||
background: #eff6ff !important;
|
||||
border-color: #bfdbfe !important;
|
||||
.item-name {
|
||||
color: #1e40af !important;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.autocomplete-search {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.5rem;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #d1d5db;
|
||||
font-size: 0.8125rem;
|
||||
outline: none;
|
||||
box-sizing: border-box;
|
||||
&:focus {
|
||||
border-color: #3b82f6;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.autocomplete-results {
|
||||
position: absolute;
|
||||
top: 100%;
|
||||
left: 0;
|
||||
right: 0;
|
||||
z-index: 50;
|
||||
background: white;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 5px;
|
||||
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
||||
margin-top: 3px;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
max-height: 180px;
|
||||
overflow-y: auto;
|
||||
|
||||
li button {
|
||||
width: 100%;
|
||||
text-align: left;
|
||||
background: none;
|
||||
border: none;
|
||||
padding: 0.5rem 0.75rem;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
&:hover {
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.name {
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
color: #1f2937;
|
||||
}
|
||||
.hint {
|
||||
font-size: 0.65rem;
|
||||
color: #9ca3af;
|
||||
margin-top: 1px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.suggestions-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.4rem;
|
||||
|
||||
.suggestions-loader {
|
||||
font-size: 0.75rem;
|
||||
color: #9ca3af;
|
||||
text-align: center;
|
||||
padding: 8px 0;
|
||||
}
|
||||
}
|
||||
|
||||
.suggestion-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background: white;
|
||||
border: 1px solid #e5e7eb;
|
||||
border-radius: 6px;
|
||||
padding: 0.4rem 0.6rem;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover:not([disabled]) {
|
||||
border-color: #3b82f6;
|
||||
background: #f0f9ff;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
|
||||
&[disabled] {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.info {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
max-width: 210px;
|
||||
|
||||
.name {
|
||||
font-size: 0.75rem;
|
||||
color: #374151;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.meta {
|
||||
font-size: 0.65rem;
|
||||
color: #9ca3af;
|
||||
margin-top: 1px;
|
||||
}
|
||||
}
|
||||
.add-icon {
|
||||
font-size: 1rem;
|
||||
color: #3b82f6;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
|
||||
.trend-main {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.trend-toggles-bar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
padding: 0.6rem 1rem;
|
||||
border: 1px solid #e5e7eb;
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
|
||||
|
||||
.toggle-label {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 500;
|
||||
color: #4b5563;
|
||||
}
|
||||
}
|
||||
|
||||
.toggle-buttons {
|
||||
display: flex;
|
||||
background: #f3f4f6;
|
||||
border-radius: 6px;
|
||||
padding: 2px;
|
||||
|
||||
button {
|
||||
padding: 0.4rem 0.8rem;
|
||||
border: none;
|
||||
background: none;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
color: #4b5563;
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover:not(.active) {
|
||||
color: #111827;
|
||||
background: rgba(255, 255, 255, 0.5);
|
||||
}
|
||||
&.active {
|
||||
background: white;
|
||||
color: #3b82f6;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.chart-holder-card {
|
||||
position: relative;
|
||||
background: white;
|
||||
border-radius: 10px;
|
||||
border: 1px solid #e5e7eb;
|
||||
overflow: hidden;
|
||||
min-height: 400px;
|
||||
}
|
||||
|
||||
.chart-topic-tabs {
|
||||
display: flex;
|
||||
gap: 0.4rem;
|
||||
padding: 0.6rem 1rem;
|
||||
background: #f9fafb;
|
||||
border-bottom: 1px solid #e5e7eb;
|
||||
|
||||
button {
|
||||
padding: 0.3rem 0.6rem;
|
||||
border: 1px solid #e5e7eb;
|
||||
background: white;
|
||||
color: #4b5563;
|
||||
border-radius: 5px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.15s ease;
|
||||
|
||||
&:hover {
|
||||
border-color: #3b82f6;
|
||||
color: #3b82f6;
|
||||
background: #f0f9ff;
|
||||
}
|
||||
|
||||
&.active {
|
||||
background: #3b82f6;
|
||||
border-color: #3b82f6;
|
||||
color: white;
|
||||
font-weight: 600;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.loader-overlay {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(255, 255, 255, 0.8);
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
|
||||
.spinner {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border: 3px solid rgba(59, 130, 246, 0.1);
|
||||
border-radius: 50%;
|
||||
border-top-color: #3b82f6;
|
||||
animation: spin 0.8s linear infinite;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: 0.8125rem;
|
||||
color: #4b5563;
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.search-trend-btn-container {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding: 0.5rem 0;
|
||||
}
|
||||
|
||||
.search-trend-btn {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.6rem 1.2rem;
|
||||
border-radius: 6px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
border: 1px solid #3b82f6;
|
||||
background-color: #3b82f6;
|
||||
color: white;
|
||||
transition: all 0.2s ease;
|
||||
|
||||
&:hover:not([disabled]) {
|
||||
background-color: #2563eb;
|
||||
border-color: #2563eb;
|
||||
box-shadow: 0 4px 6px -1px rgba(59, 130, 246, 0.2);
|
||||
}
|
||||
|
||||
&[disabled] {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
background-color: #9ca3af;
|
||||
border-color: #9ca3af;
|
||||
}
|
||||
|
||||
&.dirty {
|
||||
animation: pulse-border 2s infinite;
|
||||
background-color: #2563eb;
|
||||
border-color: #1d4ed8;
|
||||
}
|
||||
}
|
||||
|
||||
.year-selectors {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
align-items: center;
|
||||
|
||||
select {
|
||||
flex: 1;
|
||||
padding: 0.4rem;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #d1d5db;
|
||||
font-size: 0.8125rem;
|
||||
background: white;
|
||||
outline: none;
|
||||
&:focus {
|
||||
border-color: #3b82f6;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to {
|
||||
transform: rotate(360deg);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes pulse-border {
|
||||
0% {
|
||||
box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7);
|
||||
}
|
||||
70% {
|
||||
box-shadow: 0 0 0 6px rgba(59, 130, 246, 0);
|
||||
}
|
||||
100% {
|
||||
box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { UniTrendParentComponent } from './uni-trend-parent.component';
|
||||
|
||||
describe('UniTrendParentComponent', () => {
|
||||
let component: UniTrendParentComponent;
|
||||
let fixture: ComponentFixture<UniTrendParentComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [UniTrendParentComponent],
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(UniTrendParentComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
+470
@@ -0,0 +1,470 @@
|
||||
import {
|
||||
Component,
|
||||
ChangeDetectionStrategy,
|
||||
input,
|
||||
computed,
|
||||
signal,
|
||||
effect,
|
||||
inject,
|
||||
OnInit,
|
||||
} from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { UniTrendChartComponent } from '../uni-trend-chart/uni-trend-chart.component';
|
||||
import { UniversityRankingsService } from '../../../api/api/university-rankings.service';
|
||||
import { InstitutionsService } from '../../../api/api/institutions.service';
|
||||
import {
|
||||
TrendResponse,
|
||||
TopInstitutionItem,
|
||||
AutocompleteLocationItem,
|
||||
AutocompleteResponse,
|
||||
} from '../../../api';
|
||||
|
||||
@Component({
|
||||
selector: 'app-uni-trend-parent',
|
||||
standalone: true,
|
||||
imports: [CommonModule, FormsModule, UniTrendChartComponent],
|
||||
templateUrl: './uni-trend-parent.component.html',
|
||||
styleUrl: './uni-trend-parent.component.scss',
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
})
|
||||
export class UniTrendParentComponent implements OnInit {
|
||||
private readonly apiService = inject(UniversityRankingsService);
|
||||
private readonly institutionsApiService = inject(InstitutionsService);
|
||||
private readonly route = inject(ActivatedRoute);
|
||||
private readonly router = inject(Router);
|
||||
|
||||
// Input containing extracted/selected keywords from parent
|
||||
readonly keywords = input<string[]>([]);
|
||||
|
||||
// Trend settings signals
|
||||
readonly selectedTrendTopics = signal<string[]>([]);
|
||||
readonly primaryInstitution = signal<string | null>(null);
|
||||
readonly primaryInstitutionName = signal<string>(
|
||||
'University of Duisburg-Essen',
|
||||
);
|
||||
readonly comparisonInstitutions = signal<{ id: string; name: string }[]>([]);
|
||||
readonly comparisonCountries = signal<{ id: string; name: string }[]>([]);
|
||||
readonly comparisonRegions = signal<{ id: string; name: string }[]>([]);
|
||||
readonly startYear = signal<number>(2015);
|
||||
readonly endYear = signal<number>(new Date().getFullYear() - 1);
|
||||
readonly chartMode = signal<'normalized' | 'total'>('total');
|
||||
readonly activeChartTopic = signal<string>('');
|
||||
|
||||
// Cached parameters of the last active search request to track "isDirty"
|
||||
readonly fetchedTopics = signal<string[]>([]);
|
||||
readonly fetchedInstitutions = signal<{ id: string; name: string }[]>([]);
|
||||
readonly fetchedCountries = signal<{ id: string; name: string }[]>([]);
|
||||
readonly fetchedRegions = signal<{ id: string; name: string }[]>([]);
|
||||
|
||||
// Results & Loading signals
|
||||
readonly rawTrendResponse = signal<TrendResponse | null>(null);
|
||||
readonly isTrendLoading = signal<boolean>(false);
|
||||
readonly topSuggestions = signal<TopInstitutionItem[]>([]);
|
||||
readonly isSuggestionsLoading = signal<boolean>(false);
|
||||
|
||||
// Autocomplete UI state
|
||||
readonly searchQuery = signal<string>('');
|
||||
readonly searchResults = signal<any[]>([]);
|
||||
readonly locationQuery = signal<string>('');
|
||||
readonly locationSearchResults = signal<AutocompleteLocationItem[]>([]);
|
||||
|
||||
// Years array for selects
|
||||
readonly availableYears: number[] = [];
|
||||
|
||||
// Computed: filters out items locally if they were removed from list
|
||||
readonly trendResponse = computed(() => {
|
||||
const raw = this.rawTrendResponse();
|
||||
if (!raw) return null;
|
||||
|
||||
const currentInstIds = new Set(
|
||||
this.comparisonInstitutions().map((i) => i.id.toLowerCase()),
|
||||
);
|
||||
const currentCountryIds = new Set(
|
||||
this.comparisonCountries().map((c) => c.id.toLowerCase()),
|
||||
);
|
||||
const currentRegionIds = new Set(
|
||||
this.comparisonRegions().map((r) => r.id.toLowerCase()),
|
||||
);
|
||||
const defaultInstId = 'https://openalex.org/I62318514'.toLowerCase();
|
||||
|
||||
const filteredSeries = raw.series.filter((s: any) => {
|
||||
const id = s.entity_id.toLowerCase();
|
||||
if (s.entity_type === 'institution') {
|
||||
return id === defaultInstId || currentInstIds.has(id);
|
||||
} else if (s.entity_type === 'country') {
|
||||
return currentCountryIds.has(id);
|
||||
} else if (s.entity_type === 'region') {
|
||||
return currentRegionIds.has(id);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
return {
|
||||
...raw,
|
||||
series: filteredSeries,
|
||||
};
|
||||
});
|
||||
|
||||
// Computed dirty state to enable "Search" button
|
||||
readonly isDirty = computed(() => {
|
||||
const fetchedInsts = this.fetchedInstitutions()
|
||||
.map((i) => i.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
const currentInsts = this.comparisonInstitutions()
|
||||
.map((i) => i.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
if (fetchedInsts !== currentInsts) return true;
|
||||
|
||||
const fetchedC = this.fetchedCountries()
|
||||
.map((c) => c.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
const currentC = this.comparisonCountries()
|
||||
.map((c) => c.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
if (fetchedC !== currentC) return true;
|
||||
|
||||
const fetchedR = this.fetchedRegions()
|
||||
.map((r) => r.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
const currentR = this.comparisonRegions()
|
||||
.map((r) => r.id)
|
||||
.sort()
|
||||
.join(',');
|
||||
if (fetchedR !== currentR) return true;
|
||||
|
||||
const fetchedT = this.fetchedTopics().sort().join(',');
|
||||
const currentT = this.selectedTrendTopics().sort().join(',');
|
||||
if (fetchedT !== currentT) return true;
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
constructor() {
|
||||
// Generate year range from 2000 to current year
|
||||
const currentYear = new Date().getFullYear();
|
||||
for (let y = 2000; y <= currentYear; y++) {
|
||||
this.availableYears.push(y);
|
||||
}
|
||||
|
||||
// Effect: automatically sync checkboxes list whenever input keywords change
|
||||
effect(
|
||||
() => {
|
||||
const kws = this.keywords();
|
||||
if (kws.length > 0) {
|
||||
// Find existing selections that are still in keywords, or default to all
|
||||
const validSelections = this.selectedTrendTopics().filter((t) =>
|
||||
kws.includes(t),
|
||||
);
|
||||
if (validSelections.length > 0) {
|
||||
this.selectedTrendTopics.set(validSelections);
|
||||
} else {
|
||||
this.selectedTrendTopics.set(kws);
|
||||
}
|
||||
|
||||
// Initialize active chart topic to the first selected topic if empty or invalid
|
||||
const active = this.activeChartTopic();
|
||||
if (!active || !this.selectedTrendTopics().includes(active)) {
|
||||
this.activeChartTopic.set(this.selectedTrendTopics()[0] || '');
|
||||
}
|
||||
} else {
|
||||
this.selectedTrendTopics.set([]);
|
||||
}
|
||||
},
|
||||
{ allowSignalWrites: true },
|
||||
);
|
||||
|
||||
// Handle initialization and synchronization of URL state
|
||||
this.route.queryParams.subscribe((params) => {
|
||||
// Parse institutions serialized as id|name
|
||||
if (params['institutions']) {
|
||||
const insts = Array.isArray(params['institutions'])
|
||||
? params['institutions']
|
||||
: [params['institutions']];
|
||||
const parsed = insts.map((i) => {
|
||||
const parts = i.split('|');
|
||||
return { id: parts[0], name: parts[1] || parts[0] };
|
||||
});
|
||||
this.comparisonInstitutions.set(parsed);
|
||||
}
|
||||
// Parse countries
|
||||
if (params['countries']) {
|
||||
const ccs = Array.isArray(params['countries'])
|
||||
? params['countries']
|
||||
: [params['countries']];
|
||||
const parsed = ccs.map((c) => {
|
||||
const parts = c.split('|');
|
||||
return { id: parts[0], name: parts[1] || parts[0] };
|
||||
});
|
||||
this.comparisonCountries.set(parsed);
|
||||
}
|
||||
// Parse regions
|
||||
if (params['regions']) {
|
||||
const regs = Array.isArray(params['regions'])
|
||||
? params['regions']
|
||||
: [params['regions']];
|
||||
const parsed = regs.map((r) => {
|
||||
const parts = r.split('|');
|
||||
return { id: parts[0], name: parts[1] || parts[0] };
|
||||
});
|
||||
this.comparisonRegions.set(parsed);
|
||||
}
|
||||
|
||||
if (params['start_year']) {
|
||||
this.startYear.set(Number(params['start_year']));
|
||||
}
|
||||
if (params['end_year']) {
|
||||
this.endYear.set(Number(params['end_year']));
|
||||
}
|
||||
if (params['chart_mode']) {
|
||||
this.chartMode.set(params['chart_mode'] as any);
|
||||
}
|
||||
if (params['active_chart_topic']) {
|
||||
this.activeChartTopic.set(params['active_chart_topic']);
|
||||
}
|
||||
|
||||
// Auto-trigger load once on initial page render if keywords are present
|
||||
if (this.rawTrendResponse() === null && this.keywords().length > 0) {
|
||||
this.fetchTrendData();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.loadTrendSuggestions();
|
||||
}
|
||||
|
||||
// Fetch recommended institutions for comparison based on current topics
|
||||
loadTrendSuggestions() {
|
||||
this.isSuggestionsLoading.set(true);
|
||||
const topics = this.selectedTrendTopics();
|
||||
const topic =
|
||||
topics.length > 0
|
||||
? topics[0]
|
||||
: this.keywords().length > 0
|
||||
? this.keywords()[0]
|
||||
: 'Artificial Intelligence';
|
||||
|
||||
const countryFilter =
|
||||
this.comparisonCountries().length > 0
|
||||
? this.comparisonCountries()[0].id.split('/').pop()
|
||||
: undefined;
|
||||
const regionFilter =
|
||||
this.comparisonRegions().length > 0
|
||||
? this.comparisonRegions()[0].name
|
||||
: undefined;
|
||||
|
||||
this.apiService
|
||||
.getTopInstitutionsApiInseServiceUniversityRankingsTopInstitutionsGet(
|
||||
topic,
|
||||
regionFilter,
|
||||
countryFilter,
|
||||
6,
|
||||
)
|
||||
.subscribe({
|
||||
next: (res) => {
|
||||
this.topSuggestions.set(res || []);
|
||||
this.isSuggestionsLoading.set(false);
|
||||
},
|
||||
error: () => {
|
||||
this.isSuggestionsLoading.set(false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Trigger search trend data (only when "Search" is clicked)
|
||||
fetchTrendData() {
|
||||
const topics = this.selectedTrendTopics();
|
||||
if (topics.length === 0) return;
|
||||
|
||||
this.isTrendLoading.set(true);
|
||||
|
||||
// Save parameters of this request to check dirty state later
|
||||
this.fetchedTopics.set([...topics]);
|
||||
this.fetchedInstitutions.set([...this.comparisonInstitutions()]);
|
||||
this.fetchedCountries.set([...this.comparisonCountries()]);
|
||||
this.fetchedRegions.set([...this.comparisonRegions()]);
|
||||
|
||||
this.apiService
|
||||
.getTrendApiInseServiceUniversityRankingsTrendGet(
|
||||
topics,
|
||||
this.comparisonInstitutions().map((i) => i.id),
|
||||
this.comparisonCountries().map((c) => c.id),
|
||||
this.comparisonRegions().map((r) => r.id),
|
||||
)
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
this.rawTrendResponse.set(response);
|
||||
this.isTrendLoading.set(false);
|
||||
this.updateUrlParams();
|
||||
},
|
||||
error: () => {
|
||||
this.isTrendLoading.set(false);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Update query params to synchronize state
|
||||
updateUrlParams() {
|
||||
const queryParams: any = {
|
||||
trend_topics: this.selectedTrendTopics(),
|
||||
institutions:
|
||||
this.comparisonInstitutions().length > 0
|
||||
? this.comparisonInstitutions().map((i) => `${i.id}|${i.name}`)
|
||||
: null,
|
||||
countries:
|
||||
this.comparisonCountries().length > 0
|
||||
? this.comparisonCountries().map((c) => `${c.id}|${c.name}`)
|
||||
: null,
|
||||
regions:
|
||||
this.comparisonRegions().length > 0
|
||||
? this.comparisonRegions().map((r) => `${r.id}|${r.name}`)
|
||||
: null,
|
||||
start_year: this.startYear(),
|
||||
end_year: this.endYear(),
|
||||
chart_mode: this.chartMode(),
|
||||
active_chart_topic: this.activeChartTopic(),
|
||||
};
|
||||
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.route,
|
||||
queryParams,
|
||||
queryParamsHandling: 'merge',
|
||||
});
|
||||
}
|
||||
|
||||
// Checkbox topic toggling
|
||||
onTrendTopicToggle(topic: string, event: Event) {
|
||||
const isChecked = (event.target as HTMLInputElement).checked;
|
||||
const current = this.selectedTrendTopics();
|
||||
let updated: string[];
|
||||
|
||||
if (isChecked) {
|
||||
updated = current.includes(topic) ? current : [...current, topic];
|
||||
} else {
|
||||
updated =
|
||||
current.length > 1 ? current.filter((t) => t !== topic) : current;
|
||||
if (current.length === 1) {
|
||||
(event.target as HTMLInputElement).checked = true;
|
||||
}
|
||||
}
|
||||
|
||||
this.selectedTrendTopics.set(updated);
|
||||
|
||||
// Auto-update active tab selection if previous was removed
|
||||
const active = this.activeChartTopic();
|
||||
if (!updated.includes(active)) {
|
||||
this.activeChartTopic.set(updated[0] || '');
|
||||
}
|
||||
}
|
||||
|
||||
// Add comparison institution
|
||||
addInstitution(id: string, name: string) {
|
||||
const current = this.comparisonInstitutions();
|
||||
if (!current.some((i) => i.id === id) && id !== this.primaryInstitution()) {
|
||||
this.comparisonInstitutions.set([...current, { id, name }]);
|
||||
}
|
||||
this.searchQuery.set('');
|
||||
this.searchResults.set([]);
|
||||
}
|
||||
|
||||
// Remove comparison institution (data reuses instantly via computed)
|
||||
removeInstitution(id: string) {
|
||||
const current = this.comparisonInstitutions();
|
||||
this.comparisonInstitutions.set(current.filter((i) => i.id !== id));
|
||||
}
|
||||
|
||||
// Add country or region
|
||||
addLocation(loc: AutocompleteLocationItem) {
|
||||
if (loc.type === 'country') {
|
||||
const current = this.comparisonCountries();
|
||||
if (!current.some((c) => c.id === loc.id)) {
|
||||
this.comparisonCountries.set([
|
||||
...current,
|
||||
{ id: loc.id, name: loc.display_name },
|
||||
]);
|
||||
}
|
||||
} else if (loc.type === 'continent') {
|
||||
const current = this.comparisonRegions();
|
||||
if (!current.some((r) => r.id === loc.id)) {
|
||||
this.comparisonRegions.set([
|
||||
...current,
|
||||
{ id: loc.id, name: loc.display_name },
|
||||
]);
|
||||
}
|
||||
}
|
||||
this.locationQuery.set('');
|
||||
this.locationSearchResults.set([]);
|
||||
this.loadTrendSuggestions();
|
||||
}
|
||||
|
||||
// Remove country or region
|
||||
removeLocation(id: string, type: 'country' | 'continent') {
|
||||
if (type === 'country') {
|
||||
const current = this.comparisonCountries();
|
||||
this.comparisonCountries.set(current.filter((c) => c.id !== id));
|
||||
} else if (type === 'continent') {
|
||||
const current = this.comparisonRegions();
|
||||
this.comparisonRegions.set(current.filter((r) => r.id !== id));
|
||||
}
|
||||
this.loadTrendSuggestions();
|
||||
}
|
||||
|
||||
// Scaling mode toggle (data reuses instantly)
|
||||
setChartMode(mode: 'normalized' | 'total') {
|
||||
this.chartMode.set(mode);
|
||||
this.updateUrlParams();
|
||||
}
|
||||
|
||||
// Topic tab toggle (data reuses instantly)
|
||||
updateActiveChartTopic(topic: string) {
|
||||
this.activeChartTopic.set(topic);
|
||||
this.updateUrlParams();
|
||||
}
|
||||
|
||||
// Autocomplete query changes for institutions
|
||||
onSearchChange(query: string) {
|
||||
this.searchQuery.set(query);
|
||||
if (query.trim().length >= 3) {
|
||||
this.institutionsApiService
|
||||
.autocompleteInstitutions(query, undefined, 'education', 10)
|
||||
.subscribe({
|
||||
next: (res) => {
|
||||
this.searchResults.set(res.results || []);
|
||||
},
|
||||
error: () => {
|
||||
this.searchResults.set([]);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.searchResults.set([]);
|
||||
}
|
||||
}
|
||||
|
||||
// Autocomplete query changes for locations
|
||||
onLocationSearchChange(query: string) {
|
||||
this.locationQuery.set(query);
|
||||
if (query.trim().length >= 2) {
|
||||
this.apiService
|
||||
.getAutocompleteLocationsApiInseServiceUniversityRankingsAutocompleteLocationsGet(
|
||||
query,
|
||||
)
|
||||
.subscribe({
|
||||
next: (res) => {
|
||||
this.locationSearchResults.set(res || []);
|
||||
},
|
||||
error: () => {
|
||||
this.locationSearchResults.set([]);
|
||||
},
|
||||
});
|
||||
} else {
|
||||
this.locationSearchResults.set([]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user