fileviewer/js/ViewerPanel.js

138 lines
3.5 KiB
JavaScript

Ext.namespace('Zarafa.plugins.fileviewer');
/**
* @class Zarafa.plugins.fileviewer.ViewerPanel
* @extends Ext.Panel
* @xtype fileviewer.viewerpanel
*
* The main panel which contains the iframe to display a PDF or ODF document.
*/
Zarafa.plugins.fileviewer.ViewerPanel = Ext.extend(Ext.Panel, {
/**
* @cfg {String} viewerjsPath Path to the ViewerJS installation.
* This is a relative path starting from the plugin root.
*/
viewerjsPath: 'external/ViewerJS/index.html#',
/**
* @cfg {String} src The Iframe source. This will be applied if no record is set.
* If a records was passed to this panel, the return value of the "getInlineImageUrl" function
* will be used.
*/
src: '',
/**
* @cfg {String} title The panel title. This will be applied if no record is set.
* If a records was passed to this panel, the "name" or "filename" property of the record
* will be used.
*/
title: '',
/**
* @cfg {Boolean} autoResize This flag specifies if the panel gets automatically resized if the window size has changed.
* Defaults to false.
*/
autoResize: false,
/**
* @cfg {Number} defaultScale The default scaling of the panel.
* Defaults to 0.8 meaning 80% of the browser width.
*/
defaultScale: 0.8,
/**
* @constructor
* @param config
*/
constructor: function (config) {
config = config || {};
if (Ext.isDefined(config.record)) {
this.src = this.getIframeURL(config.record.getInlineImageUrl());
this.title = config.record.get('name') || config.record.get('filename');
} else {
this.src = config.src;
this.title = config.title;
}
if (Ext.isDefined(config.autoResize)) {
this.autoResize = config.autoResize;
}
Ext.applyIf(config, {
xtype : 'fileviewer.viewerpanel',
layout: 'anchor',
anchor: '100%',
items : [{
xtype : 'component',
autoEl: {
tag : 'iframe',
src : this.src,
style : {
width : '100%',
height: '100%'
},
frameborder : 0,
allowfullscreen: true
}
}],
title : this.title,
header: false,
height: Ext.getBody().getViewSize().height * this.defaultScale,
width : Ext.getBody().getViewSize().width * this.defaultScale
});
Zarafa.plugins.fileviewer.ViewerPanel.superclass.constructor.call(this, config);
},
/**
* Initializes the events. This function is called during rendering of the panel.
* @private
*/
initEvents: function () {
if (this.autoResize) {
Ext.EventManager.onWindowResize(this.resizePanel, this);
}
},
/**
* Removes the resize event listener. This function is called when the panel gets destroyed.
* @private
*/
onDestroy: function () {
if (this.autoResize) {
Ext.EventManager.removeResizeListener(this.resizePanel, this);
}
Zarafa.plugins.fileviewer.ViewerPanel.superclass.onDestroy.call(this, arguments);
},
/**
*
* @param {Number} width The window width.
* @param {Number} height The window height.
* @private
*/
resizePanel: function (width, height) {
this.setWidth(width * this.defaultScale);
this.setHeight(height * this.defaultScale);
// center the panel
if (this.ownerCt instanceof Ext.Window) {
this.ownerCt.center();
}
},
/**
* Generate the complete Iframe URL including the ViewerJS path.
*
* @param {String} url The inline url of the attachment to view.
* @returns {string}
*/
getIframeURL: function (url) {
var pluginRoot = container.getBasePath() + 'plugins/fileviewer/';
return pluginRoot + this.viewerjsPath + url;
}
});
Ext.reg('fileviewer.viewerpanel', Zarafa.plugins.fileviewer.ViewerPanel);