Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!-- | |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | |
| 4 that can be found in the LICENSE file. | |
| 5 | |
| 6 This element requires version 1.0.1 or greater of the npm package | |
|
stephana
2016/08/11 17:09:53
IMO, requirements should be lower down in the docs
kjlubick
2016/08/11 20:00:21
Done.
| |
| 7 skia-common-js and that the common.js from that package is included before | |
| 8 the instantiation of this element. | |
| 9 | |
| 10 This in an HTML Import-able file that contains the definition | |
| 11 of the following elements: | |
| 12 | |
| 13 <url-param> | |
| 14 | |
| 15 This element uses two-way* data binding to synchronize a URL parameter with | |
| 16 a variable. On page load, if the parameter is provided in the URL, its value | |
| 17 is assigned to the variable. When the variable changes, its new value is | |
| 18 updated in the URL. | |
| 19 | |
| 20 * It's not exactly two-way, because the URL is not watched for changes. This | |
| 21 is fine in most cases, since the page reloads when the URL is changed by the | |
| 22 user, and it should be rare that the parameter is changed by a different | |
| 23 piece of code. | |
| 24 | |
| 25 | |
| 26 Attributes: | |
| 27 default_value: String, Default value to be used. This will be clobbered | |
| 28 by any value given in an element's properties. | |
| 29 default_values: Array<String>, Default values to be used if multi is set to | |
| 30 true. This will be clobbered by any value given in an element's | |
| 31 properties. | |
| 32 name: String, The name of the URL parameter. | |
| 33 value: String|Array<String>, The value(s) of the URL parameter. | |
| 34 multi: Boolean, Whether the variable can take multiple values. Default is | |
| 35 false. If true, 'value' must be an array of strings and default_values | |
| 36 will be used instead of default_value. | |
| 37 valid: Array<String> Acceptable values. Default is null. If empty or | |
| 38 null, any value is accepted. If an invalid value is provided in the | |
| 39 URL parameters, the existing or default value is used. | |
| 40 | |
| 41 Events: | |
| 42 None | |
| 43 | |
| 44 Methods: | |
| 45 None | |
| 46 --> | |
| 47 | |
| 48 <link rel="import" href="/res/imp/bower_components/paper-toast/paper-toast.html" > | |
| 49 | |
| 50 <dom-module id="url-param"> | |
| 51 <template> | |
| 52 <paper-toast id="toast"></paper-toast> | |
| 53 </template> | |
| 54 <script> | |
| 55 (function(){ | |
| 56 Polymer({ | |
| 57 is: 'url-param', | |
| 58 properties: { | |
| 59 default_value: { | |
| 60 type: String, | |
| 61 }, | |
| 62 default_values: { | |
| 63 type: Array, | |
| 64 }, | |
| 65 multi: { | |
| 66 type: Boolean, | |
| 67 value: false, | |
| 68 }, | |
| 69 name: { | |
| 70 type: String, | |
| 71 }, | |
| 72 valid: { | |
| 73 type: Array, | |
| 74 }, | |
| 75 value: { | |
| 76 type: String, | |
| 77 value: '', | |
| 78 notify: true, | |
| 79 observer: 'valueChanged', | |
| 80 }, | |
| 81 | |
| 82 _loaded: { | |
| 83 type: Boolean, | |
| 84 value: false, | |
| 85 } | |
| 86 }, | |
| 87 // Listens to array changes for multi urls | |
| 88 observers: ["valueChanged(value.splices)"], | |
| 89 | |
| 90 ready: function () { | |
| 91 this._loaded = true; | |
| 92 | |
| 93 // Read the URL parameters. If our variable is set, save its value. | |
| 94 // Otherwise, place our value in the URL. | |
| 95 var val = this.getURL(); | |
| 96 if (val && this.isValid(val)) { | |
| 97 this.set('value', val); | |
| 98 } else if (this.default_value && this.isValid(this.default_value)) { | |
| 99 this.set('value', this.default_value); | |
| 100 } | |
| 101 else if (this.multi && this.default_values && this.isValid(this.default_ values)) { | |
| 102 this.set('value', this.default_values); | |
| 103 } | |
| 104 else { | |
| 105 this.putURL(); | |
| 106 } | |
| 107 }, | |
| 108 // Retrieve the value for our variable from the URL. | |
| 109 getURL: function () { | |
|
stephana
2016/08/11 17:09:53
getURL should be documented if it's part of the AP
kjlubick
2016/08/11 20:00:21
Done.
| |
| 110 var vals = sk.query.toParamSet(window.location.search.substring(1))[this .name]; | |
| 111 if (!vals) { | |
| 112 return null; | |
| 113 } | |
| 114 if (this.multi) { | |
| 115 return vals; | |
| 116 } | |
| 117 if (vals.length > 1) { | |
| 118 this.error('Multiple values provided for ' + this.name + ' but only on e accepted: ' + vals); | |
| 119 return null; | |
| 120 } | |
| 121 return vals[0]; | |
| 122 }, | |
| 123 // Store the value for our variable in the URL. | |
| 124 putURL: function () { | |
| 125 var params = sk.query.toParamSet(window.location.search.substring(1)); | |
| 126 delete params[this.name]; | |
| 127 if (!this.value || Array.isArray(this.value) && this.value.length == 0) { | |
| 128 } else | |
| 129 // Don't insert undefined/empty values. | |
| 130 { | |
| 131 if (this.multi) { | |
| 132 params[this.name] = this.value; | |
| 133 } else { | |
| 134 params[this.name] = [this.value]; | |
| 135 } | |
| 136 } | |
| 137 var newUrl = window.location.href.split('?')[0] + '?' + sk.query.fromPar amSet(params); | |
| 138 window.history.replaceState('', '', newUrl); | |
| 139 }, | |
| 140 // Check to see whether the given value is valid. | |
| 141 isValid: function (val) { | |
| 142 var checkValid = function (val) { | |
| 143 if (this.valid) { | |
| 144 for (var i = 0; i < this.valid.length; i++) { | |
| 145 if (val == this.valid[i]) { | |
| 146 return true; | |
| 147 } | |
| 148 } | |
| 149 this.error('Invalid value for ' + this.name + ': "' + val + '". Must be one of: ' + this.valid); | |
| 150 return false; | |
| 151 } | |
| 152 return true; | |
| 153 }.bind(this); | |
| 154 if (this.multi) { | |
| 155 // Verify that it's an array and that all elements are valid. | |
| 156 if (!Array.isArray(val)) { | |
| 157 this.error('url-param-sk: Value is not an array: ' + val); | |
| 158 return false; | |
| 159 } | |
| 160 for (var i = 0; i < val.length; i++) { | |
| 161 if (!checkValid(val[i])) { | |
| 162 return false; | |
| 163 } | |
| 164 } | |
| 165 } else { | |
| 166 if (Array.isArray(val)) { | |
| 167 this.error('Multiple values provided for ' + this.name + ' but only one accepted: ' + val); | |
| 168 } | |
| 169 return checkValid(val); | |
| 170 } | |
| 171 return true; | |
| 172 }, | |
| 173 valueChanged: function () { | |
| 174 if (this._loaded) { | |
| 175 // Save our value to the URL. | |
| 176 this.putURL(); | |
| 177 } | |
| 178 }, | |
| 179 error: function (msg) { | |
| 180 console.log('[ERROR] '+msg); | |
| 181 this.set('$.toast.text', msg); | |
| 182 this.$.toast.show(); | |
| 183 } | |
| 184 }); | |
| 185 })() | |
| 186 </script> | |
| 187 </dom-module> | |
| OLD | NEW |