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.HorizScrollControl.js

/**
 *  Модуль.
 *  ваш К.О.
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 *  @param {object} App Application
 *  @return {object} API
 */
App.HorizScrollControl = function(window, $, EventMgr, App) {
  'use strict';

  var CACHE = {};

  function bindHorizScrollControll(e, data) {
    var $leftBtn = $(data.leftBtn),
        $rightBtn = $(data.rightBtn),
        $innerBox = $(data.innerBox),
        $actElem = $(data.actElem),
        id = data.id,
        step = data.step;

    var sc = new ScrollControl({
      leftBtn: $leftBtn,
      rightBtn: $rightBtn,
      innerBox: $innerBox,
      actElem: $actElem,
      step: step
    });
    sc.init();
    CACHE[id] = sc;
  }

  function updateHorizScrollControll(e, data) {
    var id = data.id;
    if (CACHE[id] && CACHE[id].update) {
      CACHE[id].update();
    }
  }

  var ScrollControl = function(options) {
    this.leftBtn = options.leftBtn;
    this.rightBtn = options.rightBtn;
    this.innerBox = options.innerBox;
    this.outterBox = this.innerBox.parent();
    this.actElem = options.actElem;
    this.step = options.step || 150;
  };

  ScrollControl.fn = ScrollControl.prototype;

  ScrollControl.fn.init = function() {
    this.leftBtn.addClass('b-hsc__btn-left');
    this.rightBtn.addClass('b-hsc__btn-right');
    this.leftBtn.bind('click', $.proxy(this.moveLeft, this));
    this.rightBtn.bind('click', $.proxy(this.moveRight, this));
    $(window).bind('resize', $.proxy(this.update, this));
    this.getWidth();
    this.moveToActElem();
    this.checkPosition();
  };

  ScrollControl.fn.update = function() {
    var self = this;
    clearTimeout(this.updTimeout);
    this.updTimeout = setTimeout(function() {
      self.getWidth();
      self.moveToActElem();
      self.checkPosition();
    }, 300);
  };

  ScrollControl.fn.moveRight = function(e) {
    e.preventDefault();
    var curLeft = parseFloat(this.innerBox.css('marginLeft'));
    if (isNaN(curLeft)) { curLeft = 0; }
    curLeft -= this.step;
    if (curLeft < this.maxLeft) {
      curLeft = this.maxLeft;
    }
    var self = this;
    this.innerBox.animate({
      marginLeft: curLeft + 'px'
    }, 150, function() {
      self.checkPosition();
    });
  };

  ScrollControl.fn.moveLeft = function(e) {
    e.preventDefault();
    var curLeft = parseFloat(this.innerBox.css('marginLeft'));
    if (isNaN(curLeft)) { curLeft = 0; }
    curLeft += this.step;
    if (curLeft > 0) {
      curLeft = 0;
    }
    var self = this;
    this.innerBox.animate({
      marginLeft: curLeft + 'px'
    }, 150, function() {
      self.checkPosition();
    });
  };

  ScrollControl.fn.getWidth = function() {
    this.outterBoxWidth = this.outterBox.width();
    this.innerBoxWidth = this.innerBox.width();
    this.maxLeft = this.outterBoxWidth - this.innerBoxWidth;
    this.overWidth = (this.maxLeft < 0);
    this.step = Math.ceil(this.innerBoxWidth / (Math.floor(this.innerBoxWidth / this.step)));
    if (isNaN(this.step)) {
      this.step = 150;
    }
  };

  ScrollControl.fn.moveToActElem = function() {
    if (this.actElem.length > 0 && this.overWidth) {
      this.actElemWidth = this.actElem.width();
      var offsetLeft = this.actElem.position().left,
          rightPoint = this.actElemWidth + offsetLeft,
          overLeft = this.outterBoxWidth - rightPoint;
      if (overLeft < 0) {
        var mLeft = parseInt(this.innerBox.css('marginLeft'), 10) + overLeft;
        mLeft = Math.ceil(mLeft / this.step) * this.step - this.step;
        if (mLeft < this.maxLeft) {
          mLeft = this.maxLeft;
        }
        this.innerBox.css('margin-left', mLeft + 'px');
      }
    }
  };

  ScrollControl.fn.checkPosition = function() {
    this.outterBox.removeClass('b-hsc_overwidth_right b-hsc_overwidth_left' +
        ' b-hsc_overwidth_full');
    if (this.outterBoxWidth < this.innerBoxWidth) {
      //need to buttons
      var marginLeft = parseInt(this.innerBox.css('marginLeft'), 10);
      if (marginLeft === 0) {
        //show right btn
        this.outterBox.addClass('b-hsc_overwidth_right');
      } else if (marginLeft === this.maxLeft) {
        //show left btn
        this.outterBox.addClass('b-hsc_overwidth_left');
      } else {
        //show both btn
        this.outterBox.addClass('b-hsc_overwidth_full');
      }
    }
  };

  function clearCache(e, data) {
    var tabId = data.tabId;
    if (CACHE && CACHE[tabId]) {
      delete CACHE[tabId];
      if (CACHE['filter-' + tabId]) {
        delete CACHE['filter-' + tabId];
      }
    }
  }

  function init() {
    EventMgr.bind('bindHorizScrollControl', bindHorizScrollControll);
    EventMgr.bind('updateHorizScrollControl', updateHorizScrollControll);
    EventMgr.bind('closeTabEvent', clearCache);
  }

  var api = {
    init: init
  };

  return api;

} (window, $, EventMgr, App);