forked from ivpusic/angular-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-cookie.js
111 lines (101 loc) · 3.32 KB
/
angular-cookie.js
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
/*
* Copyright 2013 Ivan Pusic
* Contributors:
* Matjaz Lipus
*/
angular.module('ivpusic.cookie', ['ipCookie']);
angular.module('ipCookie', ['ng']).
factory('ipCookie', ['$document',
function ($document) {
'use strict';
return (function () {
function cookieFun(key, value, options) {
var cookies,
list,
i,
cookie,
pos,
name,
hasCookies,
all,
expiresFor;
options = options || {};
if (value !== undefined) {
// we are setting value
value = typeof value === 'object' ? JSON.stringify(value) : String(value);
if (typeof options.expires === 'number') {
expiresFor = options.expires;
options.expires = new Date();
// Trying to delete a cookie; set a date far in the past
if (expiresFor === -1) {
options.expires = new Date('Thu, 01 Jan 1970 00:00:00 GMT');
// A new
} else if (options.expirationUnit !== undefined) {
if (options.expirationUnit === 'hours') {
options.expires.setHours(options.expires.getHours() + expiresFor);
} else if (options.expirationUnit === 'minutes') {
options.expires.setMinutes(options.expires.getMinutes() + expiresFor);
} else if (options.expirationUnit === 'seconds') {
options.expires.setSeconds(options.expires.getSeconds() + expiresFor);
} else {
options.expires.setDate(options.expires.getDate() + expiresFor);
}
} else {
options.expires.setDate(options.expires.getDate() + expiresFor);
}
}
return ($document[0].cookie = [
encodeURIComponent(key),
'=',
encodeURIComponent(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '',
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
list = [];
all = $document[0].cookie;
if (all) {
list = all.split('; ');
}
cookies = {};
hasCookies = false;
for (i = 0; i < list.length; ++i) {
if (list[i]) {
cookie = list[i];
pos = cookie.indexOf('=');
name = cookie.substring(0, pos);
value = decodeURIComponent(cookie.substring(pos + 1));
if (key === undefined || key === name) {
try {
cookies[name] = JSON.parse(value);
} catch (e) {
cookies[name] = value;
}
if (key === name) {
return cookies[name];
}
hasCookies = true;
}
}
}
if (hasCookies && key === undefined) {
return cookies;
}
}
cookieFun.remove = function (key, options) {
var hasCookie = cookieFun(key) !== undefined;
if (hasCookie) {
if (!options) {
options = {};
}
options.expires = -1;
cookieFun(key, '', options);
}
return hasCookie;
};
return cookieFun;
}());
}
]);