actions.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* Offset of the first opened window.
  2. Used to stagger/cascade future open windows */
  3. /*const WINDOW_DEFAULT_TOP = 25;
  4. const WINDOW_DEFAULT_LEFT = 7;
  5. const WINDOW_STAGGER_TOP = 14;
  6. const WINDOW_STAGGER_LEFT = 12;*/
  7. const Actions = {
  8. /* Parse an action
  9. Parameters:
  10. action: An action function defined as function:argument */
  11. "parseFunction": function (action) {
  12. let actions = action.split("::");
  13. /* This references the function from the Actions function array */
  14. let fn = Actions[actions[0]];
  15. /* Remove the function name so we can get just the arguments */
  16. actions.splice(0, 1);
  17. /* Call the function with zero or more arguments */
  18. fn(...(actions));
  19. },
  20. /* Open a window
  21. Parameters:
  22. wrapperID: The id of the window wrapper
  23. windowClass: The class of the window (e.g. "folder" or "info")
  24. transferAnimation: If true, animate the opening of the window
  25. callbackFn: A callback function*/
  26. "openWindow": function (wrapperID, windowClass, transferAnimation = false, callbackFn = undefined) {
  27. let desktopDBItem = findDesktopDBItem(g_DesktopDB, "id", wrapperID);
  28. if (wrapperID === undefined || windowClass === undefined) return;
  29. if (g_openWindows.some(e => e.id === wrapperID && e.type === windowClass)) {
  30. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"]");
  31. $wrapper.find(".window." + windowClass).trigger("focus");
  32. if (callbackFn && $wrapper) {
  33. callbackFn($wrapper);
  34. }
  35. return $wrapper;
  36. } else {
  37. let $wrapper = $("<div class=\"window-wrapper\"></div>").appendTo(".desktop");
  38. let $window = $("<div class=\"window " + windowClass + "\"></div>").appendTo($wrapper);
  39. let windowViewFile = "window." + windowClass + ".html";
  40. /* Hide the window initially so we don't see it loading */
  41. $window.hide();
  42. g_openWindows.push({
  43. "id": wrapperID,
  44. "type": windowClass
  45. });
  46. $window.load(windowViewFile, function () {
  47. $window.children(".header").first().load("window.header.html", function () {
  48. $wrapper.attr("data-id", wrapperID);
  49. $wrapper.attr("data-type", windowClass);
  50. initWindow(wrapperID, windowClass);
  51. /* Reset saved position of window */
  52. if (desktopDBItem) {
  53. $wrapper.css("top", desktopDBItem.top);
  54. $wrapper.css("left", desktopDBItem.left);
  55. }
  56. /* Transfer animation */
  57. if (transferAnimation) {
  58. $("figure[data-id=\"" + wrapperID + "\"]").find("img").transfer({
  59. to: $window.parent(".window-wrapper"),
  60. duration: SETTINGS_TRANSFER_ANIMATION_DURATION
  61. }, function () {
  62. $window.show();
  63. /* Initialize jScrollPane
  64. We always reinitialise right after because adding the scrollbars might wrap
  65. the contents down.
  66. */
  67. let $jsp = $wrapper.find(".body");
  68. if ($jsp.length) {
  69. let jsp = $jsp.jScrollPane(g_jScrollPaneSettings);
  70. jsp.data("jsp").reinitialise();
  71. }
  72. /* TODO: Resizable windows */
  73. /*$wrapper.find(".window").resizable({
  74. alsoResize: $window.find(".contents, .body, .grid"),
  75. handles: "se",
  76. minWidth: 200,
  77. minHeight: 200,
  78. resize: function() {
  79. jsp.data("jsp").reinitialise();
  80. }
  81. });*/
  82. $window.trigger("focus");
  83. /* TODO: Staggered windows */
  84. /*offset = $window.offset();
  85. $window.offset({
  86. top: offset.top + (WINDOW_STAGGER_TOP * g_openWrappers.length),
  87. left: offset.left + (WINDOW_STAGGER_LEFT * g_openWrappers.length)
  88. });*/
  89. });
  90. } else {
  91. $window.show();
  92. $window.trigger("focus");
  93. }
  94. if (callbackFn && $wrapper) {
  95. callbackFn($wrapper);
  96. }
  97. });
  98. });
  99. return $wrapper;
  100. }
  101. },
  102. /* Close a window
  103. Parameters:
  104. $wrapper: The window wrapper JQuery object */
  105. "closeWindow": function ($wrapper, animate = true) {
  106. let wrapperID = $wrapper.attr("data-id");
  107. let wrapperType = $wrapper.attr("data-type");
  108. let $figure = $("figure[data-id=\"" + wrapperID + "\"]");
  109. let $iconImg = $figure.find("img");
  110. let icon = $figure.attr("data-icon");
  111. let desktopDBItem = findDesktopDBItem(g_DesktopDB, "id", wrapperID);
  112. /* Save the position of the window */
  113. if (desktopDBItem) {
  114. desktopDBItem.top = $wrapper.css("top");
  115. desktopDBItem.left = $wrapper.css("left");
  116. }
  117. /* Checks if the window was opened by a folder or is an application and animates accordingly */
  118. if (($figure.hasClass("folder") || $figure.hasClass("application") || $figure.hasClass("trash")) && $iconImg.length && animate) {
  119. $wrapper.transfer({
  120. to: $iconImg,
  121. duration: SETTINGS_TRANSFER_ANIMATION_DURATION
  122. }, function () {
  123. /* Remove the wrapper from the open wrappers list */
  124. g_openWindows.splice(g_openWindows.findIndex(e => e.id === wrapperID && e.type === wrapperType), 1);
  125. $wrapper.remove();
  126. /* Remove the icon mask */
  127. $iconImg.attr("src", SETTINGS_ICONS_LARGE_PATH + icon + ".png");
  128. $figure.children("a").first().trigger("focus");
  129. if (g_openWindows.length)
  130. Actions.focusWindow(g_openWindows[g_openWindows.length - 1]);
  131. });
  132. } else {
  133. /* This block is repeated because the previous instance
  134. is an async call inside an animation */
  135. g_openWindows.splice(g_openWindows.findIndex(e => e.id === wrapperID && e.type === wrapperType), 1);
  136. if (g_openWindows.length) {
  137. /* Remove any focus class before explicitly focusing */
  138. $(".desktop figure.focus").removeClass("focus");
  139. Actions.focusWindow(g_openWindows[g_openWindows.length - 1]);
  140. } else {
  141. /* If there is a focused figure on the desktop then explicitly
  142. focus it in case it's just a class focus */
  143. $(".desktop figure.focus > a").trigger("focus");
  144. }
  145. $wrapper.remove();
  146. }
  147. },
  148. /* Focus a window.
  149. Parameters:
  150. objWrapper:
  151. .id: the ID of the wrapper
  152. .type: the type of window */
  153. "focusWindow": function (objWrapper) {
  154. let $wrapper = $(".window-wrapper[data-id=\"" + objWrapper.id + "\"][data-type=\"" + objWrapper.type + "\"]");
  155. let $window = $wrapper.children(".window");
  156. $window.trigger("focus");
  157. },
  158. "getInfoOnFocusedFigure": function () {
  159. let $figure = $("figure.focus");
  160. let figureID = $figure.attr("data-id");
  161. if ($figure.length > 0) {
  162. Actions.openWindow(figureID, "info", false);
  163. }
  164. },
  165. /* Download a file
  166. Parameters:
  167. desktopDBItem: The Desktop DB object of the file to download */
  168. "downloadFile": function (desktopDBItem) {
  169. let startTime = (new Date()).getTime();
  170. let desktopDBItemParentName = findDesktopDBItemParent(g_DesktopDB, "id", desktopDBItem.id).name;
  171. let $wrapper = Actions.openWindow(desktopDBItem.id, "download", false, function ($wrapper) {
  172. $wrapper.find("button").on("click", function () {
  173. $wrapper.data("jqXHR").abort();
  174. });
  175. });
  176. /* Create an AJAX request to download the file */
  177. let jqXHR = $.ajax(desktopDBItem.url, {
  178. type: "GET",
  179. cache: false,
  180. xhrFields: {
  181. responseType: "blob" // binary data
  182. },
  183. success: function (data) {
  184. let a = document.createElement("a");
  185. let url = window.URL.createObjectURL(data);
  186. a.href = url;
  187. a.download = desktopDBItem.name;
  188. document.body.append(a);
  189. a.click();
  190. a.remove();
  191. window.URL.revokeObjectURL(url);
  192. $wrapper.find(".progress .right").addClass("done");
  193. $wrapper.find(".download-calculated-remaining").html("Done");
  194. /* Wait a couple seconds before closing the window */
  195. setTimeout(function () {
  196. Actions.closeWindow($wrapper);
  197. }, 2000);
  198. },
  199. /* error will also trigger on an abort */
  200. error: function () {
  201. $wrapper.find(".download-calculated-remaining").html("Cancelled");
  202. setTimeout(function () {
  203. Actions.closeWindow($wrapper);
  204. }, 2000);
  205. },
  206. progress: function downloadProgress(e) {
  207. if (e.lengthComputable) {
  208. let currentTime = (new Date()).getTime();
  209. let duration = (currentTime - startTime) / 1000;
  210. let estimatedSecondsRemaining = (e.total / (e.loaded / duration)) - duration;
  211. let percentage = (e.loaded * 100) / e.total;
  212. $wrapper.find(".progress .middle").css("width", percentage + "%");
  213. $wrapper.find(".download-calculated-remaining").html(formatTimeRemaining(estimatedSecondsRemaining));
  214. $wrapper.find("td.download-filename").html(desktopDBItem.name);
  215. $wrapper.find("td.download-from").html(desktopDBItemParentName);
  216. $wrapper.find("td.download-bytes").html(formatBytes(e.loaded) + " of " + formatBytes(e.total));
  217. }
  218. }
  219. });
  220. /* Put the jqXHR object into the wrapper data so that it can be referenced to be aborted */
  221. $wrapper.data("jqXHR", jqXHR);
  222. },
  223. /* Open an alert
  224. Parameters:
  225. id: The id of the alert */
  226. "alert": function (id, parameter = undefined) {
  227. let $wrapper = $(".alert-wrapper");
  228. let alertFile = "alert." + id + ".html";
  229. $wrapper.load(alertFile, function () {
  230. $(".modal").show();
  231. initAlert(id, parameter);
  232. });
  233. },
  234. /* Open a figure by its ID
  235. Parameters:
  236. figureID: The ID of the figure */
  237. "openFigureID": function (figureID) {
  238. let $figure = $("figure[data-id=\"" + figureID + "\"] > a");
  239. $figure.trigger("dblclick");
  240. },
  241. /* Open the currently focused figure */
  242. "openFocusedFigure": function () {
  243. let $figure = $("figure.focus");
  244. if ($figure.length > 0)
  245. $figure.find("> a").trigger("dblclick");
  246. },
  247. /* Close the currently focused window */
  248. "closeFocusedWindow": function () {
  249. let $wrapper = $(".window.focus").first().parent(".window-wrapper");
  250. if ($wrapper.length > 0)
  251. Actions.closeWindow($wrapper);
  252. },
  253. /* Wake up the website */
  254. "wake": function (callbackFn) {
  255. let $sleep = $(".sleep");
  256. $sleep.fadeTo("slow", 0, function () {
  257. $sleep.toggleClass("active");
  258. $sleep.hide();
  259. if (callbackFn) {
  260. callbackFn();
  261. }
  262. });
  263. },
  264. /* Put the website to sleep */
  265. "sleep": function (callbackFn) {
  266. let $sleep = $(".sleep");
  267. $sleep.toggleClass("active");
  268. $sleep.show();
  269. $sleep.fadeTo("slow", 1.0, callbackFn);
  270. },
  271. /* Reload the page. This is needed for callbacks */
  272. "reload": function () {
  273. location.reload();
  274. },
  275. /* Restart the website */
  276. "restart": function () {
  277. Actions.sleep(Actions.reload);
  278. },
  279. /* Shutdown the website */
  280. "shutdown": function () {
  281. window.location.href = SETTINGS_SHUTDOWN_URL;
  282. }
  283. };