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

/**
 * Update elements size
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 *  @param {object} App Application
 */
App.UpSize = function(window, $, EventMgr, App) {
  'use strict';
  var init = function() {
    EventMgr.obind($window, 'resize', getSizes);
    EventMgr.obind($window, 'resize', updateMenuHeight);
    EventMgr.obind($window, 'resize', updateHeightTabContent);
    EventMgr.obind($window, 'resize', updateTableHeight);
    EventMgr.obind($window, 'resize', updFormContent);
    EventMgr.bind('loadPage', getSizes);
    EventMgr.bind('loadPage', updateMenuHeight);
    EventMgr.bind('appended', setStyleTabContent);
    EventMgr.bind('appendList', updateTableHeight);
    EventMgr.bind('appendList', setRowsWidth);
    EventMgr.bind('setRowsWidth', setRowsWidth);
    EventMgr.bind('changedTab', updateDashTable);
    EventMgr.bind('menuPositionChanged', updateDashTable);
    EventMgr.bind('menuPositionChanged', updateTableHeightFunc);
    EventMgr.bind('updTableHeight', updateTableHeight);
    EventMgr.bind('updFormHeight', updFormContent);
    EventMgr.bind('appendForm', updFormContent);
    EventMgr.bind('appendForm, setValuesDone', updateHeightFormListBlocks);
    EventMgr.bind('verticalScroll', updateButtonBar);
  },
      // offset topBar
      top = 0,
      // height work window
      tabHeight = 0,
      //window height
      windowHeight = 0,

      //windowWidth = 0,

      getWindowHeight = function() {
        windowHeight = $window.height();
        //windowWidth = $window.width();
      },

      calcHeight = function() {
        tabHeight = windowHeight - getTopF();
      },
      //offset topBar
      getTopF = function() {
        if (top === 0 && $menu().length > 0) {
          top = $menu().offset().top;
        }
        return top;
      },

      $menu = function() {
        return App.u.selectorCache('#menu-items-wr');
      },

      $window = function() {
        return $(window);
      }(),
      //calculate height
      getSizes = function() {
        getWindowHeight();
        calcHeight();
      },

      updateMenuHeight = function() {
        $menu().height(tabHeight);
      },
      //@todo add update
      setStyleTabContent = function(e, data) {
        var tabId = data.tabId,
            tabCont = App.Dom.byId('cont-' + tabId);
        if (tabCont !== null) {
          tabCont.style.height = tabHeight + 'px';
          if (tabId === 'tab0') {
            tabCont.style.marginTop = '-' + tabHeight + 'px';
          }
        }
        tabCont = null;
      },

      updFormContent = function(e, data) {
        setTimeout(function() {
          var formHeight = windowHeight - getTopF(),
              tabId, formWrapper, topForm, availableHeight;
          if (data) {
            tabId = data.tabId;
          } else {
            var tabAct = $('.tab-content_st_active');
            if (tabAct.attr('data-tab-type') === 'form') {
              tabId = tabAct.attr('data-tabid');
            } else {
              return;
            }
          }
          formWrapper = $('#incont-' + tabId + ' .topWrapper');
          if (formWrapper.length > 0) {
            //set height for formwrapper
            $('.tab-content')
                .height(formHeight);
            var formScrollCont = $('#form-scroll-' + tabId);
            topForm = formWrapper[0].offsetHeight;
            availableHeight = tabHeight - topForm;
            var allFormHeight = formScrollCont[0].scrollHeight;
            if (tabId === 'modal1') {
              formScrollCont.height('auto');
              var innerFormHeigth = formScrollCont.height();
              formScrollCont.height(innerFormHeigth + 'px');
              if (allFormHeight < availableHeight) {
                $('#cont-modal1').height('auto');
              }
            } else {
              //set height for form inner
              formScrollCont.height(availableHeight);
              EventMgr.trigger('updateScroll', { id: 'form-scroll-' + tabId });
              //fixed or not buttons
              if (allFormHeight > availableHeight) {
                $('#incont-' + tabId + ' .i-buttons_form-type_form')
                    .addClass('l-buttons_pos_fixed');
                $('#form-scroll-in-' + tabId)
                    .addClass('l-form__inner_fly_buttons');
              } else {
                $('#incont-' + tabId + ' .i-buttons_form-type_form')
                    .removeClass('l-buttons_pos_fixed');
                $('#form-scroll-in-' + tabId)
                    .removeClass('l-form__inner_fly_buttons');
              }
            }
          }
        }, 0);
      },

      updateHeightTabContent = function() {
        $('#cont-tab0')
            .css('marginTop', '-' + tabHeight + 'px')
            .height(tabHeight);
        $('.tab-content')
          .height(tabHeight);
      },

      updateTableHeight = function() {
        // setTimeout('APP.upSize.updth()', '100');
        setTimeout(function() { updateTableHeightFunc(); }, '100');
      },

      updateTableHeightFunc = function() {
        var self,
            tabId,
            statusBarHeight,
            topTable,
            availableHeight;
        $('.tab-content_st_active .content').each(function() {
          tabId = this.getAttribute('data-tabid');
          self = $(this);
          //height table
          statusBarHeight = '27';
          // offset top table
          topTable = this.offsetTop;
          // availible height for table
          availableHeight = tabHeight - topTable - statusBarHeight;
          // new table height
          self
            .height(availableHeight);
          updateWidthTableHead(tabId);
          EventMgr.trigger('updateScroll', { id: 'ltwr-' + tabId });
        });
        EventMgr.trigger('updatedTableHeight', { tabId: tabId });
      },
      //set row width for sorting table
      setRowsWidth = function(e, data) {
        var id = data.tabId,
            table = App.Dom.byId('sort_table-' + id),
            tabElem = $('#cont-' + id),
            sBarCells;
        if (!table) { return;}

        var tableWidth = table.offsetWidth,
            cellsSort = table.rows['0'].cells,
            statusBar = App.Dom.byId('statusbar-' + id);
        if (statusBar !== null) {
          sBarCells = statusBar.rows['0'].cells;
        }
        if (tableWidth === 0) {
          return null;
        }
        var parentTable = App.Dom.byId('lt-' + id);
        if (parentTable === null) { return; }
        var cellsParent = parentTable.rows['0'].cells,
            len = cellsSort.length,
            lenTmp = len,
            width,
            per,
            autoSize = false,
            colWithoutWidth = 0,
            widthPer,
            widths = [],
            widthsPer = [],
            sumWidthPer = 0,
            sumWidth = 0,
            halfSumWidth, otherWidths = 0;

        tabElem.addClass('table-manip');
        //get all width
        while (lenTmp--) {
          widths[lenTmp] = cellsParent[lenTmp].clientWidth;
          widthPer = parseInt(cellsSort[lenTmp].getAttribute('width'), 10);
          if (isNaN(widthPer)) {
            colWithoutWidth++;
          } else {
            widthsPer[lenTmp] = widthPer;
          }
          sumWidth += widths[lenTmp];
          sumWidthPer += widthsPer[lenTmp];
        }
        if (colWithoutWidth || (sumWidthPer < 90 && sumWidthPer > 110)) {
          autoSize = true;
        }
        //check for more 50% width
        halfSumWidth = sumWidth / 2;
        lenTmp = len;
        while (lenTmp--) {
          if (widths[lenTmp] > halfSumWidth) {
            for (var i = 0; i < len; i++) {
              if (i !== lenTmp) {
                otherWidths += widths[i];
              }
            }
            widths[lenTmp] = otherWidths;
            sumWidth = otherWidths * 2;
            break;
          }
        }
        //set width in percent
        while (len--) {
          width = widths[len];
          if (autoSize) {
            per = Math.round((width / sumWidth) * 100) + '%';
          } else {
            per = widthsPer[len] + '%';
          }
          cellsSort[len].style.width = per;
          cellsParent[len].style.width = per;
          try {
            sBarCells[len].style.width = per;
          } catch (ex) {}
        }
        parentTable.style.tableLayout = 'fixed';
        table.style.tableLayout = 'fixed';
        setTimeout(function() {
          tabElem.removeClass('table-manip');
          tabElem.removeClass('init');
        }, 100);

      },

      updateWidthTableHead = function(tabId) {
        var parentTable = App.Dom.byId('lt-' + tabId),
            sortTable = App.Dom.byId('sort_table-' + tabId),
            statusTable = App.Dom.byId('statusbar-' + tabId),
            width;
        if (parentTable !== null) {
          width = parentTable.offsetWidth + 'px';
          if (sortTable !== null) {
            sortTable.style.width = width;
          }
          if (statusTable !== null) {
            statusTable.style.width = width;
          }
        }
        parentTable = null;
        sortTable = null;
        statusTable = null;
      },

      updateDashTable = function(e, data) {
        var tabId = data.tabId;
        if (tabId === 'tab0') {
          EventMgr.trigger('updateScroll', {});
        }
      },
      /**
       * Set height for all blocks in form list block
       * @param {object} e
       * @param {object} data
       */
      updateHeightFormListBlocks = function(e, data) {
        var tabId = data.tabId,
            selector = '.' + tabId + '-list .b-form-blocks__block',
            maxHeight = 0,
            curHeight,
            $elems = $(selector);
        if ($elems.length > 0) {
          $elems.each(function() {
            curHeight = this.offsetHeight;
            if (curHeight > maxHeight) {
              maxHeight = curHeight;
            }
          });
          $elems.each(function() {
            this.style.height = maxHeight + 'px';
            App.Dom.addClass(this, 'b-form-block__block_set_height');
          });
        }
      },
      /**
       * update class for buttons wrapper
       * @param {object} e
       */
      updateButtonBar = function(e) {
        var data, $btnWrapper;
        if (e.originalEvent && e.originalEvent.detail) {
          data = e.originalEvent.detail;
          //check for is it form scroll
          if (data.id && data.id.match('form-scroll')) {
            $btnWrapper = $('#' + data.id + ' .l-buttons_pos_fixed');
            if ($btnWrapper.length > 0) {
              //check for scroll state top/middle/bottom
              if (data.bottom) {
                if (!$btnWrapper.hasClass('l-buttons_pos_bottom')) {
                  $btnWrapper.addClass('l-buttons_pos_bottom');
                }
              } else {
                if ($btnWrapper.hasClass('l-buttons_pos_bottom')) {
                  $btnWrapper.removeClass('l-buttons_pos_bottom');
                }
              }
            }
          }
        }
      };
  return {
    init: init,
    updth: updateTableHeightFunc
  };
}(window, $, EventMgr, App);