main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** drop target **/
  2. var _target = document.getElementById('drop');
  3. /** Spinner **/
  4. var spinner;
  5. var _workstart = function() { spinner = new Spinner().spin(_target); }
  6. var _workend = function() { spinner.stop(); }
  7. /** Alerts **/
  8. var _badfile = function() {
  9. 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:[email protected]?subject=I+broke+your+stuff">[email protected]</a> so we can take a look.', function(){});
  10. };
  11. var _pending = function() {
  12. alertify.alert('Please wait until the current file is processed.', function(){});
  13. };
  14. var _large = function(len, cb) {
  15. 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);
  16. };
  17. var _failed = function(e) {
  18. console.log(e, e.stack);
  19. alertify.alert('We unfortunately dropped the ball here. We noticed some issues with the grid recently, so please test the file using the <a href="/js-xlsx/">raw parser</a>. If there are issues with the file processor, please send this file to <a href="mailto:[email protected]?subject=I+broke+your+stuff">[email protected]</a> so we can make things right.', function(){});
  20. };
  21. /** Handsontable magic **/
  22. var boldRenderer = function (instance, td, row, col, prop, value, cellProperties) {
  23. Handsontable.TextCell.renderer.apply(this, arguments);
  24. $(td).css({'font-weight': 'bold'});
  25. };
  26. var $container, $parent, $window, availableWidth, availableHeight;
  27. var calculateSize = function () {
  28. var offset = $container.offset();
  29. availableWidth = Math.max($window.width() - 250,600);
  30. availableHeight = Math.max($window.height() - 250, 400);
  31. };
  32. $(document).ready(function() {
  33. $container = $("#hot"); $parent = $container.parent();
  34. $window = $(window);
  35. $window.on('resize', calculateSize);
  36. });
  37. /* make the buttons for the sheets */
  38. var make_buttons = function(sheetnames, cb) {
  39. var $buttons = $('#buttons');
  40. $buttons.html("");
  41. sheetnames.forEach(function(s,idx) {
  42. var button= $('<button/>').attr({ type:'button', name:'btn' +idx, text:s });
  43. button.append('<h3>' + s + '</h3>');
  44. button.click(function() { cb(idx); });
  45. $buttons.append(button);
  46. $buttons.append('<br/>');
  47. });
  48. };
  49. var _onsheet = function(json, sheetnames, select_sheet_cb) {
  50. //$('#footnote').hide();
  51. make_buttons(sheetnames, select_sheet_cb);
  52. calculateSize();
  53. /* add header row for table */
  54. if(!json) json = [];
  55. json.forEach(function(r) {
  56. if(json[0].length < r.length) json[0].length = r.length;
  57. });
  58. calculateSize();
  59. /* showtime! */
  60. $("#hot").handsontable({
  61. data: json,
  62. startRows: 5,
  63. startCols: 3,
  64. stretchH: 'all',
  65. rowHeaders: true,
  66. colHeaders: true,
  67. width: function () { return availableWidth; },
  68. height: function () { return availableHeight; },
  69. stretchH: 'all'
  70. });
  71. };
  72. /** Drop it like it's hot **/
  73. DropSheet({
  74. drop: _target,
  75. on: {
  76. workstart: _workstart,
  77. workend: _workend,
  78. sheet: _onsheet,
  79. foo: 'bar'
  80. },
  81. errors: {
  82. badfile: _badfile,
  83. pending: _pending,
  84. failed: _failed,
  85. large: _large,
  86. foo: 'bar'
  87. }
  88. })