2 var current_model
= {};
5 return document
.getElementById(id
);
9 $(id
).style
.display
= 'block';
13 $(id
).style
.display
= 'none';
17 return str
.match(/[^\s,]+/g) || [];
20 function get_model_titles(titles
) {
21 return titles
.map(e
=> {
25 return ((e
.vendor
|| '') + ' ' + (e
.model
|| '') + ' ' + (e
.variant
|| '')).trim();
30 function build_asa_request() {
31 if (!current_model
|| !current_model
.id
) {
36 function showStatus(text
) {
38 $('buildstatus').innerHTML
= text
;
42 function handleError(response
) {
47 var message
= mobj
['message'] || '<span class="tr-build-failed"></span>';
48 if (mobj
.buildlog
== true) {
49 var url
= config
.asu_url
+ '/store/' + mobj
.bin_dir
+ '/buildlog.txt';
50 showStatus('<a href="' + url
+ '">' + message
+ '</a>');
61 showStatus('<span class="tr-request-image"></span>');
64 'target': current_model
.target
,
65 'profile': current_model
.id
,
66 'packages': split($('packages').value
),
67 'version': $('versions').value
70 fetch(config
.asu_url
+ '/api/build', {
72 headers
: { 'Content-Type': 'application/json' },
73 body
: JSON
.stringify(request_data
)
76 switch (response
.status
) {
79 showStatus('<span class="tr-build-successful"></span>');
83 var download_url
= config
.asu_url
+ '/store/' + mobj
.bin_dir
;
88 get_model_titles(mobj
.titles
),
89 download_url
, mobj
, true
94 showStatus('<span class="tr-check-again"></span>');
95 setTimeout(_
=> { build_asa_request() }, 5000);
97 case 400: // bad request
98 case 422: // bad package
99 case 500: // build failed
100 handleError(response
);
105 hide('buildspinner');
110 function setupSelectList(select
, items
, onselection
) {
111 for (var i
= 0; i
< items
.length
; i
+= 1) {
112 var option
= document
.createElement('OPTION');
113 option
.innerHTML
= items
[i
];
114 select
.appendChild(option
);
117 select
.addEventListener('change', e
=> {
118 onselection(items
[select
.selectedIndex
]);
121 if (select
.selectedIndex
>= 0) {
122 onselection(items
[select
.selectedIndex
]);
126 // Change the translation of the entire document
127 function translate() {
128 var mapping
= translations
[config
.language
];
129 for (var tr
in mapping
) {
130 Array
.from(document
.getElementsByClassName(tr
))
131 .forEach(e
=> { e
.innerText
= mapping
[tr
]; })
135 function setupAutocompleteList(input
, items
, as_list
, onbegin
, onend
) {
136 var currentFocus
= -1;
138 // sort numbers and other characters separately
139 var collator
= new Intl
.Collator(undefined, {numeric
: true, sensitivity
: 'base'});
141 items
.sort(collator
.compare
);
143 input
.oninput = function(e
) {
147 var value
= this.value
;
151 // automcomplete last text item
152 offset
= this.value
.lastIndexOf(' ') + 1;
153 value
= this.value
.substr(offset
);
154 value_list
= split(this.value
.substr(0, offset
));
157 // close any already open lists of autocompleted values
164 // create a DIV element that will contain the items (values):
165 var list
= document
.createElement('DIV');
166 list
.setAttribute('id', this.id
+ '-autocomplete-list');
167 list
.setAttribute('class', 'autocomplete-items');
168 // append the DIV element as a child of the autocomplete container:
169 this.parentNode
.appendChild(list
);
172 for (var i
= 0; i
< items
.length
; i
+= 1) {
176 var j
= item
.toUpperCase().indexOf(value
.toUpperCase());
181 // do not offer a duplicate item
182 if (as_list
&& value_list
.indexOf(item
) != -1) {
188 var div
= document
.createElement('DIV');
189 div
.innerHTML
= '...';
190 list
.appendChild(div
);
193 var div
= document
.createElement('DIV');
194 // make the matching letters bold:
195 div
.innerHTML
= item
.substr(0, j
)
196 + '<strong>' + item
.substr(j
, value
.length
) + '</strong>'
197 + item
.substr(j
+ value
.length
)
198 + '<input type="hidden" value="' + item
+ '">';
200 div
.addEventListener('click', function(e
) {
201 // include selected value
202 var selected
= this.getElementsByTagName('input')[0].value
;
204 input
.value
= value_list
.join(' ') + ' ' + selected
;
206 input
.value
= selected
;
208 // close the list of autocompleted values,
213 list
.appendChild(div
);
218 input
.onkeydown = function(e
) {
219 var x
= document
.getElementById(this.id
+ '-autocomplete-list');
220 if (x
) x
= x
.getElementsByTagName('div');
221 if (e
.keyCode
== 40) {
224 // and and make the current item more visible:
226 } else if (e
.keyCode
== 38) {
229 // and and make the current item more visible:
231 } else if (e
.keyCode
== 13) {
232 // If the ENTER key is pressed, prevent the form from being submitted,
234 if (currentFocus
> -1) {
235 // and simulate a click on the 'active' item:
236 if (x
) x
[currentFocus
].click();
241 input
.onfocus = function() {
246 input
.onblur = function() {
250 function setActive(x
) {
251 // a function to classify an item as 'active':
252 if (!x
) return false;
253 // start by removing the 'active' class on all items:
254 for (var i
= 0; i
< x
.length
; i
++) {
255 x
[i
].classList
.remove('autocomplete-active');
257 if (currentFocus
>= x
.length
) currentFocus
= 0;
258 if (currentFocus
< 0) currentFocus
= (x
.length
- 1);
259 // add class 'autocomplete-active':
260 x
[currentFocus
].classList
.add('autocomplete-active');
263 function closeAllLists(elmnt
) {
264 // close all autocomplete lists in the document,
265 // except the one passed as an argument:
266 var x
= document
.getElementsByClassName('autocomplete-items');
267 for (var i
= 0; i
< x
.length
; i
++) {
268 if (elmnt
!= x
[i
] && elmnt
!= input
) {
269 x
[i
].parentNode
.removeChild(x
[i
]);
274 // execute a function when someone clicks in the document:
275 document
.addEventListener('click', e
=> {
276 closeAllLists(e
.target
);
280 // for attended sysupgrade
281 function updatePackageList(version
, target
) {
282 // set available packages
283 fetch(config
.versions
[version
] + '/' + target
+ '/index.json')
284 .then(response
=> response
.json())
285 .then(all_packages
=> {
286 setupAutocompleteList($('packages'), all_packages
, true, _
=> {}, textarea
=> {
287 textarea
.value
= split(textarea
.value
)
288 // make list unique, ignore minus
289 .filter((value
, index
, self
) => {
290 var i
= self
.indexOf(value
.replace(/^\-/, ''));
291 return (i
=== index
) || (i
< 0);
293 // limit to available packages, ignore minus
294 .filter((value
, index
) => all_packages
.indexOf(value
.replace(/^\-/, '')) !== -1)
300 function updateImages(version
, code
, date
, model
, url
, mobj
, is_custom
) {
301 // add download button for image
302 function addLink(type
, file
) {
303 var a
= document
.createElement('A');
304 a
.classList
.add('download-link');
306 .replace('{target}', mobj
.target
)
307 .replace('{version}', version
)
309 var span
= document
.createElement('SPAN');
310 span
.appendChild(document
.createTextNode(''));
312 a
.appendChild(document
.createTextNode(type
.toUpperCase()));
314 if (config
.showHelp
) {
315 a
.onmouseover = function() {
316 // hide all help texts
317 Array
.from(document
.getElementsByClassName('download-help'))
318 .forEach(e
=> e
.style
.display
= 'none');
319 var lc
= type
.toLowerCase();
320 if (lc
.includes('sysupgrade')) {
321 show('sysupgrade-help');
322 } else if (lc
.includes('factory') || lc
== 'trx' || lc
== 'chk') {
323 show('factory-help');
324 } else if (lc
.includes('kernel') || lc
.includes('zimage') || lc
.includes('uimage')) {
326 } else if (lc
.includes('root')) {
328 } else if (lc
.includes('sdcard')) {
330 } else if (lc
.includes('tftp')) {
338 $('download-links').appendChild(a
);
341 function switchClass(id
, from_class
, to_class
) {
342 $(id
).classList
.remove(from_class
);
343 $(id
).classList
.add(to_class
);
346 // remove all download links
347 Array
.from(document
.getElementsByClassName('download-link'))
348 .forEach(e
=> e
.remove());
350 // hide all help texts
351 Array
.from(document
.getElementsByClassName('download-help'))
352 .forEach(e
=> e
.style
.display
= 'none');
354 if (version
&& code
&& date
&& model
&& url
&& mobj
) {
355 var target
= mobj
.target
;
356 var images
= mobj
.images
;
358 // change between "version" and "custom" title
360 switchClass('images-title', 'tr-version-build', 'tr-custom-build');
361 switchClass('downloads-title', 'tr-version-downloads', 'tr-custom-downloads');
363 switchClass('images-title', 'tr-custom-build', 'tr-version-build');
364 switchClass('downloads-title', 'tr-custom-downloads', 'tr-version-downloads');
366 // update title translation
369 // fill out build info
370 $('image-model').innerText
= model
;
371 $('image-target').innerText
= target
;
372 $('image-version').innerText
= version
;
373 $('image-code').innerText
= code
;
374 $('image-date').innerText
= date
;
376 images
.sort((a
, b
) => a
.name
.localeCompare(b
.name
));
378 for (var i
in images
) {
379 addLink(images
[i
].type
, images
[i
].name
);
382 if (config
.asu_url
) {
383 updatePackageList(version
, target
);
393 var build_date
= "unknown"
394 setupSelectList($('versions'), Object
.keys(config
.versions
), version
=> {
395 fetch(config
.versions
[version
] + '/profiles.json')
397 build_date
= obj
.headers
.get('last-modified');
401 // handle native openwrt json format
402 if ('profiles' in obj
) {
404 for (const [key
, value
] of Object
.entries(obj
['profiles'])) {
405 obj
['models'][get_model_titles(value
.titles
)] = value
406 obj
['models'][get_model_titles(value
.titles
)]['id'] = key
412 setupAutocompleteList($('models'), Object
.keys(obj
['models']), false, updateImages
, models
=> {
413 var model
= models
.value
;
414 if (model
in obj
['models']) {
415 var url
= obj
.url
|| 'unknown';
416 var code
= obj
.version_code
|| 'unknown';
417 var mobj
= obj
['models'][model
];
418 updateImages(version
, code
, build_date
, model
, url
, mobj
, false);
419 current_model
= mobj
;
426 // trigger model update when selected version changes
427 $('models').onfocus();
431 if (config
.asu_url
) {
438 var user_lang
= (navigator
.language
|| navigator
.userLanguage
).split('-')[0];
439 if (user_lang
in translations
) {
440 config
.language
= user_lang
;
441 $('language-selection').value
= user_lang
;
446 $('language-selection').onclick = function() {
447 config
.language
= this.children
[this.selectedIndex
].value
;