File size: 8,286 Bytes
56ea105
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// File Sorting Functionality

let currentSortMethod = localStorage.getItem('sortMethod') || 'date-desc';
let currentDirectoryData = null;

// Initialize sort dropdown
document.addEventListener('DOMContentLoaded', function() {
    const sortBtn = document.getElementById('sort-toggle-btn');
    const sortMenu = document.getElementById('sort-menu');
    
    if (!sortBtn || !sortMenu) return;
    
    // Toggle sort menu
    sortBtn.addEventListener('click', (e) => {
        e.stopPropagation();
        sortMenu.classList.toggle('show');
    });
    
    // Close menu when clicking outside
    document.addEventListener('click', (e) => {
        if (!sortMenu.contains(e.target) && !sortBtn.contains(e.target)) {
            sortMenu.classList.remove('show');
        }
    });
    
    // Handle sort option clicks
    const sortOptions = sortMenu.querySelectorAll('.sort-option');
    sortOptions.forEach(option => {
        // Highlight current sort
        if (option.getAttribute('data-sort') === currentSortMethod) {
            option.classList.add('active');
        }
        
        option.addEventListener('click', (e) => {
            e.stopPropagation();
            
            // Remove active from all
            sortOptions.forEach(opt => opt.classList.remove('active'));
            
            // Add active to clicked
            option.classList.add('active');
            
            // Update sort method
            currentSortMethod = option.getAttribute('data-sort');
            localStorage.setItem('sortMethod', currentSortMethod);
            
            // Re-sort and display
            if (currentDirectoryData) {
                sortAndDisplayDirectory(currentDirectoryData);
            }
            
            // Close menu
            sortMenu.classList.remove('show');
            
            showToast(`Sorted by: ${option.textContent.trim()}`, 'info', 2000);
        });
    });
});

// Enhanced showDirectory function with sorting
function sortAndDisplayDirectory(data) {
    currentDirectoryData = data;
    const contents = data['contents'];
    document.getElementById('directory-data').innerHTML = '';
    const isTrash = getCurrentPath().startsWith('/trash');

    let html = '';
    
    // Separate folders and files
    let entries = Object.entries(contents);
    let folders = entries.filter(([key, value]) => value.type === 'folder');
    let files = entries.filter(([key, value]) => value.type === 'file');

    // Apply sorting
    folders = applySorting(folders);
    files = applySorting(files);

    // Generate HTML for folders
    for (const [key, item] of folders) {
        html += generateFolderHTML(item, isTrash);
    }

    // Generate HTML for files
    for (const [key, item] of files) {
        html += generateFileHTML(item, isTrash);
    }
    
    document.getElementById('directory-data').innerHTML = html;

    // Add event listeners
    if (!isTrash) {
        document.querySelectorAll('.folder-tr').forEach(div => {
            if (isMobileDevice() || isTouchDevice()) {
                // Single tap for mobile
                div.onclick = openFolder;
            } else {
                // Double click for desktop
                div.ondblclick = openFolder;
            }
        });
        
        document.querySelectorAll('.file-tr').forEach(div => {
            if (isMobileDevice() || isTouchDevice()) {
                // Single tap for mobile
                div.onclick = openFile;
            } else {
                // Double click for desktop
                div.ondblclick = openFile;
            }
        });
    }

    document.querySelectorAll('.more-btn').forEach(div => {
        div.addEventListener('click', function (event) {
            event.preventDefault();
            event.stopPropagation();
            openMoreButton(div);
        });
    });
}

function applySorting(items) {
    const sortMethod = currentSortMethod;
    
    switch(sortMethod) {
        case 'date-desc': // Newest first (default)
            return items.sort((a, b) => new Date(b[1].upload_date) - new Date(a[1].upload_date));
        
        case 'date-asc': // Oldest first
            return items.sort((a, b) => new Date(a[1].upload_date) - new Date(b[1].upload_date));
        
        case 'name-asc': // A to Z
            return items.sort((a, b) => a[1].name.localeCompare(b[1].name));
        
        case 'name-desc': // Z to A
            return items.sort((a, b) => b[1].name.localeCompare(a[1].name));
        
        case 'size-desc': // Largest first
            return items.sort((a, b) => (b[1].size || 0) - (a[1].size || 0));
        
        case 'size-asc': // Smallest first
            return items.sort((a, b) => (a[1].size || 0) - (b[1].size || 0));
        
        default:
            return items.sort((a, b) => new Date(b[1].upload_date) - new Date(a[1].upload_date));
    }
}

function generateFolderHTML(item, isTrash) {
    let html = `<tr data-path="${item.path}" data-id="${item.id}" class="body-tr folder-tr">
        <td><div class="td-align"><img src="static/assets/folder-solid-icon.svg">${item.name}</div></td>
        <td><div class="td-align"></div></td>
        <td><div class="td-align"><a data-id="${item.id}" class="more-btn"><img src="static/assets/more-icon.svg" class="rotate-90"></a></div></td>
    </tr>`;

    if (isTrash) {
        html += `<div data-path="${item.path}" id="more-option-${item.id}" data-name="${item.name}" class="more-options">
            <input class="more-options-focus" readonly="readonly" style="height:0;width:0;border:none;position:absolute">
            <div id="restore-${item.id}" data-path="${item.path}"><img src="static/assets/load-icon.svg"> Restore</div>
            <hr>
            <div id="delete-${item.id}" data-path="${item.path}"><img src="static/assets/trash-icon.svg"> Delete</div>
        </div>`;
    } else {
        html += `<div data-path="${item.path}" id="more-option-${item.id}" data-name="${item.name}" class="more-options">
            <input class="more-options-focus" readonly="readonly" style="height:0;width:0;border:none;position:absolute">
            <div id="rename-${item.id}"><img src="static/assets/pencil-icon.svg"> Rename</div>
            <hr>
            <div id="trash-${item.id}"><img src="static/assets/trash-icon.svg"> Trash</div>
            <hr>
            <div id="folder-share-${item.id}"><img src="static/assets/share-icon.svg"> Share</div>
        </div>`;
    }
    
    return html;
}

function generateFileHTML(item, isTrash) {
    const size = convertBytes(item.size);
    let html = `<tr data-path="${item.path}" data-id="${item.id}" data-name="${item.name}" class="body-tr file-tr">
        <td><div class="td-align"><img src="static/assets/file-icon.svg">${item.name}</div></td>
        <td><div class="td-align">${size}</div></td>
        <td><div class="td-align"><a data-id="${item.id}" class="more-btn"><img src="static/assets/more-icon.svg" class="rotate-90"></a></div></td>
    </tr>`;

    if (isTrash) {
        html += `<div data-path="${item.path}" id="more-option-${item.id}" data-name="${item.name}" class="more-options">
            <input class="more-options-focus" readonly="readonly" style="height:0;width:0;border:none;position:absolute">
            <div id="restore-${item.id}" data-path="${item.path}"><img src="static/assets/load-icon.svg"> Restore</div>
            <hr>
            <div id="delete-${item.id}" data-path="${item.path}"><img src="static/assets/trash-icon.svg"> Delete</div>
        </div>`;
    } else {
        html += `<div data-path="${item.path}" id="more-option-${item.id}" data-name="${item.name}" class="more-options">
            <input class="more-options-focus" readonly="readonly" style="height:0;width:0;border:none;position:absolute">
            <div id="rename-${item.id}"><img src="static/assets/pencil-icon.svg"> Rename</div>
            <hr>
            <div id="trash-${item.id}"><img src="static/assets/trash-icon.svg"> Trash</div>
            <hr>
            <div id="share-${item.id}"><img src="static/assets/share-icon.svg"> Share</div>
        </div>`;
    }
    
    return html;
}

// Override the original showDirectory function
window.showDirectory = sortAndDisplayDirectory;