Advertisement
Guest User

chat

a guest
Feb 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template lang="html">
  2.   <div class="chat">
  3.     <div class="message">
  4.       <div v-for="message in messages">
  5.         <div :class="account">
  6.           {{message.messageText}}<br />{{message.messageText}}
  7.         </div>
  8.       </div>
  9.       <div v-for="picstring in picstrings">
  10.         <img :src="picstring" alt="Testbild" /><br />
  11.       </div>
  12.     </div>
  13.     <div class="action">
  14.       <input v-model="textField" type="text" />
  15.       <button type="button" @click="send" name="send">send</button>
  16.       <button type="button" @click="showActionSheetWithIcons" name="open">open</button><br />
  17.       <input v-model="userName" type="text" />
  18.     </div>
  19.   </div>
  20. </template>
  21. <script>
  22. import deepstream from "deepstream.io-client-js"
  23. import { ActionSheet, Toast } from 'quasar'
  24. export default {
  25.   data: function() {
  26.     return {
  27.       record: null,
  28.       userName: 'unknown',
  29.       user: '',
  30.       messageText: '',
  31.       side: 'left',
  32.       foobar: '',
  33.       picstrings: [],
  34.       client: null,
  35.       textField: "",
  36.       messages: [],
  37.       message: {user: '', messageText: ''},
  38.       plist: null,
  39.       types: [
  40.         {
  41.           label: 'Gallery with Icons',
  42.           handler () {
  43.             showActionSheetWithIcons(true)
  44.           }
  45.         }
  46.       ],
  47.     }
  48.   },
  49.   created() {
  50.     this.client = deepstream('5.9.106.46:6020').login()
  51.     this.record = this.client.record.getRecord('whynot')
  52.     this.plist = this.client.record.getList('pics')
  53.     // when a message was added to mlist, it becomes pushed to the messages array
  54.     /*this.mlist.on('entry-added', (message) => {
  55.       if(message.substring(0,1) == this.accountName){
  56.         this.account = 'right'
  57.       }else{
  58.         this.account = 'left'
  59.       }
  60.       this.messages.push(message.substring(1,message.length))
  61.     }),*/
  62.     // when a base64 string (a picture) was added to plist, it becomes pushed to the picstring array
  63.     this.record.subscribe('whynot', transmessage => {
  64.       console.log('hi')
  65.       this.message.user = transmessage.user
  66.       this.message.messageText = transmessage.messageText
  67.       this.messages.push(this.message)
  68.     }),
  69.     this.plist.on('entry-added', (picstring) => {
  70.       this.picstrings.push(picstring)
  71.     })
  72.     //user.subscribe(userDataChanged)
  73.   },
  74.   methods: {
  75.     send() {
  76.       this.record.set({user: this.userName, messageText: this.textField})
  77.       this.textField = ''
  78.     },
  79.     savePic() {
  80.       // adds content of foobar (base64 string) to the plist
  81.       this.plist.addEntry(this.foobar)
  82.     },
  83.     openCamera() {
  84.     var srcType = Camera.PictureSourceType.CAMERA;
  85.     var options = this.setOptions(srcType);
  86.     var func = this.createNewFileEntry;
  87.     // opens camera when srcType ist CAMERA
  88.     navigator.camera.getPicture((imageData) => {
  89.       // saves the base64 string into foobar
  90.       this.foobar = "data:image/jpeg;base64," + imageData;
  91.       // calls methode savePic to add foobar into plist
  92.       this.savePic();
  93.     }, (error) => {
  94.         console.debug("Unable to obtain picture: " + error, "app");
  95.       }, options);
  96.     },
  97.     setOptions(srcType) {
  98.       var options = {
  99.           quality: 100,
  100.           // DATA_URL returns pic as base64, FILE_URI returns pic's path
  101.           destinationType: Camera.DestinationType.DATA_URL,
  102.           sourceType: srcType,
  103.           encodingType: Camera.EncodingType.JPEG,
  104.           mediaType: Camera.MediaType.PICTURE,
  105.           // allows to resize the pic after taking it
  106.           allowEdit: true,
  107.           // allows to differentiate between partrait and horizontal format
  108.           correctOrientation: true,
  109.           targetWidth: 1500
  110.       }
  111.       return options;
  112.     },
  113.     createNewFileEntry(imgUri) {
  114.       window.resolveLocalFileSystemURL(cordova.file.cacheDirectory, function success(dirEntry) {
  115.           dirEntry.getFile("tempFile.jpeg", { create: true, exclusive: false }, function (fileEntry) {
  116.               console.log("got file: " + fileEntry.fullPath);
  117.           }, function() {
  118.             console.log("Error Create File")
  119.           });
  120.       }, function() {
  121.         console.log("ErrorResolveUrl")
  122.         });
  123.       },
  124.     openFilePicker(selection) {
  125.       var srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM;
  126.       var options = this.setOptions(srcType);
  127.       var func = this.createNewFileEntry;
  128.       // opens gallery when srcType is SAVEDPHOTOALBUM
  129.       navigator.camera.getPicture((imageData) => {
  130.         // saves the base64 string into foobar
  131.         this.foobar = "data:image/jpeg;base64," + imageData;
  132.         // calls methode savePic to add foobar into plist
  133.         this.savePic();
  134.       }, function cameraError(error) {
  135.           console.debug("Unable to obtain picture: " + error, "app");
  136.       }, options);
  137.     },
  138.     // opens actionsheet
  139.     showActionSheetWithIcons(gallery) {
  140.       let that = this
  141.       ActionSheet.create({
  142.         title: 'Article Actions',
  143.         gallery: gallery,
  144.         // every array element is another icon
  145.         actions: [
  146.           {
  147.             label: 'Collections',
  148.             icon: 'collections',
  149.             handler () {
  150.               // calls methode openFilePicker to load a pic of gallery
  151.               that.openFilePicker()
  152.               Toast.create('Open Collection')
  153.             }
  154.           },
  155.           {
  156.             label: 'Camera',
  157.             icon: 'camera',
  158.             handler () {
  159.               // calls methode openCamera to take a pic
  160.               that.openCamera()
  161.               Toast.create('Open Camera')
  162.             }
  163.           }
  164.         ],
  165.         dismiss: {
  166.           label: 'Cancel',
  167.           icon: 'cancel',
  168.           classes: 'text-primary',
  169.           handler () {
  170.             Toast.create('Cancelled...')
  171.           }
  172.         }
  173.       })
  174.    }
  175.   }
  176. }
  177. </script>
  178. <style lang="css">
  179.   .chat {
  180.     padding: 20px;
  181.   }
  182.   .message {
  183.     height: 400px;
  184.     width: 200px;
  185.     overflow: auto;
  186.   }
  187.   .action {
  188.     position: fixed;
  189.     bottom: 0px;
  190.   }
  191.   .left{
  192.     margin: 5px;
  193.     background-color: grey;
  194.     width: 75px;
  195.     left: 5px
  196.   }
  197.   .right{
  198.     margin: 5px;
  199.     background-color: white;
  200.     width: 75px;
  201.     right: 5px
  202.   }
  203. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement