PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Documentation/Help/scripts/CookieDataStore.js

#
JavaScript | 136 lines | 88 code | 40 blank | 8 comment | 19 complexity | 4883587e079cf93494e8de965bb45304 MD5 | raw file
Possible License(s): MIT
  1. function setCookie(name, value, expires, path, domain, secure) {
  2. var text = name + "=" + escape(value);
  3. if (expires) {
  4. var currentDate = new Date();
  5. var expireDate = new Date( currentDate.getTime() + expires*24*60*60*1000 );
  6. text = text + ";expires=" + expireDate.toGMTString();
  7. }
  8. if (path) text = text + ";path=" + path;
  9. if (domain) text = text + ";domain=" + domain;
  10. if (secure) text = text + ";secure";
  11. document.cookie = text;
  12. }
  13. function getCookie(name) {
  14. var text = document.cookie;
  15. var index = text.indexOf(name + "=");
  16. if (index < 0) return(null);
  17. var start = index + name.length + 1;
  18. var end = text.indexOf(";", start);
  19. if (end < 0) end = text.length;
  20. var value = unescape( text.substring(start, end) );
  21. return(value);
  22. }
  23. function removeCookie(name) {
  24. setCookie(name, "", -1);
  25. }
  26. // cookie data store
  27. function CookieDataStore(name) {
  28. this.name = name;
  29. this.load();
  30. }
  31. CookieDataStore.prototype.load = function () {
  32. // create a key/value store
  33. this.data = new Object();
  34. // get cookie text
  35. var text = getCookie(this.name);
  36. if (text == null) return;
  37. // populate the store using the cookie text
  38. var data = text.split(';');
  39. for (var i=0; i<data.length; i++) {
  40. var datum = data[i];
  41. var index = datum.indexOf('=');
  42. if (index > 0) {
  43. var key = datum.substring(0,index);
  44. var value = datum.substring(index+1);
  45. this.data[key] = value;
  46. }
  47. }
  48. }
  49. CookieDataStore.prototype.save = function () {
  50. // prepare a cookie string
  51. var text = "";
  52. // construct the string
  53. for (var key in this.data) {
  54. var datum = key + "=" + this.data[key];
  55. text = text + datum + ";";
  56. }
  57. // set it
  58. setCookie(this.name, text);
  59. }
  60. CookieDataStore.prototype.clear = function () {
  61. this.data = new Object();
  62. }
  63. CookieDataStore.prototype.set = function(key, value) {
  64. this.data[key] = value;
  65. }
  66. CookieDataStore.prototype.get = function(key) {
  67. return(this.data[key]);
  68. }
  69. CookieDataStore.prototype.remove = function(key) {
  70. delete(this.data[key]);
  71. }
  72. CookieDataStore.prototype.count = function() {
  73. var i = 0;
  74. for (var key in this.data) {
  75. i++;
  76. }
  77. return(i);
  78. }
  79. // The following logic needs to be re-factored out of this file
  80. function selectLanguage(value) {
  81. if (value == null) return;
  82. var selector = document.getElementById('languageSelector');
  83. if (selector == null) return;
  84. var options = selector.options;
  85. for(var i=0; i<options.length; i++) {
  86. if (options[i].value == value) {
  87. selector.selectedIndex = i;
  88. setLanguage(value);
  89. }
  90. }
  91. }
  92. function setLanguage(value) {
  93. var names = value.split(' ');
  94. toggleVisibleLanguage(names[1]);
  95. lfc.switchLanguage(names[0]);
  96. }