Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cram = (function() {
- function appendDiv(fn) {
- if (!document.createElement)
- return null;
- var body = document.getElementsByTagName('body');
- body = body.length > 0 ? body[0] : null;
- if (!body)
- return null;
- var div = document.createElement('div');
- if (!div)
- return null;
- fn(div);
- body.appendChild(div);
- return div;
- }
- var msieVersion = function() {
- if (navigator.appName == "Microsoft Internet Explorer" && /MSIE ([0-9]+[\.0-9]*)/.test(navigator.userAgent)) {
- return parseFloat(RegExp.$1);
- }
- return 0;
- };
- var backend = function() {
- this.store = {};
- this.get = function(key) {
- return this.store[key];
- }
- this.set = function(key, value) {
- this.store[key] = value;
- }
- this.remove = function(key) {
- delete this.store[key];
- }
- };
- backend.valid = function() {
- return true;
- }
- backend.create = function(valid, init) {
- var k = init || function() {};
- k.valid = valid || backend.valid;
- k.prototype = new backend();
- return k;
- };
- var html5 = backend.create(function() {
- return window.localStorage && window.localStorage.getItem;
- }, function() {
- var store = window.localStorage;
- this.get = function(key) {
- if (store)
- return store.getItem(key);
- }
- this.set = function(k, v) {
- if (store)
- store.setItem(k, v);
- }
- this.remove = function(key) {
- if (store)
- store.removeItem(key);
- }
- });
- var userData = backend.create(function() {
- return !! window.ActiveXObject && msieVersion() >= 7.0;
- }, function() {
- var store = appendDiv(function(div) {
- div.id = '_cram_userData';
- div.style.display = 'none';
- div.addBehavior('#default#userData');
- });
- store.load("_cram");
- this.get = function(k) {
- if (store)
- return store.getAttribute(k);
- };
- this.set = function(k, v) {
- if (store)
- store.setAttribute(k, v);
- store.save("_cram");
- }
- this.remove = function(k) {
- store.removeAttribute(k);
- }
- this.free = function() {
- store = null;
- };
- });
- var flash = backend.create(function() {
- return window.SWFObject;
- }, function() {
- if (document.getElementById('_cram_flash')) {
- return;
- }
- var div = appendDiv(function(div) {
- div.id = '_cram_flash';
- div.style.position = 'absolute';
- div.style.top = '-100px';
- div.style.left = '-100px';
- });
- if (!div)
- return;
- var so = new SWFObject("/cram.swf", "_cram_swf", "1", "1", "9");
- so.addParam("allowScriptAccess", "sameDomain");
- if (so.write("_cram_flash")) {
- var swf = document.getElementById('_cram_swf');
- if (swf) {
- this.get = function(k) {
- if (swf && swf.get)
- return swf.get(k);
- };
- this.set = function(k, v) {
- if (swf && swf.set)
- swf.set(k, v);
- };
- this.remove = function(k) {
- if (swf && swf.remove)
- swf.remove(k);
- };
- this.free = function() {
- swf = null;
- };
- }
- }
- });
- var methods = {
- html5: html5,
- flash: flash
- };
- var order = ['html5', 'flash'];
- var store = null;
- var self = {
- load: function() {
- for (var i = 0; i < order.length; i++) {
- var method = methods[order[i]];
- if (method.valid()) {
- store = new method();
- break;
- }
- }
- document.fire('cram:load');
- },
- methods: methods,
- valid: function() {
- return !! store;
- },
- unload: function() {
- if (store && store.free)
- store.free();
- document.fire('cram:unload');
- },
- setStore: function(manualStore) {
- store = manualStore;
- },
- get: function(key) {
- var r = null;
- if (store) {
- var value = store.get(key) || "null";
- r = unescape(value).evalJSON(true);
- }
- return r;
- },
- set: function(key, value) {
- if (store)
- store.set(key, escape(Object.toJSON(value)));
- },
- remove: function(key) {
- if (store)
- store.remove(key);
- }
- };
- if (Event && Event.observe) {
- Event.observe(window, "load", self.load);
- Event.observe(window, "unload", self.unload);
- }
- return self;
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement