Merge pull request #1 from 45Drives/dev-dawson

add js type definitions
This commit is contained in:
Josh Boudreau 2021-05-28 14:52:50 -03:00 committed by GitHub
commit 2e73ee4aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 195 additions and 19 deletions

View File

@ -17,7 +17,13 @@
along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>. along with Cockpit Navigator. If not, see <https://www.gnu.org/licenses/>.
*/ */
function property_entry_html(/*string*/ key, /*string*/ value) { /**
*
* @param {string} key
* @param {string} value
* @returns {string}
*/
function property_entry_html(key, value) {
var html = '<div class="nav-property-pair">'; var html = '<div class="nav-property-pair">';
html += '<span class="nav-property-pair-key">' + key + '</span>'; html += '<span class="nav-property-pair-key">' + key + '</span>';
html += '<span class="nav-property-pair-value">' + value + '</span>'; html += '<span class="nav-property-pair-value">' + value + '</span>';
@ -25,7 +31,12 @@ function property_entry_html(/*string*/ key, /*string*/ value) {
return html; return html;
} }
function format_bytes(/*int*/ bytes) { /**
*
* @param {number} bytes
* @returns {string}
*/
function format_bytes(bytes) {
if (bytes === 0) if (bytes === 0)
return "0 B"; return "0 B";
var units = [" B", " KiB", " MiB", " GiB", " TiB", " PiB"]; var units = [" B", " KiB", " MiB", " GiB", " TiB", " PiB"];
@ -35,12 +46,22 @@ function format_bytes(/*int*/ bytes) {
return formatted.toFixed(2).toString() + units[index]; return formatted.toFixed(2).toString() + units[index];
} }
function format_time(/*int*/ timestamp) { /**
*
* @param {number} timestamp
* @returns {string}
*/
function format_time(timestamp) {
var date = new Date(timestamp * 1000); var date = new Date(timestamp * 1000);
return date.toLocaleString(); return date.toLocaleString();
} }
function format_permissions(/*int*/ mode) { /**
*
* @param {number} mode
* @returns {string}
*/
function format_permissions(mode) {
var bit_list = ["x", "w", "r"]; var bit_list = ["x", "w", "r"];
var result = ""; var result = "";
for (let bit = 8; bit >= 0; bit--) { for (let bit = 8; bit >= 0; bit--) {
@ -55,6 +76,10 @@ function format_permissions(/*int*/ mode) {
return result; return result;
} }
/**
*
* @param {NavWindow} nav_window
*/
function load_hidden_file_state(nav_window) { function load_hidden_file_state(nav_window) {
const state = localStorage.getItem('show-hidden-files') === 'true'; const state = localStorage.getItem('show-hidden-files') === 'true';
const element = document.querySelector('#nav-show-hidden'); const element = document.querySelector('#nav-show-hidden');
@ -90,7 +115,11 @@ function set_last_theme_state() {
} }
} }
function switch_theme(/*event*/ e) { /**
*
* @param {Event} e
*/
function switch_theme(e) {
var icon = document.getElementById("houston-theme-icon"); var icon = document.getElementById("houston-theme-icon");
var logo = document.getElementById("logo-45d"); var logo = document.getElementById("logo-45d");
var state = ""; var state = "";
@ -110,7 +139,13 @@ function switch_theme(/*event*/ e) {
} }
class NavEntry { class NavEntry {
constructor(/*string or array*/ path, /*dict*/ stat, /*NavWindow*/ nav_window_ref) { /**
*
* @param {string} path
* @param {object} stat
* @param {NavWindow} nav_window_ref
*/
constructor(path, stat, nav_window_ref) {
this.nav_window_ref = nav_window_ref; this.nav_window_ref = nav_window_ref;
if (typeof path == "string") if (typeof path == "string")
this.path = path.split("/").splice(1); this.path = path.split("/").splice(1);
@ -129,7 +164,12 @@ class NavEntry {
this.dom_element.addEventListener("click", this); this.dom_element.addEventListener("click", this);
this.is_hidden_file = this.filename().startsWith('.'); this.is_hidden_file = this.filename().startsWith('.');
} }
handleEvent(/*event*/ e) {
/**
*
* @param {Event} e
*/
handleEvent(e) {
switch (e.type) { switch (e.type) {
case "click": case "click":
this.show_properties(); this.show_properties();
@ -138,6 +178,7 @@ class NavEntry {
break; break;
} }
} }
destroy() { destroy() {
while (this.dom_element.firstChild) { while (this.dom_element.firstChild) {
this.dom_element.removeChild(this.dom_element.firstChild); this.dom_element.removeChild(this.dom_element.firstChild);
@ -145,28 +186,59 @@ class NavEntry {
if (this.dom_element.parentElement) if (this.dom_element.parentElement)
this.dom_element.parentElement.removeChild(this.dom_element); this.dom_element.parentElement.removeChild(this.dom_element);
} }
/**
*
* @returns {string}
*/
filename() { filename() {
var name = this.path[this.path.length - 1]; var name = this.path[this.path.length - 1];
if (!name) if (!name)
name = "/"; name = "/";
return name; return name;
} }
/**
*
* @returns {string}
*/
path_str() { path_str() {
return "/" + this.path.join("/"); return "/" + this.path.join("/");
} }
/**
*
* @returns {string}
*/
parent_dir() { parent_dir() {
return this.path.slice(0, this.path.length - 1); return this.path.slice(0, this.path.length - 1);
} }
show() { show() {
document.getElementById("nav-contents-view").appendChild(this.dom_element); document.getElementById("nav-contents-view").appendChild(this.dom_element);
} }
/**
*
* @returns {object}
*/
get_properties() { get_properties() {
return this.stat; return this.stat;
} }
/**
*
* @returns {number}
*/
get_permissions() { get_permissions() {
return this.stat["mode"] & 0o777; return this.stat["mode"] & 0o777;
} }
async chmod(/*int*/ new_perms) {
/**
*
* @param {number} new_perms
*/
async chmod(new_perms) {
var proc = cockpit.spawn( var proc = cockpit.spawn(
["chmod", (new_perms & 0o777).toString(8), this.path_str()], ["chmod", (new_perms & 0o777).toString(8), this.path_str()],
{superuser: "try", err: "out"} {superuser: "try", err: "out"}
@ -176,7 +248,13 @@ class NavEntry {
}); });
await proc; await proc;
} }
async chown(/*string*/ new_owner, /*string*/ new_group) {
/**
*
* @param {string} new_owner
* @param {string} new_group
*/
async chown(new_owner, new_group) {
var proc = cockpit.spawn( var proc = cockpit.spawn(
["chown", [new_owner, new_group].join(":"), this.path_str()], ["chown", [new_owner, new_group].join(":"), this.path_str()],
{superuser: "try", err: "out"} {superuser: "try", err: "out"}
@ -186,7 +264,12 @@ class NavEntry {
}); });
await proc; await proc;
} }
async mv(/*string*/ new_path) {
/**
*
* @param {string} new_path
*/
async mv(new_path) {
var proc = cockpit.spawn( var proc = cockpit.spawn(
["mv", "-n", this.path_str(), [this.nav_window_ref.pwd().path_str(), new_path].join("/")], ["mv", "-n", this.path_str(), [this.nav_window_ref.pwd().path_str(), new_path].join("/")],
{superuser: "try", err: "out"} {superuser: "try", err: "out"}
@ -196,7 +279,12 @@ class NavEntry {
}); });
await proc; await proc;
} }
show_properties(/*string*/ extra_properties = "") {
/**
*
* @param {string} extra_properties
*/
show_properties(extra_properties = "") {
var selected_name_fields = document.getElementsByClassName("nav-info-column-filename"); var selected_name_fields = document.getElementsByClassName("nav-info-column-filename");
for (let elem of selected_name_fields) { for (let elem of selected_name_fields) {
elem.innerText = this.filename(); elem.innerText = this.filename();
@ -212,6 +300,7 @@ class NavEntry {
html += extra_properties; html += extra_properties;
document.getElementById("nav-info-column-properties").innerHTML = html; document.getElementById("nav-info-column-properties").innerHTML = html;
} }
populate_edit_fields() { populate_edit_fields() {
document.getElementById("nav-edit-filename").value = this.filename(); document.getElementById("nav-edit-filename").value = this.filename();
var mode_bits = [ var mode_bits = [
@ -230,13 +319,24 @@ class NavEntry {
} }
class NavFile extends NavEntry { class NavFile extends NavEntry {
constructor(/*string or array*/ path, /*dict*/ stat, nav_window_ref) { /**
*
* @param {string|string[]} path
* @param {object} stat
* @param {NavWindow} nav_window_ref
*/
constructor(path, stat, nav_window_ref) {
super(path, stat, nav_window_ref); super(path, stat, nav_window_ref);
this.nav_type = "file"; this.nav_type = "file";
this.dom_element.nav_item_icon.classList.add("fas", "fa-file"); this.dom_element.nav_item_icon.classList.add("fas", "fa-file");
this.double_click = false; this.double_click = false;
} }
handleEvent(/*event*/ e) {
/**
*
* @param {Event} e
*/
handleEvent(e) {
switch(e.type){ switch(e.type){
case "click": case "click":
if(this.double_click) if(this.double_click)
@ -253,6 +353,7 @@ class NavFile extends NavEntry {
} }
super.handleEvent(e); super.handleEvent(e);
} }
async rm() { async rm() {
var proc = cockpit.spawn( var proc = cockpit.spawn(
["rm", "-f", this.path_str()], ["rm", "-f", this.path_str()],
@ -263,6 +364,7 @@ class NavFile extends NavEntry {
}); });
await proc; await proc;
} }
async show_edit_file_contents() { async show_edit_file_contents() {
for (let button of document.getElementsByTagName("button")) { for (let button of document.getElementsByTagName("button")) {
if (!button.classList.contains("editor-btn")) if (!button.classList.contains("editor-btn"))
@ -284,12 +386,14 @@ class NavFile extends NavEntry {
document.getElementById("nav-contents-view").style.display = "none"; document.getElementById("nav-contents-view").style.display = "none";
document.getElementById("nav-edit-contents-view").style.display = "flex"; document.getElementById("nav-edit-contents-view").style.display = "flex";
} }
async write_to_file() { async write_to_file() {
var new_contents = document.getElementById("nav-edit-contents-textarea").value; var new_contents = document.getElementById("nav-edit-contents-textarea").value;
await cockpit.script("echo -n \"$1\" > $2", [new_contents, this.path_str()], {superuser: "try"}); await cockpit.script("echo -n \"$1\" > $2", [new_contents, this.path_str()], {superuser: "try"});
this.nav_window_ref.refresh(); this.nav_window_ref.refresh();
this.hide_edit_file_contents(); this.hide_edit_file_contents();
} }
hide_edit_file_contents() { hide_edit_file_contents() {
document.getElementById("nav-edit-contents-view").style.display = "none"; document.getElementById("nav-edit-contents-view").style.display = "none";
document.getElementById("nav-contents-view").style.display = "flex"; document.getElementById("nav-contents-view").style.display = "flex";
@ -301,13 +405,24 @@ class NavFile extends NavEntry {
} }
class NavDir extends NavEntry { class NavDir extends NavEntry {
constructor(/*string or array*/ path, /*dict*/ stat, nav_window_ref) { /**
*
* @param {string|string[]} path
* @param {object} stat
* @param {NavWindow} nav_window_ref
*/
constructor(path, stat, nav_window_ref) {
super(path, stat, nav_window_ref); super(path, stat, nav_window_ref);
this.nav_type = "dir"; this.nav_type = "dir";
this.dom_element.nav_item_icon.classList.add("fas", "fa-folder"); this.dom_element.nav_item_icon.classList.add("fas", "fa-folder");
this.double_click = false; this.double_click = false;
} }
handleEvent(/*event*/ e) {
/**
*
* @param {Event} e
*/
handleEvent(e) {
switch (e.type) { switch (e.type) {
case "click": case "click":
if (this.double_click) if (this.double_click)
@ -325,7 +440,14 @@ class NavDir extends NavEntry {
} }
super.handleEvent(e); super.handleEvent(e);
} }
async get_children(/*NavWindow*/ nav_window_ref, /*boolean*/ no_alert = false) {
/**
*
* @param {NavWindow} nav_window_ref
* @param {boolean} no_alert
* @returns {object[]}
*/
async get_children(nav_window_ref, no_alert = false) {
var children = []; var children = [];
var proc = cockpit.spawn( var proc = cockpit.spawn(
["/usr/share/cockpit/navigator/scripts/ls.py", this.path_str()], ["/usr/share/cockpit/navigator/scripts/ls.py", this.path_str()],
@ -358,6 +480,7 @@ class NavDir extends NavEntry {
}); });
return children; return children;
} }
async rm() { async rm() {
var proc = cockpit.spawn( var proc = cockpit.spawn(
["rmdir", this.path_str()], ["rmdir", this.path_str()],
@ -368,6 +491,11 @@ class NavDir extends NavEntry {
}); });
await proc; await proc;
} }
/**
*
* @returns {any}
*/
async cephfs_dir_stats() { async cephfs_dir_stats() {
try { try {
var proc = await cockpit.spawn( var proc = await cockpit.spawn(
@ -379,6 +507,7 @@ class NavDir extends NavEntry {
return null; return null;
} }
} }
async show_properties() { async show_properties() {
var extra_properties = ""; var extra_properties = "";
if(!this.hasOwnProperty("ceph_stats")) if(!this.hasOwnProperty("ceph_stats"))
@ -435,6 +564,7 @@ class NavWindow {
this.window = document.getElementById("nav-contents-view"); this.window = document.getElementById("nav-contents-view");
this.window.addEventListener("click", this); this.window.addEventListener("click", this);
} }
handleEvent(e) { handleEvent(e) {
switch (e.type) { switch (e.type) {
case "click": case "click":
@ -443,6 +573,7 @@ class NavWindow {
break; break;
} }
} }
async refresh() { async refresh() {
localStorage.setItem('navigator-path', `/${this.path_stack[this.path_stack_index].path.join('/')}`); localStorage.setItem('navigator-path', `/${this.path_stack[this.path_stack_index].path.join('/')}`);
@ -471,10 +602,20 @@ class NavWindow {
document.getElementById("nav-num-files").innerText = num_files.toString(); document.getElementById("nav-num-files").innerText = num_files.toString();
this.stop_load(); this.stop_load();
} }
/**
*
* @returns {NavDir}
*/
pwd() { pwd() {
return this.path_stack[this.path_stack_index]; return this.path_stack[this.path_stack_index];
} }
cd(/*NavDir*/ new_dir) {
/**
*
* @param {NavDir} new_dir
*/
cd(new_dir) {
this.path_stack.length = this.path_stack_index + 1; this.path_stack.length = this.path_stack_index + 1;
this.path_stack.push(new_dir); this.path_stack.push(new_dir);
this.path_stack_index = this.path_stack.length - 1; this.path_stack_index = this.path_stack.length - 1;
@ -482,22 +623,31 @@ class NavWindow {
this.back(); this.back();
}); });
} }
back() { back() {
this.path_stack_index = Math.max(this.path_stack_index - 1, 0); this.path_stack_index = Math.max(this.path_stack_index - 1, 0);
this.refresh(); this.refresh();
} }
forward() { forward() {
this.path_stack_index = Math.min(this.path_stack_index + 1, this.path_stack.length - 1); this.path_stack_index = Math.min(this.path_stack_index + 1, this.path_stack.length - 1);
this.refresh(); this.refresh();
} }
up() { up() {
if(this.pwd().path_str() !== '/') if(this.pwd().path_str() !== '/')
this.cd(new NavDir(this.pwd().parent_dir())); this.cd(new NavDir(this.pwd().parent_dir()));
} }
show_selected_properties() { show_selected_properties() {
this.selected_entry.show_properties(); this.selected_entry.show_properties();
} }
set_selected(/*NavEntry*/ entry) {
/**
*
* @param {any} entry
*/
set_selected(entry) {
this.hide_edit_selected(); this.hide_edit_selected();
this.selected_entry.dom_element.classList.remove("nav-item-selected"); this.selected_entry.dom_element.classList.remove("nav-item-selected");
if (this.selected_entry.nav_type === "dir") { if (this.selected_entry.nav_type === "dir") {
@ -511,6 +661,7 @@ class NavWindow {
this.selected_entry.dom_element.nav_item_icon.classList.add("fa-folder-open"); this.selected_entry.dom_element.nav_item_icon.classList.add("fa-folder-open");
} }
} }
show_edit_selected() { show_edit_selected() {
var dangerous_dirs = [ var dangerous_dirs = [
"/", "/",
@ -543,10 +694,16 @@ class NavWindow {
document.getElementById("nav-edit-properties").style.display = "block"; document.getElementById("nav-edit-properties").style.display = "block";
document.getElementById("nav-show-properties").style.display = "none"; document.getElementById("nav-show-properties").style.display = "none";
} }
hide_edit_selected() { hide_edit_selected() {
document.getElementById("nav-show-properties").style.display = "block"; document.getElementById("nav-show-properties").style.display = "block";
document.getElementById("nav-edit-properties").style.display = "none"; document.getElementById("nav-edit-properties").style.display = "none";
} }
/**
*
* @returns {number}
*/
get_new_permissions() { get_new_permissions() {
var category_list = ["other", "group", "owner"]; var category_list = ["other", "group", "owner"];
var action_list = ["exec", "write", "read"]; var action_list = ["exec", "write", "read"];
@ -561,12 +718,14 @@ class NavWindow {
} }
return result; return result;
} }
update_permissions_preview() { update_permissions_preview() {
var new_perms = this.get_new_permissions(); var new_perms = this.get_new_permissions();
var text = format_permissions(new_perms); var text = format_permissions(new_perms);
text += " (" + (new_perms & 0o777).toString(8) + ")"; text += " (" + (new_perms & 0o777).toString(8) + ")";
document.getElementById("nav-mode-preview").innerText = text; document.getElementById("nav-mode-preview").innerText = text;
} }
async apply_edit_selected() { async apply_edit_selected() {
// do mv last so the rest don't fail from not finding path // do mv last so the rest don't fail from not finding path
var new_owner = document.getElementById("nav-edit-owner").value; var new_owner = document.getElementById("nav-edit-owner").value;
@ -588,6 +747,7 @@ class NavWindow {
this.refresh(); this.refresh();
this.hide_edit_selected(); this.hide_edit_selected();
} }
async delete_selected() { async delete_selected() {
if ( if (
!window.confirm( !window.confirm(
@ -599,6 +759,7 @@ class NavWindow {
await this.selected_entry.rm().catch(/*ignore, caught in rm*/); await this.selected_entry.rm().catch(/*ignore, caught in rm*/);
this.refresh(); this.refresh();
} }
async mkdir() { async mkdir() {
var new_dir_name = window.prompt("Directory Name: "); var new_dir_name = window.prompt("Directory Name: ");
if (new_dir_name === null) if (new_dir_name === null)
@ -617,6 +778,7 @@ class NavWindow {
await proc; await proc;
this.refresh(); this.refresh();
} }
async touch() { async touch() {
var new_file_name = window.prompt("File Name: "); var new_file_name = window.prompt("File Name: ");
if (new_file_name === null) if (new_file_name === null)
@ -635,7 +797,12 @@ class NavWindow {
await proc; await proc;
this.refresh(); this.refresh();
} }
nav_bar_event_handler(/*event*/ e) {
/**
*
* @param {Event} e
*/
nav_bar_event_handler(e) {
switch(e.key){ switch(e.key){
case 'Enter': case 'Enter':
this.nav_bar_cd(); this.nav_bar_cd();
@ -644,10 +811,12 @@ class NavWindow {
break; break;
} }
} }
nav_bar_cd() { nav_bar_cd() {
var new_path = document.getElementById("pwd").value; var new_path = document.getElementById("pwd").value;
this.cd(new NavDir(new_path)); this.cd(new NavDir(new_path));
} }
async nav_bar_update_choices() { async nav_bar_update_choices() {
var list = document.getElementById("possible-paths"); var list = document.getElementById("possible-paths");
var partial_path_str = document.getElementById("pwd").value; var partial_path_str = document.getElementById("pwd").value;
@ -673,6 +842,7 @@ class NavWindow {
list.appendChild(option); list.appendChild(option);
}); });
} }
start_load() { start_load() {
document.getElementById("nav-loader-container").hidden = false; document.getElementById("nav-loader-container").hidden = false;
var buttons = document.getElementsByTagName("button"); var buttons = document.getElementsByTagName("button");
@ -680,6 +850,7 @@ class NavWindow {
button.disabled = true; button.disabled = true;
} }
} }
stop_load() { stop_load() {
document.getElementById("nav-loader-container").hidden = true; document.getElementById("nav-loader-container").hidden = true;
var buttons = document.getElementsByTagName("button"); var buttons = document.getElementsByTagName("button");
@ -687,6 +858,11 @@ class NavWindow {
button.disabled = false; button.disabled = false;
} }
} }
/**
*
* @param {Event} e
*/
toggle_show_hidden(e) { toggle_show_hidden(e) {
localStorage.setItem('show-hidden-files', e.target.checked); localStorage.setItem('show-hidden-files', e.target.checked);