Your IP : 18.219.40.177


Current Path : /usr/local/mgr5/skins/orion/src/
Upload File :
Current File : //usr/local/mgr5/skins/orion/src/App.HorizontScroll.js

/**
 * App.HorizontScroll
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 *  @param {object} App Application
 */
App.HorizontScroll = function(window, $, EventMgr, App) {
  'use strict';
  var init = function() {
    EventMgr.obind($rB(), 'mousedown', keyDownRight);
    EventMgr.obind($lB(), 'mousedown', keyDownLeft);
    EventMgr.obind($rB(), 'click', clickRight);
    EventMgr.obind($lB(), 'click', clickLeft);
    EventMgr.obind($window(), 'resize', getSizeElems);
    EventMgr.bind('loadPage', getSizeElems);
    EventMgr.bind('loadPage', start);
    EventMgr.bind('appended', checkSize);
    EventMgr.bind('switchTab', checkSize);
    EventMgr.bind('closeTab', checkSize);
  },

      delta = 10,

      cWidth = '',

      extra = 0,

      moveVar = false,

      intId = null,

      intMLId = null,

      $window = function() {
        return $(window);
      },

      $obj = function() {
        return $('#tab-cont');
      },

      $rB = function() {
        return $('#tab-mover-left');
      },

      $lB = function() {
        return $('#tab-mover-right');
      },

      start = function() {
        $obj().css('margin-left', '0px');
      },

      getSizeElems = function() {
        var offsetWidth = document.body.offsetWidth;
        cWidth = offsetWidth - 205 - 108 - 50;
      },

      keyDownRight = function(e) {
        e = e || window.event;
        clearInterval(intMLId);
        document.onmouseup = cancelMove;
        intId = setInterval(function() { move(10); }, 10);
        if (e.preventDefault) { e.preventDefault(); }
      },

      keyDownLeft = function(e) {
        e = e || window.event;
        clearInterval(intMLId);
        document.onmouseup = cancelMove;
        intId = setInterval(function() { move(-10); }, 10);
        if (e.preventDefault) { e.preventDefault(); }
      },

      clickRight = function(e) {
        e = e || window.event;
        move(delta);
        if (e.preventDefault) { e.preventDefault(); }
      },

      clickLeft = function(e) {
        e = e || window.event;
        move(delta);
        if (e.preventDefault) { e.preventDefault(); }
      },

      checkSize = function(e, data) {
        var tabId = data.tabId;
        setTimeout(function() { checkSizeFunc(tabId); tabId = null; }, 50);
      },

      checkSizeFunc = function(tabId) {
        getSizeElems();
        var objWidth = $obj().width(),
            $body = $('body');

        extra = objWidth - cWidth;

        if (objWidth > cWidth) {
          $body.addClass('tabscrollactive');
          focusActiveTab(tabId);
        } else {
          $body.removeClass('tabscrollactive');
          $obj().css('margin-left', '0');
        }
      },

      focusActiveTab = function(tabId) {
        clearInterval(intMLId);
        if (tabId === undefined) {return;}
        var activeTab = App.Dom.byId(tabId);
        if (activeTab === null) { return; }
        var width = activeTab.offsetWidth,
            left = activeTab.offsetLeft,
            allWidth = width + left,
            moveLen;
        if (cWidth < allWidth) {
          moveLen = allWidth - cWidth - parseInt($obj().css('margin-left'), 10);
          intMLId = setInterval(function() { moveLeft(moveLen); }, 10);
        } else if (left < 25) {
          moveLen = - (parseInt($obj().css('margin-left'), 10) - left + 25);
          //25 is left mover + padding
          intMLId = setInterval(function() { moveRight(moveLen); }, 10);
        }
        activeTab = null;
      },

      moveLeft = function(moveLen) {
        var curMargin = - parseInt($obj().css('margin-left'), 10);

        if (curMargin > moveLen) {
          clearInterval(intMLId);
        } else {
          $obj().css('margin-left', (- curMargin - 10) + 'px');
        }
      },

      moveRight = function(moveLen) {
        var curMargin = - parseInt($obj().css('margin-left'), 10);

        if ((curMargin < moveLen) || - curMargin > 0) {
          clearInterval(intMLId);
        } else {
          $obj().css('margin-left', (- curMargin + 10) + 'px');
        }
      },

      move = function(delta) {
        if (extra) {
          var curMar = parseInt($obj().css('margin-left'), 10),
              futMar = curMar + delta;
          if (futMar < 0) {
            if (((extra + futMar) > 0)) {
              $obj().css('margin-left', futMar + 'px');
            } else {
              $obj().css('margin-left', - extra + 'px');
            }
            if (moveVar) { clearInterval(intId); }

          } else {
            $obj().css('margin-left', '0px');
            if (moveVar) {
              clearInterval(intId);
            }
          }
        }
      },

      cancelMove = function() {
        moveVar = false;
        clearInterval(intId);
      };

  return {
    init: init,
    checkSizeFunc: checkSizeFunc,
    move: move
  };
}(window, $, EventMgr, App);