file-receiver.html 4.0 KB

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