1 /**
  2  * localStorage support for ie<8
  3  * @author yiminghe@gmail.com
  4  */
  5 KISSY.add("editor/plugin/localStorage/index", function (S, Editor, Overlay, FlashBridge) {
  6 
  7     // 原生或者已经定义过立即返回
  8     // ie 使用 flash 模拟的 localStorage,序列化性能不行
  9     if (!S.UA['ie'] && window.localStorage) {
 10         //原生的立即可用
 11         return window.localStorage;
 12     }
 13 
 14     // 国产浏览器用随机数/时间戳试试 ! 是可以的
 15     var movie = Editor.Utils.debugUrl("plugin/localStorage/swfstore.swf?t=" + (+new Date()));
 16 
 17     var store = new FlashBridge({
 18         movie:movie,
 19         flashVars:{
 20             useCompression:true
 21         },
 22         methods:["setItem", "removeItem", "getItem", "setMinDiskSpace", "getValueOf"]
 23     });
 24 
 25     store.swf.height = 138;
 26 
 27     //Dialog 不行
 28     var o = new Overlay({
 29         width:"0px",
 30         prefixCls:'ks-editor-',
 31         elStyle:{
 32             overflow:'hidden'
 33         },
 34         content:"<h1 style='border:1px solid black;" +
 35             "border-bottom:none;" +
 36             "background:white;" +
 37             "text-align:center;'>请点击允许</h1>",
 38         zIndex:Editor.baseZIndex(Editor.zIndexManager.STORE_FLASH_SHOW)
 39     });
 40     o.render();
 41     o.get("contentEl").append(store.swf);
 42     // 必须在视窗范围内才可以初始化,触发 contentReady 事件
 43     o.center();
 44     o.show();
 45 
 46     store.on("pending", function () {
 47         o.set("width", 215);
 48         o.center();
 49         o.show();
 50         // 轮训,直到用户允许
 51         setTimeout(function () {
 52             store.retrySave();
 53         }, 1000);
 54     });
 55 
 56     store.on("save", function () {
 57         o.set("width", 0);
 58     });
 59 
 60     var oldSet = store.setItem;
 61 
 62     S.mix(store, {
 63         _ke:1,
 64         getItem:function (k) {
 65             return this['getValueOf'](k);
 66         },
 67         retrySave:function () {
 68             var self = this;
 69             self.setItem(self.lastSave.k, self.lastSave.v);
 70         },
 71         setItem:function (k, v) {
 72             var self = this;
 73             self.lastSave = {k:k, v:v};
 74             oldSet.call(self, k, v);
 75         }
 76     });
 77 
 78     //非原生,等待flash通知
 79     store.on("contentReady", function () {
 80         store._ready = 1;
 81     });
 82 
 83     /*
 84      "quotaExceededError"
 85      "error"
 86      "save"
 87      "inadequateDimensions"
 88      */
 89 
 90     return store;
 91 }, {
 92     //important
 93     //不能立即运行,ie6 可能会没有 domready 添加 flash 节点
 94     //导致:operation aborted
 95     attach:false,
 96     requires:["editor", "overlay", "../flashBridge/"]
 97 });