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

/**
 * Progressbar module
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 *  @param {object} App Application
 *  @param {object} templates Templates of App
 */
App.ProgressBar = function(window, $, EventMgr, App, templates) {
  'use strict';

  var CACHE = {};
  // закончившиеся wait прогрессбары, на случай когда прогрессбар пришел раньше респонса формы
  var ENDED_WAIT_PROGRESSBAR = {};
  /**
   * Remove ProgressBar
   * @param {string} tabId
   */
  function removeProgressBar(tabId) {
    var tabCont = App.Dom.byId('cont-' + tabId);
    App.Dom.removeClass(tabCont, 'tab-content__progressbar_show');
    var progressbar = App.Dom.byId('cont-' + tabId + '-progressbar');
    if (progressbar === null) { return; }
    progressbar.style.display = 'none';
    progressbar.innerHTML = '';
    //show default loader
    var defProgress = App.Dom.byId('cont-' + tabId + '-progressbar-default');
    if (defProgress === null) { return false; }
    if (defProgress.style.display === 'none') {
      defProgress.style.display = null;
    }
    progressbar = null;
  }

  /**
   * Wait progressbar response after panel restart
   * @param {object} e
   * @param {object} data
   */
  function waitProgressBar(e, data) {
    if (data.progresstype === 'wait' || (data.invar && data.invar.progresstype === 'wait')) {
      setTimeout(function() {
        EventMgr.trigger('ajaxRequest', {
          param: data.param,
          invar: data.invar,
          type: 'get',
          outtype: 'json',
          trfunc: 'progressBarResponse',
          failfunc: 'progressBarResponseFail',
          queue: 'noqueue' });
      }, 1500);
    }
  }

  /**
   * Render Progressbar HTML
   * @param {object} e
   * @param {object} data
   */
  function renderProgressBar(e, data) {
    var start = data.start,
        progressType = data.progresstype,
        tabId = '', tabExist = false,
        progressId = data.param.elid;
    if (start !== '' && start !== undefined) {
      var done = data.done,
          now = data.now,
          steps = data.steps,
          comment = data.comment,
          param = data.param,
          colorClass = '';
      tabId = data.tabId;
      var per = 0,
          left = '';
      if (done !== '' && steps !== '') {
        per = Math.round((done / steps) * 100);
      }
      if (done !== '' && steps !== '' && progressType !== 'notime') {
        var elapsedTime = (now - start),
            elapsedSteps = steps - done;
        if (elapsedSteps !== 0 && done !== 0) {
          left = Math.round((elapsedTime / done) * elapsedSteps) + 'sec';
        }
      }
      var width = per * (0.98);
      if (per < 6) {
        per = '.';
        colorClass = 'blue-color';
      } else {
        if (per > 100) {
          per = 100;
          width = per * (0.98);
        }
        per += '%';
      }
      var html = templates.progressBar({
        tabId: tabId,
        per: per,
        width: width,
        left: '',
        comment: comment,
        colorClass: colorClass });
      tabExist = appendProgressBar(tabId, progressId, html);
      //if exist tab
      if (tabExist) {
        setTimeout(function() {
          EventMgr.trigger('ajaxRequest', {
            param: param,
            invar: { tabId: tabId, param: param, progresstype: progressType },
            type: 'get',
            outtype: 'json',
            trfunc: 'progressBarResponse',
            failfunc: 'progressBarResponseFail',
            queue: 'noqueue' });
          param = null;tabId = null;progressType = null;
        }, 1000);
      }

    } else {
      tabId = data.tabId;
      removeProgressBar(tabId);
      //@todo remove cache
    }
    if (data.ok && progressType === 'wait') {
      if (data.param && data.param.elid) {
        ENDED_WAIT_PROGRESSBAR[data.param.elid] = true;
        if (CACHE[data.param.elid]) {
          CACHE[data.param.elid].progresstype = null;
          if (CACHE[data.param.elid].progressok) {
            CACHE[data.param.elid].__dataSource.rp = ['progressok'];
            CACHE[data.param.elid].__dataSource.rp = ['progressok'];
            EventMgr.trigger('tabLoading', { tabId: data.tabId });
            EventMgr.trigger('ajaxRequest', CACHE[data.param.elid].__dataSource);
          } else {
            EventMgr.trigger('ajaxFormResponse', CACHE[data.param.elid]);
          }
          delete CACHE[data.param.elid];
        }
      }
    }
  }

  /**
   * Append Progressbar to form
   * @param {string} tabId
   * @param {string} html
   * @return {boolean}
   */
  function appendProgressBar(tabId, progressId,  html) {
    var progressChecker = App.Dom.byId(progressId),
        tabCont = App.Dom.byId('cont-' + tabId);
    if (!progressChecker) { return false; }
    App.Dom.addClass(tabCont, 'tab-content__progressbar_show');
    var progressbar = App.Dom.byId('cont-' + tabId + '-progressbar');
    if (progressbar === null) { return false; }
    progressbar.style.display = 'block';
    progressbar.innerHTML = html;
    progressbar = null;
    var defProgress = App.Dom.byId('cont-' + tabId + '-progressbar-default');
    if (defProgress === null) { return false; }
    defProgress.style.display = 'none';
    return true;
  }

  function progressBarSaveState(e, data) {
    // если wait прогресс уже пришел с ok
    if (ENDED_WAIT_PROGRESSBAR[data.progressid]) {
      data.progresstype = null;
      EventMgr.trigger('ajaxFormResponse', data);
    } else if (data && data.progressid) {
      CACHE[data.progressid] = data;
    }
   
  }

  function init() {
    EventMgr.bind('progressBarSaveState', progressBarSaveState);
    EventMgr.bind('progressBarResponse', renderProgressBar);
    EventMgr.bind('progressBarResponseFail', waitProgressBar);
  }

  return {
    init: init
  };

}(window, $, EventMgr, App, templates);