dfs-dust.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. (function ($, dust, exports) {
  19. "use strict";
  20. var filters = {
  21. 'fmt_bytes': function (v) {
  22. var UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'ZB'];
  23. var prev = 0, i = 0;
  24. while (Math.floor(v) > 0 && i < UNITS.length) {
  25. prev = v;
  26. v /= 1024;
  27. i += 1;
  28. }
  29. if (i > 0 && i < UNITS.length) {
  30. v = prev;
  31. i -= 1;
  32. }
  33. return Math.round(v * 100) / 100 + ' ' + UNITS[i];
  34. },
  35. 'fmt_percentage': function (v) {
  36. return Math.round(v * 100) / 100 + '%';
  37. },
  38. 'fmt_time': function (v) {
  39. var s = Math.floor(v / 1000), h = Math.floor(s / 3600);
  40. s -= h * 3600;
  41. var m = Math.floor(s / 60);
  42. s -= m * 60;
  43. var res = s + " sec";
  44. if (m !== 0) {
  45. res = m + " mins, " + res;
  46. }
  47. if (h !== 0) {
  48. res = h + " hrs, " + res;
  49. }
  50. return res;
  51. },
  52. 'date_tostring' : function (v) {
  53. return new Date(Number(v)).toLocaleString();
  54. },
  55. 'helper_to_permission': function (v) {
  56. var symbols = [ '---', '--x', '-w-', '-wx', 'r--', 'r-x', 'rw-', 'rwx' ];
  57. var sticky = v > 1000;
  58. var res = "";
  59. for (var i = 0; i < 3; ++i) {
  60. res = symbols[(v % 10)] + res;
  61. v = Math.floor(v / 10);
  62. }
  63. if (sticky) {
  64. var otherExec = ((v % 10) & 1) == 1;
  65. res = res.substr(0, res.length - 1) + (otherExec ? 't' : 'T');
  66. }
  67. return res;
  68. },
  69. 'helper_to_directory' : function (v) {
  70. return v === 'DIRECTORY' ? 'd' : '-';
  71. }
  72. };
  73. $.extend(dust.filters, filters);
  74. /**
  75. * Load a sequence of JSON.
  76. *
  77. * beans is an array of tuples in the format of {url, name}.
  78. */
  79. function load_json(beans, success_cb, error_cb) {
  80. var data = {}, error = false, to_be_completed = beans.length;
  81. $.each(beans, function(idx, b) {
  82. if (error) {
  83. return false;
  84. }
  85. $.get(b.url, function (resp) {
  86. data[b.name] = resp;
  87. to_be_completed -= 1;
  88. if (to_be_completed === 0) {
  89. success_cb(data);
  90. }
  91. }).error(function (jqxhr, text, err) {
  92. error = true;
  93. error_cb(b.url, jqxhr, text, err);
  94. });
  95. });
  96. }
  97. exports.load_json = load_json;
  98. }($, dust, window));