text-input-dialog.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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="../../bower_components/paper-input/paper-textarea.html">
  8. <link rel="import" href="linkify.html">
  9. <link rel="import" href="clipboard-behavior.html">
  10. <link rel="import" href="../sound-notification/sound-notification-behavior.html">
  11. <dom-module id="text-input-dialog">
  12. <template>
  13. <style>
  14. :host {
  15. display: block;
  16. }
  17. #sendDialog,
  18. #receiveDialog {
  19. width: 324px;
  20. z-index: 101;
  21. max-height: 320px;
  22. overflow: hidden;
  23. margin: 16px;
  24. }
  25. @media all and (max-height: 600px) {
  26. #sendDialog {
  27. padding-top: 24px;
  28. top:0px !important;
  29. }
  30. }
  31. #receivedText {
  32. word-break: break-all;
  33. word-break: break-word;
  34. }
  35. paper-textarea {
  36. max-height: 200px;
  37. width: calc(100% - 48px);
  38. overflow-x: hidden;
  39. overflow-y: auto;
  40. }
  41. #receivedText {
  42. max-height: 200px;
  43. overflow: hidden;
  44. width: calc(100% - 48px);
  45. text-overflow: ellipsis;
  46. -webkit-line-clamp: 9;
  47. clamp: 9;
  48. }
  49. </style>
  50. <paper-dialog id="sendDialog" entry-animation="scale-up-animation" exit-animation="fade-out-animation" with-backdrop modal>
  51. <h2>Send Text</h2>
  52. <paper-textarea id="textInput" label="Enter Text" value="{{textToSend}}" autofocus></paper-textarea>
  53. <div class="buttons">
  54. <paper-button dialog-dismiss>Discard</paper-button>
  55. <paper-button dialog-dismiss on-tap="_send">Send</paper-button>
  56. </div>
  57. </paper-dialog>
  58. <paper-dialog id="receiveDialog" entry-animation="scale-up-animation" exit-animation="fade-out-animation" with-backdrop modal>
  59. <h2>Text Received</h2>
  60. <div>
  61. <div id="receivedText">
  62. </div>
  63. </div>
  64. <div class="buttons">
  65. <paper-button dialog-dismiss>Discard</paper-button>
  66. <a href="tel:{{tel}}" hidden$="{{!tel}}">
  67. <paper-button dialog-dismiss>Call</paper-button>
  68. </a>
  69. <paper-button on-tap="_copy" autofocus>Copy</paper-button>
  70. </div>
  71. </paper-dialog>
  72. </template>
  73. <script>
  74. 'use strict';
  75. (function() {
  76. /*
  77. *
  78. * /^\+?[0-9x]*$/ is the first usuful Text sent via Snapdrop 2015/1/2 5:30
  79. *
  80. */
  81. var phoneNumbers = /^\+?[0-9x/ ]*$/;
  82. Polymer({
  83. is: 'text-input-dialog',
  84. behaviors: [Chat.ClipboardBehavior,Chat.SoundNotificationBehavior],
  85. properties: {
  86. textToSend: {
  87. type: String
  88. },
  89. receivedText: {
  90. type: String
  91. },
  92. contact: {
  93. type: Object
  94. },
  95. tel: {
  96. computed: '_isPhoneNumber(receivedText)',
  97. value: false
  98. }
  99. },
  100. open: function(contact) {
  101. this.contact = contact;
  102. this.$.sendDialog.open();
  103. },
  104. attached: function() {
  105. this.async(function() {
  106. app.conn.addEventListener('text-received', function(e) {
  107. var receivedText = e.detail.text;
  108. this.receivedText = receivedText;
  109. this.$.receivedText.textContent = receivedText;
  110. window.linkifyElement(this.$.receivedText, {}, document);
  111. this.$.receiveDialog.open();
  112. this.playSound();
  113. }.bind(this), false);
  114. }, 200);
  115. this.$.textInput.addEventListener('keypress', function(e) {
  116. if (e.which === 13 || e.charCode === 13) {
  117. var key;
  118. var isShift;
  119. if (window.event) {
  120. key = window.event.keyCode;
  121. isShift = !!window.event.shiftKey; // typecast to boolean
  122. } else {
  123. key = e.which;
  124. isShift = !!e.shiftKey;
  125. }
  126. if (!isShift) {
  127. e.preventDefault();
  128. e.stopPropagation();
  129. this._send();
  130. }
  131. }
  132. }.bind(this), false);
  133. },
  134. _send: function() {
  135. this.$.sendDialog.close();
  136. app.conn.sendText(this.contact.peerId, this.textToSend);
  137. },
  138. _copy: function() {
  139. this.copyToClipboard(this.receivedText);
  140. this.$.receiveDialog.close();
  141. console.log('text copied', this.receivedText);
  142. },
  143. _isPhoneNumber: function(text) {
  144. if (!text || text.length < 5 || text.length > 100) {
  145. return false;
  146. }
  147. if (phoneNumbers.test(text)) {
  148. return text;
  149. }
  150. },
  151. });
  152. }());
  153. </script>
  154. </dom-module>