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

/**
 * App.Listsorting
 * sorting table list
 * @param {object} window global object
 * @param {function} $ jQuery
 * @param {object} EventMgr EventManager
 * @param {object} App EventManager
 *
 */
App.ListSorting = function(window, $, EventMgr, App) {
  'use strict';
  var init = function() {
    EventMgr.on('#main-wrapper', '.td_sort .ovf', 'click', sortRows);
    EventMgr.on('#main-wrapper', '.td_client_sort .ovf', 'click',
        sortRowsOnClient);
  },
      sortCol = function(a, b) {
        if (b.number === a.number) { return 0; }
        if (b.number < a.number) { return 1; } else { return -1; }
      },

      sortA = function(a, b) {
        if (b.data === a.data) { return 0; }
        if (b.data < a.data) { return 1; } else { return -1; }
      },

      sortD = function(a, b) {
        if (!a.data) { return -1; } else if (!b.data) { return 1; }
        a = a.data.replace(/[A-Za-z!,@,#,$,%,^,&,*,?,_,~"'/=<>|]+\-/g, '')
            .replace(/[A-Za-z!,@,#,$,%,^,&,*,?,_,~"'/=<>|]/g, '')
            .replace(/[-]+\s/g, '').replace(/-{2,}/, '-');
        b = b.data.replace(/[A-Za-z!,@,#,$,%,^,&,*,?,_,~"'/=<>|]+\-/g, '')
            .replace(/[A-Za-z!,@,#,$,%,^,&,*,?,_,~"'/=<>|]/g, '')
            .replace(/[-]+\s/g, '').replace(/-{2,}/, '-');
        return parseFloat(a) - parseFloat(b);
      },

      sortT = function(a, b) {
        if (!a.data) { return -1; } else if (!b.data) { return 1; }
        a = Date.parse(a.data);
        b = Date.parse(b.data);
        return a.getTime() - b.getTime();
      },

      sortRowsOnClient = function(e) {
        //for selected text
        if (typeof window.getSelection !== 'undefined') {
          var selection = window.getSelection();
          if (selection.toString() !== '') {
            return;
          }
        }
        var sortType = this.getAttribute('data-sorttype'),
            porder = this.getAttribute('data-order'),
            coln = this.getAttribute('data-coln'),
            parent = $(this).parents('.block-table'),
            tableId = parent.attr('id'),
            table = document.getElementById('lt-' + tableId);
        if (table === null) { return; }
        var body = table.tBodies[0],
            rows = body.rows,
            len = rows.length,
            orig, $col, $colContent,
            mRows = [],
            o = {},
            i, j, $row, tableHead, HeadList, hLRow,
            attrs, l, $hRow, hRow, startI = 0, cLen = 0;
        if (!porder || porder === 'desc') {
          porder = true;
        } else {
          porder = false;
        }

        for (i = startI; i < len; i++) {
          o = {};
          o.row = [];
          for (j = 0; j < rows[i].cells.length; j++) {
            o.row.push(rows[i].cells[j].innerHTML);
          }
          $row = $(rows[i]);
          //copy and remove all attributes
          attrs = rows[i].attributes;
          l = attrs.length;
          o.attrs = [];
          while (l--) {
            if (attrs[l].nodeName !== 'id') {
              o.attrs.push({
                name: attrs[l].nodeName,
                value: attrs[l].nodeValue
              });
              rows[i].removeAttribute(attrs[l].nodeName);
            }
          }
          $col = $row.find('.data-wrapper')[coln];
          if ($col) {
            //check for orig value
            orig = null;
            $colContent = $($col).find('.b-list__table-col-content');
            if ($colContent.length) {
              orig = $colContent[0].getAttribute('data-orig');
            }
            if (orig) {
              o.data = orig;
            } else {
              o.data = $colContent.length ? $colContent.html() : $col.innerHTML;
            }
          }
          mRows.push(o);
        }

        if (sortType === 'alpha') { mRows.sort(sortA); }
        else if (sortType === 'digit') { mRows.sort(sortD); }
        else if (sortType === 'time') { mRows.sort(sortT); }

        if (!porder) {
          mRows.reverse();
        }

        for (i = startI; i < len; i++) {
          cLen = rows[i].cells.length;
          for (j = 0; j < cLen; j++) {
            App.u.replaceHtml(rows[i].cells[j], mRows[i].row[j]);
          }
          //copy all attributes
          if (mRows[i].attrs) {
            attrs = mRows[i].attrs;
            l = attrs.length;
            while (l--) {
              rows[i].setAttribute(attrs[l].name, attrs[l].value);
            }
          }
          //even class add
          if (i % 2 !== 0) {
            if (!rows[i].className.match(/even/)) {
              rows[i].className += ' even';
            }
          } else {
            rows[i].className = rows[i].className.replace(/even/, '');
          }

        }

        tableHead = document.getElementById('lt-' + tableId);
        if (tableHead === null) { return; }
        hRow = tableHead.tHead.rows[0].cells[coln];
        $hRow = $(hRow);
        $('#lt-' + tableId + ' .triangle')
            .removeClass('triangl-bot')
            .removeClass('triangl-top');

        if (!porder) {
          $hRow.find('.triangle').addClass('triangl-top');
          $hRow.find('.ovf').attr('data-order', 'desc');
        } else {
          $hRow.find('.triangle').addClass('triangl-bot');
          $hRow.find('.ovf').attr('data-order', 'asc');
        }
      },

      serverSorting = function(elem, block, ctrl) {
        var csorting = elem.getAttribute('sorting'),
            ccolname = elem.getAttribute('data-colname'),
            params = {},
            dashboard = '', pNum, func, tabId, id,
            cols = [], sorting, colname, pCurrent = '';
        //table in dashboard and not
        if (!block) {
          tabId = $(elem).parents('.content').attr('data-tabid');
          var tabCont = $('#cont-' + tabId);
          pNum = tabCont.attr('data-pnum');
        } else {
          var parent = $(elem).parents('.block-table'),
              name = parent.attr('data-block-name');
          id = parent.attr('id');
          tabId = id;
          dashboard = '&dashboard=' + name;
          pNum = parent.attr('data-block-pnum');
          params.dashboard = name;
          func = parent.attr('data-block-func');
        }
        //if add field to sorting, build string soting
        if (ctrl) {
          $('#sort_table-' + tabId).find('.td_sort .ovf').each(function() {
            sorting = this.getAttribute('sorting');
            colname = this.getAttribute('data-colname');
            if (sorting) {
              cols.push({
                name: this.getAttribute('data-colname'),
                value: sorting,
                number: String(sorting).charAt(1)
              });
            }
          });
          cols.sort(sortCol);
          //make string
          for (var i = 0, l = cols.length; i < l; i++) {
            if (i !== 0) {
              pCurrent += ',';
            }
            pCurrent += cols[i].value.charAt(0) + cols[i].name;
          }
        } else {
          //add sorting if exist
          if (csorting) {
            ccolname = csorting.charAt(0) + ccolname;
          }
        }

        /* jslint camelcase: false */
        //format: '+/-name,+/-name';
        params.p_current = pCurrent;
        params.p_num = pNum;
        //format: '+/-name';
        params.p_col = ccolname;
        /* jslint camelcase: true */
        if (!block) {
          EventMgr.trigger('reloadTab', { tabId: tabId, param: params });
        } else {
          params.func = func;
          EventMgr.trigger('ajaxRequest', {
            param: params,
            invar: { blockId: id },
            type: 'get',
            outtype: 'json',
            trfunc: 'ajaxResponseForDashboard',
            failfunc: 'failedAjaxResponseForDashboard',
            queue: 'noqueue' });
        }
      },

      sortRows = function(e, data) {
        //for selected text
        if (typeof window.getSelection !== 'undefined') {
          var selection = window.getSelection();
          if (selection.toString() !== '') {
            return;
          }
        }
        var block = false;
        if ($(this).parents('.block-table').length > 0) {
          block = true;
        }

        serverSorting(this, block, (e.ctrlKey || e.metaKey));

        blockEvent(e);
        e.preventDefault();
      };

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