Ext.namespace("Zarafa.plugins.webodf");Zarafa.plugins.webodf.ABOUT="<p>Copyright (C) 2012 KO GmbH <copyright@kogmbh.com></p><p>@licstartThe JavaScript code in this page is free software: you can redistribute itand/or modify it under the terms of the GNU Affero General Public License(GNU AGPL) as published by the Free Software Foundation, either version 3 ofthe License, or (at your option) any later version.  The code is distributedWITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY orFITNESS FOR A PARTICULAR PURPOSE.  See the GNU AGPL for more details.</p><p>As additional permission under GNU AGPL version 3 section 7, youmay distribute non-source (e.g., minimized or compacted) forms ofthat code without the copy of the GNU GPL normally required bysection 4, provided you include this license notice and a URLthrough which recipients can access the Corresponding Source.</p><p>As a special exception to the AGPL, any HTML file which merely makes functioncalls to this code, and for that purpose includes it by reference shall bedeemed a separate work for copyright law purposes. In addition, the copyrightholders of this code give you permission to combine this code with freesoftware libraries that are released under the GNU LGPL. You may copy anddistribute such a system following the terms of the GNU AGPL for this codeand the LGPL for the libraries. If you modify this code, you may extend thisexception to your version of the code, but you are not obligated to do so.If you do not wish to do so, delete this exception statement from yourversion.</p><p>This license applies to this entire compilation.@licend@source: http://www.webodf.org/@source: http://gitorious.org/webodf/webodf/ </p>";
Ext.namespace("Zarafa.plugins.webodf");
Zarafa.plugins.webodf.WebOdfBox=Ext.extend(Object,{odfCanvas:null,pages:[],currentPage:0,addMargin:10,elements:{},constructor:function(){this.initMarkup()},initMarkup:function(){this.generateLightBox();Ext.each([this.elements.overlay,this.elements.webodfbox],function(a){a.setVisibilityMode(Ext.Element.DISPLAY);a.hide()})},generateLightBox:function(){var a={},b=this.getTemplate();a.webodfbox=b.append(document.body,{close:"&#215;"},true);Ext.each(["outerDocumentContainer","titlebar","title","canvas",
"hoverNav","navPrev","navNext","outerDataContainer","dataContainer","data","details","caption","pageNumber","bottomNav","navClose","outerContainer"],function(c){a[c]=Ext.get("ux-webodfbox-"+c)});a.overlay=Ext.DomHelper.append(document.body,{id:"ux-webodfbox-overlay"},true);this.elements=a;this.initEvents()},initEvents:function(){this.elements.overlay.on("click",this.closeODF,this);this.elements.navClose.on("click",this.closeODF,this);this.elements.navPrev.on("click",this.showPreviousPage,this);this.elements.navNext.on("click",
this.showNextPage,this)},getTemplate:function(){return new Ext.Template('<div id="ux-webodfbox">','<div id="ux-webodfbox-titlebar">','<span id="ux-webodfbox-title"></span>',"</div>",'<div id="ux-webodfbox-outerContainer">','<div id="ux-webodfbox-outerDocumentContainer">','<div id="ux-webodfbox-canvas">',"</div>",'<div id="ux-webodfbox-hoverNav">','<a href="#" id="ux-webodfbox-navPrev"></a>','<a href="#" id="ux-webodfbox-navNext"></a>',"</div>","</div>","</div>",'<div id="ux-webodfbox-outerDataContainer">',
'<div id="ux-webodfbox-dataContainer">','<div id="ux-webodfbox-data">','<div id="ux-webodfbox-details">','<span id="ux-webodfbox-pageNumber"></span>',"</div>",'<div id="ux-webodfbox-bottomNav">','<a href="#" id="ux-webodfbox-navClose"></a>',"</div>","</div>","</div>","</div>","</div>")},open:function(a){this.enableKeyClose();this.setViewSize();this.elements.overlay.fadeIn({duration:a.overlayDuration});this.elements.webodfbox.setStyle({top:this.getViewSize().domHeight+"px"}).show();this.initialize(a)},
initialize:function(a){this.loadMask=new Ext.LoadMask(Ext.getBody(),{msg:_("Please wait...")});this.loadMask.show();this.overlayDuration=a.overlayDuration;this.resizeDuration=a.resizeDuration;this.odfCanvas=new odf.OdfCanvas(this.elements.canvas.dom);this.odfCanvas.load(a.href);this.elements.title.update(a.title);var b=this;this.odfCanvas.addListener("statereadychange",function(){var c=b.odfCanvas.odfContainer().rootElement,d=b.getDocumentType(b,c);if(d=="presentation"){b.pages=b.getPages(c);b.showPage(1);
b.enableKeyNav()}else{b.elements.pageNumber.hide();b.elements.hoverNav.hide();b.elements.navPrev.hide();b.elements.navNext.hide()}b.parseScale(d)})},getDocumentType:function(a,b){return b.getElementsByTagNameNS(a.nsResolver("office"),"text").length>0?"text":b.getElementsByTagNameNS(a.nsResolver("office"),"presentation").length>0?"presentation":null},nsResolver:function(a){return{draw:"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0",presentation:"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0",
text:"urn:oasis:names:tc:opendocument:xmlns:text:1.0",office:"urn:oasis:names:tc:opendocument:xmlns:office:1.0"}[a]||alert("prefix ["+a+"] unknown.")},getPages:function(a){a=a.getElementsByTagNameNS(this.nsResolver("draw"),"page");for(var b=[],c=0,d=a.length;c<d;c+=1){var e=[a[c].getAttribute("draw:name"),a[c]];b.push(e)}return b},parseScale:function(a){var b=this.getViewSize(),c=this.resizeCanvas(a),d=Math.max(0,Math.round((b.domWidth-c.newWidth)/2)-this.addMargin);b=Math.max(0,Math.round((b.domHeight-
c.newHeight)/2)-this.addMargin*4);if(a=="presentation"){this.odfCanvas.fitToContainingElement(c.newWidth,c.newHeight);a=this.elements.canvas.getWidth();var e=this.elements.canvas.getHeight();this.updateCanvas(a,e,c.overFlow,d,b)}else this.updateCanvas(c.newWidth,c.newHeight,c.overFlow,d,b)},resizeCanvas:function(a){var b=this.getViewSize(),c=0,d="hidden";c=this.checkSize();if(a=="presentation"){a=b.domWidth/2;c=(b.domHeight+b.domWidth)/4}else{a=c.width;c=c.height;d="auto"}return{newWidth:a,newHeight:c,
overFlow:d}},checkSize:function(){var a=this.getViewSize(),b=null,c=null;b=this.elements.webodfbox.getWidth();c=this.elements.webodfbox.getHeight();var d=this.elements.canvas.getWidth(),e=this.elements.canvas.getHeight();b=b<a.domWidth?b>d?d:d>a.domWidth?a.domWidth-100:d+this.addMargin*2:b>d?b>a.domWidth?a.domWidth-100:d:a.domWidth-100;c=c>e?e:(a.domHeight+a.domWidth)/4;return{width:b,height:c}},updateCanvas:function(a,b,c,d,e){this.elements.webodfbox.setStyle({left:d+"px",top:e+"px"});this.elements.outerDocumentContainer.setStyle({width:a+
"px",height:b+"px",overflow:c});this.elements.hoverNav.setStyle({top:this.elements.titlebar.getHeight()+"px",width:a+this.addMargin*2+"px",height:b+this.addMargin*2+"px"});this.elements.details.setStyle({height:this.elements.navClose.getHeight()+"%"});this.loadMask.hide()},showPage:function(a){if(a<=0)a=1;else if(a>this.pages.length)a=this.pages.length;this.odfCanvas.showPage(a);this.currentPage=a;this.updateDetail()},updateDetail:function(){if(this.pages.length>0){this.elements.pageNumber.update(String.format(_("Page {0} of {1}"),
this.currentPage,this.pages.length));this.elements.pageNumber.show()}this.elements.dataContainer.fadeIn({scope:this,callback:function(){this.updateNav()}})},updateNav:function(){this.currentPage<this.pages.length?this.elements.navNext.show():this.elements.navNext.hide();this.currentPage===1?this.elements.navPrev.hide():this.elements.navPrev.show()},showNextPage:function(){this.showPage(this.currentPage+1)},showPreviousPage:function(){this.showPage(this.currentPage-1)},enableKeyNav:function(){Ext.get(document).on("keydown",
this.keyNavAction,this)},disableKeyNav:function(){Ext.get(document).un("keydown",this.keyNavAction,this)},enableKeyClose:function(){Ext.get(document).on("keydown",this.keyClose,this)},disableKeyClose:function(){Ext.get(document).un("keydown",this.keyClose,this)},keyNavAction:function(a){switch(a.keyCode){case a.UP:case a.LEFT:a.preventDefault();this.showPreviousPage();break;case a.DOWN:case a.RIGHT:a.preventDefault();this.showNextPage();break}},keyClose:function(a){switch(a.keyCode){case a.ESC:a.preventDefault();
this.closeODF();break}},setViewSize:function(){var a=this.getViewSize();this.elements.overlay.setStyle({width:a.domWidth+"px",height:a.domHeight+"px"})},closeODF:function(){this.defaultCanvasSize();this.disableKeyNav();this.disableKeyClose();this.pages.clear();this.elements.webodfbox.hide();this.elements.overlay.fadeOut({duration:this.overlayDuration})},defaultCanvasSize:function(){var a=Ext.get(this.elements.outerDocumentContainer.dom);a.scrollTo("top",0);a.scrollTo("left",0);this.elements.outerDocumentContainer.shift({width:500,
height:500,callback:function(){this.elements.hoverNav.setStyle({width:"500px",height:"500px"})},scope:this});this.loadMask.hide();a=document.getElementsByTagName("head")[0].getElementsByTagName("style");for(var b=a.length;b--;)a[b].getAttribute("media")==="screen, print, handheld, projection"&&a[b].parentNode.removeChild(a[b])},isODFDocument:function(a){return a.match(/^.*\.od[tps]$/i)?true:false},getViewSize:function(){return{domWidth:Ext.lib.Dom.getViewWidth(),domHeight:Ext.lib.Dom.getViewHeight()}}});
Zarafa.plugins.webodf.WebOdfBox=new Zarafa.plugins.webodf.WebOdfBox;Ext.namespace("Zarafa.plugins.webodf");
Zarafa.plugins.webodf.WebOdfPlugin=Ext.extend(Zarafa.core.Plugin,{doOpen:function(a){a={resizeDuration:0.4,overlayDuration:0.6,href:a.getInlineImageUrl(),title:a.get("name")};Zarafa.plugins.webodf.WebOdfBox.open(a)},bidSharedComponent:function(a,b){var c=-1;switch(a){case Zarafa.core.data.SharedComponentType["common.view"]:if(b instanceof Zarafa.core.data.IPMAttachmentRecord){var d=b.get("name");if(Zarafa.plugins.webodf.WebOdfBox.isODFDocument(d))c=1}break}return c},getSharedComponent:function(a){var b;
switch(a){case Zarafa.core.data.SharedComponentType["common.view"]:b=this;break}return b}});Zarafa.onReady(function(){container.registerPlugin(new Zarafa.core.PluginMetaData({name:"webodf",displayName:_("WebODF Plugin"),about:Zarafa.plugins.webodf.ABOUT,pluginConstructor:Zarafa.plugins.webodf.WebOdfPlugin}))});
