forked from googlearchive/polymer-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolymer-cookie.html
161 lines (144 loc) · 4.36 KB
/
polymer-cookie.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!--
Copyright 2013 The Polymer Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<!--
The `polymer-cookie` element can be used to set and read cookies.
You should have one `polymer-cookie` element per cookie you want to use,
defined by its `name` property.
You can then read the `value` of the cookie, or save the cookie by setting
the `value` attribute or explicitely calling the `save` method.
##### Example
<polymer-cookie name="mycookie"></polymer-cookie>
@element polymer-cookie
@blurb Element to set and read cookie values
@status alpha
@homepage github.io
-->
<polymer-element name="polymer-cookie" hidden attributes="name value expires secure domain path max-age">
<script>
(function() {
var EXPIRE_NOW = 'Thu, 01 Jan 1970 00:00:00 GMT';
var FOREVER = 'Fri, 31 Dec 9999 23:59:59 GMT';
var cookieProps = ['expires', 'secure', 'max-age', 'domain', 'path'];
Polymer('polymer-cookie', {
/**
* Name of the cookie
*
* @attribute name
* @type string
*/
/**
* Value of the cookie
*
* @attribute value
* @type string
*/
/**
* Expiry-date of the cookie as UTC/GMT-formatted String.
*
* Example: `"Sun, 15 Jul 2012 00:00:01 GMT"`
*
* @attribute expires
* @type string
* @default never
*/
expires: FOREVER,
/**
* If true, cookie will only be transmitted over secure protocol as https
*
* @attribute secure
* @type boolean
* @default false
*/
secure: false,
/**
* The domain from where the cookie will be readable.
* E.g., "example.com", ".example.com" (includes all subdomains)
* or "subdomain.example.com"; if not specified, defaults to the
* host portion of the current document location
*
* @attribute domain
* @type string
*/
/**
* The path from where the cookie will be readable.
* E.g., "/", "/mydir"; if not specified, defaults to the current
* path of the current document location.
*
* @attribute path
* @type string
*/
/**
* The maximum age of the cookie in seconds.
*
* @attribute max-age
* @type number
*/
ready: function() {
this.load();
},
parseCookie: function() {
var pairs = document.cookie.split(/\s*;\s*/);
var map = pairs.map(function(kv) {
var eq = kv.indexOf('=');
return {
name: unescape(kv.slice(0, eq)),
value: unescape(kv.slice(eq + 1))
};
});
var nom = this.name;
return map.filter(function(kv){ return kv.name === nom; })[0];
},
load: function() {
var kv = this.parseCookie();
this.value = kv && kv.value;
},
observe: {
value: 'save',
expires: 'save',
secure: 'save',
domain: 'save',
path: 'save',
maxAge: 'save'
},
/**
* Returns true if the cookie is currently set.
*
* @method isCookieStored
*/
isCookieStored: function() {
return Boolean(this.parseCookie());
},
/**
* Deletes the cookie.
*
* @method deleteCookie
*/
deleteCookie: function() {
this.expires = EXPIRE_NOW;
},
prepareProperties: function() {
var prepared = '';
for (var i = 0, k; i < cookieProps.length; i++) {
k = cookieProps[i];
if (this[k]) {
prepared += (';' + k + '=' + this[k]);
}
}
return prepared;
},
/**
* Sets the cookie with the currently defined parameters.
*
* @method save
*/
save: function() {
document.cookie = escape(this.name) + '=' + escape(this.value) + this.prepareProperties();
}
});
})();
</script>
</polymer-element>