Skip to content

Commit

Permalink
Add ability to sort entries with directories first by adding `data-so…
Browse files Browse the repository at this point in the history
…rt-directories-first` attribute to an element on the page. This fixes #55.
  • Loading branch information
dom111 committed May 6, 2020
1 parent dfd8c94 commit 9c292f1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/lib/DAV.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default class DAV extends EventObject {
#bypassCheck;
#cache;
#http;
#sortDirectoriesFirst;

#validDestination = (destination) => {
const hostname = `${location.protocol}//${location.hostname}${location.port ? `:${location.port}` : ''}`,
Expand Down Expand Up @@ -46,11 +47,13 @@ export default class DAV extends EventObject {
};

constructor({
bypassCheck
bypassCheck,
sortDirectoriesFirst,
}, cache = new Map(), http = new HTTP()) {
super();

this.#bypassCheck = bypassCheck;
this.#sortDirectoriesFirst = sortDirectoriesFirst;
this.#cache = cache;
this.#http = http;

Expand Down Expand Up @@ -113,7 +116,9 @@ export default class DAV extends EventObject {

const data = await this.#http.PROPFIND(uri),
response = new Response(await data.text()),
collection = response.collection()
collection = response.collection({
sortDirectoriesFirst: this.#sortDirectoriesFirst
})
;

this.#cache.set(uri, collection);
Expand Down
16 changes: 13 additions & 3 deletions src/lib/DAV/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import joinPath from '../joinPath.js';
export default class Collection extends EventObject {
#path;
#entries;
#sortDirectoriesFirst;

// don't need to handle equal paths as that's invalid
#sort = () => this.#entries.sort((a, b) => a.fullPath < b.fullPath ? -1 : 1);

constructor(items) {
#sort = () => this.#entries.sort((a, b) => this.#sortDirectoriesFirst &&
this.#sortDirectories(a, b) ||
this.#sortAlphabetically(a, b)
);
#sortAlphabetically = (a, b) => a.fullPath < b.fullPath ? -1 : 1;
#sortDirectories = (a, b) => b.directory - a.directory;

constructor(items, {
sortDirectoriesFirst = false
} = {}) {
super();

this.#sortDirectoriesFirst = sortDirectoriesFirst;

this.#entries = items
.map((item) => new Entry({
...item,
Expand Down
9 changes: 7 additions & 2 deletions src/lib/DAV/Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@ export default class Response {
this.#document = parser.parseFromString(rawDocument, 'application/xml');
}

collection() {
collection({
sortDirectoriesFirst = false
} = {}) {
if (! this.#collection) {
this.#collection = new Collection(
this.responseToPrimitives(
this.#document.querySelectorAll('response')
)
),
{
sortDirectoriesFirst,
}
);
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib/UI/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export default class UI extends EventObject {
#dav;
#options;

constructor(container, options, dav = new DAV({
bypassCheck: options.bypassCheck
constructor(container, options = {}, dav = new DAV({
bypassCheck: options.bypassCheck,
sortDirectoriesFirst: options.sortDirectoriesFirst,
})) {
super();

Expand Down
2 changes: 1 addition & 1 deletion src/webdav-min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/webdav.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'whatwg-fetch'; // IE11 compatibility
import NativeDOM from './lib/UI/NativeDOM.js';

const ui = new NativeDOM(document.body, {
bypassCheck: !! document.querySelector('[data-disable-check]')
bypassCheck: !! document.querySelector('[data-disable-check]'),
sortDirectoriesFirst: !! document.querySelector('[data-sort-directories-first]'),
});

if (document.readyState === 'loading') {
Expand Down

0 comments on commit 9c292f1

Please sign in to comment.