/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:28
*/
KISSY.add("editor/plugin/draft/index",function(e,j,g,r,s){function f(a,b,c){for(a+="";a.length<b;)a=c+a;return a}function k(a){e.isNumber(a)&&(a=new Date(a));return a instanceof Date?[a.getFullYear(),"-",f(a.getMonth()+1,2,"0"),"-",f(a.getDate(),2,"0")," ",f(a.getHours(),2,"0"),":",f(a.getMinutes(),2,"0"),":",f(a.getSeconds(),2,"0")].join(""):a}function l(a,b){this.editor=a;this.config=b;this._init()}function m(a,b){var c=new l(a,b);a.on("destroy",function(){c.destroy()})}function n(a){this.config=
a||{}}var h=e.Node,o=e.Event,p=e.JSON,i=j.Utils.addRes,t=j.Utils.destroyRes;e.augment(l,{_getSaveKey:function(){var a=this.config;return a.draft&&a.draft.saveKey||"ks-editor-draft-save20110503"},_getDrafts:function(){if(!this.drafts){var a=g.getItem(this._getSaveKey()),b=[];a&&(b=g==window.localStorage?p.parse(decodeURIComponent(a)):a);this.drafts=b}return this.drafts},_init:function(){var a=this,b=a.editor,c=b.get("statusBarEl"),d=this.config;d.draft=d.draft||{};a.draftInterval=d.draft.interval=
d.draft.interval||5;a.draftLimit=d.draft.limit=d.draft.limit||5;c=(new h("<div class='ks-editor-draft'><span class='ks-editor-draft-title'>内容正文每"+d.draft.interval+"分钟自动保存一次。</span></div>")).appendTo(c);a.timeTip=(new h("<span class='ks-editor-draft-time'/>")).appendTo(c);var q=(new h("<a href='#' onclick='return false;' class='ks-editor-button ks-editor-draft-save-btn ks-inline-block' style='vertical-align:middle;padding:1px 9px;'><span class='ks-editor-draft-save'></span><span>立即保存</span></a>")).unselectable().appendTo(c),
e=new s({render:c,collapseOnClick:!0,width:"100px",prefixCls:"ks-editor-",menuCfg:{width:"225px",align:{points:["tr","br"]}},matchElWidth:!1,autoRender:!0,content:"恢复编辑历史"});a.versions=e;e.on("beforeCollapsedChange",function(b){b.newValue||(e.detach("beforeCollapsedChange",arguments.callee),a.sync())});q.on("click",function(b){a.save(!1);b.halt()});i.call(a,q);b.get("textarea")[0].form&&function(){function c(){a.save(!0)}var d=b.get("textarea")[0].form;o.on(d,"submit",c);i.call(a,function(){o.remove(d,
"submit",c)})}();var g=setInterval(function(){a.save(!0)},6E4*a.draftInterval);i.call(a,function(){clearInterval(g)});e.on("click",a.recover,a);i.call(a,e);a.holder=c;if(d.draft.helpHtml){var f=(new h('<a tabindex="0" hidefocus="hidefocus" class="ks-editor-draft-help" title="点击查看帮助" href="javascript:void(\'点击查看帮助 \')">点击查看帮助</a>')).unselectable().appendTo(c);f.on("click",function(){f[0].focus();a.helpPopup&&a.helpPopup.get("visible")?a.helpPopup.hide():a._prepareHelp()});f.on("blur",function(){a.helpPopup&&
a.helpPopup.hide()});a.helpBtn=f;i.call(a,f);j.Utils.lazyRun(a,"_prepareHelp","_realHelp")}i.call(a,c)},_prepareHelp:function(){var a=new h(this.config.draft.helpHtml||""),b=new h("<div style='height:0;position:absolute;fontSize:0;width:0;border:8px #000 solid;border-color:#000 transparent transparent transparent;border-style:solid dashed dashed dashed;border-top-color:#CED5E0;'><div style='height:0;position:absolute;fontSize:0;width:0;border:8px #000 solid;border-color:#000 transparent transparent transparent;border-style:solid dashed dashed dashed;left:-8px;top:-10px;border-top-color:white;'></div></div>");
a.append(b);a.css({border:"1px solid #ACB4BE","text-align":"left"});this.helpPopup=new r({content:a,prefixCls:"ks-editor-",autoRender:!0,width:a.width()+"px",zIndex:j.baseZIndex(j.zIndexManager.OVERLAY),mask:!1});this.helpPopup.get("el").css("border","none");this.helpPopup.arrow=b},_realHelp:function(){var a=this.helpPopup,b=this.helpBtn,c=a.arrow;a.show();b=b.offset();a.get("el").offset({left:b.left-a.get("el").width()+17,top:b.top-a.get("el").height()-7});c.offset({left:b.left-2,top:b.top-8})},
disable:function(){this.holder.css("visibility","hidden")},enable:function(){this.holder.css("visibility","")},sync:function(){var a;a=this.draftLimit;var b=this.timeTip,c=this.versions,d=this._getDrafts(),e;d.length>a&&d.splice(0,d.length-a);for(a=0;a<d.length;a++)e=d[a],e=(e.auto?"自动":"手动")+"保存于 : "+k(e.date),c.addItem({xclass:"menuitem",content:e,value:a});b.html(e);g.setItem(this._getSaveKey(),g==window.localStorage?encodeURIComponent(p.stringify(d)):d)},save:function(a){var b=this._getDrafts(),
c=this.editor.get("formatData");c&&(b[b.length-1]&&c==b[b.length-1].content&&(b.length-=1),this.drafts=b.concat({content:c,date:(new Date).getTime(),auto:a}),this.sync())},recover:function(a){var b=this.editor,c=this._getDrafts(),d=a.target.get("value");confirm("确认恢复 "+k(c[d].date)+" 的编辑历史？")&&(b.execCommand("save"),b.set("data",c[d].content),b.execCommand("save"));a.halt()},destroy:function(){t.call(this)}});e.augment(n,{renderUI:function(a){var b=this.config;g.ready?g.ready(function(){m(a,b)}):
m(a,b)}});return n},{requires:["editor","../localStorage/","overlay","../menubutton/"]});
