Skip to content

Small ajax sidebar file tree browser without dependencies.

Notifications You must be signed in to change notification settings

jenstornell/staircase.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

staircase.js

Version 1.9 - Changelog

Small ajax sidebar file tree browser without dependencies.

Screenshot

In short

  • Vanilla JS
  • Ajax driven
  • Zero dependencies
  • Small filesize
  • Super simple setup
  • Grouped files and folders
  • Sorted files and folders
  • Method support
  • Callback support

Setup

CSS

Put the CSS within <head></head>.

<link rel="stylesheet" href="assets/css/dist/staircase.min.css">

HTML

Add the staircase selector to your html body like below.

<body data-staircase-selector="stair-case">

Put the stair-case element within <body></body>. You also need to add the ajax url.

<stair-case data-staircase-path="example/ajax/ajax.php"></stair-case>

Javascript

Put the javascript before </body>. The script will then be autoloaded.

<script src="assets/js/src/staircase.js"></script>

Ajax

Ajax is used for getting files and folders from a parent. To demonstrate how it works I will use PHP.

Create an ajax.php file and put it in your root.

Example

$post = json_decode(file_get_contents('php://input'), true);

$data = [
    'one.txt',
    'two/',
    'three/',
    'file.png'
];

echo json_encode($data);
  • The first row with $post is needed to get the $_POST variables sent with ajax in PHP.
  • The $data contains a list of files and folder.
  • The $data should probably depend on what is inside your $post.
  • To mark that it's a folder, just place a / at the end.
  • The files and folders will be grouped and sorted alphabetically so you don't need to think about that.
  • You need to print out the data as json.

If you want to see that something really is loading with ajax, you can put the code below inside your PHP file. It will delay the result by 0.25 seconds.

usleep(250000);

Callbacks

To use callbacks you need to create a class like below.

class StaircaseCallbacks {
  load(params) {
    console.log(params);
  }
  toggle(params) {
    console.log(params);
  }
  select(params) {
    console.log(params);
  }
  open(params) {
    console.log(params);
  }
}
  • load - Is only triggered when staircase is loaded
  • toggle - Is triggered every time when a folder is clicked
  • select - Is triggered every time you select a file or a folder
  • open - Is triggered when staircase.open() method is done

Callback params

  • id is the file or folder path, something like myfolder/myfile.txt.
  • element is the closest list element which also contains additional data to extract.
  • type is the type like file or folder.
  • state is the state of the folder, open or close.
  • success is a check if the ajax has been loaded correctly. It contains true or false.

Methods

Be aware. These methods will not add any files or folders to the system. They will only add new visual items. It can be useful to populate the tree with them when using ajax.

staircase.add(id, type);
staircase.delete(id);

staircase.open(id);
staircase.close(id);

staircase.rename(id, name);

staircase.select(id, callback = true);
staircase.deselect(callback = true);

staircase.refresh(id);

staircase.dirname(id);
staircase.basename(id);
staircase.join(parent_id, name);

Methods variables

  • parent_id - The parent id of my/parent/current would be my/parent
  • id - The id is the path of the item, something like my/folder/filename.txt
  • type - The type is file or folder
  • name - A new name like my_new_filename.txt,
  • callback - Optional value. In case you don't want to trigger the callback, set it to false.

Methods explained

  • add - Add a new item
  • delete - Delete an item
  • open - Opens a folder. It can also open a nested folder
  • close - Closes a folder if it's open
  • rename - Rename an item
  • select - Select an item
  • deselect - Deselect an item
  • refresh - Refresh a folder and all it's sub contents
  • join - Join two ids together like my/id + text.md and resolves slashes at the same time. The result is returned.
  • dirname - From an id, return the parent_id (inspired by PHP).
  • basename - From an id, return the filename (inspired by PHP).

To see all possible params, visit Mozilla - Fetch

Help appreciated

You you want to help me improve staircase.js?

I'm specially interested in these things:

  • Bug fixes if you find any.
  • Enhancements and improvements that makes already existing things a bit better.
  • Pull requests or code in an issue. Both are equally fine.

Libraries used

  • Remixicon - A special thanks to xiaochunjimmy for not only providing a beautiful icon pack with 24px icons, but also for making custom designed 18px icons for this filetree.

Donate

Donate to DevoneraAB if you want.

License

MIT

About

Small ajax sidebar file tree browser without dependencies.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published