fix typo
[web/firmware-selector-openwrt-org.git] / index.js
index 894f1a12ea562e14d35a531db68022682dd043cc..fab5b878d14666fd2d64b08c295d831e89cbdfff 100644 (file)
--- a/index.js
+++ b/index.js
@@ -13,26 +13,34 @@ function hide(id) {
   $(id).style.display = 'none';
 }
 
+function split(str) {
+  return str.match(/[^\s,]+/g) || [];
+}
+
+function get_model_titles(titles) {
+  return titles.map(e => {
+    if (e.title) {
+      return e.title;
+    } else {
+      return ((e.vendor || '') + ' ' + (e.model || '') + ' ' + (e.variant || '')).trim();
+    }
+  }).join(' / ');
+}
+
+function get_version_url(version) {
+  if (version == 'SNAPSHOT') {
+    return config.base_url + '/snapshots/targets/{target}'
+  } else {
+    return config.base_url + '/releases/{version}/targets/{target}'.replace('{version}', version)
+  }
+}
+
 function build_asa_request() {
   if (!current_model || !current_model.id) {
     alert('bad profile');
     return;
   }
 
-  function split(str) {
-    return str.match(/[^\s,]+/g) || [];
-  }
-
-  function get_model_titles(titles) {
-    return titles.map(e => {
-      if (e.title) {
-        return e.title;
-      } else {
-        return ((e.vendor || '') + ' ' + (e.model || '') + ' ' + (e.variant || '')).trim();
-      }
-    }).join('/');
-  }
-
   function showStatus(text) {
     show('buildstatus');
     $('buildstatus').innerHTML = text;
@@ -132,9 +140,7 @@ function translate() {
   }
 }
 
-function setupAutocompleteList(input, items, onselection) {
-  // the setupAutocompleteList function takes two arguments,
-  // the text field element and an array of possible autocompleted values:
+function setupAutocompleteList(input, items, as_list, onbegin, onend) {
   var currentFocus = -1;
 
   // sort numbers and other characters separately
@@ -142,12 +148,20 @@ function setupAutocompleteList(input, items, onselection) {
 
   items.sort(collator.compare);
 
-  // execute a function when someone writes in the text field:
   input.oninput = function(e) {
-    // clear images
-    updateImages();
+    onbegin();
 
+    var offset = 0;
     var value = this.value;
+    var value_list = [];
+
+    if (as_list) {
+      // automcomplete last text item
+      offset = this.value.lastIndexOf(' ') + 1;
+      value = this.value.substr(offset);
+      value_list = split(this.value.substr(0, offset));
+    }
+
     // close any already open lists of autocompleted values
     closeAllLists();
 
@@ -162,7 +176,6 @@ function setupAutocompleteList(input, items, onselection) {
     // append the DIV element as a child of the autocomplete container:
     this.parentNode.appendChild(list);
 
-    // for each item in the array...
     var c = 0;
     for (var i = 0; i < items.length; i += 1) {
       var item = items[i];
@@ -173,6 +186,11 @@ function setupAutocompleteList(input, items, onselection) {
         continue;
       }
 
+      // do not offer a duplicate item
+      if (as_list && value_list.indexOf(item) != -1) {
+        continue;
+      }
+
       c += 1;
       if (c >= 15) {
         var div = document.createElement('DIV');
@@ -188,13 +206,16 @@ function setupAutocompleteList(input, items, onselection) {
           + '<input type="hidden" value="' + item + '">';
 
         div.addEventListener('click', function(e) {
-          // set text field to selected value
-          input.value = this.getElementsByTagName('input')[0].value;
+          // include selected value
+          var selected = this.getElementsByTagName('input')[0].value;
+          if (as_list) {
+            input.value = value_list.join(' ') + ' ' + selected;
+          } else {
+            input.value = selected;
+          }
           // close the list of autocompleted values,
-          // (or any other open lists of autocompleted values:
           closeAllLists();
-          // callback
-          onselection(input.value);
+          onend(input);
         });
 
         list.appendChild(div);
@@ -226,7 +247,12 @@ function setupAutocompleteList(input, items, onselection) {
   };
 
   input.onfocus = function() {
-    onselection(input.value);
+    onend(input);
+  }
+
+  // focus lost
+  input.onblur = function() {
+    onend(input);
   }
 
   function setActive(x) {
@@ -259,6 +285,21 @@ function setupAutocompleteList(input, items, onselection) {
   });
 }
 
+// for attended sysupgrade
+function updatePackageList(target) {
+  // set available packages
+  fetch(config.asu_url + '/' + target + '/packages.json')
+  .then(response => response.json())
+  .then(all_packages => {
+    setupAutocompleteList($('packages'), all_packages, true, _ => {}, textarea => {
+      textarea.value = split(textarea.value)
+        .filter((value, index, self) => self.indexOf(value) === index) // make list unique
+        //.filter((value, index) => all_packages.indexOf(value) !== -1) // limit to available packages
+        .join(' ');
+    });
+  });
+}
+
 function updateImages(version, code, date, model, url, mobj, is_custom) {
   // add download button for image
   function addLink(type, file) {
@@ -341,6 +382,10 @@ function updateImages(version, code, date, model, url, mobj, is_custom) {
       addLink(images[i].type, images[i].name);
     }
 
+    if (config.asu_url) {
+      updatePackageList(target);
+    }
+
     show('images');
   } else {
     hide('images');
@@ -351,11 +396,23 @@ function init() {
   setupSelectList($('versions'), Object.keys(config.versions), version => {
     fetch(config.versions[version]).then(data => {
       data.json().then(obj => {
-        setupAutocompleteList($('models'), Object.keys(obj['models']), model => {
+        // handle native openwrt json format
+        if ('profiles' in obj) {
+          obj['url'] = get_version_url(version)
+          obj['models'] = {}
+          for (const [key, value] of Object.entries(obj['profiles'])) {
+            obj['models'][get_model_titles(value.titles)] = value
+            obj['models'][get_model_titles(value.titles)]['id'] = key
+          }
+        }
+        return obj 
+      }).then(obj => {
+        setupAutocompleteList($('models'), Object.keys(obj['models']), false, updateImages, models => {
+          var model = models.value;
           if (model in obj['models']) {
             var url = obj.url || 'unknown';
             var code = obj.version_code || 'unknown';
-            var date = obj.build_data || 'unknown';
+            var date = obj.build_date || 'unknown';
             var mobj = obj['models'][model];
             updateImages(version, code, date, model, url, mobj, false);
             current_model = mobj;