PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/static/common/jquery/plugin/jquery.cookie.js

https://gitlab.com/liyue/yii-fis-bk-fe
JavaScript | 288 lines | 99 code | 44 blank | 145 comment | 35 complexity | 93c304d80702ab49da87cfd022ecefd4 MD5 | raw file
  1. /**
  2. * jQuery Cookie Plugin
  3. * @Frank.F
  4. * V1.1.2
  5. * @Change log:
  6. * @2013.11.07
  7. * 1. Redeclare jQuery namespace, in order to use jQuery.cookie() without jQuery library.
  8. * 2. Change the logic of cookie overflow. Now you cannot write the cookie when it's overflow.
  9. * 3. Add callback function named 'error' into 'set' function's options, it will be called when writing a cookie and it is overflow.
  10. * 4. Simplify jQuery.cookie.set function's logic.
  11. * 5. Commented move and clear functions for safe.
  12. * 6. Some other minor changes and improvements.
  13. * @Usage:
  14. * Original jQuery Cookie Set: $.cookie(key, value[, option]);
  15. * Original jQuery Cookie Get: $.cookie(key);
  16. * Combined Cookie Set: $.cookie.set(key, value[, expires]);
  17. * Combined Cookie Get: $.cookie.set(key);
  18. */
  19. window.jQuery || (window.jQuery = {});
  20. ;;(function ($, DOC, NAME) {
  21. var COOKIENAME = "HCD", // Current cookie name
  22. ENCODE = encodeURIComponent,
  23. DECODE = decodeURIComponent,
  24. NULL = null,
  25. MAXLENGTH = 2048, // Max length of the whole cookie data
  26. // EACHMAXLENGTH = 50, // Max length of each single/combined cookie data
  27. EXPIREDAYS = 2000, // The combined cookie's expired days
  28. /**
  29. * Whether a string is not empty
  30. * @param {String} o String value
  31. * @return {Boolean} True is not empty; false is empty
  32. */
  33. notEmpty = function (o) {
  34. return o + "" === o && o !== "";
  35. },
  36. /**
  37. * Convert expire time's format from day to millisecond
  38. * @param {Number} expires Expire days
  39. * @return {String} Expire time in duotricemary notation
  40. */
  41. toTime = function (expires) {
  42. var time = expires == 0 ? 0 : ((new Date).getTime() + expires * 86400000);
  43. // convert to duotricemary notation
  44. return time.toString(32);
  45. },
  46. /**
  47. * Convert expire time UTC string in millisecond
  48. * @param {Number} expires Expire days
  49. * @return {String} UTC String of the expire time
  50. */
  51. toExpires = function (expires) {
  52. var date = new Date;
  53. if (typeof(expires) === "number") {
  54. if (expires == 0) return "";
  55. // conver to decimal system
  56. date.setTime(parseInt(toTime(expires), 32));
  57. } else {
  58. date = expires;
  59. }
  60. return date.toUTCString();
  61. },
  62. /**
  63. * Set cookie (original usage)
  64. * @param {String} name Cookie's name
  65. * @param {String|Number} value Cookie's value
  66. * @param {Object} Option options
  67. */
  68. set = function (name, value, option) {
  69. option = option || {};
  70. //session cookie as default
  71. var expires = option.expires;
  72. // delete the cookie data while value is equal to null
  73. if (value === NULL) {
  74. expires = -1;
  75. }
  76. // overflow judgement
  77. if (expires !== -1) {
  78. var newCookie = DOC.cookie,
  79. matchedCookie = newCookie.match('(?:^|; )' + name + '(?:(?:=([^;]*))|;|$)'),
  80. setCookie = '; ' + name + '=' + value; // Cookie will be set
  81. /*if (name !== COOKIENAME && setCookie.length > EACHMAXLENGTH) {
  82. // error callback
  83. option.error && option.error();
  84. // refuse to write cookie when it's overflow
  85. return false;
  86. }*/
  87. // imitate the new cookie data before writing
  88. if (matchedCookie) {
  89. newCookie = /^;/.test(matchedCookie[0]) ? newCookie.replace(matchedCookie[0], setCookie) : newCookie.replace(matchedCookie[0], name + '=' + value);
  90. } else {
  91. newCookie += setCookie;
  92. }
  93. if (newCookie.length > MAXLENGTH) {
  94. // error callback
  95. option.error && option.error();
  96. // refuse to write cookie when it's overflow
  97. return false;
  98. }
  99. }
  100. // Write cookie
  101. DOC.cookie = [
  102. name, '=',
  103. value,
  104. expires ? '; expires=' + toExpires(expires) : '',
  105. notEmpty(option.domain) ? '; domain=' + option.domain : '',
  106. notEmpty(option.path) ? '; path=' + option.path : '',
  107. option.secure ? '; secure' : ''
  108. ].join('');
  109. },
  110. /**
  111. * Get cookie (original usage)
  112. * @param {String} name Cookie's name
  113. * @return {String} Cookie's value
  114. */
  115. get = function (name) {
  116. var result = notEmpty(name) ? DOC.cookie.match('(?:^| )' + name + '(?:(?:=([^;]*))|;|$)') : NULL;
  117. return result ? result[1] : result;
  118. },
  119. /**
  120. * Match data with name, in the format of 'name|value|time'
  121. * @param {String} name
  122. * @param {String} data
  123. * @return {String} Matched result
  124. */
  125. matchAll = function (name, data) {
  126. var result = (data || "").match('(?:^|,)(' + ENCODE(name) + '\\|[^,]+)');
  127. return result && result[1];
  128. },
  129. /**
  130. * Check whether the data is overflow and if do so, delete the last one
  131. * @param {String} data
  132. * @param {Number} length Limited length
  133. * @return {String} Result data value
  134. */
  135. /*checkLength = function (data, length) {
  136. if (data.length > length) {
  137. data = data.split(",");
  138. data.pop();
  139. data = data.join(",");
  140. data = checkLength(data, length);
  141. return data;
  142. } else {
  143. return data;
  144. }
  145. },*/
  146. /**
  147. * $.cookie() function's entrance
  148. * @param {String} name Cookie's name
  149. * @param {String|Number} value Cookie's value, call get func while is empty, otherwise call set func.
  150. * @param {Object} option Options
  151. * @return {[type]} Set or get cookie function's return value
  152. */
  153. _ = function (name, value, option) {
  154. return (value === "0" || value === 0 || value || value === NULL || value === "") ? set(ENCODE(name), value === NULL ? NULL : ENCODE(value), option) : (get(ENCODE(name)) ? DECODE(get(ENCODE(name))) : NULL);
  155. };
  156. /**
  157. * Set cookie into the combined one, you can set expires only, the other options are default as {domain:"", path:"/", secure:""}
  158. * @param {String} name Cookie's name
  159. * @param {String|Number} value Cookie's value
  160. * @param {Object} option Options
  161. */
  162. _.set = function (name, value, option) {
  163. option = option || {};
  164. //anyway, delete the original cookie data
  165. set(ENCODE(name), NULL);
  166. var expires = option.expires,
  167. result = get(COOKIENAME) || "",
  168. //old data
  169. matchData = matchAll(name, result) || "";
  170. // remove the original cookie and set the new data as the first place
  171. /*if ((new RegExp("^" + matchData.replace(/\|/g, "\\|"), "g")).test(result)) {
  172. var newData = result.replace(matchData, [ENCODE(name), ENCODE(value), toTime(expires || 0)].join("|") + (result.length && !matchData ? "," : ""));
  173. } else {
  174. var reg = new RegExp(",?" + matchData.replace(/\|/g, "\\|"), "g"),
  175. newData = ([ENCODE(name), ENCODE(value), toTime(expires || 0)].join("|") + (result.length ? "," : "")).concat(result.replace(reg, ""));
  176. }*/
  177. /*if ([ENCODE(name), ENCODE(value)].join("|").length > EACHMAXLENGTH) {
  178. // error callback
  179. option.error && option.error();
  180. // refuse to write cookie when it's overflow
  181. return false;
  182. }*/
  183. // remove the original cookie and get the new entire HCD cookie data
  184. if (new RegExp(matchData.replace(/\|/g, "\\|"), "g").test(result)) {
  185. var newData = result.replace(matchData, [ENCODE(name), ENCODE(value), toTime(expires || 0)].join("|") + (result.length && !matchData ? "," : ""));
  186. }
  187. // if the data's length is overflow, then delete the last one
  188. // newData = checkLength(newData, MAXLENGTH);
  189. //expires ==> timestamp
  190. set(COOKIENAME, newData, { expires: EXPIREDAYS, domain: '', error: option.error });
  191. return _;
  192. };
  193. /**
  194. * Get cookie from the combined one
  195. * @param {String} name Cookie's name
  196. * @return {String} Cookie's value
  197. */
  198. _.get = function (name) {
  199. //timestamp
  200. var _data = get(ENCODE(name));
  201. if (_data !== NULL) return DECODE(_data);
  202. var data = get(COOKIENAME) || "",
  203. matchData = matchAll(name, data);
  204. //null
  205. if (!matchData) return matchData;
  206. matchData = matchData.split("|");
  207. //is not expired
  208. if (matchData[2] == 0 || parseInt(matchData[2], 32) > (new Date).getTime()) return DECODE(matchData[1]);
  209. //delete when the cookie is exist but expired
  210. _.del(ENCODE(name));
  211. return NULL;
  212. };
  213. /**
  214. * COMMENTED: Migrate the old cookie data into the combined one
  215. * @param {String} name Cookie's name which need to be migrated
  216. * @param {Number} expires Expire days
  217. */
  218. /*_.move = function (name, expires) {
  219. var value = get(ENCODE(name));
  220. value !== NULL && _.set(name, DECODE(value), expires || 365);
  221. return _;
  222. };*/
  223. /**
  224. * Delete a cookie from the combined one
  225. * @param {String} name Cookie's name which need to be deleted
  226. */
  227. _.del = function (name) {
  228. //anyway, kill old
  229. set(ENCODE(name), NULL);
  230. var data = get(COOKIENAME) || "",
  231. matchData = matchAll(name, data) || "",
  232. matchReg = new RegExp(",?" + matchData.replace(/\|/g, "\\|") + ",?", "g");
  233. matchData && set(COOKIENAME, data.replace(matchReg, ""));
  234. return _;
  235. };
  236. /** COMMENTED: Clear the combined cookie */
  237. /*_.clear = function () {
  238. set(COOKIENAME, "");
  239. return _;
  240. };*/
  241. _.is = !!navigator.cookieEnabled;
  242. $[NAME] = _;
  243. })(jQuery, document, "cookie");