1<!-- 2@license 3Copyright (c) 2015 The Polymer Project Authors. All rights reserved. 4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt 5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt 6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt 7Code distributed by Google as part of the polymer project is also 8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt 9--> 10 11<link rel="import" href="../polymer/polymer.html"> 12 13 14<script> 15 'use strict'; 16 17 Polymer({ 18 is: 'iron-query-params', 19 properties: { 20 paramsString: { 21 type: String, 22 notify: true, 23 observer: 'paramsStringChanged', 24 }, 25 paramsObject: { 26 type: Object, 27 notify: true, 28 value: function() { 29 return {}; 30 } 31 }, 32 _dontReact: { 33 type: Boolean, 34 value: false 35 } 36 }, 37 hostAttributes: { 38 hidden: true 39 }, 40 observers: [ 41 'paramsObjectChanged(paramsObject.*)' 42 ], 43 paramsStringChanged: function() { 44 this._dontReact = true; 45 this.paramsObject = this._decodeParams(this.paramsString); 46 this._dontReact = false; 47 }, 48 paramsObjectChanged: function() { 49 if (this._dontReact) { 50 return; 51 } 52 this.paramsString = this._encodeParams(this.paramsObject) 53 .replace(/%3F/g, '?').replace(/%2F/g, '/').replace(/'/g, '%27'); 54 }, 55 _encodeParams: function(params) { 56 var encodedParams = []; 57 for (var key in params) { 58 var value = params[key]; 59 if (value === '') { 60 encodedParams.push(encodeURIComponent(key)); 61 } else if (value) { 62 encodedParams.push( 63 encodeURIComponent(key) + 64 '=' + 65 encodeURIComponent(value.toString()) 66 ); 67 } 68 } 69 return encodedParams.join('&'); 70 }, 71 _decodeParams: function(paramString) { 72 var params = {}; 73 74 // Work around a bug in decodeURIComponent where + is not 75 // converted to spaces: 76 paramString = (paramString || '').replace(/\+/g, '%20'); 77 78 var paramList = paramString.split('&'); 79 for (var i = 0; i < paramList.length; i++) { 80 var param = paramList[i].split('='); 81 if (param[0]) { 82 params[decodeURIComponent(param[0])] = 83 decodeURIComponent(param[1] || ''); 84 } 85 } 86 return params; 87 } 88 }); 89</script> 90