/lib/cookie.polypage.jquery.js

https://github.com/alvarlaigna/polypage · JavaScript · 120 lines · 89 code · 13 blank · 18 comment · 27 complexity · 20463795aef1768337662aacb75ebcf0 MD5 · raw file

  1. /**
  2. * Cookie plugin
  3. *
  4. * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  5. * Dual licensed under the MIT and GPL licenses:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. * http://www.gnu.org/licenses/gpl.html
  8. *
  9. */
  10. /*
  11. * Altered by Natalie Downe <nat@natbat.net> to make $cookie() with no arguments return an array of the set cookies
  12. * eg [["cookiename1", "cookievalue1"], ["cookiename2", "cookievalue2"]]
  13. */
  14. jQuery.cookie = function(name, value, options) {
  15. if (typeof value != 'undefined') { // name and value given, set cookie
  16. options = options || {};
  17. if (value === null) {
  18. value = '';
  19. options.expires = -1;
  20. }
  21. var expires = '';
  22. if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
  23. var date;
  24. if (typeof options.expires == 'number') {
  25. date = new Date();
  26. date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  27. } else {
  28. date = options.expires;
  29. }
  30. expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
  31. }
  32. var path = options.path ? '; path=' + options.path : '';
  33. var domain = options.domain ? '; domain=' + options.domain : '';
  34. var secure = options.secure ? '; secure' : '';
  35. document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  36. } else if(typeof name != 'undefined') { // only name given, get cookie
  37. var cookieValue = null;
  38. if (document.cookie && document.cookie != '') {
  39. var cookies = document.cookie.split(';');
  40. for (var i = 0; i < cookies.length; i++) {
  41. var cookie = jQuery.trim(cookies[i]);
  42. // Does this cookie string begin with the name we want?
  43. if (cookie.substring(0, name.length + 1) == (name + '=')) {
  44. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  45. break;
  46. }
  47. }
  48. }
  49. return cookieValue;
  50. } else {
  51. // not given anything
  52. var cookies = document.cookie.split(';');
  53. for (var i = 0; i < cookies.length; i++) {
  54. cookies[i] = jQuery.trim(cookies[i]).split('=')[0];
  55. }
  56. return cookies;
  57. }
  58. };
  59. // =============================
  60. // = PolyPage Cookie Extension =
  61. // =============================
  62. (function($) {
  63. $.polypage.extension('Cookie',
  64. {
  65. expires: null, // can be a Date object or a integer number of days
  66. namespace:'pp_'
  67. },
  68. {
  69. init: function(){
  70. this.checkForCookieSupport();
  71. this.setInitialStateFromCookie();
  72. this.bindStateChangeListener();
  73. },
  74. checkForCookieSupport: function(){
  75. if($.cookie===undefined) throw "The jQuery Cookie plugin is required to use the ppCookie() extension but was not found at $.cookie()";
  76. },
  77. setInitialStateFromCookie: function(){
  78. var states = this.polypage.statesList();
  79. for(var i in states) {
  80. var stateName = states[i];
  81. if(this.getCookie(stateName)) {
  82. var obj = {}
  83. obj[stateName]=true
  84. this.scope.trigger('pp_setState', obj);
  85. }
  86. }
  87. },
  88. setCookie: function(state,val) {
  89. if(!state.length) return false;
  90. $.cookie(this.prefixed(state), val ? 'yes' : null, {"path":"/", "expires":this.options.expires});
  91. return true;
  92. },
  93. getCookie: function(state) {
  94. if(!state.length) return false;
  95. return $.cookie(this.prefixed(state))!=null;
  96. },
  97. prefixed: function(state) {
  98. return this.options.namespace+state;
  99. },
  100. bindStateChangeListener: function(){
  101. var c = this;
  102. this.polypageScope.bind('pp_stateChange', function(e, state) {
  103. c.setCookie(state.name, state.value);
  104. });
  105. }
  106. });
  107. })(jQuery);