PageRenderTime 98ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/angular.js/1.4.0-rc.1/angular-cookies.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 320 lines | 82 code | 25 blank | 213 comment | 4 complexity | a3bb97c6b50dc8d90aee4233e799f9d9 MD5 | raw file
  1. /**
  2. * @license AngularJS v1.4.0-rc.1
  3. * (c) 2010-2015 Google, Inc. http://angularjs.org
  4. * License: MIT
  5. */
  6. (function(window, angular, undefined) {'use strict';
  7. /**
  8. * @ngdoc module
  9. * @name ngCookies
  10. * @description
  11. *
  12. * # ngCookies
  13. *
  14. * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
  15. *
  16. *
  17. * <div doc-module-components="ngCookies"></div>
  18. *
  19. * See {@link ngCookies.$cookies `$cookies`} and
  20. * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
  21. */
  22. angular.module('ngCookies', ['ng']).
  23. /**
  24. * @ngdoc provider
  25. * @name $cookiesProvider
  26. * @description
  27. * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
  28. * */
  29. provider('$cookies', [function $CookiesProvider() {
  30. /**
  31. * @ngdoc property
  32. * @name $cookiesProvider#defaults
  33. * @description
  34. *
  35. * Object containing default options to pass when setting cookies.
  36. *
  37. * The object may have following properties:
  38. *
  39. * - **path** - `{string}` - The cookie will be available only for this path and its
  40. * sub-paths. By default, this would be the URL that appears in your base tag.
  41. * - **domain** - `{string}` - The cookie will be available only for this domain and
  42. * its sub-domains. For obvious security reasons the user agent will not accept the
  43. * cookie if the current domain is not a sub domain or equals to the requested domain.
  44. * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
  45. * or a Date object indicating the exact date/time this cookie will expire.
  46. * - **secure** - `{boolean}` - The cookie will be available only in secured connection.
  47. *
  48. * Note: by default the address that appears in your <base> tag will be used as path.
  49. * This is import so that cookies will be visible for all routes in case html5mode is enabled
  50. *
  51. **/
  52. var defaults = this.defaults = {};
  53. function calcOptions(options) {
  54. return options ? angular.extend({}, defaults, options) : defaults;
  55. }
  56. /**
  57. * @ngdoc service
  58. * @name $cookies
  59. *
  60. * @description
  61. * Provides read/write access to browser's cookies.
  62. *
  63. * BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
  64. * current browser cookie values. Now you must use the get/put/remove/etc. methods
  65. * as described below.
  66. *
  67. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  68. *
  69. * @example
  70. *
  71. * ```js
  72. * angular.module('cookiesExample', ['ngCookies'])
  73. * .controller('ExampleController', ['$cookies', function($cookies) {
  74. * // Retrieving a cookie
  75. * var favoriteCookie = $cookies.get('myFavorite');
  76. * // Setting a cookie
  77. * $cookies.put('myFavorite', 'oatmeal');
  78. * }]);
  79. * ```
  80. */
  81. this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
  82. return {
  83. /**
  84. * @ngdoc method
  85. * @name $cookies#get
  86. *
  87. * @description
  88. * Returns the value of given cookie key
  89. *
  90. * @param {string} key Id to use for lookup.
  91. * @returns {string} Raw cookie value.
  92. */
  93. get: function(key) {
  94. return $$cookieReader()[key];
  95. },
  96. /**
  97. * @ngdoc method
  98. * @name $cookies#getObject
  99. *
  100. * @description
  101. * Returns the deserialized value of given cookie key
  102. *
  103. * @param {string} key Id to use for lookup.
  104. * @returns {Object} Deserialized cookie value.
  105. */
  106. getObject: function(key) {
  107. var value = this.get(key);
  108. return value ? angular.fromJson(value) : value;
  109. },
  110. /**
  111. * @ngdoc method
  112. * @name $cookies#getAll
  113. *
  114. * @description
  115. * Returns a key value object with all the cookies
  116. *
  117. * @returns {Object} All cookies
  118. */
  119. getAll: function() {
  120. return $$cookieReader();
  121. },
  122. /**
  123. * @ngdoc method
  124. * @name $cookies#put
  125. *
  126. * @description
  127. * Sets a value for given cookie key
  128. *
  129. * @param {string} key Id for the `value`.
  130. * @param {string} value Raw value to be stored.
  131. * @param {Object=} options Options object.
  132. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  133. */
  134. put: function(key, value, options) {
  135. $$cookieWriter(key, value, calcOptions(options));
  136. },
  137. /**
  138. * @ngdoc method
  139. * @name $cookies#putObject
  140. *
  141. * @description
  142. * Serializes and sets a value for given cookie key
  143. *
  144. * @param {string} key Id for the `value`.
  145. * @param {Object} value Value to be stored.
  146. * @param {Object=} options Options object.
  147. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  148. */
  149. putObject: function(key, value, options) {
  150. this.put(key, angular.toJson(value), options);
  151. },
  152. /**
  153. * @ngdoc method
  154. * @name $cookies#remove
  155. *
  156. * @description
  157. * Remove given cookie
  158. *
  159. * @param {string} key Id of the key-value pair to delete.
  160. * @param {Object=} options Options object.
  161. * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
  162. */
  163. remove: function(key, options) {
  164. $$cookieWriter(key, undefined, calcOptions(options));
  165. }
  166. };
  167. }];
  168. }]);
  169. angular.module('ngCookies').
  170. /**
  171. * @ngdoc service
  172. * @name $cookieStore
  173. * @deprecated
  174. * @requires $cookies
  175. *
  176. * @description
  177. * Provides a key-value (string-object) storage, that is backed by session cookies.
  178. * Objects put or retrieved from this storage are automatically serialized or
  179. * deserialized by angular's toJson/fromJson.
  180. *
  181. * Requires the {@link ngCookies `ngCookies`} module to be installed.
  182. *
  183. * <div class="alert alert-danger">
  184. * **Note:** The $cookieStore service is deprecated.
  185. * Please use the {@link ngCookies.$cookies `$cookies`} service instead.
  186. * </div>
  187. *
  188. * @example
  189. *
  190. * ```js
  191. * angular.module('cookieStoreExample', ['ngCookies'])
  192. * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
  193. * // Put cookie
  194. * $cookieStore.put('myFavorite','oatmeal');
  195. * // Get cookie
  196. * var favoriteCookie = $cookieStore.get('myFavorite');
  197. * // Removing a cookie
  198. * $cookieStore.remove('myFavorite');
  199. * }]);
  200. * ```
  201. */
  202. factory('$cookieStore', ['$cookies', function($cookies) {
  203. return {
  204. /**
  205. * @ngdoc method
  206. * @name $cookieStore#get
  207. *
  208. * @description
  209. * Returns the value of given cookie key
  210. *
  211. * @param {string} key Id to use for lookup.
  212. * @returns {Object} Deserialized cookie value, undefined if the cookie does not exist.
  213. */
  214. get: function(key) {
  215. return $cookies.getObject(key);
  216. },
  217. /**
  218. * @ngdoc method
  219. * @name $cookieStore#put
  220. *
  221. * @description
  222. * Sets a value for given cookie key
  223. *
  224. * @param {string} key Id for the `value`.
  225. * @param {Object} value Value to be stored.
  226. */
  227. put: function(key, value) {
  228. $cookies.putObject(key, value);
  229. },
  230. /**
  231. * @ngdoc method
  232. * @name $cookieStore#remove
  233. *
  234. * @description
  235. * Remove given cookie
  236. *
  237. * @param {string} key Id of the key-value pair to delete.
  238. */
  239. remove: function(key) {
  240. $cookies.remove(key);
  241. }
  242. };
  243. }]);
  244. /**
  245. * @name $$cookieWriter
  246. * @requires $document
  247. *
  248. * @description
  249. * This is a private service for writing cookies
  250. *
  251. * @param {string} name Cookie name
  252. * @param {string=} value Cookie value (if undefined, cookie will be deleted)
  253. * @param {Object=} options Object with options that need to be stored for the cookie.
  254. */
  255. function $$CookieWriter($document, $log, $browser) {
  256. var cookiePath = $browser.baseHref();
  257. var rawDocument = $document[0];
  258. function buildCookieString(name, value, options) {
  259. var path, expires;
  260. options = options || {};
  261. expires = options.expires;
  262. path = angular.isDefined(options.path) ? options.path : cookiePath;
  263. if (value === undefined) {
  264. expires = 'Thu, 01 Jan 1970 00:00:00 GMT';
  265. value = '';
  266. }
  267. if (angular.isString(expires)) {
  268. expires = new Date(expires);
  269. }
  270. var str = encodeURIComponent(name) + '=' + encodeURIComponent(value);
  271. str += path ? ';path=' + path : '';
  272. str += options.domain ? ';domain=' + options.domain : '';
  273. str += expires ? ';expires=' + expires.toUTCString() : '';
  274. str += options.secure ? ';secure' : '';
  275. // per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
  276. // - 300 cookies
  277. // - 20 cookies per unique domain
  278. // - 4096 bytes per cookie
  279. var cookieLength = str.length + 1;
  280. if (cookieLength > 4096) {
  281. $log.warn("Cookie '" + name +
  282. "' possibly not set or overflowed because it was too large (" +
  283. cookieLength + " > 4096 bytes)!");
  284. }
  285. return str;
  286. }
  287. return function(name, value, options) {
  288. rawDocument.cookie = buildCookieString(name, value, options);
  289. };
  290. }
  291. $$CookieWriter.$inject = ['$document', '$log', '$browser'];
  292. angular.module('ngCookies').provider('$$cookieWriter', function $$CookieWriterProvider() {
  293. this.$get = $$CookieWriter;
  294. });
  295. })(window, window.angular);