// Local js definitions:
/* global getCurrentValue, getVirtualKey, updateLocalStorage, updateSystemTheme */

(function () {
    function changeSetting(settingName, value) {
        updateLocalStorage("rustdoc-" + settingName, value);

        switch (settingName) {
            case "preferred-dark-theme":
            case "preferred-light-theme":
            case "use-system-theme":
                updateSystemTheme();
                break;
        }
    }

    function handleKey(ev) {
        // Don't interfere with browser shortcuts
        if (ev.ctrlKey || ev.altKey || ev.metaKey) {
            return;
        }
        switch (getVirtualKey(ev)) {
            case "Enter":
            case "Return":
            case "Space":
                ev.target.checked = !ev.target.checked;
                ev.preventDefault();
                break;
        }
    }

    function setEvents() {
        var elems = {
            toggles: Array.prototype.slice.call(document.getElementsByClassName("slider")),
            selects: Array.prototype.slice.call(document.getElementsByClassName("select-wrapper")),
        };
        var i;

        if (elems.toggles && elems.toggles.length > 0) {
            for (i = 0; i < elems.toggles.length; ++i) {
                var toggle = elems.toggles[i].previousElementSibling;
                var settingId = toggle.id;
                var settingValue = getSettingValue(settingId);
                if (settingValue !== null) {
                    toggle.checked = settingValue === "true";
                }
                toggle.onchange = function() {
                    changeSetting(this.id, this.checked);
                };
                toggle.onkeyup = handleKey;
                toggle.onkeyrelease = handleKey;
            }
        }

        if (elems.selects && elems.selects.length > 0) {
            for (i = 0; i < elems.selects.length; ++i) {
                var select = elems.selects[i].getElementsByTagName("select")[0];
                var settingId = select.id;
                var settingValue = getSettingValue(settingId);
                if (settingValue !== null) {
                    select.value = settingValue;
                }
                select.onchange = function() {
                    changeSetting(this.id, this.value);
                };
            }
        }
    }

    window.addEventListener("DOMContentLoaded", setEvents);
})();

@@ -906,359 +906,79 @@
     
-var elems = %7B%0A            toggles: Array.prototype.slice.call(document.getElementsByClassName(%22slider%22)),%0A            selects: Array.prototype.slice.call(document.getElementsByClassName(%22select-wrapper%22)),%0A        %7D;%0A        var i;%0A%0A        if (elems.toggles && elems.toggles.length %3E 0) %7B%0A            for (i = 0; i %3C elems.toggles.length; ++i) %7B%0A    
+onEachLazy(document.getElementsByClassName(%22slider%22), function(elem) %7B%0A
     
@@ -1006,20 +1006,8 @@
 elem
-s.toggles%5Bi%5D
 .pre
@@ -1027,20 +1027,16 @@
 ibling;%0A
-    
         
@@ -1070,36 +1070,32 @@
 id;%0A            
- 
-   
 var settingValue
@@ -1129,36 +1129,32 @@
 d);%0A            
-    
 if (settingValue
@@ -1178,28 +1178,24 @@
             
-  
-  
 toggle.check
@@ -1232,32 +1232,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -1242,34 +1242,32 @@
   %7D%0A            
-  
 toggle.onchange 
@@ -1273,36 +1273,32 @@
  = function() %7B%0A
-    
                 
@@ -1343,35 +1343,27 @@
             
-   
- 
 %7D;%0A
-    
             
@@ -1394,36 +1394,32 @@
 ey;%0A            
-   
- 
 toggle.onkeyrele
@@ -1447,143 +1447,99 @@
     
-    %7D%0A        %7D%0A%0A        if (elems.selects && elems.selects.length %3E 0) %7B%0A            for (i = 0; i %3C elems.selects.length; ++i) %7B%0A    
+%7D);%0A        onEachLazy(document.getElementsByClassName(%22select-wrapper%22), function(elem) %7B%0A
     
@@ -1567,20 +1567,8 @@
 elem
-s.selects%5Bi%5D
 .get
@@ -1603,36 +1603,32 @@
 0%5D;%0A            
-    
 var settingId = 
@@ -1642,36 +1642,32 @@
 id;%0A            
-  
-  
 var settingValue
@@ -1701,36 +1701,32 @@
 d);%0A            
-    
 if (settingValue
@@ -1750,28 +1750,24 @@
             
-  
-  
 select.value
@@ -1791,33 +1791,26 @@
             
-    %7D%0A   
+%7D%0A
             
@@ -1805,25 +1805,24 @@
             
- 
 select.oncha
@@ -1832,36 +1832,32 @@
  = function() %7B%0A
-    
                 
@@ -1896,36 +1896,32 @@
 e);%0A            
-   
- 
 %7D;%0A            %7D
@@ -1919,23 +1919,11 @@
     
-    %7D%0A        %7D
+%7D);
 %0A   

Improve settings.js code by using 'onEachLazy' directly instead of looping through DOM iterators