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

/**
 * Module with utilits
 *  @param {object} window  global object
 *  @param {function} $ jQuery library
 *  @param {object} EventMgr EventMgr library
 */
/*global App:true*/
App.u = App.Common = function(window, $, EventMgr) {
  'use strict';
  var init = function() {
    EventMgr.on($(window.document), linkSelector,
        'keydown', linkKeyDownHandler);
  },

      ENTERKEY = 13,

      SPACE = 32,

      linkSelector = 'a',

      getQueryString = function(queryObj, noesc) {
        var j = 0,
            paramString = '',
            curValue,
            keyVar;
        for (keyVar in queryObj) {
          if (keyVar !== '') {
            if (j !== 0) {
              paramString += '&';
            }
            curValue = queryObj[keyVar];
            //encode params
            if (noesc) {
              paramString += keyVar + '=' + curValue;
            } else {
              paramString += keyVar + '=' + encodeURIComponent(curValue);
            }
            j++;
          }
        }
        return paramString;
      },
      //parse url to object
      //@todo unit test
      parseParams = function(paramString) {
        if (paramString !== undefined && paramString !== null) {
          paramString = paramString.replace(/^\?/, '');
          var paramStirngArray = paramString.split('&'),
              paramStirngObj = {},
              re = /=(.+)?/,
              tmp;
          for (var i = 0; i < paramStirngArray.length; i++) {
            tmp = paramStirngArray[i].split(re);
            paramStirngObj[tmp[0]] = (tmp[1]) ? tmp[1] : '';
          }
          return paramStirngObj;
        } else {
          return {};
        }
      },
      //@todo unit test
      serializeForAttr = function(obj) {
        var keyVar, paramStr = '', i = 0;
        /* jslint forin:true */
        for (keyVar in obj) {
          i++;
          if (i !== 1) {
            paramStr += '&';
          }
          paramStr += keyVar + '=' + encodeURIComponent(obj[keyVar]);
        }
        return paramStr;
      },

      //from here
      // http://www.jonefox.com/blog/2009/05/21/internet-explorer-and-the-innerhtml-property/
      replaceHtml = function(el, html) {
        if (el) {
          var oldEl = (typeof el === 'string' ?
                  document.getElementById(el) : el),
              newEl = document.createElement(oldEl.nodeName);
          //Preserve any properties we care about (id and class in this example)
          newEl.id = oldEl.id;
          newEl.className = oldEl.className;
          //set the new HTML and insert back into the DOM
          newEl.innerHTML = html;
          if (oldEl.parentNode) {
            oldEl.parentNode.replaceChild(newEl, oldEl);
          } else {
            oldEl.innerHTML = html;
          }
          //return a reference to the new element in case we need it
          oldEl = null;
          return newEl;
        }
      },

      linkKeyDownHandler = function(e) {
        var codeKey = e.which || e.keyCode;
        if (ENTERKEY === codeKey || SPACE === codeKey) {
          $(this).trigger('click');
          e.preventDefault();
        }
      },

      wordWrap = function(str, wordLen) {
        if (!str) {return str;}
        wordLen = wordLen || 100;
        var wArr = str.split(' '),
            length = wArr.length,
            cWord, cWordLen, newWord,
            wordLenReg = new RegExp('(.{' + wordLen + '})', 'g');
        for (var i = 0; i < length; i++) {
          if (wArr[i].length > wordLen) {
            cWord = wArr[i].split(wordLenReg);
            cWordLen = cWord.length;
            newWord = '';
            for (var j = 0; j < cWordLen; j++) {
              if (cWord[j] !== '') {
                newWord += cWord[j] + '<br>';
              }
            }
            str = str.replace(wArr[i], newWord);
            //replace \n to <br/>
            str = str.replace(/\n/g, '<br/>');
          }
        }
        return str;
      },

      injectCss = function(url) {
        var link = document.createElement('link');
        link.setAttribute('rel', 'stylesheet');
        link.setAttribute('type', 'text/css');
        link.setAttribute('href', url);
        document.getElementsByTagName('head')[0].appendChild(link);
      },

      required = function(file, callback, param) {
        var script = document.getElementsByTagName('script')[0],
            newjs = document.createElement('script');

        newjs.onreadystatechange = function() {
          if (newjs.readyState === 'loaded' ||
              newjs.readyState === 'complete') {
            newjs.onreadystatechange = null;
            if (App.u.isFunction(callback)) {
                callback(param);
                param = null;
                file = null;
            }
          }
        };

        newjs.onload = function() {
          if (App.u.isFunction(callback)) {
              callback(param);
              param = null;
              file = null;
          }
        };

        newjs.src = file;
        script.parentNode.insertBefore(newjs, script);
      },

      noMoreId = {},

      noMoreCheckTimeId,

      noMoreThan = function(id, timeOut, func) {
        var time = new Date().getTime(),
            dTime;
            timeOut = timeOut ? timeOut : 100;
        if (!noMoreId[id]) {
          noMoreId[id] = {
            q: false,
            lastRun: time,
            timeOut: timeOut
          };
          runFunc(func, time, id);
        } else {
          dTime = time - noMoreId[id].lastRun;
          //check for timeout
          if (dTime > timeOut) {
            runFunc(func, time, id);
            clearTimeout(noMoreCheckTimeId);
          //to queue
          } else {
            noMoreId[id].q = true;
            clearTimeout(noMoreCheckTimeId);
            //todo closure
            noMoreCheckTimeId = setTimeout(function() {
              checkLastCall(func, time, id);
            }, timeOut * 2);
          }
        }
      },

      checkLastCall = function(func, time, id) {
        if (noMoreId[id].q === true) {
          runFunc(func, time, id);
        }
      },

      runFunc = function(func, time, id) {
        if (typeof func === 'function') {
          func();
          noMoreId[id].lastRun = time;
          noMoreId[id].q = false;
        }
      },

      clone = function(obj) {
        // Handle the 3 simple types, and null or undefined
        if (null === obj || 'object' !== typeof obj) { return obj; }
        var copy;
        // Handle Date
        if (obj instanceof Date) {
          copy = new Date();
          copy.setTime(obj.getTime());
          return copy;
        }

        // Handle Array
        if (obj instanceof Array) {
          copy = [];
          for (var i = 0, len = obj.length; i < len; i++) {
            copy[i] = clone(obj[i]);
          }
          return copy;
        }

        // Handle Object
        if (obj instanceof Object) {
          copy = {};
          for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) { copy[attr] = clone(obj[attr]); }
          }
          return copy;
        }

        throw new Error("Unable to copy obj! Its type isn't supported.");
      },
      msg = {},

      storage = { breadcrumb: {}, dashboard: {}},
      /**
       * Cache jQuery Selector for static elems
       */
      selectorCache = function() {
        var selectors = {};
        return function(selector) {
          if (selectors[selector]) {
            return selectors.selector;
          }
          selectors.selector = $(selector);
          return selectors.selector;
        };
      }(),
      //check for string
      isString = function(string) {
        return (typeof string === 'string');
      },
      //check for function
      isFunction = function(func) {
        return (typeof func === 'function');
      },
      //check for type Number
      isNumber = function(num) {
        return (typeof num === 'number');
      },
      //escape regexp expression
      escapeRegExp = function(str) {
        return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
      },
      //escape qoute & double quote
      escapeQuote = function(str) {
        return String(str).replace(/"/g, '&quot;');
      },

      getCaretPosition = function(obj) {
        obj.focus();
        if (obj.selectionStart) {
          return obj.selectionStart;
        } else if (document.selection) {
          var sel = document.selection.createRange();
          var clone = sel.duplicate();
          sel.collapse(true);
          clone.moveToElementText(obj);
          clone.setEndPoint('EndToEnd', sel);
          return clone.text.length;
        }
        return 0;
      },

      CSSEscape = function(value) {
        var string = String(value);
        var length = string.length;
        var index = -1;
        var codeUnit;
        var result = '';
        var firstCodeUnit = string.charCodeAt(0);
        while (++index < length) {
        codeUnit = string.charCodeAt(index);
        // Note: there’s no need to special-case astral symbols, surrogate
        // pairs, or lone surrogates.
        // If the character is NULL (U+0000), then throw an
        // `InvalidCharacterError` exception and terminate these steps.
        if (codeUnit == 0x0000) {
          throw new InvalidCharacterError(
            'Invalid character: the input contains U+0000.'
          );
        }
        if (
          // If the character is in the range [\1-\1F] (U+0001 to U+001F) or is
          // U+007F, […]
          (codeUnit >= 0x0001 && codeUnit <= 0x001F) || codeUnit == 0x007F ||
          // If the character is the first character and is in the range [0-9]
          // (U+0030 to U+0039), […]
          (index == 0 && codeUnit >= 0x0030 && codeUnit <= 0x0039) ||
          // If the character is the second character and is in the range [0-9]
          // (U+0030 to U+0039) and the first character is a `-` (U+002D), […]
          (
            index == 1 &&
            codeUnit >= 0x0030 && codeUnit <= 0x0039 &&
            firstCodeUnit == 0x002D
          )
        ) {
          // http://dev.w3.org/csswg/cssom/#escape-a-character-as-code-point
          result += '\\' + codeUnit.toString(16) + ' ';
          continue;
        }
        // If the character is not handled by one of the above rules and is
        // greater than or equal to U+0080, is `-` (U+002D) or `_` (U+005F), or
        // is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to
        // U+005A), or [a-z] (U+0061 to U+007A), […]
        if (
          codeUnit >= 0x0080 ||
          codeUnit == 0x002D ||
          codeUnit == 0x005F ||
          codeUnit >= 0x0030 && codeUnit <= 0x0039 ||
          codeUnit >= 0x0041 && codeUnit <= 0x005A ||
          codeUnit >= 0x0061 && codeUnit <= 0x007A
        ) {
          // the character itself
          result += string.charAt(index);
          continue;
        }
        // Otherwise, the escaped character.
        // http://dev.w3.org/csswg/cssom/#escape-a-character
        result += '\\' + string.charAt(index);
        }
        return result;
      },

      insertStringAfterCaret = function(elem, textToInsert, removeLine) {
        if (!elem || !textToInsert) {
          return;
        }
        elem.focus();
        var val = elem.value,
            end = (val.length === 0) ? 0 : getCaretPosition(elem),
            start = removeLine ? val.lastIndexOf('\n', end - 1) + 1 : end,
            before = val.substring(0, start),
            after = val.substring(end);
        elem.value = before + textToInsert + after;
        var r, index = (before + textToInsert).length;
        if (elem.setSelectionRange) {
          elem.setSelectionRange(index, index);
        } else {
          r = elem.createTextRange();
          r.collapse(true);
          r.select(index, index);
        }
      },

      formatDateTime = function(timestamp) {
        var date = new Date(timestamp);
        return date.getFullYear() + '-' +
          ('0' + (date.getMonth() + 1)).slice(-2) + '-' +
          ('0' + date.getDate()).slice(-2) + ' ' +
          ('0' + date.getHours()).slice(-2) + ':' +
          ('0' + date.getMinutes()).slice(-2) + ':' +
          ('0' + date.getSeconds()).slice(-2);
      },

      parseDate = function(timeString) {
        var cArr = String(timeString).split(' ');
        if (cArr.length === 2) {
          var dArr = cArr[0].split('-'),
              tArr = cArr[1].split(':');
          if (dArr.length === 3 && tArr.length === 3) {
            return new Date(dArr[0], parseInt(dArr[1], 10) - 1, dArr[2], tArr[0], tArr[1], tArr[2]);
          }
        }
        return new Date();
      },
      //check file size function
      checkFileSize = function(tabId, name) {
        var inputFile = name ? $('#' + name + '-' + tabId) : $('#form-wrapper-' + tabId + ' input[type="file"]'),
            msg = null,
            field = null;
        inputFile.each(function() {
           var maxSize = this.getAttribute('data-max-size'),
               msgSize = this.getAttribute('data-maxsize-msg');
           field = this;
           if (this.files && maxSize) {
             maxSize -= 0;
             for (var i = 0, l = this.files.length; i < l; i++) {
               if (this.files[i].size > maxSize) {
                 msg = String(pageInfo.messages.fileMaxSize).replace('__VALUE__', msgSize);
                 return false;
               }
             }
           }
        });
        return {
          msg: msg,
          field: field
        };
      },

      openInNewWindow = function(url, param) {
        var newwindow = window.open('', param);
        if (newwindow) {
          newwindow.openner = null;
          newwindow.location = url;
        }
      },

      removeParam = function(form, param) {
        var len = form.length,
            i, name;
        for (i = 0; i < len; i++) {
          name = form[i].name;
          delete param[name];
        }
        return param;
      }, 
      /**
       *  Устанавливаем позиционирование выпадающего списка на форме
       */ 
      setOptionListPosition = function(prefixId, id) {
        var elemHeight, elem, bodyHeight, elemTop, formTop, form, tabId;
        bodyHeight = window.document.body.offsetHeight;
        //calculate position for list options
        elem = $(prefixId + id);
        elem[0].style.top = '';
        elemTop = elem.offset().top;
        elemHeight = elem[0].offsetHeight;
        if ((bodyHeight - elemTop) < elemHeight) {
          tabId = elem[0].getAttribute('data-tabid');
          form = $('#form-scroll-' + tabId);
          if (form.length === 0) {
            //if it filter
            form = $('#tcw-' + tabId);
          }
          formTop = form.offset().top;
          if (formTop > (elemTop - elemHeight - 19)) {
            elem[0].style.top = '-' + (elemHeight / 2) + 'px';
          } else {
            elem[0].style.top = '-' + elemHeight + 'px';
          }
        }
      },


      /**
       * Получить предыдущую html ноду
       * @param {HTMLElement} node - html node
       * @return {null | HTMLElement}
       */
      getPreviousNode = function(node) {
        var el = node;
        var previousSibling = null;
        if (el) {
          while (!previousSibling && el) {
            el = el.previousSibling;
            if (el.nodeType === 1) {
              previousSibling = el;
            }
          }
        }

        return previousSibling;
      },

      /**
       * Check on refresh menu flag in response
       */
      checkRefreshMenu = function(data) {
        if (data.refreshMenu) {
          EventMgr.trigger('favoriteMenuUpdateDone');
        }
     }
      ;

  return {
    init: init,
    setOptionListPosition: setOptionListPosition,
    parseParams: parseParams,
    replaceHtml: replaceHtml,
    serializeForAttr: serializeForAttr,
    wordWrap: wordWrap,
    required: required,
    noMoreThan: noMoreThan,
    checkRefreshMenu: checkRefreshMenu,
    clone: clone,
    injectCss: injectCss,
    msg: msg,
    removeParam: removeParam,
    storage: storage,
    selectorCache: selectorCache,
    isNumber: isNumber,
    isString: isString,
    isFunction: isFunction,
    getQueryString: getQueryString,
    escapeRegExp: escapeRegExp,
    insertStringAfterCaret: insertStringAfterCaret,
    getCaretPosition: getCaretPosition,
    escapeQuote: escapeQuote,
    formatDateTime: formatDateTime,
    parseDate: parseDate,
    checkFileSize: checkFileSize,
    openInNewWindow: openInNewWindow,
    CSSEscape: CSSEscape,
    getPreviousNode: getPreviousNode
  };
}(window, $, EventMgr);