helper.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {
  20. return this.charAt(0).toUpperCase() + this.slice(1);
  21. }
  22. Em.CoreObject.reopen({
  23. t: function(key, attrs){
  24. return Em.I18n.t(key, attrs)
  25. }
  26. });
  27. Handlebars.registerHelper('log', function(variable) {
  28. console.log(variable);
  29. });
  30. Handlebars.registerHelper('warn', function(variable) {
  31. console.warn(variable);
  32. });
  33. String.prototype.format = function() {
  34. var args = arguments;
  35. return this.replace(/{(\d+)}/g, function(match, number) {
  36. return typeof args[number] != 'undefined' ? args[number] : match;
  37. });
  38. };
  39. /**
  40. * Convert byte size to other metrics.
  41. * @param {Number} precision Number to adjust precision of return value. Default is 0.
  42. * @param {String} parseType JS method name for parse string to number. Default is "parseInt".
  43. * @remarks The parseType argument can be "parseInt" or "parseFloat".
  44. * @return {String) Returns converted value with abbreviation.
  45. */
  46. Number.prototype.bytesToSize = function(precision, parseType/* = 'parseInt' */) {
  47. if (arguments[1] === undefined) {
  48. parseType = 'parseInt';
  49. }
  50. var value = this;
  51. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  52. var posttxt = 0;
  53. if (this == 0) return 'n/a';
  54. while( value >= 1024 ) {
  55. posttxt++;
  56. value = value / 1024;
  57. }
  58. if (parseType == 'parseInt') {
  59. var parsedValue = parseInt(value);
  60. } else if (parseType == 'parseFloat') {
  61. var parsedValue = parseFloat(value);
  62. } else {
  63. console.warn('Parameter parseType incorrect');
  64. }
  65. return parsedValue.toFixed(precision) + " " + sizes[posttxt];
  66. }
  67. Number.prototype.toDaysHoursMinutes = function() {
  68. var formatted = {},
  69. dateDiff = this,
  70. minK = 60, // sec
  71. hourK = 60 * minK, // sec
  72. dayK = 24 * hourK;
  73. dateDiff = parseInt(dateDiff/1000);
  74. formatted.d = Math.floor(dateDiff/dayK);
  75. dateDiff -= formatted.d * dayK;
  76. formatted.h = Math.floor(dateDiff/hourK);
  77. dateDiff -= formatted.h * hourK;
  78. formatted.m = Math.floor(dateDiff/minK);
  79. dateDiff -= formatted.m * minK;
  80. return formatted;
  81. }
  82. Number.prototype.countPercentageRatio = function(maxValue) {
  83. var usedValue = this;
  84. return Math.round((usedValue/maxValue) * 100) + "%";
  85. }