<%@ Page Title="Queue Reordering" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %> 
<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">Queue Reordering - AjaxUploader v5.2 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">Queue Reordering</asp:Content> 
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 
<div class="demo-card"> <div class="demo-card-header">Drag-to-Reorder Queue</div> <div class="demo-card-body"> <p class="demo-description"> Reorder queued files before uploading begins. Use the up/down buttons to change the upload
 order. Files are uploaded sequentially in the displayed order.
 </p> <div class="demo-expect"><strong>What you should see:</strong> drag rows to reorder before starting - auto-upload is off and concurrency is 1, so the order you set is exactly the order used.</div> <div id="uploader"></div> <div class="mt-2"> <button type="button" class="btn btn-primary btn-sm" id="btnStart" disabled>Start Upload</button> </div> <div id="queueList" class="mt-3"></div> <div class="code-panel"> <pre><code>var uploader = AjaxUploader.create('#uploader', {
 uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/upload") %>',
 multiple: true,
 autoUpload: false, // manual start after reordering
 concurrency: 1,
 onSelect: function(files) {
 renderQueue();
 }
});

// Reorder with uploader.moveTask(taskId, newIndex)
// Then start: uploader.startUpload();</code></pre> </div> </div>
</div> 
</asp:Content> 
<asp:Content ID="ScriptsContent" ContentPlaceHolderID="ScriptsContent" runat="server">
<script> var queueList = document.getElementById('queueList');
 var btnStart = document.getElementById('btnStart');
 var taskList = [];

 var host = document.getElementById('uploader');
 var uploader = host && window.AjaxUploader && typeof AjaxUploader.create === 'function' ? AjaxUploader.create(host, {
 uploadUrl: '<%= ResolveUrl("~/ajaxupload.axd/upload") %>',
 multiple: true,
 autoUpload: false,
 concurrency: 1,
 onSelect: function(files) {
 files.forEach(function(f) {
 taskList.push({ id: f.id || taskList.length, name: f.name, size: f.size });
 });
 if (btnStart) btnStart.disabled = false;
 renderQueue();
 },
 onQueueRender: function(queue) {
 renderQueue();
 },
 onComplete: function() {
 if (btnStart) btnStart.disabled = true;
 }
 }) : null;

 function renderQueue() {
 if (!queueList) return;
 queueList.textContent = '';
 taskList.forEach(function(item, i) {
 var div = document.createElement('div');
 div.className = 'p-2 mb-1 border rounded d-flex justify-content-between align-items-center';
 var info = document.createElement('span');
 info.appendChild(document.createTextNode((i + 1) + '. ' + (item.name || 'File') + ' '));
 var size = document.createElement('small');
 size.className = 'text-muted';
 size.textContent = '(' + AjaxUploader.formatSize(item.size) + ')';
 info.appendChild(size);
 var controls = document.createElement('span');
 var up = document.createElement('button');
 up.type = 'button';
 up.className = 'btn btn-outline-secondary btn-sm me-1';
 up.dataset.move = 'up';
 up.dataset.index = i;
 up.textContent = '^';
 var down = document.createElement('button');
 down.type = 'button';
 down.className = 'btn btn-outline-secondary btn-sm';
 down.dataset.move = 'down';
 down.dataset.index = i;
 down.textContent = 'v';
 controls.appendChild(up);
 controls.appendChild(down);
 div.appendChild(info);
 div.appendChild(controls);
 queueList.appendChild(div);
 });
 }

 function moveUp(index) {
 if (index <= 0) return;
 var temp = taskList[index];
 taskList[index] = taskList[index - 1];
 taskList[index - 1] = temp;
 renderQueue();
 }

 function moveDown(index) {
 if (index >= taskList.length - 1) return;
 var temp = taskList[index];
 taskList[index] = taskList[index + 1];
 taskList[index + 1] = temp;
 renderQueue();
 }

 if (queueList) queueList.addEventListener('click', function(event) {
 var button = event.target.closest('button[data-move]');
 if (!button) return;
 var index = Number(button.dataset.index);
 if (button.dataset.move === 'up') moveUp(index);
 else moveDown(index);
 });

 if (btnStart) btnStart.addEventListener('click', function() {
 if (uploader && typeof uploader.startUpload === 'function') uploader.startUpload();
 btnStart.disabled = true;
 });
</script>
</asp:Content>
