1/* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17function createToggleControl(elm, iconName, onChangeCb) { 18 let icon = document.createElement("span"); 19 icon.classList.add("toggle-control-icon"); 20 icon.classList.add("material-icons-outlined"); 21 if (iconName) { 22 icon.appendChild(document.createTextNode(iconName)); 23 } 24 elm.appendChild(icon); 25 let toggle = document.createElement("label"); 26 toggle.classList.add("toggle-control-switch"); 27 let input = document.createElement("input"); 28 input.type = "checkbox"; 29 toggle.appendChild(input); 30 let slider = document.createElement("span"); 31 slider.classList.add("toggle-control-slider"); 32 toggle.appendChild(slider); 33 elm.classList.add("toggle-control"); 34 elm.appendChild(toggle); 35 if (onChangeCb) { 36 input.onchange = e => onChangeCb(e.target.checked); 37 } 38} 39