LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code

  
    
    
    Ionic Elastichat
    
     
    
    
    
    
  
  
    
      
      
    
    
    
    
  
CSS
body {
  cursor: url('https://ionicframework.com/img/finger.png'), auto;
}

/* allows the bar-footer to be elastic /*
/* optionally set a max-height */
/* maxlength on the textarea will prevent /*
/* it from getting too large also */
.bar-footer {
  overflow: visible !important;
}

.bar-footer textarea {
  resize: none;
  height: 25px;
}

/* fixes an ios bug bear */
button.ion-android-send {
  padding-top: 2px;
}

/* add this to keep your footer buttons down 
at the bottom as the textarea resizes */
.footer-btn-wrap {
  position: relative; 
  height: 100%; 
  width: 50px; 
  top: 7px;
}

.footer-btn {
  position: absolute !important; 
  bottom: 0;
}

img.profile-pic {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  position: absolute;
  bottom: 10px;
}

img.profile-pic.left {
  left: 10px;
}

img.profile-pic.right {
  right: 10px;
}

.ion-email {
  float: right;
  font-size: 32px;
  vertical-align: middle;
}

.message {
  font-size: 14px;
}

.message-detail {
  white-space: nowrap;
  font-size: 14px;
}

.bar.item-input-inset .item-input-wrapper input {
  width: 100% !important;
}

.message-wrapper {
  position: relative;
}

.message-wrapper:last-child {
  margin-bottom: 10px;
}

.chat-bubble {
  border-radius: 5px;
  display: inline-block;
  padding: 10px 18px;
  position: relative;
  margin: 10px;
  max-width: 80%;
}

.chat-bubble:before {
  content: "\00a0";
  display: block;
  height: 16px;
  width: 9px;
  position: absolute;
  bottom: -7.5px;
}

.chat-bubble.left {
  background-color: #e6e5eb;
  float: left;
  margin-left: 55px;
}

.chat-bubble.left:before {
  background-color: #e6e5eb;
  left: 10px;
  -webkit-transform: rotate(70deg) skew(5deg);
}

.chat-bubble.right {
  background-color: #158ffe;
  color: #fff;
  float: right;
  margin-right: 55px;
}

.chat-bubble.right:before {
  background-color: #158ffe;
  right: 10px;
  -webkit-transform: rotate(118deg) skew(-5deg);
}

.chat-bubble.right a.autolinker {
  color: #fff;
  font-weight: bold;
}

.user-messages-top-icon {
  font-size: 28px;
  display: inline-block;
  vertical-align: middle;
  position: relative;
  top: -3px;
  right: 5px;
}

.msg-header-username {
  display: inline-block;
  vertical-align: middle;
  position: relative;
  top: -3px;
}

input, textarea, .item-input, .item-input-wrapper {
  background-color: #f4f4f4 !important;
}

.bold {
  font-weight: bold;
}

.cf {
  clear: both !important;
}

a.autolinker {
  color: #3b88c3;
  text-decoration: none;
}

/* loading */
.loader-center {
  height: 100%;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-direction: normal;
  -moz-box-direction: normal;
  -webkit-box-orient: horizontal;
  -moz-box-orient: horizontal;
  -webkit-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-flex-wrap: nowrap;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
  -webkit-box-pack: center;
  -moz-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-align-content: stretch;
  -ms-flex-line-pack: stretch;
  align-content: stretch;
  -webkit-box-align: center;
  -moz-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}

.loader .ion-loading-c {
  font-size: 64px;
}
JS
angular.module('elastichat', ['ionic', 'monospaced.elastic', 'angularMoment'])

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('UserMessages', {
    url: '/UserMessages',
    templateUrl: 'templates/UserMessages.html',
    controller: 'UserMessagesCtrl'
  });

  $urlRouterProvider.otherwise('/UserMessages');
})

.controller('UserMessagesCtrl', ['$scope', '$rootScope', '$state',
  '$stateParams', 'MockService', '$ionicActionSheet',
  '$ionicPopup', '$ionicScrollDelegate', '$timeout', '$interval',
  function($scope, $rootScope, $state, $stateParams, MockService,
    $ionicActionSheet,
    $ionicPopup, $ionicScrollDelegate, $timeout, $interval) {

    // mock acquiring data via $stateParams
    $scope.toUser = {
      _id: '534b8e5aaa5e7afc1b23e69b',
      pic: 'https://ionicframework.com/img/docs/venkman.jpg',
      username: 'Venkman'
    }

    // this could be on $rootScope rather than in $stateParams
    $scope.user = {
      _id: '534b8fb2aa5e7afc1b23e69c',
      pic: 'https://ionicframework.com/img/docs/mcfly.jpg',
      username: 'Marty'
    };

    $scope.input = {
      message: localStorage['userMessage-' + $scope.toUser._id] || ''
    };

    var messageCheckTimer;

    var viewScroll = $ionicScrollDelegate.$getByHandle('userMessageScroll');
    var footerBar; // gets set in $ionicView.enter
    var scroller;
    var txtInput; // ^^^

    $scope.$on('$ionicView.enter', function() {
      console.log('UserMessages $ionicView.enter');

      getMessages();
      
      $timeout(function() {
        footerBar = document.body.querySelector('#userMessagesView .bar-footer');
        scroller = document.body.querySelector('#userMessagesView .scroll-content');
        txtInput = angular.element(footerBar.querySelector('textarea'));
      }, 0);

      messageCheckTimer = $interval(function() {
        // here you could check for new messages if your app doesn't use push notifications or user disabled them
      }, 20000);
    });

    $scope.$on('$ionicView.leave', function() {
      console.log('leaving UserMessages view, destroying interval');
      // Make sure that the interval is destroyed
      if (angular.isDefined(messageCheckTimer)) {
        $interval.cancel(messageCheckTimer);
        messageCheckTimer = undefined;
      }
    });

    $scope.$on('$ionicView.beforeLeave', function() {
      if (!$scope.input.message || $scope.input.message === '') {
        localStorage.removeItem('userMessage-' + $scope.toUser._id);
      }
    });

    function getMessages() {
      // the service is mock but you would probably pass the toUser's GUID here
      MockService.getUserMessages({
        toUserId: $scope.toUser._id
      }).then(function(data) {
        $scope.doneLoading = true;
        $scope.messages = data.messages;

        $timeout(function() {
          viewScroll.scrollBottom();
        }, 0);
      });
    }

    $scope.$watch('input.message', function(newValue, oldValue) {
      console.log('input.message $watch, newValue ' + newValue);
      if (!newValue) newValue = '';
      localStorage['userMessage-' + $scope.toUser._id] = newValue;
    });

    $scope.sendMessage = function(sendMessageForm) {
      var message = {
        toId: $scope.toUser._id,
        text: $scope.input.message
      };

      // if you do a web service call this will be needed as well as before the viewScroll calls
      // you can't see the effect of this in the browser it needs to be used on a real device
      // for some reason the one time blur event is not firing in the browser but does on devices
      keepKeyboardOpen();
      
      //MockService.sendMessage(message).then(function(data) {
      $scope.input.message = '';

      message._id = new Date().getTime(); // :~)
      message.date = new Date();
      message.username = $scope.user.username;
      message.userId = $scope.user._id;
      message.pic = $scope.user.picture;

      $scope.messages.push(message);

      $timeout(function() {
        keepKeyboardOpen();
        viewScroll.scrollBottom(true);
      }, 0);

      $timeout(function() {
        $scope.messages.push(MockService.getMockMessage());
        keepKeyboardOpen();
        viewScroll.scrollBottom(true);
      }, 2000);

      //});
    };
    
    // this keeps the keyboard open on a device only after sending a message, it is non obtrusive
    function keepKeyboardOpen() {
      console.log('keepKeyboardOpen');
      txtInput.one('blur', function() {
        console.log('textarea blur, focus back on it');
        txtInput[0].focus();
      });
    }

    $scope.onMessageHold = function(e, itemIndex, message) {
      console.log('onMessageHold');
      console.log('message: ' + JSON.stringify(message, null, 2));
      $ionicActionSheet.show({
        buttons: [{
          text: 'Copy Text'
        }, {
          text: 'Delete Message'
        }],
        buttonClicked: function(index) {
          switch (index) {
            case 0: // Copy Text
              //cordova.plugins.clipboard.copy(message.text);

              break;
            case 1: // Delete
              // no server side secrets here :~)
              $scope.messages.splice(itemIndex, 1);
              $timeout(function() {
                viewScroll.resize();
              }, 0);

              break;
          }
          
          return true;
        }
      });
    };

    // this prob seems weird here but I have reasons for this in my app, secret!
    $scope.viewProfile = function(msg) {
      if (msg.userId === $scope.user._id) {
        // go to your profile
      } else {
        // go to other users profile
      }
    };
    
    // I emit this event from the monospaced.elastic directive, read line 480
    $scope.$on('taResize', function(e, ta) {
      console.log('taResize');
      if (!ta) return;
      
      var taHeight = ta[0].offsetHeight;
      console.log('taHeight: ' + taHeight);
      
      if (!footerBar) return;
      
      var newFooterHeight = taHeight + 10;
      newFooterHeight = (newFooterHeight > 44) ? newFooterHeight : 44;
      
      footerBar.style.height = newFooterHeight + 'px';
      scroller.style.bottom = newFooterHeight + 'px'; 
    });

}])

// services
.factory('MockService', ['$http', '$q',
  function($http, $q) {
    var me = {};

    me.getUserMessages = function(d) {
      /*
      var endpoint =
        'http://www.mocky.io/v2/547cf341501c337f0c9a63fd?callback=JSON_CALLBACK';
      return $http.jsonp(endpoint).then(function(response) {
        return response.data;
      }, function(err) {
        console.log('get user messages error, err: ' + JSON.stringify(
          err, null, 2));
      });
      */
      var deferred = $q.defer();
      
		 setTimeout(function() {
      	deferred.resolve(getMockMessages());
	    }, 1500);
      
      return deferred.promise;
    };

    me.getMockMessage = function() {
      return {
        userId: '534b8e5aaa5e7afc1b23e69b',
        date: new Date(),
        text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
      };
    }

    return me;
  }
])

// fitlers
.filter('nl2br', ['$filter',
  function($filter) {
    return function(data) {
      if (!data) return data;
      return data.replace(/\n\r?/g, '
'); }; } ]) // directives .directive('autolinker', ['$timeout', function($timeout) { return { restrict: 'A', link: function(scope, element, attrs) { $timeout(function() { var eleHtml = element.html(); if (eleHtml === '') { return false; } var text = Autolinker.link(eleHtml, { className: 'autolinker', newWindow: false }); element.html(text); var autolinks = element[0].getElementsByClassName('autolinker'); for (var i = 0; i < autolinks.length; i++) { angular.element(autolinks[i]).bind('click', function(e) { var href = e.target.href; console.log('autolinkClick, href: ' + href); if (href) { //window.open(href, '_system'); window.open(href, '_blank'); } e.preventDefault(); return false; }); } }, 0); } } } ]) function onProfilePicError(ele) { this.ele.src = ''; // set a fallback } function getMockMessages() { return {"messages":[{"_id":"535d625f898df4e80e2a125e","text":"Ionic has changed the game for hybrid app development.","userId":"534b8fb2aa5e7afc1b23e69c","date":"2014-04-27T20:02:39.082Z","read":true,"readDate":"2014-12-01T06:27:37.944Z"},{"_id":"535f13ffee3b2a68112b9fc0","text":"I like Ionic better than ice cream!","userId":"534b8e5aaa5e7afc1b23e69b","date":"2014-04-29T02:52:47.706Z","read":true,"readDate":"2014-12-01T06:27:37.944Z"},{"_id":"546a5843fd4c5d581efa263a","text":"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.","userId":"534b8fb2aa5e7afc1b23e69c","date":"2014-11-17T20:19:15.289Z","read":true,"readDate":"2014-12-01T06:27:38.328Z"},{"_id":"54764399ab43d1d4113abfd1","text":"Am I dreaming?","userId":"534b8e5aaa5e7afc1b23e69b","date":"2014-11-26T21:18:17.591Z","read":true,"readDate":"2014-12-01T06:27:38.337Z"},{"_id":"547643aeab43d1d4113abfd2","text":"Is this magic?","userId":"534b8fb2aa5e7afc1b23e69c","date":"2014-11-26T21:18:38.549Z","read":true,"readDate":"2014-12-01T06:27:38.338Z"},{"_id":"547815dbab43d1d4113abfef","text":"Gee wiz, this is something special.","userId":"534b8e5aaa5e7afc1b23e69b","date":"2014-11-28T06:27:40.001Z","read":true,"readDate":"2014-12-01T06:27:38.338Z"},{"_id":"54781c69ab43d1d4113abff0","text":"I think I like Ionic more than I like ice cream!","userId":"534b8fb2aa5e7afc1b23e69c","date":"2014-11-28T06:55:37.350Z","read":true,"readDate":"2014-12-01T06:27:38.338Z"},{"_id":"54781ca4ab43d1d4113abff1","text":"Yea, it's pretty sweet","userId":"534b8e5aaa5e7afc1b23e69b","date":"2014-11-28T06:56:36.472Z","read":true,"readDate":"2014-12-01T06:27:38.338Z"},{"_id":"5478df86ab43d1d4113abff4","text":"Wow, this is really something huh?","userId":"534b8fb2aa5e7afc1b23e69c","date":"2014-11-28T20:48:06.572Z","read":true,"readDate":"2014-12-01T06:27:38.339Z"},{"_id":"54781ca4ab43d1d4113abff1","text":"Create amazing apps - ionicframework.com","userId":"534b8e5aaa5e7afc1b23e69b","date":"2014-11-29T06:56:36.472Z","read":true,"readDate":"2014-12-01T06:27:38.338Z"}],"unread":0}; } // configure moment relative time moment.locale('en', { relativeTime: { future: "in %s", past: "%s ago", s: "%d sec", m: "a minute", mm: "%d minutes", h: "an hour", hh: "%d hours", d: "a day", dd: "%d days", M: "a month", MM: "%d months", y: "a year", yy: "%d years" } }); /* * angular-elastic v2.4.2 * (c) 2014 Monospaced http://monospaced.com * License: MIT */ angular.module('monospaced.elastic', []) .constant('msdElasticConfig', { append: '' }) .directive('msdElastic', [ '$timeout', '$window', 'msdElasticConfig', function($timeout, $window, config) { 'use strict'; return { require: 'ngModel', restrict: 'A, C', link: function(scope, element, attrs, ngModel) { // cache a reference to the DOM element var ta = element[0], $ta = element; // ensure the element is a textarea, and browser is capable if (ta.nodeName !== 'TEXTAREA' || !$window.getComputedStyle) { return; } // set these properties before measuring dimensions $ta.css({ 'overflow': 'hidden', 'overflow-y': 'hidden', 'word-wrap': 'break-word' }); // force text reflow var text = ta.value; ta.value = ''; ta.value = text; var append = attrs.msdElastic ? attrs.msdElastic.replace(/\\n/g, '\n') : config.append, $win = angular.element($window), mirrorInitStyle = 'position: absolute; top: -999px; right: auto; bottom: auto;' + 'left: 0; overflow: hidden; -webkit-box-sizing: content-box;' + '-moz-box-sizing: content-box; box-sizing: content-box;' + 'min-height: 0 !important; height: 0 !important; padding: 0;' + 'word-wrap: break-word; border: 0;', $mirror = angular.element('