file-receiver.html 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <link rel="import" href="../../bower_components/paper-dialog/paper-dialog.html">
  2. <link rel="import" href="../../bower_components/paper-button/paper-button.html">
  3. <link rel="import" href="../../bower_components/neon-animation/animations/scale-up-animation.html">
  4. <link rel="import" href="../../bower_components/neon-animation/animations/fade-out-animation.html">
  5. <link rel="import" href="../../bower_components/iron-pages/iron-pages.html">
  6. <link rel="import" href="../../bower_components/paper-spinner/paper-spinner.html">
  7. <dom-module id="file-receiver">
  8. <template>
  9. <style>
  10. :host {
  11. display: block;
  12. }
  13. #dialog,
  14. #download {
  15. width: 300px;
  16. z-index: 101;
  17. }
  18. b {
  19. word-break: break-word;
  20. }
  21. </style>
  22. <paper-dialog id="dialog" entry-animation="scale-up-animation" exit-animation="fade-out-animation" with-backdrop modal>
  23. <h2>Download File</h2>
  24. <p><b>{{file.name}}</b></p>
  25. <div class="buttons">
  26. <paper-button dialog-dismiss on-tap="_decline">Discard</paper-button>
  27. <paper-button dialog-confirm on-tap="_accept" autofocus>Download</paper-button>
  28. </div>
  29. </paper-dialog>
  30. <paper-dialog id="download" entry-animation="scale-up-animation" exit-animation="fade-out-animation" with-backdrop modal>
  31. <h2>File Received</h2>
  32. <p>Right Click and "Save as"...</p>
  33. <div class="buttons">
  34. <paper-button dialog-dismiss>Discard</paper-button>
  35. <a href="{{dataUri}}" target="_blank">
  36. <paper-button dialog-confirm autofocus>Download</paper-button>
  37. </a>
  38. </div>
  39. </paper-dialog>
  40. </template>
  41. <script>
  42. 'use strict';
  43. (function() {
  44. Polymer({
  45. is: 'file-receiver',
  46. attached: function() {
  47. this.async(function() {
  48. app.p2p.addEventListener('file-offer', function(e) {
  49. this.file = e.detail;
  50. this.$.dialog.open();
  51. }.bind(this), false);
  52. app.p2p.addEventListener('file-received', function(e) {
  53. this._fileReceived(e.detail);
  54. }.bind(this), false);
  55. app.p2p.addEventListener('file-declined', function(e) {
  56. app.displayToast('User declined file ' + e.detail.name);
  57. }.bind(this), false);
  58. app.p2p.addEventListener('upload-complete', function(e) {
  59. app.displayToast('User received file ' + e.detail.name);
  60. }.bind(this), false);
  61. app.p2p.addEventListener('upload-error', function(e) {
  62. app.displayToast('The other device did not respond. Please try again.');
  63. }.bind(this), false);
  64. }, 200);
  65. },
  66. _fileReceived: function(file) {
  67. this.downloadURI(file);
  68. },
  69. _decline: function() {
  70. app.p2p.decline(this.file);
  71. },
  72. _accept: function() {
  73. app.p2p.accept(this.file);
  74. },
  75. downloadURI: function(file) {
  76. var link = document.createElement('a');
  77. var uri = (window.URL || window.webkitURL).createObjectURL(file.blob);
  78. if (typeof link.download !== 'undefined') {
  79. //download attribute is supported
  80. link.href = uri;
  81. link.download = file.name || 'blank';
  82. document.body.appendChild(link);
  83. link.click();
  84. document.body.removeChild(link);
  85. } else {
  86. this.dataUri = uri;
  87. this.$.download.open();
  88. }
  89. }
  90. });
  91. }());
  92. </script>
  93. </dom-module>