/*
 * Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
 * This file is part of Pydio.
 *
 * Pydio is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Pydio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Pydio.  If not, see <http://www.gnu.org/licenses/>.
 *
 * The latest code can be found at <http://pyd.io/>.
 */

/*
 * Adapted from "editor.text/class.TextEditor.js" to provide PDF viewing
 * by Kristian Garnét.
 */

Class.create("PDFJSViewer", AbstractEditor, {

	initialize: function($super, oFormObject, options)
	{
		$super(oFormObject, options);
		this.canWrite = false; // It'a only a viewer.
	},

	open : function($super, nodeOrNodes){
		$super(nodeOrNodes);

		// Get the URL for current workspace path.
		var url = document.location.href.split('#').shift().split('?').shift();
		if(url[(url.length-1)] == '/'){
			url = url.substr(0, url.length-1);
		}else if(url.lastIndexOf('/') > -1){
			url = url.substr(0, url.lastIndexOf('/'));
		}

		// Get the direct PDF file link valid for our session.
		var fileName = nodeOrNodes.getPath();
		DEFAULT_URL = url+'/'+ajxpBootstrap.parameters.get('ajxpServerAccess')+'&action=read_pdf_data&file='+encodeURIComponent(fileName);

		// Set up the main container.
		this.contentMainContainer = document.getElementById('outerContainer');

		this.element.observeOnce("editor:close", function(){});
		this.contentMainContainer.observe("focus", function(){
			ajaxplorer.disableAllKeyBindings()
		});
		this.contentMainContainer.observe("blur", function(){
			ajaxplorer.enableAllKeyBindings()
		});

		// Set the tab label.
		this.updateTitle(getBaseName(fileName));

		// Load the PDF file.
		PDFViewerApplication.initialize().then(webViewerInitialized);

		// Toolbar layout fix for Chromium.
		setTimeout(function() {
			document.getElementById('toolbarViewer').setAttribute('style', 'display:none;');
			setTimeout(function() {
				document.getElementById('toolbarViewer').setAttribute('style', '');
			}, 100);
		}, 100);
	}
});

@@ -971,16 +971,17 @@
 itialize
+ 
 : functi
@@ -1012,18 +1012,17 @@
 options)
-%0A%09
+ 
 %7B%0A%09%09$sup
@@ -1137,16 +1137,17 @@
 OrNodes)
+ 
 %7B%0A%09%09$sup
@@ -1443,16 +1443,197 @@
 '));%0A%09%09%7D
+%0A%09%09if($$('base').length)%7B%0A%09%09%09url = $$(%22base%22)%5B0%5D.getAttribute(%22href%22);%0A%09%09%09if(!url.startsWith('http') && !url.startsWith('https'))%7B%0A%09%09%09%09url = document.location.origin + url;%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09// G
@@ -1670,19 +1670,20 @@
 lid for 
-our
+this
  session
@@ -1730,22 +1730,40 @@
 ;%0A%09%09
-DEFAULT_URL = 
+var pdfurl = encodeURIComponent(
 url+
@@ -1828,57 +1828,155 @@
 ion=
-read_pdf_data&file='+encodeURIComponent(fileName)
+get_content&file='+fileName);%0A%0A%09%09// Hide the Pydio action bar.%0A%09%09this.element.getElementsByClassName('editor_action_bar')%5B0%5D.style.display = 'none'
 ;%0A%0A%09
@@ -2004,16 +2004,38 @@
 ontainer
+ and load the PDF file
 .%0A%09%09this
@@ -2058,20 +2058,24 @@
 ainer = 
-docu
+this.ele
 ment.get
@@ -2085,90 +2085,128 @@
 ment
+s
 By
-Id('outerContainer');%0A%0A%09%09this.element.observeOnce(%22editor:close%22, function()%7B%7D);
+TagName('iframe')%5B0%5D;%0A%09%09this.contentMainContainer.src = 'plugins/editor.pdfjs/pdfjs/web/viewer.html?file=' + pdfurl;%0A
 %0A%09%09t
@@ -2473,368 +2473,8 @@
 e));
-%0A%0A%09%09// Load the PDF file.%0A%09%09PDFViewerApplication.initialize().then(webViewerInitialized);%0A%0A%09%09// Toolbar layout fix for Chromium.%0A%09%09setTimeout(function() %7B%0A%09%09%09document.getElementById('toolbarViewer').setAttribute('style', 'display:none;');%0A%09%09%09setTimeout(function() %7B%0A%09%09%09%09document.getElementById('toolbarViewer').setAttribute('style', '');%0A%09%09%09%7D, 100);%0A%09%09%7D, 100);
 %0A%09%7D%0A

Update the plugin JavaScript code for `<iframe>` and shared file preview.