My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binaryfileuploader.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. function BinaryFileUploader(o) {
  2. this.options = null;
  3. this._defaultOptions = {
  4. element: null, // HTML file element
  5. onFileLoad: function(file) {
  6. console.log(file.toString());
  7. }
  8. };
  9. this._init = function(o) {
  10. if (!this.hasFileUploaderSupport()) return;
  11. this._verifyDependencies();
  12. this.options = this._mergeObjects(this._defaultOptions, o);
  13. this._verifyOptions();
  14. this.addFileChangeListener();
  15. }
  16. this.hasFileUploaderSupport = function() {
  17. return !!(window.File && window.FileReader && window.FileList && window.Blob);
  18. }
  19. this.addFileChangeListener = function() {
  20. this.options.element.addEventListener(
  21. 'change',
  22. this._bind(this, this.onFileChange)
  23. );
  24. }
  25. this.onFileChange = function(e) {
  26. // TODO accept multiple files
  27. var file = e.target.files[0],
  28. reader = new FileReader();
  29. reader.onload = this._bind(this, this.onFileLoad);
  30. reader.readAsBinaryString(file);
  31. }
  32. this.onFileLoad = function(e) {
  33. var content = e.target.result,
  34. string = new BinaryString(content);
  35. this.options.onFileLoad(string);
  36. }
  37. this._mergeObjects = function(starting, override) {
  38. var merged = starting;
  39. for (key in override) merged[key] = override[key];
  40. return merged;
  41. }
  42. this._verifyOptions = function() {
  43. if (!(this.options.element && this.options.element.type && this.options.element.type === 'file')) {
  44. throw 'Invalid element param in options. Must be a file upload DOM element';
  45. }
  46. if (typeof this.options.onFileLoad !== 'function') {
  47. throw 'Invalid onFileLoad param in options. Must be a function';
  48. }
  49. }
  50. this._verifyDependencies = function() {
  51. if (!window.BinaryString) throw 'BinaryString is missing. Check that you\'ve correctly included it';
  52. }
  53. // helper function for binding methods to objects
  54. this._bind = function(object, method) {
  55. return function() {return method.apply(object, arguments);};
  56. }
  57. this._init(o);
  58. }