1<!DOCTYPE html> 2<!-- 3Copyright (c) 2016 The Chromium Authors. All rights reserved. 4Use of this source code is governed by a BSD-style license that can be 5found in the LICENSE file. 6--> 7 8<link rel="import" href="/tracing/ui/base/checkbox.html"> 9<link rel="import" href="/tracing/ui/base/ui.html"> 10 11<polymer-element name='tr-ui-b-checkbox-picker'> 12 <template> 13 <style> 14 #container { 15 display: flex; 16 flex-direction: column; 17 } 18 </style> 19 20 <div id="container"> 21 </div> 22 23 </template> 24 25 <script> 26 'use strict'; 27 28 Polymer({ 29 created: function() { 30 this.needsInit_ = true; 31 this.settingsKey_ = undefined; 32 this.is_ready_ = false; 33 this.checkboxes_ = undefined; 34 }, 35 36 ready: function() { 37 this.is_ready_ = true; 38 this.maybeInit_(); 39 this.maybeRenderCheckboxes_(); 40 }, 41 42 get settingsKey() { 43 return this.settingsKey_; 44 }, 45 46 set settingsKey(settingsKey) { 47 if (!this.needsInit_) 48 throw new Error('Already initialized.'); 49 this.settingsKey_ = settingsKey; 50 this.maybeInit_(); 51 }, 52 53 maybeInit_: function() { 54 if (!this.needsInit_) 55 return; 56 if (this.settingsKey_ === undefined) 57 return; 58 if (this.checkboxes_ === undefined) 59 return; 60 61 this.needsInit_ = false; 62 63 for (var key in this.checkboxes_) { 64 this.checkboxes_[key].defaultCheckedValue = false; 65 this.checkboxes_[key].settingsKey = this.settingsKey_ + key; 66 } 67 }, 68 69 set items(items) { 70 this.checkboxes_ = {}; 71 items.forEach(function(e) { 72 if (e.key in this.checkboxes_) 73 throw new Error(e.key + ' already exists'); 74 var checkboxEl = document.createElement('tr-ui-b-checkbox'); 75 checkboxEl.label = e.label; 76 this.checkboxes_[e.key] = checkboxEl; 77 }.bind(this)); 78 this.maybeInit_(); 79 this.maybeRenderCheckboxes_(); 80 }, 81 82 maybeRenderCheckboxes_: function() { 83 if (!this.is_ready_) 84 return; 85 if (this.checkboxes_ === undefined) 86 return; 87 for (var key in this.checkboxes_) 88 this.$.container.appendChild(this.checkboxes_[key]); 89 }, 90 91 selectCheckbox: function(key) { 92 if (!(key in this.checkboxes_)) 93 throw new Error(key + ' does not exists'); 94 this.checkboxes_[key].checked = true; 95 }, 96 97 unselectCheckbox: function(key) { 98 if (!(key in this.checkboxes_)) 99 throw new Error(key + ' does not exists'); 100 this.checkboxes_[key].checked = false; 101 }, 102 103 get checkedKeys() { 104 return Object.keys(this.checkboxes_).filter(function(k) { 105 return this.checkboxes_[k].checked; 106 }.bind(this)); 107 }, 108 109 }); 110 </script> 111</polymer-element> 112