This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
/
main.js
149 lines (131 loc) · 4.52 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* 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';
var isCordova;
var isWin;
var loadContentExternally = false;
var isWeb = (document.URL.startsWith('http') && !document.URL.startsWith('http://localhost:1212/'));
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 + '=([^&#]*)') ,
results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g , ' '));
}
var locale = getParameterByName('locale');
var filePath = getParameterByName('file');
var extSettings;
loadExtSettings();
// isCordova = parent.isCordova;
// isWin = parent.isWin;
// Init internationalization
i18next.init({
ns: {namespaces: ['ns.viewerZIP']} ,
debug: true ,
lng: locale ,
fallbackLng: 'en_US'
} , function() {
jqueryI18next.init(i18next, $);
$('[data-i18n]').localize();
});
function loadExtSettings() {
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 byteLength = (unitArr.byteLength > maxPreviewSize) ? maxPreviewSize : unitArr.byteLength;
for (var i = 0; i < byteLength; i++) {
previewText += String.fromCharCode(unitArr[i]);
}
var fileContent = $('<pre/>').text(previewText);
$.post('previewDialog.html' , function(uiTPL) {
//console.log('Load modal ' + uiTPL);
if ($('#previewDialog').length < 1) {
var uiTemplate = Handlebars.compile(uiTPL);
$('body').append(uiTemplate());
}
var dialogPreview = $('#previewDialog');
dialogPreview.find('.modal-body').empty().append(fileContent);
dialogPreview.modal({
backdrop: 'static' ,
show: true
});
var nanobar = new Nanobar({
bg: '#42BEDB' , //(optional) background css property, '#000' by default
target: document.getElementById('nanoBar') , //(optional) Where to put the progress bar, nanobar will be fixed to top of document if target is null
// id for new nanobar
id: 'nanoBar' // (optional) id for nanobar d
});
var progressChunk = parseInt(byteLength / 100);
var currentProgress = 0;
for (var i = 0; i < byteLength; i++) {
var check = ((i % progressChunk) === 0);
if (check) {
currentProgress++;
if (currentProgress <= 100) {
nanobar.go(currentProgress);
}
}
previewText += String.fromCharCode(unitArr[i]);
}
}).always(function() {
window.setTimeout(function() {
document.getElementById('nanoBar').remove();
} , 1000);
});
}