jPreview.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. let jPreview={
  2. config:{
  3. container:"", // 容器id
  4. staticPath:"./static", // 静态资源路径
  5. url:"", // 预览资源路径
  6. ext:"", // 资源后缀
  7. name:"", // 资源名称
  8. watermarkTxt:"", // 水印文字
  9. watermarkSize:"16px", // 水印文字大小
  10. priority:1, // 优先级 1:使用插件预览 2:使用office在线预览
  11. oburl:"https://view.officeapps.live.com/op/embed.aspx?src=", // office在线预览地址
  12. },
  13. preview(opts){
  14. this.config = $.extend({}, this.config, opts);
  15. if(this.config.url == ''){
  16. var url=this.parseUrl('src');
  17. this.config.url=url;
  18. }else{
  19. url=this.config.url;
  20. }
  21. // 检测url中是否有优先级参数
  22. let pri=this.parseUrl('pri');
  23. if(pri){
  24. this.config.priority=pri;
  25. }
  26. var spl = url.split(".");
  27. var exts=spl[spl.length-1];
  28. var ext=exts.toLowerCase();
  29. this.config.ext=ext;
  30. let name=this.parseUrl('name');
  31. if(!name){
  32. var decodedUrl = decodeURIComponent(url);
  33. var paths = decodedUrl.split("/");
  34. var fileName=paths[paths.length-1];
  35. name=fileName.replace('.'+ext, "");
  36. }
  37. this.config.name=name;
  38. // 如果url为空,或者后缀为空,则提示资源不存在
  39. if(!url || !ext){
  40. this.error('资源不存在!');
  41. return;
  42. }
  43. let self=this;
  44. // 增加文字水印
  45. if(self.config.watermarkTxt != ''){
  46. dynamicLoadJs(this.config.staticPath+"/common/js/watermark.js",function(){
  47. watermark.init({
  48. watermark_txt: self.config.watermarkTxt,
  49. watermark_x: 0,
  50. watermark_y: 0,
  51. watermark_rows: 0,
  52. watermark_cols: 0,
  53. watermark_x_space: 30,
  54. watermark_y_space: 30,
  55. watermark_font: '微软雅黑',
  56. watermark_fontsize: self.config.watermarkSize,
  57. watermark_color:'black',
  58. watermark_alpha: 0.2,
  59. watermark_width: 180,
  60. watermark_height: 80,
  61. watermark_angle: 10,
  62. });
  63. })
  64. }
  65. this.startPreviw();
  66. },
  67. startPreviw(){
  68. let self=this;
  69. let url=this.config.url;
  70. let ext=this.config.ext;
  71. let static=this.config.staticPath;
  72. let videoExt=['mp4','avi','3gp','rmvb','rm','flv','wmv','mkv','mov','mpeg','mpg','m4v','f4v','m4v'];
  73. let imgExt=['jpeg','jpg','gif','png','bmp'];
  74. let pdfExt=['pdf'];
  75. let audioExt=['mp3','wav','ogg','aac','flac','ape','m4a','mid','ram','amr','ac3','aiff','au','m4p','mmf','mpc','tta','vqf','wv','wma'];
  76. let docExt=['docx'];
  77. let pptExt=['pptx'];
  78. let xlsExt=['xls','xlsx','csv'];
  79. let olExt=["doc","docx","docm","dot","dotx","dotm","rtf","xls","xlsx","xlt","xlsb","xlsm","csv","ppt","pptx","pps","ppsx","pptm","potm","ppam","potx","ppsm","odt","ods","odp","ott","ots","otp","wps","wpt"];
  80. if($.inArray(ext,imgExt)>=0){
  81. dynamicLoadJs(static+"/viewer/viewer.js",function(){
  82. self.imgView(url);
  83. })
  84. }else if($.inArray(ext,pdfExt)>=0){
  85. self.pdfView(url);
  86. }else if($.inArray(ext,audioExt)>=0){
  87. dynamicLoadJs(static+"/common/js/audio.js",function(){
  88. self.audioView(url);
  89. })
  90. }else if($.inArray(ext,videoExt)>=0){
  91. dynamicLoadJs(static+"/common/js/superVideo.js",function(){
  92. self.videoView(url);
  93. })
  94. }else if($.inArray(ext,docExt)>=0 && this.config.priority == 1){
  95. dynamicLoadJs(static+"/docxjs/js/jszip.min.js",function(){
  96. dynamicLoadJs(static+"/docxjs/js/docx-preview.js",function(){
  97. self.docView(url,ext);
  98. })
  99. })
  100. }else if($.inArray(ext,pptExt)>=0 && this.config.priority == 1){
  101. dynamicLoadJs(static+"/pptxjs/js/jszip.min.js",function(){
  102. dynamicLoadJs(static+"/pptxjs/js/filereader.js",function(){
  103. dynamicLoadJs(static+"/pptxjs/js/d3.min.js",function(){
  104. dynamicLoadJs(static+"/pptxjs/js/nv.d3.min.js",function(){
  105. dynamicLoadJs(static+"/pptxjs/js/divs2slides.min.js",function(){
  106. dynamicLoadJs(static+"/pptxjs/js/pptxjs.min.js",function(){
  107. self.pptView(url);
  108. })
  109. })
  110. })
  111. })
  112. })
  113. })
  114. }else if($.inArray(ext,xlsExt)>=0 && this.config.priority == 1){
  115. dynamicLoadJs(static+"/luckysheet/js/plugin.js",function(){
  116. dynamicLoadJs(static+"/luckysheet/js/luckysheet.umd.js",function(){
  117. dynamicLoadJs(static+"/luckysheet/js/luckyexcel.umd.js",function(){
  118. dynamicLoadJs(static+"/luckysheet/js/xlsx.core.min.js",function(){
  119. self.xlsView(url,ext);
  120. })
  121. })
  122. })
  123. })
  124. }else if($.inArray(ext,olExt)>=0){
  125. self.olView(url);
  126. }else{
  127. self.error('不支持的文件类型!');
  128. }
  129. },
  130. olView(url){
  131. $("body").css({overflow:'hidden'});
  132. // 如果是ppt,doc文档,直接使用office在线预览
  133. $("body").html("<iframe src='"+this.config.oburl+url+"' style='width:100%;height:100%;position:absolute;left:0;top:0'></iframe>");return;
  134. },
  135. getFileInfo(url,callback){
  136. let self=this;
  137. var xhr = new XMLHttpRequest();
  138. xhr.open('GET', url);
  139. xhr.responseType = "arraybuffer";
  140. xhr.onload = function (e) {
  141. var data = xhr.response;
  142. if(!data){loading.close();loading = false;return;};
  143. var file = {name: self.config.name, ext: self.config.ext, content: data};
  144. callback(file);
  145. };
  146. xhr.send();
  147. xhr.onreadystatechange = function(){ // 请求状态
  148. if(xhr.readyState==4){
  149. if (xhr.status < 200 || (xhr.status > 300 && xhr.status != 304)) {
  150. console.log('error');
  151. }
  152. }
  153. };
  154. },
  155. parseUrl:(field,urlstr)=>{
  156. if(typeof (urlstr)==='undefined'){
  157. urlstr=window.location.href;
  158. }
  159. var param=urlstr.substring(urlstr.indexOf("?")+1);
  160. var paramArr=param.split("&");
  161. var urlArr={};
  162. for(var i=0;i<paramArr.length;i++){
  163. var str=paramArr[i];
  164. var itemArr=str.split("=");
  165. urlArr[itemArr[0]]=itemArr[1];
  166. }
  167. if(typeof (urlArr[field])==='undefined'){
  168. return '';
  169. }
  170. return urlArr[field];
  171. },
  172. loadLuckySheet(exportJson){
  173. // 获得转化后的表格数据后,使用luckysheet初始化,或者更新已有的luckysheet工作簿
  174. // 注:luckysheet需要引入依赖包和初始化表格容器才可以使用
  175. luckysheet.create({
  176. container: this.config.container, // 容器id
  177. data:exportJson.sheets,
  178. // plugins: ['chart'], // luckyexcel暂不支持导入图表——解析的数据没有chart相关内容
  179. lang: 'zh',
  180. // title:exportJson.info.name,
  181. // userInfo:exportJson.info.name.creator,
  182. // showinfobar: false,
  183. allowCopy: true, // 是否允许拷贝
  184. showtoolbar: false, // 是否显示工具栏——edit
  185. showinfobar: false, // 是否显示顶部信息栏
  186. // showsheetbar: false, // 是否显示底部sheet页按钮
  187. // showstatisticBar: false, // 是否显示底部计数栏
  188. sheetBottomConfig: false, // sheet页下方的添加行按钮和回到顶部按钮配置
  189. allowEdit: false, // 是否允许前台编辑——edit
  190. enableAddRow: false, // 允许增加行
  191. enableAddCol: false, // 允许增加列
  192. // userInfo: false, // 右上角的用户信息展示样式
  193. // showRowBar: false, // 是否显示行号区域
  194. // showColumnBar: false, // 是否显示列号区域
  195. // sheetFormulaBar: false, // 是否显示公式栏
  196. enableAddBackTop: false,//返回头部按钮
  197. // functionButton: '<button id="" class="btn btn-primary" style="padding:3px 6px;font-size: 12px;margin-right: 10px;">下载</button>', // 需要显示信息栏
  198. });
  199. },
  200. setLuckySheet : (data, callback)=>{
  201. try{
  202. callback(data);
  203. }catch(err){
  204. console.error(err);
  205. }
  206. },
  207. docView(url,ext){
  208. let container = this.config.container;
  209. try{
  210. this.getFileInfo(url,function(file){
  211. docx.renderAsync(file.content, document.getElementById(container)).then(x => console.log("docx: finished"));
  212. });
  213. // 如果预览失败,则转为线上预览
  214. window.onerror = function (message, urls, line, column, error) {
  215. self.olView(url);
  216. }
  217. }catch(err){
  218. this.error('文件已经损坏!');
  219. }
  220. },
  221. pptView(url){
  222. let container = this.config.container;
  223. let self = this;
  224. try{
  225. $('#'+container).addClass(this.isWap() ? 'is-in-wap' : 'not-in-wap');
  226. $('#'+container).addClass("pptview");
  227. $("#"+container).pptxToHtml({
  228. pptxFileUrl: url,
  229. fileInputId: "",
  230. slideMode: false,
  231. keyBoardShortCut: false,
  232. mediaProcess: false,
  233. slideModeConfig: { //on slide mode (slideMode: true)
  234. first: 1,
  235. nav: true, /** true,false : show or not nav buttons*/
  236. // nav: true, /** true,false : show or not nav buttons*/
  237. navTxtColor: "white", /** color */
  238. navNextTxt:"&#8250;", //">"
  239. navPrevTxt: "&#8249;", //"<"
  240. showPlayPauseBtn: true,/** true,false */
  241. keyBoardShortCut: false, /** true,false */
  242. showSlideNum: true, /** true,false */
  243. showTotalSlideNum: true, /** true,false */
  244. autoSlide: 2, /** false or seconds (the pause time between slides) , F8 to active(keyBoardShortCut: true) */
  245. // randomAutoSlide: false, /** true,false ,autoSlide:true */
  246. // loop: false, /** true,false */
  247. background: false, /** false or color*/
  248. transition: "fade", /** transition type: "slid","fade","default","random" , to show transition efects :transitionTime > 0.5 */
  249. transitionTime: 0 /** transition time in seconds */
  250. }
  251. });
  252. // 如果预览失败,则转为线上预览
  253. window.onerror = function (message, urls, line, column, error) {
  254. self.olView(url);
  255. }
  256. }catch(err){
  257. this.error('文件已经损坏!');
  258. }
  259. },
  260. xlsView(url,ext){
  261. let self=this;
  262. try{
  263. $('#'+this.config.container).css({height:'100vh'});
  264. this.getFileInfo(url,function(file){
  265. // 1.xlsx,直接luckyexcel读取
  266. if(ext == 'xlsx') {
  267. self.setLuckySheet(file.content, function(content){
  268. LuckyExcel.transformExcelToLucky(content, function(exportJson, luckysheetfile){
  269. self.setLuckySheet(exportJson, function(exportJson){
  270. self.loadLuckySheet(exportJson);
  271. });
  272. });
  273. });
  274. return;
  275. }
  276. var sheet = utils.getLuckySheet();
  277. // 2.csv以字符串方式读取,区分编码
  278. if(file.ext == 'csv'){
  279. var data = new Uint8Array(file.content);
  280. var code = utils.isUTF8(data) ? 'utf-8' : 'gbk';
  281. var str = new TextDecoder(code).decode(data);
  282. var wb = XLSX.read(str, { type: "string" });
  283. }
  284. // 3.xls通过SheetJs获取数据
  285. if(_.isUndefined(wb)) {
  286. var wb = XLSX.read(file.content, {type: 'buffer', cellStyles: true}); // XLSX/XLS
  287. }
  288. var sheets = [];
  289. for(var i in wb.SheetNames) {
  290. var name = wb.SheetNames[i];
  291. var _sheet = JSON.parse(JSON.stringify(sheet));
  292. _sheet.name = name;
  293. _sheet.index = _sheet.order = parseInt(i);
  294. _sheet.data = utils.xlsToLuckySheet(wb.Sheets[name], _sheet);
  295. sheets.push(_sheet);
  296. }
  297. self.setLuckySheet({sheets: sheets}, function(exportJson){
  298. self.loadLuckySheet(exportJson);
  299. if(loading){loading.close();loading = false;}
  300. });
  301. })
  302. // 如果预览失败,则转为线上预览
  303. window.onerror = function (message, urls, line, column, error) {
  304. self.olView(url);
  305. }
  306. }catch(err){
  307. this.error('文件已经损坏!');
  308. }
  309. },
  310. pdfView(url){
  311. $("body").html("<iframe src='./static/pdfjs/web/viewer.html?file="+url+"' style='width:100%;height:100vh;border:none;display:block'></iframe>");
  312. },
  313. imgView(url){
  314. $('#'+this.config.container).html('<div style="height:100vh"><img id="image" style="display:none" src="'+url+'" alt="Picture"></div>');
  315. var image = $('#image');
  316. image.viewer({
  317. inline: true,
  318. button: false,
  319. viewed: function() {
  320. image.viewer('zoomTo', 1);
  321. }
  322. });
  323. },
  324. videoView(url){
  325. let container = this.config.container;
  326. var nextControl = new Super.NextControl() // 实例化“下一个”按钮控件
  327. var Dbspeen = new Super.DbspeenControl() // 倍速控件
  328. // var BarrageControl = new Super.BarrageControl() // 弹幕控件
  329. var fullScreenControl = new Super.FullScreenControl() // 实例化“全屏”控件
  330. var video = new Super.Svideo(container, {
  331. source: new Super.VideoSource({ // 引入视频资源
  332. src: url
  333. }),
  334. leftControls: [nextControl], // 控件栏左槽插入控件
  335. rightControls: [Dbspeen, fullScreenControl], // 控件栏右槽插入控件
  336. // centerControls: [BarrageControl] // 弹幕控件插入中间插槽
  337. })
  338. $('#'+container).addClass("videoContainer");
  339. video.addEventListener('change', function(event) { // 监听video各属性变化
  340. // console.log(event)
  341. })
  342. nextControl.addEventListener('click', function(event) { // 监听“下一个”按钮控件点击事件
  343. alert('click next menu !!!')
  344. })
  345. fullScreenControl.addEventListener('fullscreen', function(event) { // 监听进入全屏
  346. console.log('is fullscreen !!!')
  347. })
  348. fullScreenControl.addEventListener('cancelfullscreen', function(event) { // 监听退出全屏
  349. console.log('cancel fullscreen !!!')
  350. })
  351. video.addEventListener('fullscreen', function(event) {
  352. console.log('is fullscreen !!!')
  353. })
  354. },
  355. audioView(url){
  356. $('#'+this.config.container).html('<div class="yAudio" id="yAudio"></div>');
  357. new YAudio({
  358. element: document.querySelector('#yAudio'),
  359. audio: {
  360. "title": this.config.name,
  361. "url": url
  362. }
  363. })
  364. },
  365. isWap(){
  366. return $(window.document).width() < 768;
  367. },
  368. error(msg){
  369. $('#'+this.config.container).css({height:'100vh',display:'flex',justifyContent: 'center',alignItems:'center',fontSize:'66px'}).text(msg);
  370. },
  371. isMobile() {
  372. const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
  373. return mobileRegex.test(navigator.userAgent);
  374. }
  375. }
  376. $(function(){
  377. var isWap = function(){
  378. return $(window.document).width() < 768;
  379. }
  380. let container = jPreview.config.container;
  381. let isTrue = false;
  382. // 文件加载完成,重置页面尺寸样式
  383. utils.functionHook($,'attr',false,function(res,args){
  384. var id = args[0].id || '';
  385. if(id != 'all_slides_warpper' || isTrue == true) {
  386. return res;
  387. } // convertToHtml结束
  388. isTrue =true;
  389. // 隐藏<#>
  390. $("#all_slides_warpper .slide .block.v-mid.content .text-block").each(function(){
  391. if ($(this).text() == '‹#›') $(this).addClass('hidden');
  392. });
  393. $("#"+container+" .slide").wrap('<div class="slide-box"></div>');
  394. $('#all_slides_warpper').height('auto');
  395. // 4.初始化主区域子节点尺寸
  396. utils.initPageSize(pageRatio(false));
  397. });
  398. // 页面尺寸随窗口变化
  399. $(window).resize(function(){
  400. var wap = isWap();
  401. // 这里可以改成阶段性变化,而不是实时变
  402. var ratio = pageRatio(wap);
  403. utils.changePageSize(ratio, 'all_slides_warpper');
  404. });
  405. var pageRatio = function(wap){
  406. // 左侧栏
  407. dfWidth = $("#all_slides_warpper .slide").first().width();
  408. dfHeight = $("#all_slides_warpper .slide").first().height();
  409. if( arguments[1] !== undefined) {
  410. return 225 / dfWidth;
  411. }
  412. // 移动端,固定以宽为基准
  413. if(wap) {
  414. return $("#"+jPreview.config.container).width() / dfWidth;
  415. }
  416. var pgWidth = $("#all_slides_warpper").width();
  417. var pgHeight = $("#all_slides_warpper").height();
  418. // 当前宽高比>原始宽高比,说明宽度较大,以高度为基准,否则相反
  419. if((pgWidth / pgHeight) > (dfWidth / dfHeight)) {
  420. return pgHeight / dfHeight;
  421. }
  422. return pgWidth / dfWidth;
  423. }
  424. });
  425. function dynamicLoadJs(url, callback) {
  426. var head = document.getElementsByTagName('head')[0]
  427. var script = document.createElement('script')
  428. script.type = 'text/javascript'
  429. script.src = url
  430. if (typeof (callback) === 'function') {
  431. script.onload = script.onreadystatechange = function () {
  432. if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
  433. callback()
  434. script.onload = script.onreadystatechange = null
  435. }
  436. }
  437. }
  438. head.appendChild(script)
  439. }
  440. // 工具函数
  441. var utils = {
  442. isUTF8: function (bytes) {
  443. var i = 0;
  444. while (i < bytes.length) {
  445. if (( // ASCII
  446. bytes[i] == 0x09 ||
  447. bytes[i] == 0x0A ||
  448. bytes[i] == 0x0D ||
  449. (0x20 <= bytes[i] && bytes[i] <= 0x7E)
  450. )) {
  451. i += 1;
  452. continue;
  453. }
  454. if ((// non-overlong 2-byte
  455. (0xC2 <= bytes[i] && bytes[i] <= 0xDF) &&
  456. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF)
  457. )) {
  458. i += 2;
  459. continue;
  460. }
  461. if (( // excluding overlongs
  462. bytes[i] == 0xE0 &&
  463. (0xA0 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  464. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  465. ) || ( // straight 3-byte
  466. ((0xE1 <= bytes[i] && bytes[i] <= 0xEC) ||
  467. bytes[i] == 0xEE ||
  468. bytes[i] == 0xEF) &&
  469. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  470. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  471. ) || ( // excluding surrogates
  472. bytes[i] == 0xED &&
  473. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x9F) &&
  474. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF)
  475. )) {
  476. i += 3;
  477. continue;
  478. }
  479. if (( // planes 1-3
  480. bytes[i] == 0xF0 &&
  481. (0x90 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  482. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  483. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  484. ) || ( // planes 4-15
  485. (0xF1 <= bytes[i] && bytes[i] <= 0xF3) &&
  486. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0xBF) &&
  487. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  488. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  489. ) || ( // plane 16
  490. bytes[i] == 0xF4 &&
  491. (0x80 <= bytes[i + 1] && bytes[i + 1] <= 0x8F) &&
  492. (0x80 <= bytes[i + 2] && bytes[i + 2] <= 0xBF) &&
  493. (0x80 <= bytes[i + 3] && bytes[i + 3] <= 0xBF)
  494. )) {
  495. i += 4;
  496. continue;
  497. }
  498. return false;
  499. }
  500. return true;
  501. },
  502. // 读取cell的数字或字母
  503. getCellNum: function(str){
  504. var n = '';
  505. var isNum = !arguments[1];
  506. for(var i in str) {
  507. var val = parseInt(str[i]);
  508. var _isNaN = isNum ? !isNaN(val) : isNaN(val);
  509. if(_isNaN) n += str[i];
  510. }
  511. return isNum ? parseInt(n) : n;
  512. },
  513. // 表头字母转数字
  514. stringToNum: function(str){
  515. str=str.toLowerCase().split("");
  516. var al = str.length;
  517. var getCharNumber = function(charx){
  518. return charx.charCodeAt() -96;
  519. };
  520. var numout = 0;
  521. var charnum = 0;
  522. for(var i = 0; i < al; i++){
  523. charnum = getCharNumber(str[i]);
  524. numout += charnum * Math.pow(26, al-i-1);
  525. };
  526. return numout;
  527. },
  528. // 数字转字母
  529. numToString: function(numm){
  530. var stringArray = [];
  531. stringArray.length = 0;
  532. var numToStringAction = function(nnum){
  533. var num = nnum - 1;
  534. var a = parseInt(num / 26);
  535. var b = num % 26;
  536. stringArray.push(String.fromCharCode(64 + parseInt(b+1)));
  537. if(a>0){
  538. numToStringAction(a);
  539. }
  540. }
  541. numToStringAction(numm);
  542. return stringArray.reverse().join("");
  543. },
  544. // sheetjs.data转luckysheet.data
  545. xlsToLuckySheet: function(sheet, _sheet){
  546. var arr = (_.get(sheet, '!ref') || ':').split(':');
  547. var cols = this.getCellNum(arr[1], true);
  548. cols = this.stringToNum(cols);
  549. cols = cols > 26 ? cols : 26; // 列,字母,不足的填充
  550. var rows = this.getCellNum(arr[1]);
  551. rows = rows > 84 ? rows : 84; // 行,数字
  552. // 表格样式
  553. var _cols = _.get(sheet, '!cols') || {};
  554. var _rows = _.get(sheet, '!rows') || {};
  555. var _merges = _.get(sheet, '!merges') || {};
  556. var obj = [];
  557. var self = this;
  558. for(var i=1; i<=rows; i++) {
  559. var row = [];
  560. for(var j=1; j<=cols; j++) {
  561. var key = self.numToString(j) + i;
  562. var cell = null;
  563. if(sheet[key]) {
  564. // https://mengshukeji.github.io/LuckysheetDocs/zh/guide/cell.html#基本单元格
  565. var value = sheet[key].v || '';
  566. var style = sheet[key].s || {};
  567. var bgColor = _.get(style, 'fgColor.rgb'); // 前景色
  568. // var ftColor = _.get(style, 'ftColor.rgb');
  569. cell = {
  570. m: value, // 显示值
  571. v: value, // 原始值
  572. ct: {fa: sheet[key].z || 'General', t: sheet[key].t || 'g'},
  573. // bg: bgColor ? '#'+bgColor : '',
  574. // bl: _.get(style, 'patternType') == 'bold' ? 1 : 0,
  575. tb: 2, // 0:截断;1:溢出;2:换行
  576. }
  577. if (bgColor) cell.bg = '#'+bgColor;
  578. }
  579. row.push(cell);
  580. _sheet.config.columnlen[j-1] = _cols[j-1] ? _cols[j-1].wpx : 73; // 默认列宽73px
  581. }
  582. obj.push(row)
  583. _sheet.config.rowlen[i-1] = _rows[i-1] ? _rows[i-1].hpt * 4 / 3 : 19; // 本来有参数hpx,但其值和hpt一样;默认值行高19px
  584. }
  585. // 合并单元格
  586. // https://mengshukeji.github.io/LuckysheetDocs/zh/guide/sheet.html#初始化配置
  587. _.each(_merges, function(opt){
  588. var r = opt.s.r; // sheet[!merges] = [{e:{r:,c:},s:{r:,c:}}]
  589. var c = opt.s.c; // s:start,e:end
  590. _sheet.config.merge[r+'_'+c] = {
  591. r: r,
  592. c: c,
  593. rs: opt.e.r - r + 1,
  594. cs: opt.e.c - c + 1,
  595. };
  596. });
  597. return obj;
  598. },
  599. // 单个sheet初始配置
  600. getLuckySheet: function(){
  601. return {
  602. "name": "Sheet1",
  603. "color": "",
  604. "status": 1,
  605. "order": 0,
  606. "data": [ // data直接替换,这里就不写null填充了
  607. [null],
  608. [null],
  609. ],
  610. "config": {
  611. rowlen: {}, // 表格行高
  612. columnlen: {}, // 表格行宽
  613. merge: {}, // 合并单元格
  614. },
  615. "index": 0,
  616. // "jfgird_select_save": [],
  617. "luckysheet_select_save": [],
  618. "visibledatarow": [],
  619. "visibledatacolumn": [],
  620. // "ch_width": 4560,
  621. // "rh_height": 1760,
  622. "luckysheet_selection_range": [],
  623. "zoomRatio": 1,
  624. "celldata": [],
  625. // "load": "1",
  626. "scrollLeft": 0,
  627. "scrollTop": 0
  628. };
  629. },
  630. functionHook: function(target,method,beforeFunc,afterFunc){
  631. var context = target || window;
  632. var _theMethod = context[method];
  633. if(!context || !_theMethod) return console.error('method error!',method);
  634. context[method] = function(){
  635. var args = arguments;
  636. if(beforeFunc){
  637. var newArgs = beforeFunc.apply(this,args);
  638. if( newArgs === false ) return;
  639. args = newArgs === undefined ? args : newArgs; //没有返回值则使用结果;
  640. }
  641. var result = _theMethod.apply(this,args);
  642. if( afterFunc ){
  643. var newResult = afterFunc.apply(this,[result,args]);
  644. result = newResult === undefined ? result : newResult;//没有返回值则使用结果
  645. }
  646. return result;
  647. }
  648. },
  649. // 初始化主页面尺寸
  650. initPageSize: function(ratio){
  651. var divId = arguments[1] === undefined ? 'all_slides_warpper' : 'left_slides_bar';
  652. return this.changePageSize(ratio, divId);
  653. },
  654. // 变更主页面尺寸
  655. changePageSize: function(ratio, divId){
  656. $('#'+divId+' .slide').css({'-webkit-transform': 'scale('+ratio+')'});
  657. var width = $('#'+divId+' .slide').width() * ratio + 'px';
  658. var height = $('#'+divId+' .slide').height() * ratio + 'px'; // 使用scale后获取到的是原始尺寸,因此需要*ratio
  659. $('#'+divId+' .slide-box').css({'width': width, 'height': height});
  660. },
  661. // 前后翻页
  662. nextSlide: function(type){
  663. if(!$('#left_slides_bar').length) return;
  664. var index = parseInt($('.slide-page-toolbar .page-cur-num').text());
  665. var total = parseInt($('.slide-page-toolbar .page-total-num').text());
  666. if((index == 1 && type == 'sub') || (index == total && type == 'add')) return;
  667. var page = type == 'sub' ? index - 1 : index + 1;
  668. this.gotoSlide(page);
  669. },
  670. // 页码变更
  671. gotoSlide: function(page){
  672. // 0.设置页码显示
  673. $('.slide-page-toolbar .page-cur-num').html(page);
  674. // 1.主区域显示
  675. $('#all_slides_warpper .slide-box').hide();
  676. $('#all_slides_warpper .slide-box').eq(page - 1).show();
  677. // 2.左右翻页图标显示
  678. this.setLtRtIcon();
  679. // 3.左侧选中样式变更
  680. $('#left_slides_bar .slide-box').removeClass('total-page-point');
  681. $('#left_slides_bar .slide-box').eq(page - 1).addClass('total-page-point');
  682. // 左侧选中项滚动到当前区域,滚轮停止后计算
  683. setTimeout(function(){
  684. if (!$(".total-page-point").length) return;
  685. var top = $(".total-page-point").offset().top;
  686. var height = $(".total-page-point").height();
  687. // 选中区在可视范围内时不滚动。margin+padding=8+10
  688. if(top >= 18 && (top + height - 8) < $("#left_slides_bar").height()) {
  689. return false;
  690. }
  691. // 滚动高度为选中区前所有兄弟元素的(高+mb)之和,理论上应该再+18
  692. var prevTop = (height + 10) * (page - 1);
  693. $("#left_slides_bar").scrollTop(prevTop + 10);
  694. }, 200);
  695. },
  696. // 左右翻页图标显示和隐藏
  697. setLtRtIcon: function(){
  698. var index = parseInt($('.slide-page-toolbar .page-cur-num').text());
  699. var total = parseInt($('.slide-page-toolbar .page-total-num').text());
  700. var funcLt = index == 1 ? 'hide' : 'show';
  701. var funcRt = index == total ? 'hide' : 'show';
  702. $('.slide-left-icon.btn')[funcLt]();
  703. $('.slide-right-icon.btn')[funcRt]();
  704. }
  705. }