2 var current_model
= {};
3 var current_language
= config
.language
;
6 return document
.getElementById(id
);
10 $(id
).style
.display
= 'block';
14 $(id
).style
.display
= 'none';
17 function build_asa_request() {
18 if (!current_model
|| !current_model
.id
) {
24 return str
.match(/[^\s,]+/g) || [];
27 function get_model_titles(titles
) {
28 return titles
.map(e
=> {
32 return ((e
.vendor
|| '') + (e
.model
|| '') + (e
.variant
|| '')).trim();
43 'profile': current_model
.id
,
44 'packages': split($('packages').value
),
45 'version': $('versions').value
48 console
.log('disable request button / show loading spinner')
50 fetch(config
.asu_url
+ '/api/build', {
52 headers
: { 'Content-Type': 'application/json' },
53 body
: JSON
.stringify(request_data
)
56 switch (response
.status
) {
60 console
.log('image found');
64 var download_url
= config
.asu_url
+ '/store/' + mobj
.bin_dir
68 get_model_titles(mobj
.titles
),
69 download_url
, mobj
, true
74 // show some spinning animation
75 console
.log('check again in 5 seconds');
76 setTimeout(_
=> { build_asa_request() }, 5000);
78 case 400: // bad request
79 case 422: // bad package
80 case 500: // build failed
82 console
.log('error (' + response
.status
+ ')');
85 if (mobj
.buildlog
== true) {
86 $('buildlog').href
= config
.asu_url
+ '/store/' + mobj
.bin_dir
+ '/buildlog.txt';
96 function loadFile(url
, callback
) {
97 var xmlhttp
= new XMLHttpRequest();
98 xmlhttp
.onreadystatechange = function() {
99 if (xmlhttp
.readyState
== 4 && xmlhttp
.status
== 200) {
100 callback(JSON
.parse(xmlhttp
.responseText
), url
);
103 xmlhttp
.open('GET', url
, true);
107 function setupSelectList(select
, items
, onselection
) {
108 for (var i
= 0; i
< items
.length
; i
+= 1) {
109 var option
= document
.createElement('OPTION');
110 option
.innerHTML
= items
[i
];
111 select
.appendChild(option
);
114 select
.addEventListener('change', e
=> {
115 onselection(items
[select
.selectedIndex
]);
118 if (select
.selectedIndex
>= 0) {
119 onselection(items
[select
.selectedIndex
]);
123 // Change the translation of the entire document
124 function applyLanguage(language
) {
126 current_language
= language
;
129 var mapping
= translations
[current_language
];
131 for (var tr
in mapping
) {
132 Array
.from(document
.getElementsByClassName(tr
))
133 .forEach(e
=> { e
.innerText
= mapping
[tr
]; })
138 function setupAutocompleteList(input
, items
, onselection
) {
139 // the setupAutocompleteList function takes two arguments,
140 // the text field element and an array of possible autocompleted values:
141 var currentFocus
= -1;
143 // sort numbers and other characters separately
144 var collator
= new Intl
.Collator(undefined, {numeric
: true, sensitivity
: 'base'});
146 items
.sort(collator
.compare
);
148 // execute a function when someone writes in the text field:
149 input
.oninput = function(e
) {
153 var value
= this.value
;
154 // close any already open lists of autocompleted values
161 // create a DIV element that will contain the items (values):
162 var list
= document
.createElement('DIV');
163 list
.setAttribute('id', this.id
+ '-autocomplete-list');
164 list
.setAttribute('class', 'autocomplete-items');
165 // append the DIV element as a child of the autocomplete container:
166 this.parentNode
.appendChild(list
);
168 // for each item in the array...
170 for (var i
= 0; i
< items
.length
; i
+= 1) {
174 var j
= item
.toUpperCase().indexOf(value
.toUpperCase());
181 var div
= document
.createElement('DIV');
182 div
.innerHTML
= '...';
183 list
.appendChild(div
);
186 var div
= document
.createElement('DIV');
187 // make the matching letters bold:
188 div
.innerHTML
= item
.substr(0, j
)
189 + '<strong>' + item
.substr(j
, value
.length
) + '</strong>'
190 + item
.substr(j
+ value
.length
)
191 + '<input type="hidden" value="' + item
+ '">';
193 div
.addEventListener('click', function(e
) {
194 // set text field to selected value
195 input
.value
= this.getElementsByTagName('input')[0].value
;
196 // close the list of autocompleted values,
197 // (or any other open lists of autocompleted values:
200 onselection(input
.value
);
203 list
.appendChild(div
);
208 input
.onkeydown = function(e
) {
209 var x
= document
.getElementById(this.id
+ '-autocomplete-list');
210 if (x
) x
= x
.getElementsByTagName('div');
211 if (e
.keyCode
== 40) {
214 // and and make the current item more visible:
216 } else if (e
.keyCode
== 38) {
219 // and and make the current item more visible:
221 } else if (e
.keyCode
== 13) {
222 // If the ENTER key is pressed, prevent the form from being submitted,
224 if (currentFocus
> -1) {
225 // and simulate a click on the 'active' item:
226 if (x
) x
[currentFocus
].click();
231 input
.onfocus = function() {
232 onselection(input
.value
);
235 function setActive(x
) {
236 // a function to classify an item as 'active':
237 if (!x
) return false;
238 // start by removing the 'active' class on all items:
239 for (var i
= 0; i
< x
.length
; i
++) {
240 x
[i
].classList
.remove('autocomplete-active');
242 if (currentFocus
>= x
.length
) currentFocus
= 0;
243 if (currentFocus
< 0) currentFocus
= (x
.length
- 1);
244 // add class 'autocomplete-active':
245 x
[currentFocus
].classList
.add('autocomplete-active');
248 function closeAllLists(elmnt
) {
249 // close all autocomplete lists in the document,
250 // except the one passed as an argument:
251 var x
= document
.getElementsByClassName('autocomplete-items');
252 for (var i
= 0; i
< x
.length
; i
++) {
253 if (elmnt
!= x
[i
] && elmnt
!= input
) {
254 x
[i
].parentNode
.removeChild(x
[i
]);
259 // execute a function when someone clicks in the document:
260 document
.addEventListener('click', e
=> {
261 closeAllLists(e
.target
);
265 function updateImages(version
, code
, model
, url
, mobj
, is_custom
) {
267 // add download button for image
268 function addLink(type
, file
) {
269 var a
= document
.createElement('A');
270 a
.classList
.add('download-link');
272 .replace('{target}', mobj
.target
)
273 .replace('{version}', version
)
275 var span
= document
.createElement('SPAN');
276 span
.appendChild(document
.createTextNode(''));
278 a
.appendChild(document
.createTextNode(type
.toUpperCase()));
280 if (config
.showHelp
) {
281 a
.onmouseover = function() {
282 // hide all help texts
283 Array
.from(document
.getElementsByClassName('download-help'))
284 .forEach(e
=> e
.style
.display
= 'none');
285 var lc
= type
.toLowerCase();
286 if (lc
.includes('sysupgrade')) {
287 show('sysupgrade-help');
288 } else if (lc
.includes('factory') || lc
== 'trx' || lc
== 'chk') {
289 show('factory-help');
290 } else if (lc
.includes('kernel') || lc
.includes('zimage') || lc
.includes('uimage')) {
292 } else if (lc
.includes('root')) {
294 } else if (lc
.includes('sdcard')) {
296 } else if (lc
.includes('tftp')) {
304 $('download-links').appendChild(a
);
307 function switchClass(id
, from_class
, to_class
) {
308 $(id
).classList
.remove(from_class
);
309 $(id
).classList
.add(to_class
);
312 // remove all download links
313 Array
.from(document
.getElementsByClassName('download-link'))
314 .forEach(e
=> e
.remove());
316 // hide all help texts
317 Array
.from(document
.getElementsByClassName('download-help'))
318 .forEach(e
=> e
.style
.display
= 'none');
320 if (version
&& code
&& model
&& url
&& mobj
) {
321 var target
= mobj
.target
;
322 var images
= mobj
.images
;
324 // change between "version" and "custom" title
326 switchClass('images-title', 'tr-version-build', 'tr-custom-build');
327 switchClass('downloads-title', 'tr-version-downloads', 'tr-custom-downloads');
329 switchClass('images-title', 'tr-custom-build', 'tr-version-build');
330 switchClass('downloads-title', 'tr-custom-downloads', 'tr-version-downloads');
332 // update title translation
335 // fill out build info
336 $('image-model').innerText
= model
;
337 $('image-target').innerText
= target
;
338 $('image-version').innerText
= version
;
339 $('image-code').innerText
= code
;
341 images
.sort((a
, b
) => a
.name
.localeCompare(b
.name
));
343 for (var i
in images
) {
344 addLink(images
[i
].type
, images
[i
].name
);
353 setupSelectList($('versions'), Object
.keys(config
.versions
), version
=> {
354 loadFile(config
.versions
[version
], obj
=> {
355 setupAutocompleteList($('models'), Object
.keys(obj
['models']), model
=> {
356 if (model
in obj
['models']) {
358 var code
= obj
.version_code
;
359 var mobj
= obj
['models'][model
];
360 updateImages(version
, code
, model
, url
, mobj
, false);
361 current_model
= mobj
;
368 // trigger model update when selected version changes
369 $('models').onfocus();
373 if (config
.asu_url
) {
379 applyLanguage(config
.language
);