index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //Photo Station Main Control Javascript for index.php
  2. var VDI = ao_module_virtualDesktop;
  3. var pwa = $("#DATA_PIPELINE_pwa").text().trim() == "true";
  4. if (VDI){
  5. //Inside VDI mode, init window properties
  6. $("#topMenu").hide();
  7. ao_module_setWindowIcon("photo");
  8. ao_module_setWindowTitle("ArOZ Photo");
  9. ao_module_setGlassEffectMode();
  10. }else if (pwa){
  11. $("#topMenu").hide();
  12. $("#contentFrame").css("padding","3px");
  13. }
  14. //Injecting variable from PHP using DOM event
  15. var folder_path = $("#DATA_PIPELINE_folder_path").text().trim();
  16. var search_keyword = $("#DATA_PIPELINE_search_keyword").text().trim();
  17. var sort_mode = $("#DATA_PIPELINE_sort_mode").text().trim();
  18. var path2name = JSON.parse($("#DATA_PIPELINE_path2name").text().trim());
  19. //Global variables
  20. var downloadOn = false;
  21. var previewmode = false;
  22. var screenWidth = 0;
  23. var screenHeight = 0;
  24. var PreviewWidth = 0;
  25. var PreviewHeight = 0;
  26. var resizecount = 0;
  27. var currentViewingPath = "";
  28. /////////////////
  29. $(document).ready(function() {
  30. //If there are search result, put it into the search bar
  31. if (search_keyword != ""){
  32. $('#searchbar').val(search_keyword);
  33. }
  34. //Update the folder path shown on file explorer (?
  35. if (folder_path != ""){
  36. $('#folderdir').html(ao_module_codec.decodeHexFoldername(folder_path));
  37. }else{
  38. $('#folderdir').html('Unsorted');
  39. }
  40. //Update Sort Mode
  41. if (sort_mode == ""){
  42. $("#sort1").attr('class', 'ts button active');
  43. $("#sort2").attr('class', 'ts button');
  44. }else if (sort_mode == "reverse"){
  45. $("#sort1").attr('class', 'ts button');
  46. $("#sort2").attr('class', 'ts button active');
  47. }
  48. //Hide the notification bar
  49. $('#nbar').hide();
  50. //Get Preview Image Size
  51. var img = document.getElementById('previewingImage');
  52. PreviewWidth = img.clientWidth;
  53. if (PreviewWidth < $('#previewImageDiv').width()){
  54. PreviewWidth = $('#previewImageDiv').width();
  55. }
  56. //Get Screen Width
  57. screenWidth = $(window).width();
  58. screenHeight = $(window).height();
  59. centerPreview(screenWidth,PreviewWidth);
  60. //console.log(path2name);
  61. });
  62. ts('.ts.dropdown:not(.basic)').dropdown();
  63. function uploadImage(){
  64. if (pwa){
  65. window.open("../Upload Manager/upload_interface.php?target=Photo&filetype=jpg,png,jpeg,gif&finishing=Image_manager.php");
  66. }else{
  67. window.location.href = "../Upload Manager/upload_interface.php?target=Photo&filetype=jpg,png,jpeg,gif&finishing=Image_manager.php";
  68. }
  69. }
  70. function openImageManager(){
  71. if (pwa){
  72. window.open("Image_manager.php");
  73. }else{
  74. window.location.href = "Image_manager.php";
  75. }
  76. }
  77. $(window).resize(function() {
  78. var img = document.getElementById('previewingImage');
  79. PreviewWidth = img.clientWidth;
  80. if (PreviewWidth < $('#previewImageDiv').width()){
  81. PreviewWidth = $('#previewImageDiv').width();
  82. }
  83. PreviewHeight = img.clientHeight;
  84. screenWidth = $(window).width();
  85. screenHeight = $(window).height();
  86. setTimeout(resizeImage, 100);
  87. console.log(PreviewWidth,screenWidth);
  88. console.log(PreviewHeight,screenHeight);
  89. });
  90. //Share this page
  91. function shareThis(){
  92. //alert(window.location.href);
  93. var currentHref = window.location.href;
  94. if (pwa){
  95. window.open("../QuickSend/index.php?share=" + currentHref);
  96. }else{
  97. window.location.href = "../QuickSend/index.php?share=" + currentHref;
  98. }
  99. }
  100. //Handle document keypress events
  101. $(document).keydown(function(e) {
  102. if (e.keyCode == 37){
  103. //Previous Image
  104. if (previewmode == true){
  105. //It is currently previewing and user want to switch to previous image
  106. var viewingCount = GetPosInFileList(currentViewingPath);
  107. TogglePreview(0);
  108. var Path = path2name[viewingCount-1][0];
  109. if (Path != "undefined"){
  110. TogglePreview(path2name[viewingCount-1][0]);
  111. }
  112. }
  113. }
  114. if(e.keyCode == 39){
  115. //Next Image
  116. if (previewmode == true){
  117. //It is currently previewing and user want to switch to next image
  118. var viewingCount = GetPosInFileList(currentViewingPath);
  119. TogglePreview(0);
  120. if (Path != "undefined"){
  121. TogglePreview(path2name[viewingCount+1][0]);
  122. }
  123. }
  124. }
  125. });
  126. function GetPosInFileList(path){
  127. var files = path2name;
  128. for(var i = 0; i < files.length;i++){
  129. if (files[i][0] == path){
  130. return i;
  131. }
  132. }
  133. return -1;
  134. }
  135. //Load Image to Preview
  136. function TogglePreview(imagepath){
  137. if (downloadOn == false){
  138. var img = document.getElementById('previewingImage');
  139. if (previewmode == false && imagepath != 0){
  140. currentViewingPath = imagepath;
  141. //Amazing hack to make the auto resize work :)
  142. setTimeout(resizeImage, 100);
  143. //Open Preview Window
  144. $("#previewingImage").attr('class', 'ts massive image');
  145. $('#previewingImage').attr("src", imagepath);
  146. $('#imagePreview').fadeIn('fast');
  147. var ext = imagepath.split(".").pop();
  148. $('#previewImagetxt').html('  <i class="image icon"></i>' + ext + '    <i class="folder open icon"></i>/' + imagepath + '    <i class="external icon"></i><a href="' + imagepath +'" target="_blank">View Raw</a>  <br><p></p>');
  149. previewmode = true;
  150. }else{
  151. //Closing preview mode
  152. $('#imagePreview').fadeOut('fast');
  153. previewmode = false;
  154. }
  155. }else{
  156. //Download Mode is On
  157. var orgname = GetFileName(imagepath)
  158. saveAs(imagepath,orgname.split(".").shift());
  159. }
  160. }
  161. //Save as specific filename
  162. function saveAs(uri, filename) {
  163. var link = document.createElement('a');
  164. if (typeof link.download === 'string') {
  165. document.body.appendChild(link); // Firefox requires the link to be in the body
  166. link.download = filename;
  167. link.href = uri;
  168. link.click();
  169. document.body.removeChild(link); // remove the link when done
  170. } else {
  171. location.replace(uri);
  172. }
  173. }
  174. //Get the real filename from the PHP JSON
  175. function GetFileName(filepath){
  176. for(var k = 0; k < path2name.length; k++){
  177. if(path2name[k][0] == filepath ){
  178. return path2name[k][1];
  179. }
  180. }
  181. }
  182. //Auto resize handler
  183. function resizeImage() {
  184. var img = document.getElementById('previewingImage');
  185. PreviewWidth = img.clientWidth;
  186. if (PreviewWidth < $('#previewImageDiv').width()){
  187. PreviewWidth = $('#previewImageDiv').width();
  188. }
  189. PreviewHeight = img.clientHeight;
  190. screenWidth = $(window).width();
  191. screenHeight = $(window).height();
  192. centerPreview(screenWidth,PreviewWidth);
  193. resizecount += 1;
  194. if (resizecount < 10){
  195. setTimeout(resizeImage, 50);
  196. }else{
  197. resizecount = 0;
  198. if (PreviewHeight > screenHeight - 150){
  199. //Change it to a smaller class
  200. console.log('Adjusting Image Size');
  201. $("#previewingImage").attr('class', 'ts large image');
  202. setTimeout(resizeImageTiny, 100);
  203. }
  204. }
  205. }
  206. function resizeImageTiny() {
  207. var img = document.getElementById('previewingImage');
  208. PreviewWidth = img.clientWidth;
  209. if (PreviewWidth < $('#previewImageDiv').width()){
  210. PreviewWidth = $('#previewImageDiv').width();
  211. }
  212. PreviewHeight = img.clientHeight;
  213. screenWidth = $(window).width();
  214. screenHeight = $(window).height();
  215. centerPreview(screenWidth,PreviewWidth);
  216. resizecount += 1;
  217. if (resizecount < 10){
  218. setTimeout(resizeImageTiny, 50);
  219. }else{
  220. resizecount = 0;
  221. if (PreviewHeight > screenHeight){
  222. //Change it to a smaller class
  223. console.log('Adjusting Image Size');
  224. $("#previewingImage").attr('class', 'ts medium image');
  225. //setTimeout(SingleImageUpdate, 100);
  226. }
  227. }
  228. }
  229. function SingleImageUpdate(){
  230. var img = document.getElementById('previewingImage');
  231. PreviewWidth = img.clientWidth;
  232. if (PreviewWidth < $('#previewImageDiv').width()){
  233. PreviewWidth = $('#previewImageDiv').width();
  234. }
  235. PreviewHeight = img.clientHeight;
  236. screenWidth = $(window).width();
  237. screenHeight = $(window).height();
  238. centerPreview(screenWidth,PreviewWidth);
  239. }
  240. //Move Preview Image from Left
  241. function centerPreview(sw,iw){
  242. var offsets = Math.round(sw / 2 - iw / 2);
  243. $("#previewImageDiv").css("left", offsets + "px");
  244. }
  245. //Download Mode
  246. function downloadmode(){
  247. if (downloadOn == false){
  248. //Turn download mode on
  249. $('#nbartxt').html('Download Mode Enabled.');
  250. $('#nbar').stop().fadeIn('slow').delay(2000).fadeOut('slow');
  251. downloadOn = true;
  252. $("#dlbtn").attr('class', 'ts button active');
  253. }else{
  254. //Turn off download mode
  255. $('#nbartxt').html('Download Mode Disabled.');
  256. $('#nbar').stop().fadeIn('slow').delay(2000).fadeOut('slow');
  257. $("#dlbtn").attr('class', 'ts button');
  258. downloadOn = false;
  259. }
  260. }
  261. //Switch sorting mode
  262. function changeSortMethod(id){
  263. var cmd = "";
  264. if (folder_path != ""){
  265. cmd += "?folder=" + folder_path;
  266. }
  267. if (search_keyword != ""){
  268. if (cmd == ""){
  269. cmd += "?search=" + search_keyword;
  270. }else{
  271. cmd += "&search=" + search_keyword;
  272. }
  273. }
  274. if (pwa){
  275. if (cmd.includes("?") == false){
  276. cmd += "?pwa=enabled";
  277. }else{
  278. cmd += "&pwa=enabled";
  279. }
  280. }
  281. if (id == 1){
  282. //Change to sort mode
  283. window.location = "index.php" + cmd;
  284. }else if (id == 2){
  285. //Change to rsort mode
  286. var pwaSupport = "";
  287. if (pwa && window.location.href.includes("pwa") == false){
  288. pwaSupport = "&pwa=enabled";
  289. }
  290. if (cmd == ""){
  291. window.location = "index.php?sort=reverse" + pwaSupport;
  292. }else{
  293. window.location = "index.php" + cmd + "&sort=reverse" + pwaSupport;
  294. }
  295. }
  296. }
  297. //Switch viewing folder
  298. function changeFolderView(folder_name){
  299. if (folder_name != 0){
  300. if (pwa){
  301. window.location = "index.php?folder=" + folder_name + "&pwa=enabled";
  302. }else{
  303. window.location = "index.php?folder=" + folder_name;
  304. }
  305. }else{
  306. if (pwa){
  307. window.location = "index.php?pwa=enabled";
  308. }else{
  309. window.location = "index.php";
  310. }
  311. }
  312. }
  313. //On Search Enter pressed
  314. $('#searchbar').on('keydown', function(e) {
  315. if (e.which == 13) {
  316. e.preventDefault();
  317. if (folder_path != ""){
  318. window.location = "index.php?folder=" + folder_path + "&search=" + $('#searchbar').val();
  319. }else{
  320. window.location = "index.php?search=" + $('#searchbar').val();
  321. }
  322. }
  323. });