Browse Source

增加预览页面生成后的执行脚本

suntg 9 years ago
parent
commit
9d5e80dbea

+ 2 - 2
src/main/webapp/WEB-INF/views/preview.html

@@ -8,6 +8,6 @@
 <body>
 	<button>export as pdf</button>
 </body>
-<script type="text/javascript" src="static/lib/jquery/jquery.min.js"></script>
-<script type="text/javascript" src="static/js/preview/app.js"></script>
+<script type="text/javascript" src="static/lib/require.js" 
+	data-main="static/js/preview/app.js"></script>
 </html>

+ 10 - 14
src/main/webapp/WEB-INF/views/viewer.html

@@ -27,26 +27,15 @@ See https://github.com/adobe-type-tools/cmap-resources
 	content="width=device-width, initial-scale=1, maximum-scale=1">
 <meta name="google" content="notranslate">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
-<title>PDF.js viewer</title>
-
+<title>在线预览</title>
 
 <link rel="stylesheet" href="static/lib/pdf.js/web/viewer.css">
 
 <script src="static/lib/pdf.js/web/compatibility.js"></script>
 
-
-
 <!-- This snippet is used in production (included from viewer.html) -->
 <link rel="resource" type="application/l10n"
 	href="static/lib/pdf.js/web/locale/locale.properties">
-<script src="static/lib/pdf.js/web/l10n.js"></script>
-<script src="static/lib/pdf.js/build/pdf.js"></script>
-
-<script src="static/lib/jquery/jquery.min.js"></script>
-<script src="static/js/preview/app.js"></script>
-
-<script src="static/lib/pdf.js/web/debugger.js"></script>
-<script src="static/lib/pdf.js/web/viewer.js"></script>
 
 </head>
 
@@ -193,7 +182,7 @@ See https://github.com/adobe-type-tools/cmap-resources
 					<div id="toolbarViewer">
 						<div id="toolbarViewerLeft">
 							<button id="sidebarToggle" class="toolbarButton"
-								title="Toggle Sidebar" tabindex="11"
+								title="Toggle Sidebar" tabindex="11" disabled="disabled"
 								data-l10n-id="toggle_sidebar">
 								<span data-l10n-id="toggle_sidebar_label">Toggle Sidebar</span>
 							</button>
@@ -510,7 +499,7 @@ See https://github.com/adobe-type-tools/cmap-resources
 </style>
 		<div class="mozPrintCallback-dialog-box">
 			<!-- TODO: Localise the following strings -->
-			Preparing document for printing...
+			正在准备预览文件...
 			<div class="progress-row">
 				<progress value="0" max="100"></progress>
 				<span class="relative-progress">0%</span>
@@ -522,5 +511,12 @@ See https://github.com/adobe-type-tools/cmap-resources
 	</div>
 
 </body>
+<script src="static/lib/pdf.js/web/l10n.js"></script>
+<script src="static/lib/pdf.js/build/pdf.js"></script>
+<script src="static/lib/jquery/jquery.min.js"></script>
+<script src="static/js/preview.js"></script>
+<script src="static/lib/pdf.js/web/debugger.js"></script>
+<script src="static/lib/pdf.js/web/viewer.js"></script>
+<script src="static/js/endview.js"></script>
 </html>
 

+ 7 - 0
src/main/webapp/resources/js/endview.js

@@ -0,0 +1,7 @@
+console.log('endview');
+$(function() {
+	console.log(pageIndex + '/' + pageSize);
+	$('#pageNumber').val(pageIndex);
+	$('#numPages').html(pageSize);
+	
+});

+ 52 - 71
src/main/webapp/resources/js/preview.js

@@ -1,86 +1,67 @@
-var DEFAULT_URL = "";// 重新定义pdf.js的默认pdf路径
-var PDFData = "";
-loadPdfData();
-/**
- * 从服务器获取报表pdf流
- */
-function loadPdfData() {
-	var url = getUrl();
-	$.ajax({
-		type : "post",
-		async : false,
-		mimeType : 'text/plain; charset=x-user-defined',
-		url : url,
-		success : function(data) {
-			// 服务器未成功填充报表
-			if (data == null || data.toString().length < 1) {
-				return;
-			}
-			var result = JSON.parse(data);
-			// 服务器端pdf流经过base64编码,需要进行解码
-			PDFData = base64_decode(result.data);
-		}
-	});
-	var rawLength = PDFData.length;
-	// 转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068
-	var array = new Uint8Array(new ArrayBuffer(rawLength));
-	for (i = 0; i < rawLength; i++) {
-		array[i] = PDFData.charCodeAt(i) & 0xff;
-	}
-	DEFAULT_URL = array;
-}
+var PDFData, DEFAULT_URL;
+
+var pageIndex = 1, pageSize = 1;// 当前页码,页码总数
+
+// 获取链接参数
+var getParameter = function(key) {
+	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
+	var r = window.location.search.substr(1).match(reg);
+	if (r != null)
+		return unescape(r[2]);
+	return null;
+};
 
-/**
- * 拼接服务器端获取pdf流的url
- * 
- * @returns {String}
- */
-function getUrl() {
+// 获取url
+var getUrl = function(page) {
 	var userName = getParameter("userName");
 	var reportName = getParameter("reportName");
 	var whereCondition = getParameter("whereCondition");
 	var otherParameters = getParameter("otherParameters");
-	var pageIndex = getParameter("pageIndex");
+	pageIndex = page || getParameter("pageIndex") || 1;
 
-	var url = "print/loadPdfData?";
-	if (userName != null && userName != "") {
-		url += "userName=" + userName;
+	var previewUrl = "print/loadPdfData?";
+	if (userName) {
+		previewUrl += "userName=" + userName;
 	}
-	if (reportName != null && reportName != "") {
-		url += "&reportName=" + reportName;
+	if (reportName) {
+		previewUrl += "&reportName=" + reportName;
 	}
-	if (whereCondition != null && whereCondition != "") {
-		url += "&whereCondition=" + whereCondition;
+	if (whereCondition) {
+		previewUrl += "&whereCondition=" + whereCondition;
 	}
-	if (otherParameters != null && otherParameters != "") {
-		url += "&otherParameters=" + otherParameters;
+	if (otherParameters) {
+		previewUrl += "&otherParameters=" + otherParameters;
 	}
-	if (pageIndex != null && pageIndex != "") {
-		url += "&pageIndex=" + pageIndex;
+	if (pageIndex) {
+		previewUrl += "&pageIndex=" + pageIndex;
 	}
-	return url;
-}
-/**
- * 从地址中获取参数
- * 
- * @param key
- *            参数名
- * @returns 参数值
- */
-function getParameter(key) {
-	var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
-	var r = window.location.search.substr(1).match(reg);
-	if (r != null)
-		return unescape(r[2]);
-	return null;
+	return previewUrl;
 }
 
-/**
- * base64解码
- * 
- * @param {Object}
- *            str
- */
+// 获取数据
+function loadData(page, callback) {
+	$.ajax({
+		type: "get",
+		async: false,
+		url: page ? getUrl(page) : getUrl(),
+		success: function(data) {
+			pageSize = data.pageSize;
+			PDFData = base64_decode(data.data);
+			var rawLength = PDFData.length;  
+			//转换成pdf.js能直接解析的Uint8Array类型,见pdf.js-4068  
+			var array = new Uint8Array(new ArrayBuffer(rawLength));    
+			for(var i = 0; i < rawLength; i++) {  
+			  array[i] = PDFData.charCodeAt(i) & 0xff;  
+			}  
+			DEFAULT_URL = array;
+			callback && callback.call();
+		}
+	});
+};
+
+loadData();
+
+// base64 解码
 function base64_decode(str) {
 	var c1, c2, c3, c4;
 	var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -136,4 +117,4 @@ function base64_decode(str) {
 		string += String.fromCharCode(((c3 & 0x03) << 6) | c4)
 	}
 	return string;
-}
+}