Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: appengine/swarming/elements/res/imp/common/url-param.html

Issue 2408743002: Move elements/ to ui/ (Closed)
Patch Set: rebase again Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 in an HTML Import-able file that contains the definition
7 of the following elements:
8
9 <url-param>
10
11 This element uses two-way* data binding to synchronize a URL parameter with
12 a variable. On page load, if the parameter is provided in the URL, its value
13 is assigned to the variable. When the variable changes, its new value is
14 updated in the URL. If there are problems, a toast message will pop up and
15 display it.
16
17 * It's not exactly two-way, because the URL is not watched for changes. This
18 is fine in most cases, since the page reloads when the URL is changed by the
19 user, and it should be rare that the parameter is changed by a different
20 piece of code.
21
22 This element requires version 1.0.1 or greater of the npm package
23 skia-common-js and that the common.js from that package is included before
24 the instantiation of this element.
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 multi: Boolean, Whether the variable can take multiple values. Default is
33 false. If true, 'value' must be an array of strings and default_values
34 will be used instead of default_value.
35 name: String, The name of the URL parameter.
36 valid: Array<String> Acceptable values. Default is null. If empty or
37 null, any value is accepted. If an invalid value is provided in the
38 URL parameters, the existing or default value is used.
39 value: String|Array<String>, The value(s) of the URL parameter.
40
41
42 Events:
43 None
44
45 Methods:
46 None
47 -->
48
49 <link rel="import" href="/res/imp/bower_components/paper-toast/paper-toast.html" >
50
51 <dom-module id="url-param">
52 <template>
53 <paper-toast id="toast"></paper-toast>
54 </template>
55 <script>
56 (function(){
57 Polymer({
58 is: 'url-param',
59 properties: {
60 default_value: {
61 type: String,
62 },
63 default_values: {
64 type: Array,
65 },
66 multi: {
67 type: Boolean,
68 value: false,
69 },
70 name: {
71 type: String,
72 },
73 valid: {
74 type: Array,
75 },
76 value: {
77 type: String,
78 value: '',
79 notify: true,
80 observer: '_valueChanged',
81 },
82
83 _loaded: {
84 type: Boolean,
85 value: false,
86 }
87 },
88 // Listens to array changes for multi urls
89 observers: ["_valueChanged(value.splices)"],
90
91 ready: function () {
92 this._loaded = true;
93
94 // Read the URL parameters. If our variable is set, save its value.
95 // Otherwise, place our value in the URL.
96 var val = this._getURL();
97 if (val && this._isValid(val)) {
98 this.set('value', val);
99 } else if (this.default_value && this._isValid(this.default_value)) {
100 this.set('value', this.default_value);
101 }
102 else if (this.multi && this.default_values && this._isValid(this.default _values)) {
103 this.set('value', this.default_values);
104 }
105 else {
106 this._putURL();
107 }
108 },
109 // Retrieve the value for our variable from the URL.
110 _getURL: function () {
111 var vals = sk.query.toParamSet(window.location.search.substring(1))[this .name];
112 if (!vals) {
113 return null;
114 }
115 if (this.multi) {
116 return vals;
117 }
118 if (vals.length > 1) {
119 this._error('Multiple values provided for ' + this.name + ' but only o ne accepted: ' + vals);
120 return null;
121 }
122 return vals[0];
123 },
124 // Store the value for our variable in the URL.
125 _putURL: function () {
126 var params = sk.query.toParamSet(window.location.search.substring(1));
127 delete params[this.name];
128 if (!this.value || Array.isArray(this.value) && this.value.length == 0) {
129 } else
130 // Don't insert undefined/empty values.
131 {
132 if (this.multi) {
133 params[this.name] = this.value;
134 } else {
135 params[this.name] = [this.value];
136 }
137 }
138 var newUrl = window.location.href.split('?')[0] + '?' + sk.query.fromPar amSet(params);
139 window.history.replaceState('', '', newUrl);
140 },
141 // Check to see whether the given value is valid.
142 _isValid: function (val) {
143 var checkValid = function (val) {
144 if (this.valid) {
145 for (var i = 0; i < this.valid.length; i++) {
146 if (val == this.valid[i]) {
147 return true;
148 }
149 }
150 this._error('Invalid value for ' + this.name + ': "' + val + '". Mus t be one of: ' + this.valid);
151 return false;
152 }
153 return true;
154 }.bind(this);
155 if (this.multi) {
156 // Verify that it's an array and that all elements are valid.
157 if (!Array.isArray(val)) {
158 this._error('url-param-sk: Value is not an array: ' + val);
159 return false;
160 }
161 for (var i = 0; i < val.length; i++) {
162 if (!checkValid(val[i])) {
163 return false;
164 }
165 }
166 } else {
167 if (Array.isArray(val)) {
168 this._error('Multiple values provided for ' + this.name + ' but only one accepted: ' + val);
169 }
170 return checkValid(val);
171 }
172 return true;
173 },
174 _valueChanged: function () {
175 if (this._loaded) {
176 // Save our value to the URL.
177 this._putURL();
178 }
179 },
180 _error: function (msg) {
181 console.log('[ERROR] '+msg);
182 this.set('$.toast.text', msg);
183 this.$.toast.show();
184 }
185 });
186 })()
187 </script>
188 </dom-module>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698