RoadFlow2.1 临时演示

jquery.cookie.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * Copyright (c) 2005 - 2010, James Auldridge
  3. * All rights reserved.
  4. *
  5. * Licensed under the BSD, MIT, and GPL (your choice!) Licenses:
  6. * http://code.google.com/p/cookies/wiki/License
  7. *
  8. */
  9. var jaaulde = window.jaaulde || {};
  10. jaaulde.utils = jaaulde.utils || {};
  11. jaaulde.utils.cookies = (function ()
  12. {
  13. var resolveOptions, assembleOptionsString, parseCookies, constructor, defaultOptions = {
  14. expiresAt: null,
  15. path: '/',
  16. domain: null,
  17. secure: false
  18. };
  19. /**
  20. * resolveOptions - receive an options object and ensure all options are present and valid, replacing with defaults where necessary
  21. *
  22. * @access private
  23. * @static
  24. * @parameter Object options - optional options to start with
  25. * @return Object complete and valid options object
  26. */
  27. resolveOptions = function (options)
  28. {
  29. var returnValue, expireDate;
  30. if (typeof options !== 'object' || options === null)
  31. {
  32. returnValue = defaultOptions;
  33. }
  34. else
  35. {
  36. returnValue = {
  37. expiresAt: defaultOptions.expiresAt,
  38. path: defaultOptions.path,
  39. domain: defaultOptions.domain,
  40. secure: defaultOptions.secure
  41. };
  42. if (typeof options.expiresAt === 'object' && options.expiresAt instanceof Date)
  43. {
  44. returnValue.expiresAt = options.expiresAt;
  45. }
  46. else if (typeof options.hoursToLive === 'number' && options.hoursToLive !== 0)
  47. {
  48. expireDate = new Date();
  49. expireDate.setTime(expireDate.getTime() + (options.hoursToLive * 60 * 60 * 1000));
  50. returnValue.expiresAt = expireDate;
  51. }
  52. if (typeof options.path === 'string' && options.path !== '')
  53. {
  54. returnValue.path = options.path;
  55. }
  56. if (typeof options.domain === 'string' && options.domain !== '')
  57. {
  58. returnValue.domain = options.domain;
  59. }
  60. if (options.secure === true)
  61. {
  62. returnValue.secure = options.secure;
  63. }
  64. }
  65. return returnValue;
  66. };
  67. /**
  68. * assembleOptionsString - analyze options and assemble appropriate string for setting a cookie with those options
  69. *
  70. * @access private
  71. * @static
  72. * @parameter options OBJECT - optional options to start with
  73. * @return STRING - complete and valid cookie setting options
  74. */
  75. assembleOptionsString = function (options)
  76. {
  77. options = resolveOptions(options);
  78. return (
  79. (typeof options.expiresAt === 'object' && options.expiresAt instanceof Date ? '; expires=' + options.expiresAt.toGMTString() : '') +
  80. '; path=' + options.path +
  81. (typeof options.domain === 'string' ? '; domain=' + options.domain : '') +
  82. (options.secure === true ? '; secure' : '')
  83. );
  84. };
  85. /**
  86. * parseCookies - retrieve document.cookie string and break it into a hash with values decoded and unserialized
  87. *
  88. * @access private
  89. * @static
  90. * @return OBJECT - hash of cookies from document.cookie
  91. */
  92. parseCookies = function ()
  93. {
  94. var cookies = {}, i, pair, name, value, separated = document.cookie.split(';'), unparsedValue;
  95. for (i = 0; i < separated.length; i = i + 1)
  96. {
  97. pair = separated[i].split('=');
  98. name = pair[0].replace(/^\s*/, '').replace(/\s*$/, '');
  99. try
  100. {
  101. value = decodeURIComponent(pair[1]);
  102. }
  103. catch (e1)
  104. {
  105. value = pair[1];
  106. }
  107. if (typeof JSON === 'object' && JSON !== null && typeof JSON.parse === 'function')
  108. {
  109. try
  110. {
  111. unparsedValue = value;
  112. value = JSON.parse(value);
  113. }
  114. catch (e2)
  115. {
  116. value = unparsedValue;
  117. }
  118. }
  119. cookies[name] = value;
  120. }
  121. return cookies;
  122. };
  123. constructor = function () { };
  124. /**
  125. * get - get one, several, or all cookies
  126. *
  127. * @access public
  128. * @paramater Mixed cookieName - String:name of single cookie; Array:list of multiple cookie names; Void (no param):if you want all cookies
  129. * @return Mixed - Value of cookie as set; Null:if only one cookie is requested and is not found; Object:hash of multiple or all cookies (if multiple or all requested);
  130. */
  131. constructor.prototype.get = function (cookieName)
  132. {
  133. var returnValue, item, cookies = parseCookies();
  134. if (typeof cookieName === 'string')
  135. {
  136. returnValue = (typeof cookies[cookieName] !== 'undefined') ? cookies[cookieName] : null;
  137. }
  138. else if (typeof cookieName === 'object' && cookieName !== null)
  139. {
  140. returnValue = {};
  141. for (item in cookieName)
  142. {
  143. if (typeof cookies[cookieName[item]] !== 'undefined')
  144. {
  145. returnValue[cookieName[item]] = cookies[cookieName[item]];
  146. }
  147. else
  148. {
  149. returnValue[cookieName[item]] = null;
  150. }
  151. }
  152. }
  153. else
  154. {
  155. returnValue = cookies;
  156. }
  157. return returnValue;
  158. };
  159. /**
  160. * filter - get array of cookies whose names match the provided RegExp
  161. *
  162. * @access public
  163. * @paramater Object RegExp - The regular expression to match against cookie names
  164. * @return Mixed - Object:hash of cookies whose names match the RegExp
  165. */
  166. constructor.prototype.filter = function (cookieNameRegExp)
  167. {
  168. var cookieName, returnValue = {}, cookies = parseCookies();
  169. if (typeof cookieNameRegExp === 'string')
  170. {
  171. cookieNameRegExp = new RegExp(cookieNameRegExp);
  172. }
  173. for (cookieName in cookies)
  174. {
  175. if (cookieName.match(cookieNameRegExp))
  176. {
  177. returnValue[cookieName] = cookies[cookieName];
  178. }
  179. }
  180. return returnValue;
  181. };
  182. /**
  183. * set - set or delete a cookie with desired options
  184. *
  185. * @access public
  186. * @paramater String cookieName - name of cookie to set
  187. * @paramater Mixed value - Any JS value. If not a string, will be JSON encoded; NULL to delete
  188. * @paramater Object options - optional list of cookie options to specify
  189. * @return void
  190. */
  191. constructor.prototype.set = function (cookieName, value, options)
  192. {
  193. if (typeof options !== 'object' || options === null)
  194. {
  195. options = {};
  196. }
  197. if (typeof value === 'undefined' || value === null)
  198. {
  199. value = '';
  200. options.hoursToLive = -8760;
  201. }
  202. else if (typeof value !== 'string')
  203. {
  204. if (typeof JSON === 'object' && JSON !== null && typeof JSON.stringify === 'function')
  205. {
  206. value = JSON.stringify(value);
  207. }
  208. else
  209. {
  210. throw new Error('cookies.set() received non-string value and could not serialize.');
  211. }
  212. }
  213. var optionsString = assembleOptionsString(options);
  214. document.cookie = cookieName + '=' + encodeURIComponent(value) + optionsString;
  215. };
  216. /**
  217. * del - delete a cookie (domain and path options must match those with which the cookie was set; this is really an alias for set() with parameters simplified for this use)
  218. *
  219. * @access public
  220. * @paramater MIxed cookieName - String name of cookie to delete, or Bool true to delete all
  221. * @paramater Object options - optional list of cookie options to specify ( path, domain )
  222. * @return void
  223. */
  224. constructor.prototype.del = function (cookieName, options)
  225. {
  226. var allCookies = {}, name;
  227. if (typeof options !== 'object' || options === null)
  228. {
  229. options = {};
  230. }
  231. if (typeof cookieName === 'boolean' && cookieName === true)
  232. {
  233. allCookies = this.get();
  234. }
  235. else if (typeof cookieName === 'string')
  236. {
  237. allCookies[cookieName] = true;
  238. }
  239. for (name in allCookies)
  240. {
  241. if (typeof name === 'string' && name !== '')
  242. {
  243. this.set(name, null, options);
  244. }
  245. }
  246. };
  247. /**
  248. * test - test whether the browser is accepting cookies
  249. *
  250. * @access public
  251. * @return Boolean
  252. */
  253. constructor.prototype.test = function ()
  254. {
  255. var returnValue = false, testName = 'cT', testValue = 'data';
  256. this.set(testName, testValue);
  257. if (this.get(testName) === testValue)
  258. {
  259. this.del(testName);
  260. returnValue = true;
  261. }
  262. return returnValue;
  263. };
  264. /**
  265. * setOptions - set default options for calls to cookie methods
  266. *
  267. * @access public
  268. * @param Object options - list of cookie options to specify
  269. * @return void
  270. */
  271. constructor.prototype.setOptions = function (options)
  272. {
  273. if (typeof options !== 'object')
  274. {
  275. options = null;
  276. }
  277. defaultOptions = resolveOptions(options);
  278. };
  279. return new constructor();
  280. })();
  281. (function ()
  282. {
  283. if (window.jQuery)
  284. {
  285. (function ($)
  286. {
  287. $.cookies = jaaulde.utils.cookies;
  288. var extensions = {
  289. /**
  290. * $( 'selector' ).cookify - set the value of an input field, or the innerHTML of an element, to a cookie by the name or id of the field or element
  291. * (field or element MUST have name or id attribute)
  292. *
  293. * @access public
  294. * @param options OBJECT - list of cookie options to specify
  295. * @return jQuery
  296. */
  297. cookify: function (options)
  298. {
  299. return this.each(function ()
  300. {
  301. var i, nameAttrs = ['name', 'id'], name, $this = $(this), value;
  302. for (i in nameAttrs)
  303. {
  304. if (!isNaN(i))
  305. {
  306. name = $this.attr(nameAttrs[i]);
  307. if (typeof name === 'string' && name !== '')
  308. {
  309. if ($this.is(':checkbox, :radio'))
  310. {
  311. if ($this.attr('checked'))
  312. {
  313. value = $this.val();
  314. }
  315. }
  316. else if ($this.is(':input'))
  317. {
  318. value = $this.val();
  319. }
  320. else
  321. {
  322. value = $this.html();
  323. }
  324. if (typeof value !== 'string' || value === '')
  325. {
  326. value = null;
  327. }
  328. $.cookies.set(name, value, options);
  329. break;
  330. }
  331. }
  332. }
  333. });
  334. },
  335. /**
  336. * $( 'selector' ).cookieFill - set the value of an input field or the innerHTML of an element from a cookie by the name or id of the field or element
  337. *
  338. * @access public
  339. * @return jQuery
  340. */
  341. cookieFill: function ()
  342. {
  343. return this.each(function ()
  344. {
  345. var n, getN, nameAttrs = ['name', 'id'], name, $this = $(this), value;
  346. getN = function ()
  347. {
  348. n = nameAttrs.pop();
  349. return !!n;
  350. };
  351. while (getN())
  352. {
  353. name = $this.attr(n);
  354. if (typeof name === 'string' && name !== '')
  355. {
  356. value = $.cookies.get(name);
  357. if (value !== null)
  358. {
  359. if ($this.is(':checkbox, :radio'))
  360. {
  361. if ($this.val() === value)
  362. {
  363. $this.attr('checked', 'checked');
  364. }
  365. else
  366. {
  367. $this.removeAttr('checked');
  368. }
  369. }
  370. else if ($this.is(':input'))
  371. {
  372. $this.val(value);
  373. }
  374. else
  375. {
  376. $this.html(value);
  377. }
  378. }
  379. break;
  380. }
  381. }
  382. });
  383. },
  384. /**
  385. * $( 'selector' ).cookieBind - call cookie fill on matching elements, and bind their change events to cookify()
  386. *
  387. * @access public
  388. * @param options OBJECT - list of cookie options to specify
  389. * @return jQuery
  390. */
  391. cookieBind: function (options)
  392. {
  393. return this.each(function ()
  394. {
  395. var $this = $(this);
  396. $this.cookieFill().change(function ()
  397. {
  398. $this.cookify(options);
  399. });
  400. });
  401. }
  402. };
  403. $.each(extensions, function (i)
  404. {
  405. $.fn[i] = this;
  406. });
  407. })(window.jQuery);
  408. }
  409. })();