|
| 1 | +/** drop target **/ |
| 2 | +var _target = document.getElementById('drop'); |
| 3 | + |
| 4 | +/** Spinner **/ |
| 5 | +var spinner; |
| 6 | + |
| 7 | +var _workstart = function() { spinner = new Spinner().spin(_target); } |
| 8 | +var _workend = function() { spinner.stop(); } |
| 9 | + |
| 10 | +/** Alerts **/ |
| 11 | +var _badfile = function() { |
| 12 | + alertify.alert('This file does not appear to be a valid Excel file. If we made a mistake, please send this file to <a href="mailto:dev@sheetjs.com?subject=I+broke+your+stuff">dev@sheetjs.com</a> so we can take a look.', function(){}); |
| 13 | +}; |
| 14 | + |
| 15 | +var _pending = function() { |
| 16 | + alertify.alert('Please wait until the current file is processed.', function(){}); |
| 17 | +}; |
| 18 | + |
| 19 | +var _large = function(len, cb) { |
| 20 | + alertify.confirm("This file is " + len + " bytes and may take a few moments. Your browser may lock up during this process. Shall we play?", cb); |
| 21 | +}; |
| 22 | + |
| 23 | +var _failed = function(e) { |
| 24 | + console.log(e, e.stack); |
| 25 | + alertify.alert('We unfortunately dropped the ball here. We noticed some issues with the grid recently, so please test the file using the direct parsers for <a href="/js-xls/">XLS</a> and <a href="/js-xlsx/">XLSX</a> files. If there are issues with the direct parsers, please send this file to <a href="mailto:dev@sheetjs.com?subject=I+broke+your+stuff">dev@sheetjs.com</a> so we can make things right.', function(){}); |
| 26 | +}; |
| 27 | + |
| 28 | +/** Handsontable magic **/ |
| 29 | +var boldRenderer = function (instance, td, row, col, prop, value, cellProperties) { |
| 30 | + Handsontable.TextCell.renderer.apply(this, arguments); |
| 31 | + $(td).css({'font-weight': 'bold'}); |
| 32 | +}; |
| 33 | + |
| 34 | +var $container, $parent, $window, availableWidth, availableHeight; |
| 35 | +var calculateSize = function () { |
| 36 | + var offset = $container.offset(); |
| 37 | + availableWidth = Math.max($window.width() - 250,600); |
| 38 | + availableHeight = Math.max($window.height() - 250, 400); |
| 39 | +}; |
| 40 | + |
| 41 | +$(document).ready(function() { |
| 42 | + $container = $("#hot"); $parent = $container.parent(); |
| 43 | + $window = $(window); |
| 44 | + $window.on('resize', calculateSize); |
| 45 | +}); |
| 46 | + |
| 47 | +/* make the buttons for the sheets */ |
| 48 | +var make_buttons = function(sheetnames, cb) { |
| 49 | + var $buttons = $('#buttons'); |
| 50 | + $buttons.html(""); |
| 51 | + sheetnames.forEach(function(s,idx) { |
| 52 | + var button= $('<button/>').attr({ type:'button', name:'btn' +idx, text:s }); |
| 53 | + button.append('<h3>' + s + '</h3>'); |
| 54 | + button.click(function() { cb(idx); }); |
| 55 | + $buttons.append(button); |
| 56 | + $buttons.append('<br/>'); |
| 57 | + }); |
| 58 | +}; |
| 59 | + |
| 60 | +var _onsheet = function(json, cols, sheetnames, select_sheet_cb) { |
| 61 | + $('#footnote').hide(); |
| 62 | + |
| 63 | + make_buttons(sheetnames, select_sheet_cb); |
| 64 | + calculateSize(); |
| 65 | + |
| 66 | + /* add header row for table */ |
| 67 | + if(!json) json = []; |
| 68 | + json.unshift(function(head){var o = {}; for(i=0;i!=head.length;++i) o[head[i]] = head[i]; return o;}(cols)); |
| 69 | + calculateSize(); |
| 70 | + /* showtime! */ |
| 71 | + $("#hot").handsontable({ |
| 72 | + data: json, |
| 73 | + startRows: 5, |
| 74 | + startCols: 3, |
| 75 | + fixedRowsTop: 1, |
| 76 | + stretchH: 'all', |
| 77 | + rowHeaders: true, |
| 78 | + columns: cols.map(function(x) { return {data:x}; }), |
| 79 | + colHeaders: cols, |
| 80 | + cells: function (r,c,p) { |
| 81 | + if(r === 0) this.renderer = boldRenderer; |
| 82 | + }, |
| 83 | + width: function () { return availableWidth; }, |
| 84 | + height: function () { return availableHeight; }, |
| 85 | + stretchH: 'all' |
| 86 | + }); |
| 87 | +}; |
| 88 | + |
| 89 | +/** Drop it like it's hot **/ |
| 90 | +DropSheet({ |
| 91 | + drop: _target, |
| 92 | + on: { |
| 93 | + workstart: _workstart, |
| 94 | + workend: _workend, |
| 95 | + sheet: _onsheet, |
| 96 | + foo: 'bar' |
| 97 | + }, |
| 98 | + errors: { |
| 99 | + badfile: _badfile, |
| 100 | + pending: _pending, |
| 101 | + failed: _failed, |
| 102 | + large: _large, |
| 103 | + foo: 'bar' |
| 104 | + } |
| 105 | +}) |
0 commit comments