OLD | NEW |
1 <!-- | 1 <!-- |
2 The common.js file must be included before this file. | 2 The common.js file must be included before this file. |
3 | 3 |
4 This in an HTML Import-able file that contains the definition | 4 This in an HTML Import-able file that contains the definition |
5 of the following elements: | 5 of the following elements: |
6 | 6 |
7 <url-param-sk> | 7 <url-param-sk> |
8 | 8 |
9 This element uses two-way* data binding to synchronize a URL parameter with | 9 This element uses two-way* data binding to synchronize a URL parameter with |
10 a variable. On page load, if the parameter is provided in the URL, its value | 10 a variable. On page load, if the parameter is provided in the URL, its value |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 valid: (array of strings) Acceptable values. Default is null. If empty or | 50 valid: (array of strings) Acceptable values. Default is null. If empty or |
51 null, any value is accepted. If an invalid value is provided in the | 51 null, any value is accepted. If an invalid value is provided in the |
52 URL parameters, the existing or default value is used. | 52 URL parameters, the existing or default value is used. |
53 | 53 |
54 Events: | 54 Events: |
55 None | 55 None |
56 | 56 |
57 Methods: | 57 Methods: |
58 None | 58 None |
59 --> | 59 --> |
60 <polymer-element name="url-param-sk"> | 60 <link rel="import" href="/res/imp/bower_components/paper-toast/paper-toast.html"
> |
| 61 <dom-module id="url-param-sk"> |
61 <template> | 62 <template> |
62 <paper-toast id="toast"></paper-toast> | 63 <paper-toast id="toast"></paper-toast> |
63 </template> | 64 </template> |
64 <script> | 65 <script> |
65 Polymer('url-param-sk', { | 66 Polymer({ |
66 publish: { | 67 is: 'url-param-sk', |
| 68 properties: { |
| 69 multi: { |
| 70 type: Boolean, |
| 71 value: false, |
| 72 notify: true, |
| 73 reflectToAttribute: true |
| 74 }, |
67 name: { | 75 name: { |
68 value: "", | 76 type: String, |
69 reflect: true, | 77 value: '', |
| 78 notify: true, |
| 79 reflectToAttribute: true |
| 80 }, |
| 81 valid: { |
| 82 type: Array, |
| 83 value: null, |
| 84 notify: true, |
| 85 reflectToAttribute: true |
70 }, | 86 }, |
71 value: { | 87 value: { |
72 value: "", | 88 type: String, |
73 reflect: true, | 89 value: '', |
| 90 notify: true, |
| 91 observer: 'valueChanged', |
74 }, | 92 }, |
75 multi: { | 93 _loaded: { |
| 94 type: Boolean, |
76 value: false, | 95 value: false, |
77 reflect: true, | 96 } |
78 }, | |
79 valid: { | |
80 value: null, | |
81 reflect: true, | |
82 }, | |
83 }, | 97 }, |
84 | 98 ready: function () { |
85 ready: function() { | 99 this._loaded = true; |
86 // Read the URL parameters. If our variable is set, save its value. | 100 // Read the URL parameters. If our variable is set, save its value. |
87 // Otherwise, place our value in the URL. | 101 // Otherwise, place our value in the URL. |
88 var val = this.getURL(); | 102 var val = this.getURL(); |
89 if (val && this.isValid(val)) { | 103 if (val && this.isValid(val)) { |
90 this.value = val; | 104 this.set('value', val); |
91 } else { | 105 } else { |
92 this.putURL(); | 106 this.putURL(); |
93 } | 107 } |
94 }, | 108 }, |
95 | |
96 // Retrieve the value for our variable from the URL. | 109 // Retrieve the value for our variable from the URL. |
97 getURL: function() { | 110 getURL: function () { |
98 var vals = sk.query.toParamSet(window.location.search.substring(1))[this
.name]; | 111 var vals = sk.query.toParamSet(window.location.search.substring(1))[this
.name]; |
99 if (!vals) { | 112 if (!vals) { |
100 return null; | 113 return null; |
101 } | 114 } |
102 if (this.multi) { | 115 if (this.multi) { |
103 return vals; | 116 return vals; |
104 } | 117 } |
105 if (vals.length > 1) { | 118 if (vals.length > 1) { |
106 this.error("Multiple values provided for " + this.name + | 119 this.error('Multiple values provided for ' + this.name + ' but only on
e accepted: ' + vals); |
107 " but only one accepted: " + vals); | |
108 return null; | 120 return null; |
109 } | 121 } |
110 return vals[0]; | 122 return vals[0]; |
111 }, | 123 }, |
112 | |
113 // Store the value for our variable in the URL. | 124 // Store the value for our variable in the URL. |
114 putURL: function() { | 125 putURL: function () { |
115 var params = sk.query.toParamSet(window.location.search.substring(1)); | 126 var params = sk.query.toParamSet(window.location.search.substring(1)); |
116 delete params[this.name]; | 127 delete params[this.name]; |
117 if (!this.value || (Array.isArray(this.value) && this.value.length == 0)
) { | 128 if (!this.value || Array.isArray(this.value) && this.value.length == 0)
{ |
| 129 } else |
118 // Don't insert undefined/empty values. | 130 // Don't insert undefined/empty values. |
119 } else { | 131 { |
120 if (this.multi) { | 132 if (this.multi) { |
121 params[this.name] = this.value; | 133 params[this.name] = this.value; |
122 } else { | 134 } else { |
123 params[this.name] = [this.value]; | 135 params[this.name] = [this.value]; |
| 136 } |
124 } | 137 } |
125 } | 138 var newUrl = window.location.href.split('?')[0] + '?' + sk.query.fromPar
amSet(params); |
126 var newUrl = window.location.href.split("?")[0] + "?" + sk.query.fromPar
amSet(params); | 139 window.history.replaceState('', '', newUrl); |
127 window.history.replaceState("", "", newUrl); | |
128 }, | 140 }, |
129 | |
130 // Check to see whether the given value is valid. | 141 // Check to see whether the given value is valid. |
131 isValid: function(val) { | 142 isValid: function (val) { |
132 var that = this; | 143 var that = this; |
133 var checkValid = function(val) { | 144 var checkValid = function (val) { |
134 if (that.valid) { | 145 if (that.valid) { |
135 for (var i = 0; i < that.valid.length; i++) { | 146 for (var i = 0; i < that.valid.length; i++) { |
136 if (val == that.valid[i]) { | 147 if (val == that.valid[i]) { |
137 return true; | 148 return true; |
138 } | 149 } |
139 } | 150 } |
140 that.error("Invalid value for " + that.name + ": \"" + val + | 151 that.error('Invalid value for ' + that.name + ': "' + val + '". Must
be one of: ' + that.valid); |
141 "\". Must be one of: " + that.valid); | |
142 return false; | 152 return false; |
143 } | 153 } |
144 return true; | 154 return true; |
145 }; | 155 }; |
146 if (this.multi) { | 156 if (this.multi) { |
147 // Verify that it's an array and that all elements are valid. | 157 // Verify that it's an array and that all elements are valid. |
148 if (!Array.isArray(val)) { | 158 if (!Array.isArray(val)) { |
149 this.console.error("url-param-sk: Value is not an array: " + val); | 159 this.console.error('url-param-sk: Value is not an array: ' + val); |
150 return false; | 160 return false; |
151 } | 161 } |
152 for (var i = 0; i < val.length; i++) { | 162 for (var i = 0; i < val.length; i++) { |
153 if (!checkValid(val[i])) { | 163 if (!checkValid(val[i])) { |
154 return false; | 164 return false; |
155 } | 165 } |
156 } | 166 } |
157 } else { | 167 } else { |
158 if (Array.isArray(val)) { | 168 if (Array.isArray(val)) { |
159 this.error("Multiple values provided for " + this.name + | 169 this.error('Multiple values provided for ' + this.name + ' but only
one accepted: ' + val); |
160 " but only one accepted: " + val); | |
161 } | 170 } |
162 return checkValid(val); | 171 return checkValid(val); |
163 } | 172 } |
164 return true; | 173 return true; |
165 }, | 174 }, |
166 | 175 valueChanged: function () { |
167 valueChanged: function() { | 176 if (this._loaded) { |
168 // Save our value to the URL. | 177 // Save our value to the URL. |
169 this.putURL(); | 178 this.putURL(); |
| 179 } |
170 }, | 180 }, |
171 | 181 error: function (msg) { |
172 error: function(msg) { | 182 this.set('$.toast.text', msg); |
173 this.$.toast.text = msg; | |
174 this.$.toast.show(); | 183 this.$.toast.show(); |
175 }, | 184 } |
176 }); | 185 }); |
177 </script> | 186 </script> |
178 </polymer-element> | 187 </dom-module> |
OLD | NEW |