Code coverage report for app/utils/helper.js

Statements: 80.41% (312 / 388)      Branches: 69.47% (157 / 226)      Functions: 82.14% (69 / 84)      Lines: 81.45% (303 / 372)      Ignored: 1 statement, 1 function     

All files » app/utils/ » helper.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122    1   1                                   1 1                   1 1131                 1 99                 1 59865                 1 1070             1 13355                 1 5                                         1 23 17   23 11   23 23 23 23 22 38 1   37 11   37   7   30 12       16                     1 11157 11157 20532                     1 3 3   3 4 4 5       3                               1 1                   1 1 1 1 1 1   1                 1 12 12 12 12                           1 138 138 84 84 84 137 137   84 16 13   138                                       1 224 224 224 224 804 804   224                                 1 182 769                                   1 37 23                               1 17 17 16   17     1 1                                             1   168       1     1       1       1                                                                   1 1824   1824 1824 1824 72   72               72     74 74 74 74 74 14 14   60   60     60   60 4   60 60       2   72 72 72   72   1752   1824                           1 7   7         7 7                           1                             1                     1 21     21                     1 12376 12376 12767 12347     12376                 1 6297 6297 6302   6297                 1 6 6 23 13     6                     1 6328 6328 6328 6328 18992   6328             1                                                                                                                                                         6010 1794 1794   4216 4216     6010   6010 6   6004       6010 5 4                           12 12 12         12 25 25 9 27   9     12                     12447 12411 11713 11713 11713 11713 2877 6334   8836 689 689 689   11713                         31 31 31 31   31 1   30   62 25   62   2 2 2   62 1 61 2 59 4   55         31         31 1   31 1   31     31     31     31       31     31     31     31     31     31       31                                         27                               5           5 12   2                         1 4       4 4 4     4                     1 1023     1023 752   752 1   752                     1 32               1 100 100       100     1 25   25 1 1   24   25     1 2     2       2                     1                                                                           1   31       29                           1   2723     1674                 1 29 29 29 29 29 29 29 292 50     29                         1 1 2 2                 1   1   1         4 4       4 4 4 2 2     2          
'use strict';
 
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
 
;require.register("utils/helper", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  var stringUtils = require('utils/string_utils');
  var timezoneUtils = require('utils/date/timezone');
 
  /**
   * Remove spaces at beginning and ending of line.
   * @example
   *  var str = "  I'm a string  "
   *  str.trim() // return "I'm a string"
   * @method trim
   * @return {string}
   */
  String.prototype.trim = function () {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  };
  /**
   * Determines whether string end within another string.
   *
   * @method endsWith
   * @param suffix {string}  substring for search
   * @return {boolean}
   */
  String.prototype.endsWith = function (suffix) {
    return this.indexOf(suffix, this.length - suffix.length) !== -1;
  };
  /**
   * Determines whether string start within another string.
   *
   * @method startsWith
   * @param prefix {string} substring for search
   * @return {boolean}
   */
  String.prototype.startsWith = function (prefix) {
    return this.indexOf(prefix) == 0;
  };
  /**
   * Determines whether string founded within another string.
   *
   * @method contains
   * @param substring {string} substring for search
   * @return {boolean}
   */
  String.prototype.contains = function (substring) {
    return this.indexOf(substring) != -1;
  };
  /**
   * Capitalize the first letter of string.
   * @method capitalize
   * @return {string}
   */
  String.prototype.capitalize = function () {
    return this.charAt(0).toUpperCase() + this.slice(1);
  };
 
  /**
   * Capitalize the first letter of string.
   * And set to lowercase other part of string
   * @method toCapital
   * @return {string}
   */
  String.prototype.toCapital = function () {
    return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
  };
 
  /**
   * Finds the value in an object where this string is a key.
   * Optionally, the index of the key can be provided where the
   * value of the nth key in the hierarchy is returned.
   *
   * Example:
   *  var tofind = 'smart';
   *  var person = {'name': 'Bob Bob', 'smart': 'no', 'age': '28', 'personality': {'smart': 'yes', 'funny': 'yes', 'emotion': 'happy'} };
   *  tofind.findIn(person); // 'no'
   *  tofind.findIn(person, 0); // 'no'
   *  tofind.findIn(person, 1); // 'yes'
   *  tofind.findIn(person, 2); // null
   *
   *  @method findIn
   *  @param multi {object}
   *  @param index {number} Occurrence count of this key
   *  @return {*} Value of key at given index
   */
  String.prototype.findIn = function (multi, index, _foundValues) {
    if (!index) {
      index = 0;
    }
    if (!_foundValues) {
      _foundValues = [];
    }
    multi = multi || '';
    var value = null;
    var str = this.valueOf();
    if ((typeof multi === 'undefined' ? 'undefined' : _typeof(multi)) == 'object') {
      for (var key in multi) {
        if (value != null) {
          break;
        }
        if (key == str) {
          _foundValues.push(multi[key]);
        }
        if (_foundValues.length - 1 == index) {
          // Found the value
          return _foundValues[index];
        }
        if (_typeof(multi[key]) == 'object') {
          value = value || this.findIn(multi[key], index, _foundValues);
        }
      }
    }
    return value;
  };
  /**
   * Replace {i} with argument. where i is number of argument to replace with.
   * @example
   *  var str = "{0} world{1}";
   *  str.format("Hello", "!") // return "Hello world!"
   *
   * @method format
   * @return {string}
   */
  String.prototype.format = function () {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function (match, number) {
      return typeof args[number] != 'undefined' ? args[number] : match;
    });
  };
  /**
   * Wrap words in string within template.
   *
   * @method highlight
   * @param {string[]} words - words to wrap
   * @param {string} [highlightTemplate="<b>{0}</b>"] - template for wrapping
   * @return {string}
   */
  String.prototype.highlight = function (words, highlightTemplate) {
    var self = this;
    highlightTemplate = highlightTemplate ? highlightTemplate : "<b>{0}</b>";
 
    words.forEach(function (word) {
      var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
      self = self.replace(searchRegExp, function (found) {
        return highlightTemplate.format(found);
      });
    });
 
    return self;
  };
  /**
   * Convert time in milliseconds to object contained days, hours and minutes.
   * @typedef ConvertedTime
   *  @type {Object}
   *  @property {number} d - days
   *  @property {number} h - hours
   *  @property {string} m - minutes
   * @example
   *  var time = 1000000000;
   *  time.toDaysHoursMinutes() // {d: 11, h: 13, m: "46.67"}
   *
   * @method toDaysHoursMinutes
   * @return {object}
   */
  Number.prototype.toDaysHoursMinutes = function () {
    var formatted = {},
        dateDiff = this,
        secK = 1000,
        //ms
    minK = 60 * secK,
        // sec
    hourK = 60 * minK,
        // sec
    dayK = 24 * hourK;
 
    dateDiff = parseInt(dateDiff);
    formatted.d = Math.floor(dateDiff / dayK);
    dateDiff -= formatted.d * dayK;
    formatted.h = Math.floor(dateDiff / hourK);
    dateDiff -= formatted.h * hourK;
    formatted.m = (dateDiff / minK).toFixed(2);
 
    return formatted;
  };
 
  /**
   *
   * @param bound1 {Number}
   * @param bound2 {Number}
   * @return {boolean}
   */
  Number.prototype.isInRange = function (bound1, bound2) {
    var upperBound, lowerBound;
    upperBound = bound1 > bound2 ? bound1 : bound2;
    lowerBound = bound1 < bound2 ? bound1 : bound2;
    return this > lowerBound && this < upperBound;
  };
 
  /**
   Sort an array by the key specified in the argument.
   Handle only native js objects as element of array, not the Ember's object.
  
   Can be used as alternative to sortProperty method of Ember library
   in order to speed up executing on large data volumes
  
   @method sortBy
   @param {String} path name(s) to sort on
   @return {Array} The sorted array.
   */
  Array.prototype.sortPropertyLight = function (path) {
    var realPath = typeof path === "string" ? path.split('.') : [];
    this.sort(function (a, b) {
      var aProperty = a;
      var bProperty = b;
      realPath.forEach(function (key) {
        aProperty = aProperty[key];
        bProperty = bProperty[key];
      });
      if (aProperty > bProperty) return 1;
      if (aProperty < bProperty) return -1;
      return 0;
    });
    return this;
  };
 
  /**
   * Create map from array with executing provided callback for each array's item
   * Example:
   * <pre>
   *   var array = [{a: 1, b: 3}, {a: 2, b: 2}, {a: 3, b: 1}];
   *   var map = array.toMapByCallback('a', function (item) {
   *    return Em.get(item, 'b');
   *   });
   *   console.log(map); // {1: 3, 2: 2, 3: 1}
   * </pre>
   * <code>map[1]</code> is much more faster than <code>array.findProperty('a', 1).get('b')</code>
   *
   * @param {string} property
   * @param {Function} callback
   * @returns {object}
   * @method toMapByCallback
   */
  Array.prototype.toMapByCallback = function (property, callback) {
    var ret = {};
    Em.assert('`property` can\'t be empty string', property.length);
    Em.assert('`callback` should be a function', 'function' === Em.typeOf(callback));
    this.forEach(function (item) {
      var key = Em.get(item, property);
      ret[key] = callback(item, property);
    });
    return ret;
  };
 
  /**
   * Create map from array
   * Example:
   * <pre>
   *   var array = [{a: 1}, {a: 2}, {a: 3}];
   *   var map = array.toMapByProperty('a'); // {1: {a: 1}, 2: {a: 2}, 3: {a: 3}}
   * </pre>
   * <code>map[1]</code> is much more faster than <code>array.findProperty('a', 1)</code>
   *
   * @param {string} property
   * @return {object}
   * @method toMapByProperty
   * @see toMapByCallback
   */
  Array.prototype.toMapByProperty = function (property) {
    return this.toMapByCallback(property, function (item) {
      return item;
    });
  };
 
  /**
   * Create wick map from array
   * Example:
   * <pre>
   *   var array = [{a: 1}, {a: 2}, {a: 3}];
   *   var map = array.toWickMapByProperty('a'); // {1: true, 2: true, 3: true}
   * </pre>
   * <code>map[1]</code> works faster than <code>array.someProperty('a', 1)</code>
   *
   * @param {string} property
   * @return {object}
   * @method toWickMapByProperty
   * @see toMapByCallback
   */
  Array.prototype.toWickMapByProperty = function (property) {
    return this.toMapByCallback(property, function () {
      return true;
    });
  };
 
  /**
   * Create wick map from array of primitives
   * Example:
   * <pre>
   *   var array = [1, 2, 3];
   *   var map = array.toWickMap(); // {1: true, 2: true, 3: true}
   * </pre>
   * <code>map[1]</code> works faster than <code>array.contains(1)</code>
   *
   * @returns {object}
   * @method toWickMap
   */
  Array.prototype.toWickMap = function () {
    var ret = {};
    this.forEach(function (item) {
      ret[item] = true;
    });
    return ret;
  };
 
  Eif (!Array.prototype.findIndex) {
    Array.prototype.findIndex = function (predicate) {
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }
      var o = Object(this),
          len = o.length >>> 0;
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }
      var thisArg = arguments[1],
          k = 0;
      while (k < len) {
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return k;
        }
        k++;
      }
      return -1;
    };
  }
 
  /** @namespace Em **/
  Em.CoreObject.reopen({
    t: function t(key, attrs) {
      return Em.I18n.t(key, attrs);
    }
  });
 
  Em.TextArea.reopen(Em.I18n.TranslateableAttributes);
 
  /** @namespace Em.Handlebars **/
  Em.Handlebars.registerHelper('log', function (variable) {
    console.log(variable);
  });
 
  Em.Handlebars.registerHelper('warn', function (variable) {
    console.warn(variable);
  });
 
  Em.Handlebars.registerHelper('highlight', function (property, words, fn) {
    var context = fn.contexts && fn.contexts[0] || this;
    property = Em.Handlebars.getPath(context, property, fn);
 
    words = words.split(";");
 
    //  if (highlightTemplate == undefined) {
    var highlightTemplate = "<b>{0}</b>";
    //  }
 
    words.forEach(function (word) {
      var searchRegExp = new RegExp("\\b" + word + "\\b", "gi");
      property = property.replace(searchRegExp, function (found) {
        return highlightTemplate.format(found);
      });
    });
 
    return new Em.Handlebars.SafeString(property);
  });
 
  /**
   * Usage:
   *
   * <div {{QAAttr "someText"}}></div>
   * <div {{QAAttr "{someProperty}"}}></div>
   * <div {{QAAttr "someText-and-{someProperty}"}}></div>
   * <div {{QAAttr "{someProperty:some-text}"}}></div>
   * <div {{QAAttr "someText-and-{someProperty:some-text}"}}></div>
   * <div {{QAAttr "{someProperty:some-text:another-text}"}}></div>
   * <div {{QAAttr "someText-and-{someProperty:some-text:another-text}"}}></div>
   * <div {{QAAttr "{someProperty::another-text}"}}></div>
   * <div {{QAAttr "someText-and-{someProperty::another-text}"}}></div>
   *
   */
  Em.Handlebars.registerHelper('QAAttr', function (text, options) {
    var _this = this;
 
    var textToReplace = text.match(/\{(.*?)\}/g);
    var attributes = void 0;
    if (textToReplace) {
      var id = ++Em.$.uuid,
          expressions = textToReplace.map(function (str) {
        var parsed = Em.View._parsePropertyPath(str.slice(1, str.length - 1)),
            normalized = Ember.Handlebars.normalizePath(_this, parsed.path, options.data),
            classNames = parsed.classNames,
            className = parsed.className,
            falsyClassName = parsed.falsyClassName,
            root = normalized.root,
            path = normalized.path;
 
        return { src: str, classNames: classNames, className: className, falsyClassName: falsyClassName, root: root, path: path };
      }),
          observer = function observer() {
        var dataQA = text;
        for (var i = expressions.length; i--;) {
          var el = Em.tryInvoke(options.data.view, '$', ['[' + attributes + ']']);
          var e = expressions[i];
          if (!el || el.length === 0) {
            Em.removeObserver(e.root, e.path, invoker);
            break;
          }
          var value = void 0,
              sourceValue = Em.Handlebars.getPath(e.root, e.path, options.data);
          Iif (e.classNames) {
            value = sourceValue ? e.className : e.falsyClassName;
          } else {
            value = sourceValue;
          }
          if (Em.isNone(value)) {
            value = '';
          }
          dataQA = dataQA.replace(e.src, value);
          el.attr('data-qa', dataQA);
        }
      },
          invoker = function invoker() {
        return Em.run.once(observer);
      };
      attributes = 'data-qa-bind-id="' + id + '"';
      expressions.forEach(function (e) {
        Em.addObserver(e.root, e.path, invoker);
      });
      Em.run.next(observer);
    } else {
      attributes = 'data-qa="' + text + '"';
    }
    return new Em.Handlebars.SafeString(attributes);
  });
 
  /**
   * Usage:
   *
   * <pre>
   *   {{#isAuthorized "SERVICE.TOGGLE_ALERTS"}}
   *     {{! some truly code }}
   *   {{else}}
   *     {{! some falsy code }}
   *   {{/isAuthorized}}
   * </pre>
   */
  Em.Handlebars.registerHelper('isAuthorized', function (property, options) {
    var permission = Ember.Object.create({
      isAuthorized: function () {
        return App.isAuthorized(property);
      }.property('App.router.wizardWatcherController.isWizardRunning')
    });
 
    // wipe out contexts so boundIf uses `this` (the permission) as the context
    options.contexts = null;
    return Ember.Handlebars.helpers.boundIf.call(permission, "isAuthorized", options);
  });
 
  /**
   * Usage:
   *
   * <pre>
   *   {{#havePermissions "SERVICE.TOGGLE_ALERTS"}}
   *     {{! some truly code }}
   *   {{else}}
   *     {{! some falsy code }}
   *   {{/havePermissions}}
   * </pre>
   */
  Em.Handlebars.registerHelper('havePermissions', function (property, options) {
    var permission = Ember.Object.create({
      havePermissions: function () {
        return App.havePermissions(property);
      }.property()
    });
 
    // wipe out contexts so boundIf uses `this` (the permission) as the context
    options.contexts = null;
    return Ember.Handlebars.helpers.boundIf.call(permission, "havePermissions", options);
  });
 
  /**
   * @namespace App
   */
  App = require('app');
 
  /**
   * Certain variables can have JSON in string
   * format, or in JSON format itself.
   *
   * @memberof App
   * @function parseJson
   * @param {string|object}
   * @return {object}
   */
  App.parseJSON = function (value) {
    Iif (typeof value == "string") {
      return jQuery.parseJSON(value);
    }
    return value;
  };
  /**
   * Check for empty <code>Object</code>, built in Em.isEmpty()
   * doesn't support <code>Object</code> type
   *
   * @memberof App
   * @method isEmptyObject
   * @param obj {Object}
   * @return {Boolean}
   */
  App.isEmptyObject = function (obj) {
    var empty = true;
    for (var prop in obj) {
      if (obj.hasOwnProperty(prop)) {
        empty = false;break;
      }
    }
    return empty;
  };
 
  /**
   * Convert object under_score keys to camelCase
   *
   * @param {Object} object
   * @return {Object}
   **/
  App.keysUnderscoreToCamelCase = function (object) {
    var tmp = {};
    for (var key in object) {
      tmp[stringUtils.underScoreToCamelCase(key)] = object[key];
    }
    return tmp;
  };
 
  /**
   * Convert dotted keys to camelcase
   *
   * @param {Object} object
   * @return {Object}
   **/
  App.keysDottedToCamelCase = function (object) {
    var tmp = {};
    for (var key in object) {
      tmp[key.split('.').reduce(function (p, c) {
        return p + c.capitalize();
      })] = object[key];
    }
    return tmp;
  };
  /**
   * Returns object with defined keys only.
   *
   * @memberof App
   * @method permit
   * @param {Object} obj - input object
   * @param {String|Array} keys - allowed keys
   * @return {Object}
   */
  App.permit = function (obj, keys) {
    var result = {};
    Iif ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object' || App.isEmptyObject(obj)) return result;
    if (typeof keys == 'string') keys = Array(keys);
    keys.forEach(function (key) {
      if (obj.hasOwnProperty(key)) result[key] = obj[key];
    });
    return result;
  };
  /**
   *
   * @namespace App
   * @namespace App.format
   */
  App.format = {
    /**
     * @memberof App.format
     * @type {object}
     * @property components
     */
    components: {
      'API': 'API',
      'DECOMMISSION_DATANODE': 'Update Exclude File',
      'DRPC': 'DRPC',
      'FLUME_HANDLER': 'Flume',
      'GLUSTERFS': 'GLUSTERFS',
      'HBASE': 'HBase',
      'HBASE_REGIONSERVER': 'RegionServer',
      'HCAT': 'HCat Client',
      'HDFS': 'HDFS',
      'HISTORYSERVER': 'History Server',
      'HIVE_SERVER': 'HiveServer2',
      'JCE': 'JCE',
      'MAPREDUCE2': 'MapReduce2',
      'MYSQL': 'MySQL',
      'REST': 'REST',
      'SECONDARY_NAMENODE': 'SNameNode',
      'STORM_REST_API': 'Storm REST API Server',
      'WEBHCAT': 'WebHCat',
      'YARN': 'YARN',
      'UI': 'UI',
      'ZKFC': 'ZKFailoverController',
      'ZOOKEEPER': 'ZooKeeper',
      'ZOOKEEPER_QUORUM_SERVICE_CHECK': 'ZK Quorum Service Check',
      'HAWQ': 'HAWQ',
      'PXF': 'PXF'
    },
 
    /**
     * @memberof App.format
     * @property command
     * @type {object}
     */
    command: {
      'INSTALL': 'Install',
      'UNINSTALL': 'Uninstall',
      'START': 'Start',
      'STOP': 'Stop',
      'EXECUTE': 'Execute',
      'ABORT': 'Abort',
      'UPGRADE': 'Upgrade',
      'RESTART': 'Restart',
      'SERVICE_CHECK': 'Check',
      'SET_KEYTAB': 'Set Keytab:',
      'Excluded:': 'Decommission:',
      'Included:': 'Recommission:'
    },
 
    /**
     * cached map of service names
     * @type {object}
     */
    stackServiceRolesMap: {},
 
    /**
     * cached map of component names
     * @type {object}
     */
    stackComponentRolesMap: {},
 
    /**
     * convert role to readable string
     *
     * @memberof App.format
     * @method role
     * @param {string} role
     * @param {boolean} isServiceRole
     * return {string}
     */
    role: function role(_role, isServiceRole) {
 
      if (isServiceRole) {
        var model = App.StackService;
        var map = this.stackServiceRolesMap;
      } else {
        var model = App.StackServiceComponent;
        var map = this.stackComponentRolesMap;
      }
 
      this.initializeStackRolesMap(map, model);
 
      if (map[_role]) {
        return map[_role];
      }
      return this.normalizeName(_role);
    },
 
    initializeStackRolesMap: function initializeStackRolesMap(map, model) {
      if (App.isEmptyObject(map)) {
        model.find().forEach(function (item) {
          map[item.get('id')] = item.get('displayName');
        });
      }
    },
 
    /**
     * Try to format non predefined names to readable format.
     *
     * @method normalizeNameBySeparator
     * @param name {String} - name to format
     * @param separators {String} - token use to split the string
     * @return {String}
     */
    normalizeNameBySeparators: function normalizeNameBySeparators(name, separators) {
      Iif (!name || typeof name != 'string') return '';
      name = name.toLowerCase();
      Iif (!separators || separators.length == 0) {
        console.debug("No separators specified. Use default separator '_' instead");
        separators = ["_"];
      }
 
      for (var i = 0; i < separators.length; i++) {
        var separator = separators[i];
        if (new RegExp(separator, 'g').test(name)) {
          name = name.split(separator).map(function (singleName) {
            return this.normalizeName(singleName.toUpperCase());
          }, this).join(' ');
          break;
        }
      }
      return name.capitalize();
    },
 
    /**
     * Try to format non predefined names to readable format.
     *
     * @method normalizeName
     * @param name {String} - name to format
     * @return {String}
     */
    normalizeName: function normalizeName(name) {
      if (!name || typeof name != 'string') return '';
      if (this.components[name]) return this.components[name];
      name = name.toLowerCase();
      var suffixNoSpaces = ['node', 'tracker', 'manager'];
      var suffixRegExp = new RegExp('(\\w+)(' + suffixNoSpaces.join('|') + ')', 'gi');
      if (/_/g.test(name)) {
        name = name.split('_').map(function (singleName) {
          return this.normalizeName(singleName.toUpperCase());
        }, this).join(' ');
      } else if (suffixRegExp.test(name)) {
        suffixRegExp.lastIndex = 0;
        var matches = suffixRegExp.exec(name);
        name = matches[1].capitalize() + matches[2].capitalize();
      }
      return name.capitalize();
    },
 
    /**
     * convert command_detail to readable string, show the string for all tasks name
     *
     * @memberof App.format
     * @method commandDetail
     * @param {string} command_detail
     * @param {string} request_inputs
     * @return {string}
     */
    commandDetail: function commandDetail(command_detail, request_inputs, ops_display_name) {
      var detailArr = command_detail.split(' ');
      var self = this;
      var result = '';
      var isIncludeExcludeFiles = false;
      //if an optional operation display name has been specified in the service metainfo.xml
      if (ops_display_name != null && ops_display_name.length > 0) {
        result = result + ' ' + ops_display_name;
      } else {
        detailArr.forEach(function (item) {
          // if the item has the pattern SERVICE/COMPONENT, drop the SERVICE part
          if (item.contains('/') && !isIncludeExcludeFiles) {
            item = item.split('/')[1];
          }
          if (item === 'DECOMMISSION,') {
            // ignore text 'DECOMMISSION,'( command came from 'excluded/included'), here get the component name from request_inputs
            var parsedInputs = jQuery.parseJSON(request_inputs);
            item = parsedInputs ? parsedInputs.slave_type || '' : '';
            isIncludeExcludeFiles = parsedInputs ? parsedInputs.is_add_or_delete_slave_request === 'true' : false;
          }
          if (self.components[item]) {
            result = result + ' ' + self.components[item];
          } else if (self.command[item]) {
            result = result + ' ' + self.command[item];
          } else if (isIncludeExcludeFiles) {
            result = result + ' ' + item;
          } else {
            result = result + ' ' + self.role(item, false);
          }
        });
      }
 
      Iif (result.indexOf('Decommission:') > -1 || result.indexOf('Recommission:') > -1) {
        // for Decommission command, make sure the hostname is in lower case
        result = result.split(':')[0] + ': ' + result.split(':')[1].toLowerCase();
      }
      //TODO check if UI use this
      if (result === ' Nagios Update Ignore Actionexecute') {
        result = Em.I18n.t('common.maintenance.task');
      }
      if (result.indexOf('Install Packages Actionexecute') != -1) {
        result = Em.I18n.t('common.installRepo.task');
      }
      Iif (result === ' Rebalancehdfs NameNode') {
        result = Em.I18n.t('services.service.actions.run.rebalanceHdfsNodes.title');
      }
      Iif (result === " Startdemoldap Knox Gateway") {
        result = Em.I18n.t('services.service.actions.run.startLdapKnox.title');
      }
      Iif (result === " Stopdemoldap Knox Gateway") {
        result = Em.I18n.t('services.service.actions.run.stopLdapKnox.title');
      }
      Iif (result === ' Refreshqueues ResourceManager') {
        result = Em.I18n.t('services.service.actions.run.yarnRefreshQueues.title');
      }
      // HAWQ custom commands on back Ops page.
      Iif (result === ' Resync Hawq Standby HAWQ Standby Master') {
        result = Em.I18n.t('services.service.actions.run.resyncHawqStandby.label');
      }
      Iif (result === ' Immediate Stop Hawq Service HAWQ Master') {
        result = Em.I18n.t('services.service.actions.run.immediateStopHawqService.label');
      }
      Iif (result === ' Immediate Stop Hawq Segment HAWQ Segment') {
        result = Em.I18n.t('services.service.actions.run.immediateStopHawqSegment.label');
      }
      Iif (result === ' Activate Hawq Standby HAWQ Standby Master') {
        result = Em.I18n.t('admin.activateHawqStandby.button.enable');
      }
      Iif (result === ' Hawq Clear Cache HAWQ Master') {
        result = Em.I18n.t('services.service.actions.run.clearHawqCache.label');
      }
      Iif (result === ' Run Hawq Check HAWQ Master') {
        result = Em.I18n.t('services.service.actions.run.runHawqCheck.label');
      }
      //<---End HAWQ custom commands--->
      return result;
    },
 
    /**
     * Convert uppercase status name to lowercase.
     * <br>
     * <br>PENDING - Not queued yet for a host
     * <br>QUEUED - Queued for a host
     * <br>IN_PROGRESS - Host reported it is working
     * <br>COMPLETED - Host reported success
     * <br>FAILED - Failed
     * <br>TIMEDOUT - Host did not respond in time
     * <br>ABORTED - Operation was abandoned
     *
     * @memberof App.format
     * @method taskStatus
     * @param {string} _taskStatus
     * @return {string}
     *
     */
    taskStatus: function taskStatus(_taskStatus) {
      return _taskStatus.toLowerCase();
    },
 
    /**
     * simplify kdc error msg
     * @param {string} message
     * @param {boolean} strict if this flag is true ignore not defined msgs return null
     *  else return input msg as is;
     * @returns {*}
     */
    kdcErrorMsg: function kdcErrorMsg(message, strict) {
      /**
       * Error messages for KDC administrator credentials error
       * is used for checking if error message is caused by bad KDC credentials
       * @type {{missingKDC: string, invalidKDC: string}}
       */
      var specialMsg = {
        "missingKDC": "Missing KDC administrator credentials.",
        "invalidKDC": "Invalid KDC administrator credentials.",
        "missingRDCForRealm": "Failed to find a KDC for the specified realm - kadmin"
      };
 
      for (var m in specialMsg) {
        if (specialMsg.hasOwnProperty(m) && message.contains(specialMsg[m])) return specialMsg[m];
      }
      return strict ? null : message;
    }
  };
 
  /**
   * wrapper to bootstrap popover
   * fix issue when popover stuck on view routing
   *
   * @memberof App
   * @method popover
   * @param {DOMElement} self
   * @param {object} options
   */
  App.popover = function (self, options) {
    var opts = $.extend(true, {
      container: 'body',
      html: true
    }, options || {});
    Iif (!self) return;
    self.popover(opts);
    self.on("remove", function () {
      $(this).trigger('mouseleave').off().removeData('bs.popover');
    });
    self = null;
  };
 
  /**
   * wrapper to bootstrap tooltip
   * fix issue when tooltip stuck on view routing
   * @memberof App
   * @method tooltip
   * @param {DOMElement} self
   * @param {object} options
   */
  App.tooltip = function (self, options) {
    var opts = $.extend(true, {
      container: 'body'
    }, options || {});
    if (!self || !self.tooltip) return;
    self.tooltip(opts);
    /* istanbul ignore next */
    self.on("remove", function () {
      $(this).trigger('mouseleave').off().removeData('bs.tooltip');
    });
    self = null;
  };
 
  /**
   * wrapper to Date().getTime()
   * fix issue when client clock and server clock not sync
   *
   * @memberof App
   * @method dateTime
   * @return {Number} timeStamp of current server clock
   */
  App.dateTime = function () {
    return new Date().getTime() + App.clockDistance;
  };
 
  /**
   *
   * @param {number} [x] timestamp
   * @returns {number}
   */
  App.dateTimeWithTimeZone = function (x) {
    var timezone = App.router.get('userSettingsController.userSettings.timezone');
    Iif (timezone) {
      var tz = Em.getWithDefault(timezone, 'zones.0.value', '');
      return moment(moment.tz(x ? new Date(x) : new Date(), tz).toArray()).toDate().getTime();
    }
    return x || new Date().getTime();
  };
 
  App.formatDateTimeWithTimeZone = function (timeStamp, format) {
    var timezone = App.router.get('userSettingsController.userSettings.timezone'),
        time;
    if (timezone) {
      var tz = Em.getWithDefault(timezone, 'zones.0.value', '');
      time = moment.tz(timeStamp, tz);
    } else {
      time = moment(timeStamp);
    }
    return moment(time).format(format);
  };
 
  App.getTimeStampFromLocalTime = function (time) {
    var timezone = App.router.get('userSettingsController.userSettings.timezone'),
        offsetString = '',
        date = moment(time).format('YYYY-MM-DD HH:mm:ss');
    Iif (timezone) {
      var offset = timezone.utcOffset;
      offsetString = moment().utcOffset(offset).format('Z');
    }
    return moment(date + offsetString).toDate().getTime();
  };
 
  /**
   * Ambari overrides the default date transformer.
   * This is done because of the non-standard data
   * sent. For example Nagios sends date as "12345678".
   * The problem is that it is a String and is represented
   * only in seconds whereas Javascript's Date needs
   * milliseconds representation.
   */
  DS.attr.transforms.date = {
    from: function from(serialized) {
      var type = typeof serialized === 'undefined' ? 'undefined' : _typeof(serialized);
      if (type === Em.I18n.t('common.type.string')) {
        serialized = parseInt(serialized);
        type = typeof serialized === 'undefined' ? 'undefined' : _typeof(serialized);
      }
      if (type === Em.I18n.t('common.type.number')) {
        if (!serialized) {
          //serialized timestamp = 0;
          return 0;
        }
        // The number could be seconds or milliseconds.
        // If seconds, then the length is 10
        // If milliseconds, the length is 13
        if (serialized.toString().length < 13) {
          serialized = serialized * 1000;
        }
        return new Date(serialized);
      } else if (serialized === null || serialized === undefined) {
        // if the value is not present in the data,
        // return undefined, not null.
        return serialized;
      } else {
        return null;
      }
    },
    to: function to(deserialized) {
      if (deserialized instanceof Date) {
        return deserialized.getTime();
      } else if (deserialized === undefined) {
        return undefined;
      } else {
        return null;
      }
    }
  };
 
  DS.attr.transforms.object = {
    from: function from(serialized) {
      return Ember.none(serialized) ? null : Object(serialized);
    },
 
    to: function to(deserialized) {
      return Ember.none(deserialized) ? null : Object(deserialized);
    }
  };
 
  /**
   * Allows EmberData models to have array properties.
   *
   * Declare the property as <code>
   *  operations: DS.attr('array'),
   * </code> and
   * during load provide a JSON array for value.
   *
   * This transform simply assigns the same array in both directions.
   */
  DS.attr.transforms.array = {
    from: function from(serialized) {
      return serialized;
    },
    to: function to(deserialized) {
      return deserialized;
    }
  };
 
  /**
   *  Utility method to delete all existing records of a DS.Model type from the model's associated map and
   *  store's persistence layer (recordCache)
   * @param type DS.Model Class
   */
  App.resetDsStoreTypeMap = function (type) {
    var allRecords = App.get('store.recordCache'); //This fetches all records in the ember-data persistence layer
    var typeMaps = App.get('store.typeMaps');
    var guidForType = Em.guidFor(type);
    var typeMap = typeMaps[guidForType];
    Eif (typeMap) {
      var idToClientIdMap = typeMap.idToCid;
      for (var id in idToClientIdMap) {
        if (idToClientIdMap.hasOwnProperty(id) && idToClientIdMap[id] && allRecords[idToClientIdMap[id]] !== undefined) {
          delete allRecords[idToClientIdMap[id]]; // deletes the cached copy of the record from the store
        }
      }
      typeMaps[guidForType] = {
        idToCid: {},
        clientIds: [],
        cidToHash: {},
        recordArrays: []
      };
    }
  };
 
  /**
   * Clean store from already loaded data.
   * @param {DS.Model[]} models to clear
   **/
  App.clearModels = function (models) {
    models.forEach(function (model) {
      var records = App.get('store').findAll(model).filterProperty('id');
      records.forEach(function (rec) {
        Ember.run(this, function () {
          rec.deleteRecord();
          App.store.fastCommit();
        });
      }, this);
    }, this);
  };
 
  App.logger = function () {
 
    var timers = {};
 
    return {
 
      maxAllowedLoadingTime: 1000,
 
      setTimer: function setTimer(name) {
        Iif (!App.get('enableLogger')) return;
        timers[name] = window.performance.now();
      },
 
      logTimerIfMoreThan: function logTimerIfMoreThan(name, loadingTime) {
        Iif (!App.get('enableLogger')) return;
        this.maxAllowedLoadingTime = loadingTime || this.maxAllowedLoadingTime;
        if (timers[name]) {
          var diff = window.performance.now() - timers[name];
          Iif (diff > this.maxAllowedLoadingTime) {
            console.debug(name + ': ' + diff.toFixed(3) + 'ms');
          }
          delete timers[name];
        }
      }
    };
  }();
});