OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <link rel="import" href="../polymer/polymer.html"> |
| 7 |
| 8 <polymer-element name="polymer-cookie" attributes="name value expires secure dom
ain path max-age"> |
| 9 <template> |
| 10 <style> |
| 11 :host { display: none; } |
| 12 </style> |
| 13 </template> |
| 14 <script> |
| 15 (function() { |
| 16 var EXPIRE_NOW = 'Thu, 01 Jan 1970 00:00:00 GMT'; |
| 17 var FOREVER = 'Fri, 31 Dec 9999 23:59:59 GMT'; |
| 18 var cookieProps = ['expires', 'secure', 'max-age', 'domain', 'path']; |
| 19 Polymer('polymer-cookie', { |
| 20 expires: FOREVER, |
| 21 ready: function() { |
| 22 this.load(); |
| 23 }, |
| 24 parseCookie: function() { |
| 25 var pairs = document.cookie.split(/\s*;\s*/); |
| 26 var map = pairs.map(function(kv) { |
| 27 var eq = kv.indexOf('='); |
| 28 return { |
| 29 name: unescape(kv.slice(0, eq)), |
| 30 value: unescape(kv.slice(eq + 1)) |
| 31 }; |
| 32 }); |
| 33 var nom = this.name; |
| 34 return map.filter(function(kv){ return kv.name === nom; })[0]; |
| 35 }, |
| 36 load: function() { |
| 37 var kv = this.parseCookie(); |
| 38 this.value = kv && kv.value; |
| 39 }, |
| 40 valueChanged: function() { |
| 41 this.expire = FOREVER; |
| 42 this.save(); |
| 43 }, |
| 44 // TODO(dfreedman): collapse these when 'multiple props -> single change
function' exists in Polymer |
| 45 expiresChanged: function() { |
| 46 this.save(); |
| 47 }, |
| 48 secureChanged: function() { |
| 49 this.save(); |
| 50 }, |
| 51 domainChanged: function() { |
| 52 this.save(); |
| 53 }, |
| 54 pathChanged: function() { |
| 55 this.save(); |
| 56 }, |
| 57 maxAgeChanged: function() { |
| 58 this.save(); |
| 59 }, |
| 60 isCookieStored: function() { |
| 61 return Boolean(this.parseCookie()); |
| 62 }, |
| 63 deleteCookie: function() { |
| 64 this.expires = EXPIRE_NOW; |
| 65 }, |
| 66 prepareProperties: function() { |
| 67 var prepared = ''; |
| 68 for (var i = 0, k; i < cookieProps.length; i++) { |
| 69 k = cookieProps[i]; |
| 70 if (this[k]) { |
| 71 prepared += (';' + k + '=' + this[k]); |
| 72 } |
| 73 } |
| 74 return prepared; |
| 75 }, |
| 76 save: function() { |
| 77 document.cookie = escape(this.name) + '=' + escape(this.value) + this.
prepareProperties(); |
| 78 } |
| 79 }); |
| 80 })(); |
| 81 </script> |
| 82 </polymer-element> |
OLD | NEW |