|
| 1 | +/*! |
| 2 | + * jQuery Cookie Plugin v1.3 |
| 3 | + * https://github.com/carhartl/jquery-cookie |
| 4 | + * |
| 5 | + * Copyright 2011, Klaus Hartl |
| 6 | + * Dual licensed under the MIT or GPL Version 2 licenses. |
| 7 | + * http://www.opensource.org/licenses/mit-license.php |
| 8 | + * http://www.opensource.org/licenses/GPL-2.0 |
| 9 | + */ |
| 10 | +(function ($, document, undefined) { |
| 11 | + |
| 12 | + var pluses = /\+/g; |
| 13 | + |
| 14 | + function raw(s) { |
| 15 | + return s; |
| 16 | + } |
| 17 | + |
| 18 | + function decoded(s) { |
| 19 | + return decodeURIComponent(s.replace(pluses, ' ')); |
| 20 | + } |
| 21 | + |
| 22 | + var config = $.cookie = function (key, value, options) { |
| 23 | + |
| 24 | + // write |
| 25 | + if (value !== undefined) { |
| 26 | + options = $.extend({}, config.defaults, options); |
| 27 | + |
| 28 | + if (value === null) { |
| 29 | + options.expires = -1; |
| 30 | + } |
| 31 | + |
| 32 | + if (typeof options.expires === 'number') { |
| 33 | + var days = options.expires, t = options.expires = new Date(); |
| 34 | + t.setDate(t.getDate() + days); |
| 35 | + } |
| 36 | + |
| 37 | + value = config.json ? JSON.stringify(value) : String(value); |
| 38 | + |
| 39 | + return (document.cookie = [ |
| 40 | + encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value), |
| 41 | + options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
| 42 | + options.path ? '; path=' + options.path : '', |
| 43 | + options.domain ? '; domain=' + options.domain : '', |
| 44 | + options.secure ? '; secure' : '' |
| 45 | + ].join('')); |
| 46 | + } |
| 47 | + |
| 48 | + // read |
| 49 | + var decode = config.raw ? raw : decoded; |
| 50 | + var cookies = document.cookie.split('; '); |
| 51 | + for (var i = 0, l = cookies.length; i < l; i++) { |
| 52 | + var parts = cookies[i].split('='); |
| 53 | + if (decode(parts.shift()) === key) { |
| 54 | + var cookie = decode(parts.join('=')); |
| 55 | + return config.json ? JSON.parse(cookie) : cookie; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + }; |
| 61 | + |
| 62 | + config.defaults = {}; |
| 63 | + |
| 64 | + $.removeCookie = function (key, options) { |
| 65 | + if ($.cookie(key) !== null) { |
| 66 | + $.cookie(key, null, options); |
| 67 | + return true; |
| 68 | + } |
| 69 | + return false; |
| 70 | + }; |
| 71 | + |
| 72 | +})(jQuery, document); |
| 73 | + |
| 74 | +/*! |
| 75 | + * Data Saver - v0.1 |
| 76 | + * |
| 77 | + * Copyright (c) 2013 Dave Olsen, http://dmolsen.com |
| 78 | + * Licensed under the MIT license |
| 79 | + */ |
| 80 | + |
| 81 | +var DataSaver = { |
| 82 | + |
| 83 | + // the name of the cookie to store the data in |
| 84 | + cookieName: "patternlab", |
| 85 | + |
| 86 | + /** |
| 87 | + * Add a given value to the cookie |
| 88 | + * @param {String} the name of the key |
| 89 | + * @param {String} the value |
| 90 | + */ |
| 91 | + addValue: function (name,val) { |
| 92 | + var cookieVal = $.cookie(this.cookieName); |
| 93 | + if ((cookieVal == null) || (cookieVal == "")) { |
| 94 | + cookieVal = name+"~"+val; |
| 95 | + } else { |
| 96 | + cookieVal = cookieVal+"|"+name+"~"+val; |
| 97 | + } |
| 98 | + $.cookie(this.cookieName,cookieVal); |
| 99 | + }, |
| 100 | + |
| 101 | + /** |
| 102 | + * Update a value found in the cookie. If the key doesn't exist add the value |
| 103 | + * @param {String} the name of the key |
| 104 | + * @param {String} the value |
| 105 | + */ |
| 106 | + updateValue: function (name,val) { |
| 107 | + if (this.findValue(name)) { |
| 108 | + var updateCookieVals = ""; |
| 109 | + var cookieVals = $.cookie(this.cookieName).split("|"); |
| 110 | + for (var i = 0; i < cookieVals.length; i++) { |
| 111 | + var fieldVals = cookieVals[i].split("~"); |
| 112 | + if (fieldVals[0] == name) { |
| 113 | + fieldVals[1] = val; |
| 114 | + } |
| 115 | + if (i > 0) { |
| 116 | + updateCookieVals += "|"+fieldVals[0]+"~"+fieldVals[1]; |
| 117 | + } else { |
| 118 | + updateCookieVals += fieldVals[0]+"~"+fieldVals[1]; |
| 119 | + } |
| 120 | + } |
| 121 | + $.cookie(this.cookieName,updateCookieVals); |
| 122 | + } else { |
| 123 | + this.addValue(name,val); |
| 124 | + } |
| 125 | + }, |
| 126 | + |
| 127 | + /** |
| 128 | + * Remove the given key |
| 129 | + * @param {String} the name of the key |
| 130 | + */ |
| 131 | + removeValue: function (name) { |
| 132 | + var updateCookieVals = ""; |
| 133 | + var cookieVals = $.cookie(this.cookieName).split("|"); |
| 134 | + var k = 0; |
| 135 | + for (var i = 0; i < cookieVals.length; i++) { |
| 136 | + var fieldVals = cookieVals[i].split("~"); |
| 137 | + if (fieldVals[0] != name) { |
| 138 | + if (k == 0) { |
| 139 | + updateCookieVals += fieldVals[0]+"~"+fieldVals[1]; |
| 140 | + } else { |
| 141 | + updateCookieVals += "|"+fieldVals[0]+"~"+fieldVals[1]; |
| 142 | + } |
| 143 | + k++; |
| 144 | + } |
| 145 | + } |
| 146 | + $.cookie(this.cookieName,updateCookieVals); |
| 147 | + }, |
| 148 | + |
| 149 | + /** |
| 150 | + * Find the value using the given key |
| 151 | + * @param {String} the name of the key |
| 152 | + * |
| 153 | + * @return {String} the value of the key or false if the value isn't found |
| 154 | + */ |
| 155 | + findValue: function (name) { |
| 156 | + if ($.cookie(this.cookieName)) { |
| 157 | + var cookieVals = $.cookie(this.cookieName).split("|"); |
| 158 | + var k = 0; |
| 159 | + for (var i = 0; i < cookieVals.length; i++) { |
| 160 | + var fieldVals = cookieVals[i].split("~"); |
| 161 | + if (fieldVals[0] == name) { |
| 162 | + return fieldVals[1]; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | +}; |
0 commit comments