helper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. String.prototype.capitalize = function () {
  19. return this.charAt(0).toUpperCase() + this.slice(1);
  20. }
  21. Em.CoreObject.reopen({
  22. t:function (key, attrs) {
  23. return Em.I18n.t(key, attrs)
  24. }
  25. });
  26. Handlebars.registerHelper('log', function (variable) {
  27. console.log(variable);
  28. });
  29. Handlebars.registerHelper('warn', function (variable) {
  30. console.warn(variable);
  31. });
  32. String.prototype.format = function () {
  33. var args = arguments;
  34. return this.replace(/{(\d+)}/g, function (match, number) {
  35. return typeof args[number] != 'undefined' ? args[number] : match;
  36. });
  37. };
  38. /**
  39. * Convert byte size to other metrics.
  40. * @param {Number} precision Number to adjust precision of return value. Default is 0.
  41. * @param {String} parseType JS method name for parse string to number. Default is "parseInt".
  42. * @remarks The parseType argument can be "parseInt" or "parseFloat".
  43. * @return {String) Returns converted value with abbreviation.
  44. */
  45. Number.prototype.bytesToSize = function (precision, parseType/* = 'parseInt' */) {
  46. if (arguments[1] === undefined) {
  47. parseType = 'parseInt';
  48. }
  49. var value = this;
  50. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  51. var posttxt = 0;
  52. if (this == 0) return 'n/a';
  53. while (value >= 1024) {
  54. posttxt++;
  55. value = value / 1024;
  56. }
  57. var parsedValue = window[parseType](value);
  58. return parsedValue.toFixed(precision) + " " + sizes[posttxt];
  59. }
  60. Number.prototype.toDaysHoursMinutes = function () {
  61. var formatted = {},
  62. dateDiff = this,
  63. minK = 60, // sec
  64. hourK = 60 * minK, // sec
  65. dayK = 24 * hourK;
  66. dateDiff = parseInt(dateDiff / 1000);
  67. formatted.d = Math.floor(dateDiff / dayK);
  68. dateDiff -= formatted.d * dayK;
  69. formatted.h = Math.floor(dateDiff / hourK);
  70. dateDiff -= formatted.h * hourK;
  71. formatted.m = Math.floor(dateDiff / minK);
  72. dateDiff -= formatted.m * minK;
  73. return formatted;
  74. }
  75. Number.prototype.countPercentageRatio = function (maxValue) {
  76. var usedValue = this;
  77. return Math.round((usedValue / maxValue) * 100) + "%";
  78. }
  79. /**
  80. * Formats the given URL template by replacing keys in 'substitutes'
  81. * with their values. If not in App.testMode, the testUrl is used.
  82. *
  83. * The substitution points in urlTemplate should be of format "...{key}..."
  84. * For example "http://apache.org/{projectName}".
  85. * The substitutes can then be{projectName: "Ambari"}.
  86. *
  87. * Keys which will be automatically taken care of are:
  88. * {
  89. * hostName: App.test_hostname,
  90. * fromSeconds: ..., // 1 hour back from now
  91. * toSeconds: ..., // now
  92. * stepSeconds: ..., // 15 seconds by default
  93. * }
  94. *
  95. * @param {String} urlTemplate URL template on which substitutions are to be made
  96. * @param substitutes Object containing keys to be replaced with respective values
  97. * @param {String} testUrl URL to be used if app is not in test mode (!App.testMode)
  98. * @return {String} Formatted URL
  99. */
  100. App.formatUrl = function (urlTemplate, substitutes, testUrl) {
  101. var formatted = urlTemplate;
  102. if (urlTemplate) {
  103. if (App.testMode) {
  104. var toSeconds = Math.round(new Date().getTime() / 1000);
  105. var allSubstitutes = {
  106. toSeconds: toSeconds,
  107. fromSeconds: toSeconds - 3600, // 1 hour back
  108. stepSeconds: 15, // 15 seconds
  109. hostName: App.test_hostname
  110. };
  111. jQuery.extend(allSubstitutes, substitutes);
  112. for (key in allSubstitutes) {
  113. var useKey = '{' + key + '}';
  114. formatted = formatted.replace(new RegExp(useKey, 'g'), allSubstitutes[key]);
  115. }
  116. } else {
  117. formatted = testUrl;
  118. }
  119. }
  120. return formatted;
  121. }