file-receiver.html 4.2 KB

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