cookiebaker.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Trex.I.CookieBaker = Trex.Faculty.create({
  2. cookieName: _NULL,
  3. cookieValue: _NULL,
  4. initCookie: function(name, maxCnt) {
  5. this.cookieName = name;
  6. this.cookieValue = function () {
  7. var cookies = _DOC.cookie.split(';');
  8. for(var i=0; i<cookies.length; i++){
  9. var cookie=cookies[i].replace(/^\s+/, '');
  10. if (cookie.indexOf(name+'=')==0) return cookie.substring(name.length+1);
  11. }
  12. return _NULL;
  13. }() || "";
  14. this.maxCnt = maxCnt || 3;
  15. },
  16. writeCookie: function(value, days){
  17. var name = this.cookieName, exp;
  18. if(days){
  19. var time = new Date();
  20. time.setTime(new Date().getTime()+days*24*60*60*1000);
  21. exp = '; expires='+time.toGMTString();
  22. }else{
  23. exp='';
  24. }
  25. if (value === _NULL) {//NOTE: #FTDUEDTR-888
  26. value = '';
  27. }
  28. _DOC.cookie=name+"="+value+exp+"; path=/";
  29. this.cookieValue = value;
  30. },
  31. readCookie: function () {
  32. if (this.cookieValue === _NULL+'') {//NOTE: #FTDUEDTR-888
  33. return _NULL;
  34. }
  35. return this.cookieValue;
  36. },
  37. eraseCookie: function () {
  38. var name = this.cookieName;
  39. this.writeCookie(name, "", -1);
  40. },
  41. extractOptions: function(options, value) {
  42. var _optionMap = options.toMap('data');
  43. var _values = [];
  44. value.split('|').compact().each(function(data) {
  45. if(_optionMap[data]) {
  46. _values.push(_optionMap[data]);
  47. }
  48. }.bind(this));
  49. return _values;
  50. },
  51. mergeValues: function(value, data) {
  52. var _values = value.split('|').compact();
  53. if(_values.contains(data)) {
  54. return value;
  55. }
  56. if(_values.length >= this.maxCnt) {
  57. _values.pop();
  58. }
  59. _values.unshift(data);
  60. return _values.join('|');
  61. }
  62. });