-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsol-menu.js
230 lines (223 loc) · 7.4 KB
/
sol-menu.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {getDefaults,rel2absIRI} from './utils/utils.js';
import {getSingletonStore,fetchRdfData,filterRdf} from './utils/rdf-utils.js';
import {menuCSS} from './utils/view-menu-template.js';
import {showLink} from './utils/view.js';
export class SolMenu extends HTMLElement {
constructor() {
super();
}
async connectedCallback(){
await getDefaults(this);
await getSingletonStore();
// let data = await getMenuDataFromRdf(this.source);
let data = await this.getMenuDataFromElement(this);
await this.createMenu(data,this);
let first = this.querySelector('.item');
first.click();
}
async getMenuDataFromElement(el){
let items = el.childNodes;
const data = [];
for(let item of items){
if(!item.tagName) continue;
let label = item.getAttribute('label')
let link = rel2absIRI( item.getAttribute('source')||"" );
if(item.tagName=='SUBMENU'){
let source = item.getAttribute('source') || el.getAttribute('source');
source = rel2absIRI( source );
source ||= el.source;
let wanted = item.getAttribute('wanted');
let linkType = item.getAttribute('linkType');
let fromRdf = await fetchRdfData({source,wanted},'raw');
// fromRdf = filterRdf(fromRdf,item)
let parts = [];
for(let row of fromRdf){
parts.push({label:(row.label||"").trim(),link:(row.recalls||"").trim(),linkType});
}
data.push({label,parts});
}
else if(item.tagName=='SUBMENU-MANUAL'){
let components = item.childNodes;
let iparts = [];
for(let component of components){
if(!component.tagName) continue;
let ilabel = component.getAttribute('label');
iparts.push({label:ilabel,link:component.getAttribute('source')});
}
data.push({label,parts:iparts});
}
else {
let linkType = item.getAttribute('linkType');
let wanted = item.getAttribute('wanted');
let limit = item.getAttribute('limit');
let view = item.getAttribute('view');
let count = item.getAttribute('count');
data.push({label,link,linkType,wanted,limit,view,count});
}
}
return data;
}
async getMenuDataFromRdf(uri){
const data = [];
let node = UI.rdf.sym(uri);
await UI.store.fetcher.load(node);
let parts = UI.store.match(node,UI.ns.ui('parts'));
if(parts) {
parts=parts[0].object.elements;
for(let part of parts){
let row = {};
let link = (UI.store.any(part,UI.ns.ui('link'))||{}).value;
let label = (UI.store.any(part,UI.ns.ui('label'))||{}).value;
if(link) row.link = link;
if(label) row.label = label;
let submenu = UI.store.any(part,UI.ns.ui('parts'));
if(submenu){
submenu=submenu.elements;
row.parts = [];
for(let sub of submenu){
let subRow = {};
let slink = (UI.store.any(sub,UI.ns.ui('link'))||{}).value;
let slabel = (UI.store.any(sub,UI.ns.ui('label'))||{}).value;
if(slink) subRow.link = slink;
if(slabel) subRow.label = slabel;
row.parts.push(subRow);
}
}
data.push(row);
}
}
else alert(`No menu items found for '${uri}'!`);
return data;
}
async createVerticalMenu( data, element ){
let doc = element.ownerDocument;
let win = doc.defaultView;
let topUL = doc.createElement('UL')
element.classList.add('sol-vertical-menu');
for(var row of data){
topUL.appendChild( await this.renderVerticalMenuItem(row,doc) )
}
element.appendChild(topUL);
}
async renderVerticalMenuItem(row,doc){
const li = doc.createElement('LI')
const span = doc.createElement('SPAN')
span.innerHTML = row.label
span.classList.add('unclickable-item');
li.appendChild(span)
li.style.cursor="pointer"
li.style.padding = "0.5em";
if(!row.parts){
li.classList.add('item');
li.name = row.link;
const self = this
li.addEventListener('click',(event,data)=>{
event.preventDefault();
data||=row;
self.showMenuLink(event,data,element);
});
}
else {
li.classList.add('caret')
let ul2 = document.createElement('UL')
li.appendChild(ul2)
ul2.classList.add('nested')
ul2.setAttribute('linkType',row.component)
for(var m in row.parts){
let newItem = row.parts[m]
ul2.appendChild( await this.renderVerticalMenuItem(newItem,doc) )
}
}
return li
}
async createMenu( data, element ){
let doc = element.ownerDocument;
let win = doc.defaultView;
let direction = element.getAttribute('direction');
direction = element.getAttribute('direction') || 'horizontal';
if(direction=="vertical") return createVerticalMenu(data,element);
let topUL = doc.createElement('UL')
element.classList.add('sol-dropdown-menu');
for(var row of data){
topUL.appendChild( await this.renderMenuItem(row,doc,element) )
}
element.appendChild(topUL);
let style = doc.createElement('style');
style.innerHTML = menuCSS;
document.head.appendChild(style);
}
async renderMenuItem(row,doc,element){
const li = doc.createElement('LI')
const span = doc.createElement('SPAN')
span.innerHTML = row.label;
if(row.count){
span.innerHTML += ` (${row.count})`;
}
li.appendChild(span)
li.style.cursor="pointer"
li.style.display = "inline-block";
li.style.padding = "0.5em";
if(!row.parts){
/*
if(!row.link){
li.classList.add('unclickable-item');
return li;
}
*/
li.classList.add('item');
li.name = row.link;
const self = this
li.addEventListener('click',(event,data)=>{
event.preventDefault();
data||=row;
for(let link of element.querySelectorAll('li')){link.classList.remove('selected')}
li.classList.add('selected');
self.showMenuLink(event,data,element);
});
}
else {
li.classList.add('caret')
let ul2 = document.createElement('UL')
li.appendChild(ul2)
ul2.classList.add('nested')
ul2.setAttribute('linkType',row.component)
for(var m in row.parts){
let newItem = row.parts[m]
ul2.appendChild( await this.renderMenuItem(newItem,doc,element) )
}
}
return li
}
showMenuLink = (event,data,element)=>{
let clicked = event.target;
let defaults=document.body.querySelector('sol-defaults') || {};
let menu = event.target.closest('.sol-menu');
let label = data.label;
let wanted = data.wanted || "";
let limit = data.limit || "";
let type = data.type || element.getAttribute('type') ;
let view = data.view || element.getAttribute('view') ;
let tabset = clicked.closest('.sol-tabset');
if(!type && tabset) type = tabset.getAttribute('type');
if(!view && tabset) view = tabset.getAttribute('view');
let compact = data.compact || "";
let source = data.link;
let currentUrl = window.origin + window.location.pathname;
if(source == currentUrl || source+'index.html'==currentUrl) source = element.source;
let linkType = data.linkType || element.getAttribute('linkType') || type || 'component';
/*
*/
element.linkContent = `
<sol-${linkType}
source = "${source}"
wanted = "${wanted}"
${view ?'view = '+view :""}
${limit ?'limit = '+limit :""}
label = "${label}"
compact = "${compact}"
/></sol-${linkType}>
`;
showLink(clicked,element);
}
}
customElements.define("sol-menu",SolMenu);