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