This repository has been archived by the owner on May 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
officejs.js
298 lines (263 loc) · 10.3 KB
/
officejs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* officejs.js
* Ver. : 1.0.1
* last update: 15/11/2017
* Author: meshesha , https://github.com/meshesha
* LICENSE: MIT
* url:https://meshesha.github.io/officeJS
*/
(function ( $ ) {
/////////////////////////////////////////////////////
$.fn.officejs = function( options ) {
var $result = $(this);
var divId = $result.attr("id");
var settings = $.extend({
// These are the defaults.
url: "",
inputObjId:""
}, options );
//1.get file memeType
//2.load all js file needed to read the file
var file = settings.url;
var inputId = settings.inputObjId;
var fileObj = null;
if(file != ""){
fileObj = {
Obj : file,
ext : file.split('.').pop().toLowerCase()
}
getContent(fileObj,divId);
}
if(inputId != ""){
//TODO
$("#"+inputId).on("change", function(e) {
//var inputFileObj = $(this)[0].files[0];
var inputFileObj = e.target.files[0];
if(inputFileObj !== undefined){
var fName = inputFileObj.name;
fileBlob = URL.createObjectURL(inputFileObj);
fileObj = {
Obj : fileBlob,
ext : fName.split('.').pop().toLowerCase()
}
}
getContent(fileObj,divId);
});
}
}
function getContent(fObj,divId){
var ext = fObj.ext;
var file = fObj.file;
switch (ext) {
case "pdf":
//handel pdf file (https://mozilla.github.io/pdf.js/) -- V
getPdfContent(fObj,divId);
break;
case "docx":
//handel docx (https://github.com/mwilliamson/mammoth.js) -- V
getDocxContent(fObj,divId);
break;
case "doc":
//handel doc file () -- X
getDocContent(fObj,divId);
break;
case "pptx":
//handel pptx file (https://meshesha.github.io/pptxjs/) -- V
getPptxContent(fObj,divId);
break;
case "ppt":
//handel ppt file () -- X
getPptContent(fObj,divId);
break;
case "xlsx":
case "xls":
case "xlw":
case "xlsb":
case "xlsm":
case "csv":
case "dbf":
case "dif":
case "slk":
case "sylk":
case "prn":
case "ods":
case "fods":
//handel sheet file (https://github.com/sheetjs/js-xlsx) -- V
getSheetContent(fObj,divId);
break;
case "gif":
case "jpg":
case "jpeg":
case "bmp":
case "tiff":
case "tif":
case "png":
case "svg":
//handel imge -- V
getImgContent(fObj,divId);
break;
default:
unknownMsg(divId);
}
return
}
/////////////////////////////////////////////////PDF//////////////////////////////////////////
function getPdfContent(fObj,divId){
$("#"+divId).html("");
// The workerSrc property shall be specified.
//PDFJS.workerSrc = './Content/pdfViewer/pdf.worker.js';
var file = fObj.Obj;
scale = 1.5;
//var options = options || { scale: 1 };
function renderPage(page) {
var viewport = page.getViewport(scale); //options.scale
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
canvas.height = viewport.height;
canvas.width = viewport.width;
$("#"+divId).append(canvas);
page.render(renderContext);
}
function renderPages(pdfDoc) {
for(var num = 1; num <= pdfDoc.numPages; num++)
pdfDoc.getPage(num).then(renderPage);
}
//PDFJS.disableWorker = true;
PDFJS.getDocument(file).then(renderPages);
return
}
/////////////////////////////////////////////////Docx//////////////////////////////////////////
function getDocxContent(fObj,divId){
$("#"+divId).html("");
var file = fObj.Obj;
JSZipUtils.getBinaryContent(file, function (err, content) {
mammoth.convertToHtml({ arrayBuffer: content })
.then(displayResult)
.done();
});
function displayResult(result) {
var rslt = result.value;
var position = rslt.search(/[\u0590-\u05FF]/);
if (position >= 0) {
$('#'+divId).attr("dir", "rtl");
} else {
$('#'+divId).attr("dir", "ltr");
}
$('#'+divId).html(rslt);
if (result.messages != "") {
var messageHtml = result.messages.map(function (message) {
return message.type + ': ' + message.message + "\n";
}).join("");
console.log("Docx viewer Messages: \n" + messageHtml + "\n");
}
}
return
}
/////////////////////////////////////////////////doc//////////////////////////////////////////
function getDocContent(fObj,divId){
$("#"+divId).html("");
var file = fObj.Obj;
var ran5 = 10000+Math.round(Math.floor()*90000);
var subDiv = $('<div/>').attr({ class:'doc_files', id:"doc_file_"+ran5,style:"color:#9d9999;font-size:30pt"});
$("#"+divId).append(subDiv);
$("#doc_file_"+ran5).html(".doc file is not supported, convert it to .docx file");
return
}
/////////////////////////////////////////////////pptx//////////////////////////////////////////
function getPptxContent(fObj,divId){
$("#"+divId).html("");
//console.log(fObj,divId);
var file = fObj.Obj;
$("#"+divId).pptxToHtml({
pptxFileUrl: file,
slideMode: false,
keyBoardShortCut: false,
mediaProcess: true
});
return
}
/////////////////////////////////////////////////ppt//////////////////////////////////////////
function getPptContent(fObj,divId){
$("#"+divId).html("");
var file = fObj.Obj;
var ran5 = 10000+Math.round(Math.floor()*90000);
var subDiv = $('<div/>').attr({ class:'ppt_files', id:"ppt_file_"+ran5,style:"color:#9d9999;font-size:30pt"});
$("#"+divId).append(subDiv);
$("#ppt_file_"+ran5).html(".ppt file is not supported, convert it to .pptx file");
return
}
/////////////////////////////////////////////////Sheet//////////////////////////////////////////
function getSheetContent(fObj,divId){
$("#"+divId).html("");
var file = fObj.Obj;
var $container, $window, availableWidth, availableHeight;
$container = $("#"+divId);
$window = $(window);
function process_wb(wb) {
var sheetNames = wb.SheetNames;
sheetNames.forEach(function(sheetName,idx) {
var subDivId = 'wbSheets_' +idx;
var json = XLSX.utils.sheet_to_json(wb.Sheets[sheetName],{header:1});
availableWidth = Math.max($window.width()-50,600);
availableHeight = Math.max($window.height()-50, 400);
$container.append('<p>' + sheetName + '</p>');
var subDiv = $('<div/>').attr({ class:'(wbSheets', id:subDivId});
$container.append(subDiv);
/* add header row for table */
if(!json) json = [];
json.forEach(function(r) {
//must "...,{header:1}"
if(json[0].length < r.length) json[0].length = r.length;
});
/* showtime! */
$subContainer = $("#"+subDivId);
$subContainer.handsontable({
data: json,
readOnly: true,
startRows: 1,
startCols: 1,
stretchH: 'all',
rowHeaders: true,
colHeaders: true,
width: function () { return availableWidth; },
height: function () { return availableHeight; }
});
});
}
var url = file;
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.responseType = "arraybuffer";
req.onload = function(e) {
var data = new Uint8Array(req.response);
var wb = XLSX.read(data, {type:"array"});
process_wb(wb);
};
req.send();
return
}
/////////////////////////////////////////////////Images//////////////////////////////////////////
function getImgContent(fObj,divId){
$("#"+divId).html("");
var file = fObj.Obj;
var img_div = $("#"+divId);
img_div.html("");
var myImage = new Image();//Image(100, 200)
myImage.src = file;
img_div.append(myImage);
return
}
/////////////////////////////////////////////////Unknown file//////////////////////////////////////////
function unknownMsg(divId){
$("#"+divId).html("");
var ran5 = 10000+Math.round(Math.floor()*90000);
var subDiv = $('<div/>').attr({ class:'unknown_files', id:"unknown_file_"+ran5,style:"color:#9d9999;font-size:30pt"});
$("#"+divId).append(subDiv);
$("#unknown_file_"+ran5).html("The file is not supported!");
return
}
}(jQuery));