Advertisement
Guest User

Untitled

a guest
Aug 9th, 2011
1,571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. var cram = (function() {
  2. function appendDiv(fn) {
  3. if (!document.createElement)
  4. return null;
  5. var body = document.getElementsByTagName('body');
  6. body = body.length > 0 ? body[0] : null;
  7. if (!body)
  8. return null;
  9. var div = document.createElement('div');
  10. if (!div)
  11. return null;
  12. fn(div);
  13. body.appendChild(div);
  14. return div;
  15. }
  16. var msieVersion = function() {
  17. if (navigator.appName == "Microsoft Internet Explorer" && /MSIE ([0-9]+[\.0-9]*)/.test(navigator.userAgent)) {
  18. return parseFloat(RegExp.$1);
  19. }
  20. return 0;
  21. };
  22. var backend = function() {
  23. this.store = {};
  24. this.get = function(key) {
  25. return this.store[key];
  26. }
  27. this.set = function(key, value) {
  28. this.store[key] = value;
  29. }
  30. this.remove = function(key) {
  31. delete this.store[key];
  32. }
  33. };
  34. backend.valid = function() {
  35. return true;
  36. }
  37. backend.create = function(valid, init) {
  38. var k = init || function() {};
  39. k.valid = valid || backend.valid;
  40. k.prototype = new backend();
  41. return k;
  42. };
  43. var html5 = backend.create(function() {
  44. return window.localStorage && window.localStorage.getItem;
  45. }, function() {
  46. var store = window.localStorage;
  47. this.get = function(key) {
  48. if (store)
  49. return store.getItem(key);
  50. }
  51. this.set = function(k, v) {
  52. if (store)
  53. store.setItem(k, v);
  54. }
  55. this.remove = function(key) {
  56. if (store)
  57. store.removeItem(key);
  58. }
  59. });
  60. var userData = backend.create(function() {
  61. return !! window.ActiveXObject && msieVersion() >= 7.0;
  62. }, function() {
  63. var store = appendDiv(function(div) {
  64. div.id = '_cram_userData';
  65. div.style.display = 'none';
  66. div.addBehavior('#default#userData');
  67. });
  68. store.load("_cram");
  69. this.get = function(k) {
  70. if (store)
  71. return store.getAttribute(k);
  72. };
  73. this.set = function(k, v) {
  74. if (store)
  75. store.setAttribute(k, v);
  76. store.save("_cram");
  77. }
  78. this.remove = function(k) {
  79. store.removeAttribute(k);
  80. }
  81. this.free = function() {
  82. store = null;
  83. };
  84. });
  85. var flash = backend.create(function() {
  86. return window.SWFObject;
  87. }, function() {
  88. if (document.getElementById('_cram_flash')) {
  89. return;
  90. }
  91. var div = appendDiv(function(div) {
  92. div.id = '_cram_flash';
  93. div.style.position = 'absolute';
  94. div.style.top = '-100px';
  95. div.style.left = '-100px';
  96. });
  97. if (!div)
  98. return;
  99. var so = new SWFObject("/cram.swf", "_cram_swf", "1", "1", "9");
  100. so.addParam("allowScriptAccess", "sameDomain");
  101. if (so.write("_cram_flash")) {
  102. var swf = document.getElementById('_cram_swf');
  103. if (swf) {
  104. this.get = function(k) {
  105. if (swf && swf.get)
  106. return swf.get(k);
  107. };
  108. this.set = function(k, v) {
  109. if (swf && swf.set)
  110. swf.set(k, v);
  111. };
  112. this.remove = function(k) {
  113. if (swf && swf.remove)
  114. swf.remove(k);
  115. };
  116. this.free = function() {
  117. swf = null;
  118. };
  119. }
  120. }
  121. });
  122. var methods = {
  123. html5: html5,
  124. flash: flash
  125. };
  126. var order = ['html5', 'flash'];
  127. var store = null;
  128. var self = {
  129. load: function() {
  130. for (var i = 0; i < order.length; i++) {
  131. var method = methods[order[i]];
  132. if (method.valid()) {
  133. store = new method();
  134. break;
  135. }
  136. }
  137. document.fire('cram:load');
  138. },
  139. methods: methods,
  140. valid: function() {
  141. return !! store;
  142. },
  143. unload: function() {
  144. if (store && store.free)
  145. store.free();
  146. document.fire('cram:unload');
  147. },
  148. setStore: function(manualStore) {
  149. store = manualStore;
  150. },
  151. get: function(key) {
  152. var r = null;
  153. if (store) {
  154. var value = store.get(key) || "null";
  155. r = unescape(value).evalJSON(true);
  156. }
  157. return r;
  158. },
  159. set: function(key, value) {
  160. if (store)
  161. store.set(key, escape(Object.toJSON(value)));
  162. },
  163. remove: function(key) {
  164. if (store)
  165. store.remove(key);
  166. }
  167. };
  168. if (Event && Event.observe) {
  169. Event.observe(window, "load", self.load);
  170. Event.observe(window, "unload", self.unload);
  171. }
  172. return self;
  173. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement