2016-01-02 02:26:07 +01:00
< link rel = "import" href = "../../bower_components/paper-dialog/paper-dialog.html" >
< link rel = "import" href = "../../bower_components/paper-button/paper-button.html" >
< link rel = "import" href = "../../bower_components/neon-animation/animations/scale-up-animation.html" >
< link rel = "import" href = "../../bower_components/neon-animation/animations/fade-out-animation.html" >
< link rel = "import" href = "../../bower_components/iron-pages/iron-pages.html" >
< link rel = "import" href = "../../bower_components/paper-spinner/paper-spinner.html" >
2016-01-02 14:23:51 +01:00
< link rel = "import" href = "../../bower_components/paper-input/paper-textarea.html" >
< link rel = "import" href = "linkify.html" >
< link rel = "import" href = "clipboard-behavior.html" >
< link rel = "import" href = "../sound-notification/sound-notification-behavior.html" >
2016-01-02 02:26:07 +01:00
< dom-module id = "text-input-dialog" >
< template >
< style >
:host {
display: block;
}
2016-01-02 14:23:51 +01:00
#sendDialog,
#receiveDialog {
width: 324px;
2016-01-02 02:26:07 +01:00
z-index: 101;
2016-01-02 14:23:51 +01:00
max-height: 320px;
overflow: hidden;
margin: 16px;
2016-01-02 02:26:07 +01:00
}
2016-01-02 14:23:51 +01:00
@media all and (max-height: 600px) {
#sendDialog {
padding-top: 24px;
2016-01-03 03:07:32 +01:00
top: 0px !important;
2016-01-02 14:23:51 +01:00
}
}
#receivedText {
2016-01-02 02:26:07 +01:00
word-break: break-all;
word-break: break-word;
}
2016-01-02 14:23:51 +01:00
2016-01-02 02:26:07 +01:00
paper-textarea {
2016-01-02 14:23:51 +01:00
max-height: 200px;
width: calc(100% - 48px);
overflow-x: hidden;
overflow-y: auto;
}
#receivedText {
max-height: 200px;
overflow: hidden;
width: calc(100% - 48px);
text-overflow: ellipsis;
-webkit-line-clamp: 9;
clamp: 9;
2016-01-02 02:26:07 +01:00
}
< / style >
2016-01-02 14:23:51 +01:00
< paper-dialog id = "sendDialog" entry-animation = "scale-up-animation" exit-animation = "fade-out-animation" with-backdrop modal >
2016-01-02 02:26:07 +01:00
< h2 > Send Text< / h2 >
2016-01-02 14:23:51 +01:00
< paper-textarea id = "textInput" label = "Enter Text" value = "{{textToSend}}" autofocus > < / paper-textarea >
2016-01-02 02:26:07 +01:00
< div class = "buttons" >
2016-01-02 14:23:51 +01:00
< paper-button dialog-dismiss > Discard< / paper-button >
< paper-button dialog-dismiss on-tap = "_send" > Send< / paper-button >
2016-01-02 02:26:07 +01:00
< / div >
< / paper-dialog >
2016-01-02 14:23:51 +01:00
< paper-dialog id = "receiveDialog" entry-animation = "scale-up-animation" exit-animation = "fade-out-animation" with-backdrop modal >
2016-01-02 02:26:07 +01:00
< h2 > Text Received< / h2 >
2016-01-02 14:23:51 +01:00
< div >
< div id = "receivedText" >
< / div >
< / div >
2016-01-02 02:26:07 +01:00
< div class = "buttons" >
2016-01-05 04:00:27 +01:00
< paper-button dialog-dismiss > Close< / paper-button >
2016-01-03 03:07:32 +01:00
< paper-button on-tap = "_copy" autofocus hidden $ = " { { ! clipboardSupported } } " > Copy< / paper-button >
2016-01-02 14:23:51 +01:00
< a href = "tel:{{tel}}" hidden $ = " { { ! tel } } " >
2016-01-03 03:07:32 +01:00
< paper-button autofocus dialog-dismiss > Call< / paper-button >
< / a >
< a href = "{{url}}" hidden $ = " { { ! url } } " target = "_blank" >
< paper-button autofocus dialog-dismiss > Open< / paper-button >
2016-01-02 02:26:07 +01:00
< / a >
< / div >
< / paper-dialog >
< / template >
< script >
'use strict';
(function() {
2016-01-02 14:23:51 +01:00
/*
*
* /^\+?[0-9x]*$/ is the first usuful Text sent via Snapdrop 2015/1/2 5:30
*
*/
var phoneNumbers = /^\+?[0-9x/ ]*$/;
2016-01-03 03:07:32 +01:00
var urls = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-; :&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&; %@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/;
2016-01-02 02:26:07 +01:00
Polymer({
is: 'text-input-dialog',
2016-01-03 03:07:32 +01:00
behaviors: [Chat.ClipboardBehavior, Chat.SoundNotificationBehavior],
2016-01-02 14:23:51 +01:00
properties: {
textToSend: {
type: String
},
receivedText: {
type: String
},
contact: {
type: Object
},
tel: {
computed: '_isPhoneNumber(receivedText)',
value: false
2016-01-03 03:07:32 +01:00
},
url: {
computed: '_isUrl(receivedText)',
value: false
},
clipboardSupported: {
value: false
},
fallback: {
computed: '_isFallback(url,tel,clipboardSupported)',
value: false
2016-01-02 14:23:51 +01:00
}
2016-01-02 02:26:07 +01:00
},
2016-01-02 14:23:51 +01:00
open: function(contact) {
this.contact = contact;
this.$.sendDialog.open();
2016-01-02 02:26:07 +01:00
},
2016-01-02 14:23:51 +01:00
attached: function() {
2016-01-03 03:07:32 +01:00
// clipboard must be initalized by user interaction
var that = this;
var hackListener = function() {
document.body.removeEventListener('touchstart', hackListener, false);
document.body.removeEventListener('click', hackListener, false);
// wait 1s to tell the ui that copy is supported
that.async(function() {
that.clipboardSupported = document.queryCommandSupported & & document.queryCommandSupported('copy');
}, 1000);
};
document.body.addEventListener('touchstart', hackListener, false);
document.body.addEventListener('click', hackListener, false);
2016-01-02 14:23:51 +01:00
this.async(function() {
app.conn.addEventListener('text-received', function(e) {
var receivedText = e.detail.text;
2016-01-03 03:07:32 +01:00
if (!receivedText || receivedText.trim() === '') {
this.playSound();
return;
}
2016-01-02 14:23:51 +01:00
this.receivedText = receivedText;
this.$.receivedText.textContent = receivedText;
window.linkifyElement(this.$.receivedText, {}, document);
this.$.receiveDialog.open();
this.playSound();
}.bind(this), false);
}, 200);
this.$.textInput.addEventListener('keypress', function(e) {
if (e.which === 13 || e.charCode === 13) {
var key;
var isShift;
if (window.event) {
key = window.event.keyCode;
isShift = !!window.event.shiftKey; // typecast to boolean
} else {
key = e.which;
isShift = !!e.shiftKey;
}
if (!isShift) {
e.preventDefault();
e.stopPropagation();
this._send();
}
}
}.bind(this), false);
2016-01-03 03:07:32 +01:00
2016-01-02 02:26:07 +01:00
},
2016-01-02 14:23:51 +01:00
_send: function() {
this.$.sendDialog.close();
app.conn.sendText(this.contact.peerId, this.textToSend);
2016-01-02 02:26:07 +01:00
},
2016-01-02 14:23:51 +01:00
_copy: function() {
this.copyToClipboard(this.receivedText);
this.$.receiveDialog.close();
console.log('text copied', this.receivedText);
2016-01-02 02:26:07 +01:00
},
2016-01-02 14:23:51 +01:00
_isPhoneNumber: function(text) {
if (!text || text.length < 5 | | text . length > 100) {
return false;
2016-01-02 02:26:07 +01:00
}
2016-01-02 14:23:51 +01:00
if (phoneNumbers.test(text)) {
return text;
}
},
2016-01-03 03:07:32 +01:00
_isUrl: function(text) {
if (!text) {
return false;
}
if (urls.test(text)) {
return text;
}
},
_isFallback: function(url, tel, clipboardSupported) {
return (!url & & !tel & & !clipboardSupported);
}
2016-01-02 02:26:07 +01:00
});
}());
< / script >
< / dom-module >