Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
viewer ext migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kristiyandd committed Jan 4, 2018
1 parent 4be38f2 commit 07ce8bd
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 58 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
@@ -1,5 +1,5 @@
The MIT License (MIT)
Copyright (c) 2018 Ilian Sapundshiev
Copyright (c) 2013-present Ilian Sapundshiev

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
3 changes: 2 additions & 1 deletion index.html
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<script type="text/javascript" src="libs/jszip/dist/jszip.min.js"></script>
<script type="text/javascript" src="libs/jszip-utils/jszip-utils.min.js"></script>
<script type="text/javascript" src="libs/handlebars/handlebars.min.js"></script>
<script type="text/javascript" src="../legacy-ext/libs/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../legacy-ext/libs/bootstrap/dist/js/bootstrap.min.js"></script>
Expand All @@ -30,7 +31,7 @@
</button>
</div>
</div>
<div id="htmlContent"></div>
<div id="zipContent"></div>
<div class="btn-group dropup extMainMenu">
<button type="button" class="btn roundButton dropdown-toggle viewerMainMenuButton" data-toggle="dropdown">
<i class="fa fa-ellipsis-v fa-2x"></i>
Expand Down
10 changes: 10 additions & 0 deletions libs/jszip-utils/jszip-utils.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 68 additions & 56 deletions main.js
@@ -1,25 +1,26 @@
/* Copyright (c) 2013-present The TagSpaces Authors.
* Use of this source code is governed by the MIT license which can be found in the LICENSE.txt file. */
/* globals Handlebars, Nanobar, marked */
"use strict";
'use strict';

var isCordova;

var isWin;
var loadContentExternally = true;
var loadContentExternally = false;
var isWeb = (document.URL.startsWith('http') && !document.URL.startsWith('http://localhost:1212/'));
var JSZip;
var JSZip, JSZipUtils;
var maxPreviewSize = (1024 * 3) || {}; //3kb limit for preview

$(document).ready(function() {
function getParameterByName(name) {
name = name.replace(/[\[]/ , "\\\[").replace(/[\]]/ , "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)") ,
name = name.replace(/[\[]/ , '\\\[').replace(/[\]]/ , '\\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)') ,
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g , " "));
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g , ' '));
}

var locale = getParameterByName("locale");
var locale = getParameterByName('locale');
var filePath = getParameterByName('file');

var extSettings;
loadExtSettings();
Expand All @@ -39,24 +40,78 @@ $(document).ready(function() {
});

function loadExtSettings() {
extSettings = JSON.parse(localStorage.getItem("viewerZIPSettings"));
extSettings = JSON.parse(localStorage.getItem('viewerZIPSettings'));
}

var zipFile;
new JSZip.external.Promise(function (resolve, reject) {
JSZipUtils.getBinaryContent( filePath, function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}).then(function (data) {
return JSZip.loadAsync(data);
})
.then(function (data) {
zipFile = data;
var $zipContent = $('#zipContent');
$zipContent.append(zipFile);

// if (filePath.indexOf('file://') === 0) {
// filePath = filePath.substring(('file://').length , filePath.length);
// }

$zipContent.append('<div/>').css({
'overflow': 'auto' ,
'padding': '5px' ,
'background-color': 'white' ,
'fontSize': 12 ,
'width': '100%' ,
'height': '100%'
});
$zipContent.append('<p><h4> Contents of file ' + filePath + '</h4></p>');
var ulFiles = $zipContent.append('<ul/>');

function showPreviewDialog(event) {
event.preventDefault();
var containFile = zipFile.files[$(this).text()];
showContentFilePreviewDialog(containFile);
}

if (!!Object.keys(zipFile.files) &&
(typeof zipFile !== 'function' ||
zipFile === null)) {
for (var fileName in zipFile.files) {
if (zipFile.files[fileName].dir === true) {
continue;
}
var linkToFile = $('<a>').attr('href' , '#').text(fileName);
linkToFile.click(showPreviewDialog);
var liFile = $('<li/>').css('list-style-type' , 'none').append(linkToFile);
ulFiles.append(liFile);
}
} else {
throw new TypeError('Object.keys called on non-object');
}
});
});

function showContentFilePreviewDialog(containFile) {
var unitArr = containFile.asUint8Array();
var previewText = "";
var previewText = '';
var byteLength = (unitArr.byteLength > maxPreviewSize) ? maxPreviewSize : unitArr.byteLength;

for (var i = 0; i < byteLength; i++) {
previewText += String.fromCharCode(unitArr[i]);
}

var fileContent = $("<pre/>").text(previewText);
var $htmlContent = $('#htmlContent');
var fileContent = $('<pre/>').text(previewText);

$.post("previewDialog.html" , function(uiTPL) {
//console.log("Load modal " + uiTPL);
$.post('previewDialog.html' , function(uiTPL) {
//console.log('Load modal ' + uiTPL);
if ($('#previewDialog').length < 1) {
var uiTemplate = Handlebars.compile(uiTPL);
$('body').append(uiTemplate());
Expand Down Expand Up @@ -92,46 +147,3 @@ function showContentFilePreviewDialog(containFile) {
} , 1000);
});
}

function setContent(content , fileDirectory) {
var $htmlContent = $('#htmlContent');
$htmlContent.append(content);

if (fileDirectory.indexOf("file://") === 0) {
fileDirectory = fileDirectory.substring(("file://").length , fileDirectory.length);
}

$htmlContent.append('<div/>').css({
'overflow': 'auto' ,
'padding': '5px' ,
'background-color': 'white' ,
'fontSize': 12 ,
'width': '100%' ,
'height': '100%'
});
$htmlContent.append("<p><h4> Contents of file " + fileDirectory + "</h4></p>");
var ulFiles = $htmlContent.append("<ul/>");
var zipFile = content;

function showPreviewDialog(event) {
event.preventDefault();
var containFile = zipFile.files[$(this).text()];
showContentFilePreviewDialog(containFile);
}

if (!!Object.keys(zipFile.files) &&
(typeof zipFile !== 'function' ||
zipFile === null)) {
for (var fileName in zipFile.files) {
if (zipFile.files[fileName].dir === true) {
continue;
}
var linkToFile = $('<a>').attr('href' , '#').text(fileName);
linkToFile.click(showPreviewDialog);
var liFile = $('<li/>').css('list-style-type' , 'none').append(linkToFile);
ulFiles.append(liFile);
}
} else {
throw new TypeError("Object.keys called on non-object");
}
}

0 comments on commit 07ce8bd

Please sign in to comment.