-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathngKookies.js
125 lines (100 loc) · 4.4 KB
/
ngKookies.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* ngKookies - relpacer for built-in $cookieStore
* Porque?? - https://github.com/angular/angular.js/issues/950
* http://github.com/voronianski/ngKookies
* (c) 2014 MIT License, https://pixelhunter.me
*/
(function (root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory(require('angular'));
} else if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else {
factory(root.angular);
}
}(this, function (angular, undefined) {
'use strict';
angular.module('ngKookies', [])
.provider('$kookies', function () {
var config = this.config = {};
var defaults = this.defaults = {};
this.setConfig = function (newConfig) {
angular.extend(config, newConfig);
};
this.setDefaults = function (newDefaults) {
angular.extend(defaults, newDefaults);
};
this.$get = function () {
var privateMethods = {};
var publicMethods = {};
privateMethods.decode = function (s) {
return config.raw ? s : decodeURIComponent(s);
};
privateMethods.encode = function (s) {
return config.raw ? s : encodeURIComponent(s);
};
privateMethods.parseCookie = function (s) {
if (s.indexOf('"') === 0) {
// unescape quoted cookie as according to RFC2068
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
try {
// replace server-side written pluses with spaces.
s = decodeURIComponent(s.replace(/\+/g, ' '));
return config.json ? angular.fromJson(s) : s;
} catch(e) {
// do nothing
}
};
privateMethods.stringifyCookie = function (value) {
return privateMethods.encode(config.json ? angular.toJson(value) : String(value));
};
privateMethods.read = function (s, converter) {
var value = config.raw ? s : privateMethods.parseCookie(s);
return angular.isFunction(converter) ? converter(value) : value;
};
publicMethods.get = function (key, converter) {
var result = key ? undefined : {};
var cookies = document.cookie ? document.cookie.split('; ') : [];
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
var name = privateMethods.decode(parts.shift());
var cookie = parts.join('=');
if (key && key === name) {
result = privateMethods.read(cookie, converter);
break;
}
if (!key && (cookie = privateMethods.read(cookie)) !== undefined) {
result[name] = cookie;
}
}
return result;
};
publicMethods.set = function (key, value, options) {
options = options || {};
options = angular.extend(options, defaults);
if (typeof options.expires === 'number') {
var days = options.expires;
var time = options.expires = new Date();
time.setTime(+time + days * 864e+5);
}
var cookies = document.cookie = [
privateMethods.encode(key), '=', privateMethods.stringifyCookie(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join('');
return cookies;
};
publicMethods.remove = function (key, options) {
if (publicMethods.get(key) === undefined) {
return false;
}
publicMethods.set(key, '', angular.extend({expires: -1}, options));
return !publicMethods.get(key);
};
return publicMethods;
};
});
}));