Procházet zdrojové kódy

Added photo swap for photo embedded viewer

TC pushbot 5 před 4 roky
rodič
revize
a8372dde48

+ 104 - 19
web/Photo/embedded.html

@@ -2,46 +2,131 @@
 <meta name="apple-mobile-web-app-capable" content="yes" />
 <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1" />
 <html>
-
 <head>
     <meta charset="UTF-8">
     <meta name="theme-color" content="#4b75ff">
-    <!-- <link rel="stylesheet" href="../script/tocas/tocas.css"> -->
-    <script src="../script/tocas/tocas.js"></script>
+    <title>Photo Preview</title>
     <script src="../script/jquery.min.js"></script>
     <script src="../script/ao_module.js"></script>
     <link rel="manifest" href="manifest.json">
+    <style>
+        .arrow{
+            width: 2em;
+            opacity: 0.5;
+            position: fixed;
+            top: calc(50% - 1em);
+            cursor: pointer;
+        }
 
-</head>
+        .left.arrow{
+            left: 2em;
+        }
 
+        .right.arrow{
+            right: 2em;
+        }
+    </style>
+</head>
 <body style="background:rgba(34,34,34,1);overflow:hidden;">
     <img id="img" style="max-height: 100vh;max-width: 100vw;">
+    <img class="left arrow" onclick="previousImage();" src="embedded/arrow-left.svg">
+    <img class="right arrow" onclick="nextImage();" src="embedded/arrow-right.svg">
     <script>
         //Get file playback info from hash
         var playbackFile = ao_module_loadInputFiles();
+        var nearbyFileList = [];
+        var currentViewingIndex = 0;
         //Only handle one file
         playbackFile = playbackFile[0];
-
-        //Update title name
-        ao_module_setWindowTitle("Photo - " + playbackFile.filename);
-        setTimeout(function() {
-            updateImgSize();
-        }, 500);
-        //Setup img src
-        $("#img").attr("src", '../media?file=' + encodeURIComponent(playbackFile.filepath))
-
-        //realigin to center
-        $('img').on('load', function() {
-            updateImgSize();
-        });
+        loadImage(playbackFile.filename, playbackFile.filepath);
+        
         $(window).on("resize ", function() {
             updateImgSize();
         });
 
+        //Load the nearby image files and allow swapping using <- and -> key
+        function loadNearbyFiles(filepath){
+            ao_module_agirun("Photo/embedded/listNearbyImage.js", {
+                path: filepath
+            }, function(data){
+                if (data.error != undefined){
+                    alert(data.error);
+                }else{
+                    nearbyFileList = data;
+
+                    //Track which index currently the user is viewing
+                    for (var i = 0; i < nearbyFileList.length; i++){
+                        var thisPath = nearbyFileList[i];
+                        if (thisPath == filepath.split("\\").join("/")){
+                            currentViewingIndex = i;
+                        }
+                    }
+                }
+            })
+        }
+
+        function nextImage(){
+            nextPhoto = currentViewingIndex + 1;
+            if (nextPhoto > nearbyFileList.length - 1){
+                nextPhoto = nearbyFileList.length - 1;
+            }
+
+            var filepath = nearbyFileList[nextPhoto];
+            var filename = filepath.split('/').pop();
+            if (nextPhoto != currentViewingIndex){
+                //Change in photo index
+                loadImage(filename, filepath);
+                 currentViewingIndex = nextPhoto;
+            }
+        }
+
+        function previousImage(){
+            nextPhoto = currentViewingIndex - 1;
+            if (nextPhoto < 0){
+                nextPhoto = 0;
+            }
+
+            var filepath = nearbyFileList[nextPhoto];
+            var filename = filepath.split('/').pop();
+            if (nextPhoto != currentViewingIndex){
+                //Change in photo index
+                loadImage(filename, filepath);
+                 currentViewingIndex = nextPhoto;
+            }
+        }
+
+        //Bind arrow key events
+        $("body").on("keydown", function(e){
+            var nextPhoto = currentViewingIndex;
+            if (e.keyCode == 37){
+                //<-
+                previousImage();
+            }else if (e.keyCode == 39){
+                //->
+                nextImage();
+            }else{
+                //Invalid keycode to operate
+                return;
+            }
+        })
+
+        loadNearbyFiles(playbackFile.filepath);
+
+        function loadImage(filename, filepath){
+            $("#img").hide();
+            ao_module_setWindowTitle("Photo - " + filename);
+            $("#img").attr("src", '../media?file=' + encodeURIComponent(filepath))
+            
+            //realigin to center
+            $('#img').on('load', function() {
+                updateImgSize();
+                $("#img").show();
+            });
+        }
 
         function updateImgSize() {
-            $('#img').css("margin-top", (window.innerHeight - $("#img").height()) / 2);
-            $('#img').css("margin-left", (window.innerWidth - $("#img").width()) / 2);
+            $('#img').css("margin-top", (window.innerHeight - $("#img").height()) / 2 - 6);
+            $('#img').css("margin-left", (window.innerWidth - $("#img").width()) / 2 - 6);
         }
     </script>
 </body>

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 27 - 0
web/Photo/embedded/arrow-left.ai


+ 8 - 0
web/Photo/embedded/arrow-left.svg

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="128px" height="128px" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve">
+<polygon fill="#FFFFFF" stroke="#231815" stroke-width="3" stroke-miterlimit="10" points="105.518,113.641 20.981,64.833 
+	105.518,16.027 "/>
+</svg>

Rozdílová data souboru nebyla zobrazena, protože soubor je příliš velký
+ 27 - 0
web/Photo/embedded/arrow-right.ai


+ 8 - 0
web/Photo/embedded/arrow-right.svg

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="128px" height="128px" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve">
+<polygon fill="#FFFFFF" stroke="#231815" stroke-width="3" stroke-miterlimit="10" points="20.981,16.026 105.518,64.833 
+	20.981,113.64 "/>
+</svg>

+ 34 - 0
web/Photo/embedded/listNearbyImage.js

@@ -0,0 +1,34 @@
+var loadedfile = requirelib("filelib");
+if (!loadedfile) {
+    console.log("Failed to load lib filelib, terminated.");
+}
+
+function listNearby(){
+    var result = [];
+    //Extract the path from the filepath
+    var dirpath = path.split("\\").join("/");
+    dirpath = dirpath.split("/");
+    dirpath.pop();
+    dirpath = dirpath.join("/");
+
+    //Get nearby files and filter out the one that is web supported photo format
+    var nearbyFiles = filelib.aglob(dirpath + "/*")
+    for (var i = 0; i < nearbyFiles.length; i++){
+        var ext = nearbyFiles[i].split(".").pop();
+        ext = ext.toLowerCase();
+        if (ext == "png" || ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "webp"){
+            result.push(nearbyFiles[i]);
+        }
+    }
+
+    sendJSONResp(JSON.stringify(result))
+}
+
+
+if (typeof(path) == "undefined"){
+    sendJSONResp(JSON.stringify({
+        "error": "Invalid path given"
+    }));
+}else{
+    listNearby();
+}

Některé soubory nejsou zobrazeny, neboť je v těchto rozdílových datech změněno mnoho souborů