Example #1
0
        /// <summary>
        /// Generates html for the gallery
        /// </summary>
        /// <param name="folder">Folder to read the pictures from</param>
        /// <param name="pageNumber">Page starting from 1</param>
        /// <param name="pageSize">Number of pictures on each page</param>
        /// <returns></returns>
        public async Task<string> GenerateGallery(StorageFolder folder, int pageNumber, int pageSize)
        {
            // Don't allow negatives
            if (pageNumber < 1) pageNumber = 1;
            if (pageSize < 1) pageSize = 1;

            var subFolders = await folder.GetFoldersAsync();
            var parentFolder = await folder.GetParentAsync();

            // JavaScript code for toggling the subfolder list
            string html = "";
            html += "<script type='text/javascript'>" +
                "function toggleSubfolderList(){"+
                "var folderNavPane = document.getElementById('folder_nav_pane');" +
                "if(folderNavPane.style.display == 'block') folderNavPane.style.display = 'none';" +
                "else folderNavPane.style.display = 'block'" +
                "}" +
                "</script>";

            html += "<table>";
            html += "<tr><td>";
            // Create breadcrumbs for folder nav
            var temp = folder;
            string breadcrumbs = "<b>"+ ((subFolders.Count > 0) ? "<a onclick='toggleSubfolderList()' href='javascript:void(0);'>" + temp.Name + "</a>" : temp.Name) + "</b>";
            while(!temp.Path.Equals(rootFolder.Path, StringComparison.OrdinalIgnoreCase))
            {
                temp = await temp.GetParentAsync();
                string hyperlink = MakeHyperlink(temp.Name, "/gallery.htm?folder=" + WebUtility.UrlEncode(temp.Path), false);
                breadcrumbs = hyperlink + " > " + breadcrumbs;
            }
            html += breadcrumbs + "<br>";

            if (subFolders.Count > 0)
            {
                // Generate subfolder navigation pane
                html += "<div id='folder_nav_pane' style='display:none'>";
                html += "<ul>";
                foreach (StorageFolder subFolder in subFolders)
                {
                    html += "<li><a href='/gallery.htm?folder=" + WebUtility.UrlEncode(subFolder.Path) + "'>" + subFolder.Name + "</a></li>";
                }
                html += "</ul></div>";
            }

            html += "<br></td></tr>";

            // Get the files in current folder and subfolders
            var queryOptions = new QueryOptions();
            queryOptions.FolderDepth = FolderDepth.Deep;

            var results = folder.CreateFileQueryWithOptions(queryOptions);
            
            StorageFile[] sortedFiles = null;

            // Use cached files if we already got the files and we're navigating to the first page
            if (cachedFiles != null && pageNumber != 1)
            {
                sortedFiles = cachedFiles;
            }
            else
            {
                var files = await results.GetFilesAsync();
                sortedFiles = files.OrderByDescending((x) => x.DateCreated).ToArray();
                cachedFiles = sortedFiles;
            }

            if (sortedFiles.Length > 0)
            {
                // Create pages
                string pagesHtml = "<form>";
                html += "<tr><td>";
                int totalPages = (int)Math.Ceiling((double)sortedFiles.Length / pageSize);
                pagesHtml += "Pages: ";

                pagesHtml += "<select name='page' onchange='this.form.submit()'>";

                for (int i = 1; i <= totalPages; i++)
                {
                    pagesHtml += "<option value='" + i + "' " + ((i == pageNumber) ? "selected='selected'" : "") + ">" + i + "</option>";
                }
                pagesHtml += "</select>";
                pagesHtml += "<input type='hidden' name='folder' value='" + folder.Path + "' />";
                pagesHtml += "<input type='hidden' name='pageSize' value='30' />";
                pagesHtml += "</form>";

                html += pagesHtml;
                html += "<br></td></tr>";

                html += "<tr><td>";

                // Pick out the subset of files we need based on page
                int startIndex = (pageNumber - 1) * pageSize;
                for (int i = startIndex; i < startIndex + pageSize; i++)
                {
                    if (i < sortedFiles.Length)
                    {
                        StorageFile file = sortedFiles[i];
                        html += "<div class='img'>";
                        html += "<a target='_blank' href='/api/gallery/" + WebUtility.UrlEncode(file.Path) + "'>";
                        html += "<img src='/api/gallery/" + WebUtility.UrlEncode(file.Path) + "' alt='" + file.Name + "' width='190'>";
                        html += "<div class='desc'><b>File Name:</b> " + file.Name + "<br><b>Date Created:</b> " + file.DateCreated + "</div>";
                        html += "</a>";
                        html += "</div>";
                    }
                }

                html += "</td></tr>";

                // Create pages
                html += "<tr><td>";
                html += "<br>" + pagesHtml;
                html += "</td></tr>";
            }
            else
            {
                html += "No pictures found in " + folder.Path;
            }

            html += "</table>";

            return html;
        }