1/** 2 * @license AngularJS v1.2.10 3 * (c) 2010-2014 Google, Inc. http://angularjs.org 4 * License: MIT 5 */ 6(function(window, angular, undefined) {'use strict'; 7 8/** 9 * @ngdoc overview 10 * @name ngCookies 11 * @description 12 * 13 * # ngCookies 14 * 15 * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies. 16 * 17 * {@installModule cookies} 18 * 19 * <div doc-module-components="ngCookies"></div> 20 * 21 * See {@link ngCookies.$cookies `$cookies`} and 22 * {@link ngCookies.$cookieStore `$cookieStore`} for usage. 23 */ 24 25 26angular.module('ngCookies', ['ng']). 27 /** 28 * @ngdoc object 29 * @name ngCookies.$cookies 30 * @requires $browser 31 * 32 * @description 33 * Provides read/write access to browser's cookies. 34 * 35 * Only a simple Object is exposed and by adding or removing properties to/from 36 * this object, new cookies are created/deleted at the end of current $eval. 37 * 38 * Requires the {@link ngCookies `ngCookies`} module to be installed. 39 * 40 * @example 41 <doc:example> 42 <doc:source> 43 <script> 44 function ExampleController($cookies) { 45 // Retrieving a cookie 46 var favoriteCookie = $cookies.myFavorite; 47 // Setting a cookie 48 $cookies.myFavorite = 'oatmeal'; 49 } 50 </script> 51 </doc:source> 52 </doc:example> 53 */ 54 factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) { 55 var cookies = {}, 56 lastCookies = {}, 57 lastBrowserCookies, 58 runEval = false, 59 copy = angular.copy, 60 isUndefined = angular.isUndefined; 61 62 //creates a poller fn that copies all cookies from the $browser to service & inits the service 63 $browser.addPollFn(function() { 64 var currentCookies = $browser.cookies(); 65 if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl 66 lastBrowserCookies = currentCookies; 67 copy(currentCookies, lastCookies); 68 copy(currentCookies, cookies); 69 if (runEval) $rootScope.$apply(); 70 } 71 })(); 72 73 runEval = true; 74 75 //at the end of each eval, push cookies 76 //TODO: this should happen before the "delayed" watches fire, because if some cookies are not 77 // strings or browser refuses to store some cookies, we update the model in the push fn. 78 $rootScope.$watch(push); 79 80 return cookies; 81 82 83 /** 84 * Pushes all the cookies from the service to the browser and verifies if all cookies were 85 * stored. 86 */ 87 function push() { 88 var name, 89 value, 90 browserCookies, 91 updated; 92 93 //delete any cookies deleted in $cookies 94 for (name in lastCookies) { 95 if (isUndefined(cookies[name])) { 96 $browser.cookies(name, undefined); 97 } 98 } 99 100 //update all cookies updated in $cookies 101 for(name in cookies) { 102 value = cookies[name]; 103 if (!angular.isString(value)) { 104 if (angular.isDefined(lastCookies[name])) { 105 cookies[name] = lastCookies[name]; 106 } else { 107 delete cookies[name]; 108 } 109 } else if (value !== lastCookies[name]) { 110 $browser.cookies(name, value); 111 updated = true; 112 } 113 } 114 115 //verify what was actually stored 116 if (updated){ 117 updated = false; 118 browserCookies = $browser.cookies(); 119 120 for (name in cookies) { 121 if (cookies[name] !== browserCookies[name]) { 122 //delete or reset all cookies that the browser dropped from $cookies 123 if (isUndefined(browserCookies[name])) { 124 delete cookies[name]; 125 } else { 126 cookies[name] = browserCookies[name]; 127 } 128 updated = true; 129 } 130 } 131 } 132 } 133 }]). 134 135 136 /** 137 * @ngdoc object 138 * @name ngCookies.$cookieStore 139 * @requires $cookies 140 * 141 * @description 142 * Provides a key-value (string-object) storage, that is backed by session cookies. 143 * Objects put or retrieved from this storage are automatically serialized or 144 * deserialized by angular's toJson/fromJson. 145 * 146 * Requires the {@link ngCookies `ngCookies`} module to be installed. 147 * 148 * @example 149 */
150 factory('$cookieStore', ['$cookies', function($cookies) { 151 152 return { 153 /** 154 * @ngdoc method 155 * @name ngCookies.$cookieStore#get 156 * @methodOf ngCookies.$cookieStore 157 * 158 * @description 159 * Returns the value of given cookie key 160 * 161 * @param {string} key Id to use for lookup. 162 * @returns {Object} Deserialized cookie value. 163 */ 164 get: function(key) { 165 var value = $cookies[key]; 166 return value ? angular.fromJson(value) : value; 167 }, 168 169 /** 170 * @ngdoc method 171 * @name ngCookies.$cookieStore#put 172 * @methodOf ngCookies.$cookieStore 173 * 174 * @description 175 * Sets a value for given cookie key 176 * 177 * @param {string} key Id for the `value`. 178 * @param {Object} value Value to be stored. 179 */ 180 put: function(key, value) { 181 $cookies[key] = angular.toJson(value); 182 }, 183 184 /** 185 * @ngdoc method 186 * @name ngCookies.$cookieStore#remove 187 * @methodOf ngCookies.$cookieStore 188 * 189 * @description 190 * Remove given cookie 191 * 192 * @param {string} key Id of the key-value pair to delete. 193 */ 194 remove: function(key) { 195 delete $cookies[key]; 196 } 197 }; 198 199 }]); 200 201 202})(window, window.angular);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.