utilities.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. let g_menubarShowDate = false;
  2. updateClock();
  3. setInterval(function () {
  4. updateClock();
  5. }, 1000);
  6. /* Update the menubar clock */
  7. function updateClock() {
  8. const today = new Date();
  9. let y = today.getFullYear();
  10. let M = String(today.getMonth() + 1).padStart(2, '0');
  11. let d = String(today.getDate()).padStart(2, '0');
  12. let h = today.getHours();
  13. let m = String(today.getMinutes()).padStart(2, '0');
  14. if (g_menubarShowDate) {
  15. $(".menubar .clock").html(y + "/" + M + "/" + d);
  16. } else {
  17. $(".menubar .clock").html(h + ":" + m);
  18. }
  19. }
  20. /* Set the active application menu
  21. * Parameters:
  22. * applicationName: The title case name of the application */
  23. function setActiveApplication(applicationName) {
  24. let $activeApplication = $(".menubar .active-application");
  25. $activeApplication.empty().append("<img src=\"img/ui/icons-small/apps/" + applicationName.toLowerCase() + ".png\"/>");
  26. $activeApplication.append("<span>" + applicationName + "</span>");
  27. return $activeApplication;
  28. }
  29. /* Recursively generate GUIDs for DesktopDB items */
  30. function generateDesktopDBIDs(tree) {
  31. for (let node of tree) {
  32. if (!node.id) node.id = getRandomUUID();
  33. if (node.contents) generateDesktopDBIDs(node.contents);
  34. }
  35. }
  36. /* Recursively generate "where" strings for Info windows */
  37. function generateWhereStrings(tree, where) {
  38. for (let node of tree) {
  39. if (!node.where) node.where = where;
  40. if (node.contents) generateWhereStrings(node.contents, node.where + node.name + ":");
  41. }
  42. }
  43. /* Recursively find a node given a property and value
  44. * Parameters:
  45. * tree: The DesktopDB object
  46. * property: The property to search
  47. * value: The value to search (i.e. property.value) */
  48. function findDesktopDBItem(tree, property, value) {
  49. for (let node of tree) {
  50. if (node[property] === value) return node;
  51. if (node.contents) {
  52. let desiredNode = findDesktopDBItem(node.contents, property, value);
  53. if (desiredNode) return desiredNode
  54. }
  55. }
  56. return undefined;
  57. }
  58. /* Recursively find a node's parent given a property and value
  59. * Parameters:
  60. * tree: The DesktopDB object
  61. * property: The property to search
  62. * value: The value to search (i.e. property.value) */
  63. function findDesktopDBItemParent(tree, property, value) {
  64. for (let node of tree) {
  65. if (node[property] === value) return tree;
  66. if (node.contents) {
  67. let desiredTree = findDesktopDBItem(node.contents, property, value);
  68. if (desiredTree) return node
  69. }
  70. }
  71. return false;
  72. }
  73. function getDesktopDBItemFileCount(tree) {
  74. let count = 0;
  75. if (tree.contents) {
  76. for (let node of tree.contents) {
  77. count++;
  78. if (node.contents) {
  79. count += getDesktopDBItemFileCount(node);
  80. }
  81. }
  82. }
  83. return count;
  84. }
  85. /* Format a number into byte sizes
  86. * Parameters:
  87. * bytes: The number to format
  88. * decimals: Output decimal places */
  89. function formatBytes(bytes, decimals = 1) {
  90. if (bytes === 0) return '0 Bytes';
  91. const k = 1024;
  92. const dm = decimals < 0 ? 0 : decimals;
  93. const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  94. const i = Math.floor(Math.log(bytes) / Math.log(k));
  95. return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
  96. }
  97. /* Format seconds remaining into text
  98. * Parameters:
  99. * seconds: The seconds remaining */
  100. function formatTimeRemaining(seconds) {
  101. if (seconds < 7) {
  102. return "About 5 seconds";
  103. } else if (seconds < 15) {
  104. return "About 10 seconds";
  105. } else if (seconds < 55) {
  106. return "Less than a minute";
  107. } else if (seconds < 80) {
  108. return "About a minute";
  109. } else if (seconds < 3300) {
  110. return "About " + Math.ceil(seconds / 60) + " minutes";
  111. } else if (seconds < 4800) {
  112. return "About an hour";
  113. } else {
  114. return "About " + Math.ceil(seconds / 3600) + " hours";
  115. }
  116. }
  117. /* Generate a random UUIDv4 */
  118. function getRandomUUID() {
  119. return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
  120. (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
  121. );
  122. }