Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Version 1: http://pastebin.com/x16d1cGm
  2. // Version 2: http://pastebin.com/m0yTf2Uu
  3. // Alpha Version: http://pastebin.com/awFjBBPg
  4.  
  5. angular.module('regidiumApp')
  6.     .directive('msgScrollbar', ['chats', function (chats) {
  7.         return {
  8.             restrict: 'AE',
  9.             scope: {
  10.                 msgScrollbarChat: '='
  11.             },
  12.             link: function (scope, element, attrs) {
  13.  
  14.  
  15.  
  16.                 /*
  17.                 * Init.
  18.                 */
  19.  
  20.                 var COUNT_OF_MESSAGES = 5;
  21.                 var MAX_OFFSET = 50;
  22.  
  23.                 element[0].className += " m--scrollable";
  24.  
  25.                 scrollArea = $('.rw-messages__list-inner')[0];
  26.  
  27.                 var track = document.createElement('div');
  28.                 track.className = 'rw-window-scrollbar-track';
  29.                 element[0].appendChild(track);
  30.  
  31.                 var thumb = document.createElement('div');
  32.                 thumb.className = 'rw-window-scrollbar-thumb';
  33.                 element[0].appendChild(thumb);
  34.  
  35.                 /* ############ */
  36.  
  37.  
  38.                 /*
  39.                 * Private methods.
  40.                 */
  41.  
  42.                 function updateThumb() {
  43.                     var ratio = element[0].offsetHeight / element[0].scrollHeight;
  44.                     $(thumb).css('top', -parseInt(scrollArea.style.marginBottom) * ratio);
  45.                     setTimeout(function() {
  46.                         ratio = element[0].offsetHeight / element[0].scrollHeight;
  47.                         $(thumb).css('height', ratio * element[0].offsetHeight);
  48.                     }, 0);
  49.                 }
  50.  
  51.                 function scrollToEnd() {
  52.                     setTimeout(function() {
  53.                         element[0].scrollTop = element[0].scrollHeight;
  54.                         updateThumb();
  55.                     }, 0);
  56.                 }
  57.  
  58.                 function changeScrollTop(offset) {
  59.  
  60.                     var ratio = element[0].scrollHeight / $(track).height();
  61.  
  62.                     element[0].scrollTop = element.scrollTop() - offset * ratio;
  63.                 }
  64.  
  65.                 /* ############ */
  66.  
  67.  
  68.  
  69.                 /*
  70.                 * Asynchronious block.
  71.                 */
  72.  
  73.                 setTimeout(function() {
  74.                     scrollToEnd();
  75.                 }, 0);
  76.  
  77.                 /* ############ */
  78.  
  79.  
  80.  
  81.                 /*
  82.                 * Watching section.
  83.                 */
  84.  
  85.                 scope.$watch('msgScrollbarChat.messages', function(value, old) {
  86.                     if(!value) return;
  87.  
  88.                     if(value.length) {
  89.  
  90.                         if(value.length === old.length + 1) {
  91.                             scrollToEnd();
  92.                             return;
  93.                         }
  94.                        
  95.                         var hh = 0;
  96.                        
  97.                         var heights = _.map(_.values($('.m--message')), function(obj) {
  98.                             return obj.offsetHeight || 0;
  99.                         }).slice( -(COUNT_OF_MESSAGES + 5) );
  100.  
  101.                         for(key in heights)
  102.                             hh += heights[key];
  103.                        
  104.                         element[0].scrollTop = hh;
  105.                         scrollArea.style.marginBottom = -hh + 'px';
  106.                     }
  107.  
  108.  
  109.                     /*
  110.                     * Upload more messages if existing messages aren't enough for sliding.
  111.                     * Example: large screen or screen with high resolution.
  112.                     */
  113.                     setTimeout(function() {
  114.  
  115.                         var heights = _.map(_.values($('.m--message')), function(obj) {
  116.                             return obj.offsetHeight || 0;
  117.                         });
  118.                        
  119.                         var globalHeight = 0;
  120.                        
  121.                         for(key in heights)
  122.                             globalHeight += heights[key];
  123.                        
  124.                         if(globalHeight < 2 * element[0].offsetHeight) {
  125.                             $(thumb).hide();
  126.                             chats.get(scope.msgScrollbarChat, true).success(function() {
  127.                                 scrollToEnd();
  128.                             });
  129.                         } else {
  130.                             $(thumb).show();
  131.                         }
  132.  
  133.                     }, 0);
  134.                    
  135.                 }, true);
  136.  
  137.  
  138.                 /*
  139.                 * If we switched into another chat just scrolled to end.
  140.                 */
  141.                 scope.$watch('msgScrollbarChat', function(value, old) {
  142.                     if(!value) return;
  143.                     scrollToEnd();
  144.                 });
  145.  
  146.                 /* ############ */
  147.  
  148.  
  149.  
  150.                 /*
  151.                 * Event binding.
  152.                 */
  153.  
  154.                 $(element).scroll(function() {
  155.  
  156.                     var _scrollTop = element.scrollTop();                    
  157.  
  158.                     if(!_scrollTop) {
  159.                         chats.get(scope.msgScrollbarChat, true).success(function(messages) {
  160.                             updateThumb();
  161.                         });
  162.                     }
  163.                    
  164.                     scrollArea.style.marginBottom = -_scrollTop + 'px';
  165.  
  166.                     updateThumb();
  167.                 });
  168.  
  169.                 $(thumb).on('mouseover', function() { $(thumb).addClass('m--over'); });
  170.                 $(thumb).on('mouseout', function() { $(thumb).removeClass('m--over'); });
  171.  
  172.                 var pressed = false;
  173.  
  174.                 $(thumb).on('mousedown', function() {
  175.                     pressed = true;
  176.                     document.onselectstart = function() { return false; };
  177.                 });
  178.                
  179.                 $(document).on('mouseup', function() {
  180.                     pressed = false;
  181.                     document.onselectstart = function() { return true; };
  182.                 });
  183.                
  184.                 $(document).on('mousemove', function(e) {
  185.                     if(pressed) {
  186.                         changeScrollTop(-e.originalEvent.movementY);
  187.                     }
  188.                 });
  189.  
  190.                 $(track).on('mousedown', function(e) {
  191.  
  192.                     var topThumbOffset = parseInt( $(thumb).css('top') );
  193.                     var offset = 0;
  194.  
  195.                     if(e.offsetY > topThumbOffset) {
  196.                         offset = -Math.min(MAX_OFFSET, e.offsetY - topThumbOffset - $(thumb).height());
  197.                     } else {
  198.                         offset = Math.min(MAX_OFFSET, topThumbOffset - e.offsetY);
  199.                     }
  200.  
  201.                     changeScrollTop(offset);
  202.                 });
  203.  
  204.                 /* ############ */
  205.  
  206.  
  207.  
  208.             }
  209.         }
  210.     }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement