windows.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. let g_openWindows = [];
  2. let g_highestZIndex = 3;
  3. function initWindow(wrapperID, windowClass) {
  4. let $wrapper = Object;
  5. switch (windowClass) {
  6. case "folder":
  7. $wrapper = initFolderWindow(wrapperID);
  8. initBaseWindow($wrapper);
  9. break;
  10. case "info":
  11. $wrapper = initInfoWindow(wrapperID);
  12. initBaseWindow($wrapper);
  13. break;
  14. case "about":
  15. $wrapper = initAboutWindow(wrapperID);
  16. initBaseWindow($wrapper);
  17. break;
  18. case "download":
  19. $wrapper = initDownloadWindow(wrapperID);
  20. initBaseWindow($wrapper);
  21. break;
  22. case "simpletext":
  23. $wrapper = initSimpleTextWindow(wrapperID);
  24. initBaseWindow($wrapper);
  25. break;
  26. case "pictureviewer":
  27. $wrapper = initPictureViewerWindow(wrapperID);
  28. initBaseWindow($wrapper);
  29. break;
  30. case "zterm":
  31. $wrapper = initZTermWindow(wrapperID);
  32. initBaseWindow($wrapper);
  33. break;
  34. default:
  35. break;
  36. }
  37. }
  38. function initFolderWindow(wrapperID) {
  39. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"folder\"]");
  40. let $window = $wrapper.children(".window");
  41. let desktopDBItem = findDesktopDBItem(g_DesktopDB, "id", wrapperID);
  42. let headerTitle = "";
  43. if (!desktopDBItem) return;
  44. /* Set the window icon and title */
  45. switch (wrapperID) {
  46. case "folder":
  47. headerTitle = "<img src=\"" + SETTINGS_ICONS_SMALL_PATH + "folder.png\" />" + desktopDBItem.name;
  48. break;
  49. case "trash":
  50. headerTitle = "<img src=\"" + SETTINGS_ICONS_SMALL_PATH + "trash.png\" />" + desktopDBItem.name;
  51. break;
  52. default:
  53. headerTitle = "<img src=\"" + SETTINGS_ICONS_SMALL_PATH + "folder.png\" />" + desktopDBItem.name;
  54. break;
  55. }
  56. $window.find(".header > .title").html(headerTitle);
  57. /* Build window (i.e. folder) contents */
  58. if (desktopDBItem.contents) {
  59. initFolderWindowContents($window, desktopDBItem.contents);
  60. /* Build icons, etc. */
  61. initFigures($window);
  62. }
  63. $window.find(".grid figure a").on("focus", function () {
  64. /* Style the window, but don't focus it as
  65. we don't want to lose the figure focus */
  66. $window.triggerHandler("focus");
  67. }).on("blur", function () {
  68. /* Since the window isn't focused, blur
  69. it here to style it if we click outside.
  70. Note that the window will focus if we click
  71. in it as that would trigger after this blur */
  72. $window.trigger("blur");
  73. });
  74. return $wrapper;
  75. }
  76. function initInfoWindow(wrapperID) {
  77. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"info\"]");
  78. let $window = $wrapper.children(".window");
  79. let $infoTable = $window.find(".info-table");
  80. let desktopDBItem = findDesktopDBItem(g_DesktopDB, "id", wrapperID);
  81. let icon = desktopDBItem.icon ? desktopDBItem.icon : desktopDBItem.kind;
  82. if (!desktopDBItem) return;
  83. /* Set the window title */
  84. $window.find(".header > .title").html(desktopDBItem.name + " Info");
  85. $window.find("img[data-info-key='icon']").attr("src", SETTINGS_ICONS_LARGE_PATH + icon + ".png");
  86. $window.find("input[data-info-key='name']").attr("value", desktopDBItem.name);
  87. /* Build info window table with figure-specific properties */
  88. switch (desktopDBItem.kind) {
  89. case "disk":
  90. $infoTable.load("window.info.table.disk.html", function () {
  91. $infoTable.find("td[data-info-key='format']").html(desktopDBItem.format);
  92. $infoTable.find("td[data-info-key='capacity']").html(desktopDBItem.capacity);
  93. $infoTable.find("td[data-info-key='available']").html(desktopDBItem.available);
  94. $infoTable.find("td[data-info-key='used']").html(desktopDBItem.used);
  95. updateInfoTableGeneralProperties($infoTable, desktopDBItem);
  96. });
  97. break;
  98. case "folder":
  99. $infoTable.load("window.info.table.folder.html", function () {
  100. $infoTable.find("td[data-info-key='size']").html(formatInfoTableFileSize(desktopDBItem.size) + ", for " + getDesktopDBItemFileCount(desktopDBItem) + " items");
  101. updateInfoTableGeneralProperties($infoTable, desktopDBItem);
  102. });
  103. break;
  104. case "application":
  105. case "download":
  106. case "document":
  107. case "video":
  108. case "picture":
  109. $infoTable.load("window.info.table.file.html", function () {
  110. $infoTable.find("td[data-info-key='size']").html(formatInfoTableFileSize(desktopDBItem.size));
  111. $infoTable.find("td[data-info-key='version']").html(desktopDBItem.version);
  112. updateInfoTableGeneralProperties($infoTable, desktopDBItem);
  113. });
  114. break;
  115. case "link":
  116. $infoTable.load("window.info.table.link.html", function () {
  117. $infoTable.find("td[data-info-key='url']").html(desktopDBItem.url);
  118. updateInfoTableGeneralProperties($infoTable, desktopDBItem);
  119. });
  120. break;
  121. case "trash":
  122. $infoTable.load("window.info.table.trash.html", function () {
  123. $infoTable.find("td[data-info-key='contents']").html(desktopDBItem.infoContents);
  124. updateInfoTableGeneralProperties($infoTable, desktopDBItem);
  125. });
  126. break;
  127. default:
  128. break;
  129. }
  130. return $wrapper;
  131. }
  132. function formatInfoTableFileSize(bytes) {
  133. if (bytes === undefined) return;
  134. let formattedBytes = formatBytes(bytes);
  135. return formattedBytes + " on disk (" + bytes.toLocaleString() + " bytes)"
  136. }
  137. function updateInfoTableGeneralProperties($infoTable, desktopDBItem) {
  138. $infoTable.find("td[data-info-key='kind']").html(desktopDBItem.kind);
  139. $infoTable.find("td[data-info-key='where']").html(desktopDBItem.where);
  140. $infoTable.find("td[data-info-key='created']").html(desktopDBItem.created);
  141. $infoTable.find("td[data-info-key='modified']").html(desktopDBItem.modified);
  142. $infoTable.find("td[data-info-key='label']").html(desktopDBItem.label);
  143. $infoTable.find("textarea[data-info-key='comments']").html(desktopDBItem.comments);
  144. }
  145. function initAboutWindow(wrapperID) {
  146. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"about\"]");
  147. let $window = $wrapper.children(".window");
  148. /* Set the window title */
  149. $window.find(".header > .title").html("About This Website");
  150. $.getJSON("json/greets.json").then(function (greetsJSON) {
  151. let $greetsList = $window.find(".about-greets ul");
  152. greetsJSON.forEach(function (greet) {
  153. $greetsList.append("<li>" + greet.name + "</li>");
  154. });
  155. /* Initialize jScrollPane */
  156. let jsp = $wrapper.find(".about-greets").jScrollPane({
  157. showArrows: true,
  158. alwaysShowHScroll: false,
  159. alwaysShowVScroll: true,
  160. verticalArrowPositions: "after",
  161. horizontalArrowPositions: "after",
  162. clickOnTrack: true,
  163. verticalDragMinHeight: 16
  164. });
  165. jsp.data("jsp").reinitialise();
  166. });
  167. return $wrapper;
  168. }
  169. function initDownloadWindow(wrapperID) {
  170. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"download\"]");
  171. let $window = $wrapper.children(".window");
  172. /* Set the window title */
  173. $window.find(".header > .title").html("Download to \"Your Computer\"");
  174. return $wrapper;
  175. }
  176. function initSimpleTextWindow(wrapperID) {
  177. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"simpletext\"]");
  178. let $window = $wrapper.children(".window");
  179. /* Set the window title */
  180. $window.find(".header > .title").html("SimpleText");
  181. return $wrapper;
  182. }
  183. function initPictureViewerWindow(wrapperID) {
  184. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"pictureviewer\"]");
  185. let $window = $wrapper.children(".window");
  186. /* Set the window title */
  187. $window.find(".header > .title").html("PictureViewer");
  188. return $wrapper;
  189. }
  190. function initZTermWindow(wrapperID) {
  191. let $wrapper = $(".window-wrapper[data-id=\"" + wrapperID + "\"][data-type=\"zterm\"]");
  192. let $window = $wrapper.children(".window");
  193. /* Set the window title */
  194. $window.find(".header > .title").html("ZTerm");
  195. return $wrapper;
  196. }
  197. function initBaseWindow($wrapper) {
  198. let $window = $wrapper.children(".window");
  199. /* Make the window focusable */
  200. $window.attr("tabindex", -1);
  201. /* Make the window draggable */
  202. $wrapper.draggable({
  203. handle: ".draggable",
  204. containment: $(".desktop")
  205. });
  206. /* Remove controls if defined */
  207. $(".window > .header.no-close-box > .close-box").remove();
  208. $(".window > .header.no-zoom-box > .zoom-box").remove();
  209. $(".window > .header.no-windowshade-box > .windowshade-box").remove();
  210. /* Padding in the header is necessary to center the title in cases where controls are hidden*/
  211. $window.find(".header > .zoom-box").siblings(".header-lines-wrapper.left").find(".header-lines.center").css("padding-left", "16px");
  212. $window.find(".header.no-close-box.no-zoom-box > .windowshade-box").siblings(".header-lines-wrapper.left").find(".header-lines.center").css("padding-left", "17px");
  213. $window.on("focusin", function () {
  214. let $this = $(this);
  215. let wrapperID = $wrapper.attr("data-id");
  216. let $figure = $("figure[data-id=\"" + wrapperID + "\"]");
  217. $this.find(".control-box, .header-lines").show();
  218. $this.find(".resize-handle").addClass("enabled");
  219. $this.removeClass("blur");
  220. $this.addClass("focus");
  221. $this.children(".header").removeClass("draggable");
  222. /* Enable scrolling */
  223. $this.find(".jspTrack").removeClass("disabled");
  224. $this.find(".jspArrow ").show();
  225. $this.find(".jspDrag").show();
  226. /* The window should have the highest z-index */
  227. $wrapper.css("z-index", ++g_highestZIndex);
  228. if ($figure.hasClass("folder") && !$this.find(".grid:focus-within").length) {
  229. $figure.addClass("focus");
  230. }
  231. /* Set the active application menu */
  232. if ($figure.hasClass("application")) {
  233. setActiveApplication($figure.find(".label").html());
  234. }
  235. });
  236. $window.find("input, textarea").on("blur", function () {
  237. $window.trigger("blur");
  238. });
  239. $window.on("focusout", function () {
  240. let $this = $(this);
  241. let wrapperID = $wrapper.attr("data-id");
  242. let $figure = $("figure[data-id=\"" + wrapperID + "\"]");
  243. let width = $this.css("width");
  244. /* Window children shouldn't steal focus styling from the window */
  245. if ($this.is(":focus-within")) {
  246. /* But if we just selected a figure then blur the
  247. window's parent figure */
  248. if ($this.hasClass("folder")) {
  249. $figure.removeClass("focus");
  250. }
  251. return;
  252. }
  253. $figure.removeClass("focus");
  254. /* Need to set draggable so it doesn't take an extra click
  255. on certain elements */
  256. $this.children(".header").addClass("draggable");
  257. $this.find(".control-box, .header-lines").hide();
  258. $this.find(".resize-handle").removeClass("enabled");
  259. $this.removeClass("focus");
  260. $this.addClass("blur");
  261. /* Disable scrolling */
  262. $this.find(".jspTrack").addClass("disabled");
  263. $this.find(".jspArrow ").hide();
  264. $this.find(".jspDrag").hide();
  265. /* When hiding elements, the size of the window
  266. may change and we want to keep it the same */
  267. $this.css("width", width);
  268. /* Default the active application menu to the Finder */
  269. setActiveApplication("Finder");
  270. });
  271. /* KLUDGE: JQueryUI will not properly handle bubbling
  272. up the focus on draggable elements so we
  273. do it here*/
  274. $window.find(".draggable").on("mousedown", function () {
  275. let $this = $(this);
  276. let $window = $this.parents(".window");
  277. $window.trigger("focus");
  278. });
  279. $window.find(".close-box").on("click", function () {
  280. Actions.closeWindow($wrapper);
  281. });
  282. /* Window shade control */
  283. $window.find(".windowshade-box").on("click", function () {
  284. let width = $window.css("width");
  285. $window.find(".statusbar, .contents").toggle();
  286. /* When hiding elements, the size of the window
  287. may change and we want to keep it the same */
  288. $window.css("width", width);
  289. });
  290. /* Double-click header also invokes window shade */
  291. $window.find(".draggable").on("dblclick", function () {
  292. $(this).siblings(".windowshade-box").trigger("click");
  293. });
  294. /* Expand arrow control */
  295. $window.find(".expand-arrow").on("mousedown", function () {
  296. $(this).addClass("active");
  297. });
  298. $window.find(".expand-arrow").on("mouseup", function () {
  299. let $this = $(this);
  300. $this.removeClass("active");
  301. $this.toggleClass("right");
  302. $this.toggleClass("down");
  303. $window.toggleClass("expanded");
  304. });
  305. }
  306. function initFolderWindowContents($window, desktopDBItemContents) {
  307. let $itemCount = $window.find(".statusbar .statusbar-text .item-count");
  308. $itemCount.html(desktopDBItemContents.length);
  309. /* Create the folder contents listing */
  310. desktopDBItemContents.forEach(function (dbFolderItem) {
  311. let $folderFigureClone = populateClone($window.find("figure.cloneable").clone(), dbFolderItem);
  312. /* Insert the file */
  313. $folderFigureClone.appendTo($window.find(".grid"));
  314. });
  315. /* Remove the cloneable figure */
  316. $("figure.cloneable").remove();
  317. }