-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoptimade_zero.view.ts
More file actions
168 lines (142 loc) · 5.64 KB
/
optimade_zero.view.ts
File metadata and controls
168 lines (142 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace $.$$ {
// Интерфейс конфигурации для каждого элемента
interface ItemRendererConfig {
// Функция для получения значения поля записи
valueGetter: (obj: $optimade_zero_entry) => string;
// Фабрика компонента, который отображает поле
component: (obj: $optimade_zero_entry) => any;
// Значение, которое считается пустым (не отображать компонент)
emptyValue?: string;
}
export class $optimade_zero extends $.$optimade_zero {
@ $mol_mem
search_params( next?: $optimade_zero_search_params ): $optimade_zero_search_params {
return this.$.$mol_state_arg.dict( next ) ?? {}
}
search_page_body() {
if( this.search_error() ) {
return [
this.Search_input(),
this.Search_error(),
]
}
// Используем вспомогательную функцию для формирования массива компонентов
return [
this.Search_input(),
... ( this.Search().arity().length > 0 ? [ this.Arity() ] : [] ),
this.Refinements(),
... ( this.Search().results().length === 0 ? [ this.Search_nothing_found() ] : [] ),
this.Search_results(),
]
}
search_results() {
if( !this.Search().params_labels().length ) return []
return this.Search().results().map( obj => this.Item( obj ) )
}
// Массив конфигураций для рендеринга полей
private itemRendererConfigs: ItemRendererConfig[] = [
{ valueGetter: o => o.id(), component: o => this.Id(o) },
{ valueGetter: o => o.bib_id(), component: o => this.Bib(o), emptyValue: '0' },
{ valueGetter: o => o.formula_html(), component: o => this.Formula(o) },
{ valueGetter: o => o.property(), component: o => this.Property(o) },
{ valueGetter: o => o.ref_link(), component: o => this.Ref(o), emptyValue: '' },
{ valueGetter: o => o.pdf_link(), component: o => this.Pdf(o), emptyValue: '' },
{ valueGetter: o => o.png_link(), component: o => this.Png(o), emptyValue: '' },
{ valueGetter: o => o.gif_link(), component: o => this.Gif(o), emptyValue: '' },
];
// Универсальный метод для рендеринга одного элемента
private renderItem(obj: $optimade_zero_entry, config: ItemRendererConfig) {
const value = config.valueGetter(obj);
// Если значение пустое (или равно заданному emptyValue), возвращаем null
if (!value || value === config.emptyValue) return null;
return config.component(obj);
}
// Метод для формирования строки записи на основе конфигураций
item_row(obj: $optimade_zero_entry) {
return this.itemRendererConfigs.map(cfg => this.renderItem(obj, cfg));
}
// Остальные методы оставляем для совместимости,
// хотя большинство делегируют работу методам из $optimade_zero_entry
item_id( obj: $optimade_zero_entry ) {
return obj.id()
}
item_thumbs( obj: $optimade_zero_entry ) {
return obj.thumbs_link()
}
item_html( obj: $optimade_zero_entry ) {
const safe_html=obj.formula_html().replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
return `<div>${ safe_html }</div>`
}
item_property( obj: $optimade_zero_entry ) {
return obj.property()
}
item_bib( obj: $optimade_zero_entry ) {
return `${ obj.bib_id() }'${ obj.year().toString().slice( -2 ) }`
}
item_ref( obj: $optimade_zero_entry ) {
return obj.ref_link()
}
item_pdf( obj: $optimade_zero_entry ) {
return obj.pdf_link()
}
item_png( obj: $optimade_zero_entry ) {
return obj.png_link()
}
item_gif( obj: $optimade_zero_entry ) {
return obj.gif_link()
}
arity_dict() {
return this.Search().arity().reduce( ( dict, name ) => {
dict[ name ] = name
return dict
}, {} as Record<string, string> )
}
@$mol_mem
refinements() {
// Порядок вывода для определённых facet'ов
const order: ( keyof $optimade_zero_search_params )[] = [
'elements',
'formulae',
'props',
'classes',
'lattices',
]
const refData = this.Search().refinements()
return order
.map( facet => refData[ facet ]?.length ? this.Refinement( facet ) : null )
.filter( Boolean )
}
refinement_title( facet: keyof $optimade_zero_search_params ) {
return this.Search().param_names()[ facet ]
}
refinement_content( facet: keyof $optimade_zero_search_params ) {
return this.Search().refinements()[ facet ]!.map( obj => this.Refinement_link( obj ) )
}
refinement_link_title( obj: typeof $optimade_zero_search_refinement_item.Value ) {
return `${ obj.value }`
}
@ $mol_mem_key
refinement_link_arg( obj: typeof $optimade_zero_search_refinement_item.Value ) {
// Создаём новый объект поиска, основанный на текущих параметрах,
// удаляем facet и добавляем новое значение для него
const newSearch = new this.$.$optimade_zero_search()
newSearch.params( this.search_params() )
newSearch.param_drop( obj.facet )
newSearch.param_add( obj.facet, obj.value )
return newSearch.params()
}
@$mol_mem
arity( next?: string ) {
if ( next !== undefined ) {
const reset = Object.values( this.Search().arity_names() )
reset.forEach( val => this.Search().param_drop( 'classes', val ) )
this.Search().param_add( 'classes', next )
}
return next ?? ''
}
}
}