ios.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* global window, document, fetch */
  2. const MAXFILESIZE = 1024 * 1024 * 1024 * 2;
  3. const EventEmitter = require('events');
  4. const emitter = new EventEmitter();
  5. function dom(tagName, attributes, children = []) {
  6. const node = document.createElement(tagName);
  7. for (const name in attributes) {
  8. if (name.indexOf('on') === 0) {
  9. node[name] = attributes[name];
  10. } else if (name === 'htmlFor') {
  11. node.htmlFor = attributes.htmlFor;
  12. } else if (name === 'className') {
  13. node.className = attributes.className;
  14. } else {
  15. node.setAttribute(name, attributes[name]);
  16. }
  17. }
  18. if (!(children instanceof Array)) {
  19. children = [children];
  20. }
  21. for (let child of children) {
  22. if (typeof child === 'string') {
  23. child = document.createTextNode(child);
  24. }
  25. node.appendChild(child);
  26. }
  27. return node;
  28. }
  29. function uploadComplete(file) {
  30. document.body.innerHTML = '';
  31. const input = dom('input', { id: 'url', value: file.url });
  32. const copy = dom(
  33. 'button',
  34. {
  35. id: 'copy-button',
  36. className: 'button',
  37. onclick: () => {
  38. window.webkit.messageHandlers['copy'].postMessage(input.value);
  39. copy.textContent = 'Copied!';
  40. setTimeout(function() {
  41. copy.textContent = 'Copy to clipboard';
  42. }, 2000);
  43. }
  44. },
  45. 'Copy to clipboard'
  46. );
  47. const node = dom(
  48. 'div',
  49. { id: 'striped' },
  50. dom('div', { id: 'white' }, [
  51. input,
  52. copy,
  53. dom(
  54. 'button',
  55. { id: 'send-another', className: 'button', onclick: render },
  56. 'Send another file'
  57. )
  58. ])
  59. );
  60. document.body.appendChild(node);
  61. }
  62. const state = {
  63. storage: {
  64. files: [],
  65. remove: function(fileId) {
  66. console.log('REMOVE FILEID', fileId);
  67. },
  68. writeFile: function(file) {
  69. console.log('WRITEFILE', file);
  70. },
  71. addFile: uploadComplete,
  72. totalUploads: 0
  73. },
  74. transfer: null,
  75. uploading: false,
  76. settingPassword: false,
  77. passwordSetError: null,
  78. route: '/'
  79. };
  80. function upload(event) {
  81. console.log('UPLOAD');
  82. event.preventDefault();
  83. const target = event.target;
  84. const file = target.files[0];
  85. if (file.size === 0) {
  86. return;
  87. }
  88. if (file.size > MAXFILESIZE) {
  89. console.log('file too big (no bigger than ' + MAXFILESIZE + ')');
  90. return;
  91. }
  92. emitter.emit('upload', { file: file, type: 'click' });
  93. }
  94. function render() {
  95. document.body.innerHTML = '';
  96. const striped = dom(
  97. 'div',
  98. { id: 'striped' },
  99. dom('div', { id: 'white' }, [
  100. dom('label', { id: 'label', htmlFor: 'input' }, 'Choose file'),
  101. dom('input', {
  102. id: 'input',
  103. type: 'file',
  104. name: 'input',
  105. onchange: upload
  106. })
  107. ])
  108. );
  109. document.body.appendChild(striped);
  110. }
  111. emitter.on('render', function() {
  112. document.body.innerHTML = '';
  113. const percent =
  114. (state.transfer.progress[0] / state.transfer.progress[1]) * 100;
  115. const node = dom(
  116. 'div',
  117. { style: 'background-color: white; width: 100%' },
  118. dom('span', {
  119. style: `display: inline-block; width: ${percent}%; background-color: blue`
  120. })
  121. );
  122. document.body.appendChild(node);
  123. });
  124. emitter.on('pushState', function(path) {
  125. console.log('pushState ' + path + ' ' + JSON.stringify(state));
  126. });
  127. const controller = require('../app/controller').default;
  128. try {
  129. controller(state, emitter);
  130. } catch (e) {
  131. console.error('error' + e);
  132. console.error(e);
  133. }
  134. function sendBase64EncodedFromSwift(encoded) {
  135. fetch(encoded)
  136. .then(res => res.blob())
  137. .then(blob => {
  138. emitter.emit('upload', { file: blob, type: 'share' });
  139. });
  140. }
  141. window.sendBase64EncodedFromSwift = sendBase64EncodedFromSwift;
  142. render();
  143. window.webkit.messageHandlers['loaded'].postMessage('');