« Show or hide components » : différence entre les versions

De Wiki Let's Role
Neo-Teyrall (discussion | contributions)
Neo-Teyrall (discussion | contributions)
ajouter une version plus factoriser du code
Ligne 1 : Ligne 1 :
= Hide / Show Components =
'''In a character sheet, you may sometimes want to hide certain parts of the sheet to make navigation easier.'''
 
To hide and show components, you need to use scripts and manage Bootstrap classes.
 
This page presents a basic method to toggle the visibility of content, followed by an improved version with a small update.
 
= Basic Hide / Show Components =


Components can be hidden using Bootstrap classes:   
Components can be hidden using Bootstrap classes:   
* '''d-none''': [https://getbootstrap.com/docs/5.3/utilities/display/#hiding-elements Hide elements]
*'''d-none''': [https://getbootstrap.com/docs/5.3/utilities/display/#hiding-elements Hide elements]
* '''invisible''': [https://getbootstrap.com/docs/5.3/utilities/visibility/ Visibility]
*'''invisible''': [https://getbootstrap.com/docs/5.3/utilities/visibility/ Visibility]  


This allows you to hide parts of a character sheet in order to keep it more compact and readable.
This allows you to hide parts of a character sheet in order to keep it more compact and readable.


== Creating a collapsible container ==
== Creating a collapsible container ==
When your character sheet contains large amounts of text, it can be useful to hide some of it to keep the sheet clear.
When your character sheet contains large amounts of text, it can be useful to hide some of it to keep the sheet clear.  
 
This is especially useful for '''Repeater''' views, which may contain a lot of text.   
This is especially useful for '''Repeater''' views, which may contain a lot of text.   


[[image repeater expanded and repeater collapsed]]
This tutorial methods show how to create a button that hide or display contente.  [[Fichier:Tuto_ex_hideshow1.png|centré|vignette|500x500px]]
 


To do this, you will need to use scripting.
To do this, you will need to use scripting.


=== Define the content to hide ===
=== Define the content to hide ===
Place the content you want to hide inside a '''Column''' component, itself inside a '''Row''' component.
Place the content you want to hide inside a '''Column''' component, itself inside a '''Row''' component.  
 
This way, you can easily hide or display the content.   
This way, you can easily hide or display the content.   


Ligne 22 : Ligne 31 :


=== Prepare the buttons ===
=== Prepare the buttons ===
To control the hidden content, create two buttons:
To control the hidden content, create two buttons:  
* a '''Show''' button
*a '''Show''' button
* a '''Hide''' button
* a '''Hide''' button


These two buttons will alternate to control the content display.   
These two buttons will alternate to control the content display.   


* If the content is visible by default → apply '''d-none''' to the "Show" button.
* If the content is visible by default → apply '''d-none''' to the "Show" button.
* If the content is hidden by default → apply '''d-none''' to the "Hide" button.
* If the content is hidden by default → apply '''d-none''' to the "Hide" button.
 
[[Fichier:Tuto_hideshow1.png|centré|vignette|700x700px]]


=== Script ===
=== Script ===
Ligne 41 : Ligne 52 :
  const initMain = function(sheet) {
  const initMain = function(sheet) {
     // initialize the main sheet
     // initialize the main sheet
     initContentVisibility(sheet,"content_to_hide_id", "hide_button_id", "show_button_id");
     initGlobalHideShow(sheet,"hideShow1_fold", "hideShow1_unfold", "hideShow1_extra");
};
const initContentVisibility = function(sheet, extra_id, hide_button_id, show_button_id){
    // Get the button.
    let hideButton = sheet.get(hide_button_id);
    let showButton = sheet.get(show_button_id);
    let content = sheet.get(extra_id);
    // hide the content, show show_button and hide hide_button.
    const hideContent = function() {
        log("hide click");
        hideButton.hide();
        showButton.show();
        content.hide();
    };
    // show the contente, hide show_button and show hide_button.
    const showContent = function() {
        log("show click");
        hideButton.show();
        showButton.hide();
        content.show();
    };
    // show
    hideButton.on('click', hideContent);
    // hide
    showButton.on('click', showContent);
  };
  };
const initGlobalHideShow = function (sheet, fold_button_id, unfold_button_id, extra_id) {
    log("START initGlobalHideShow");
    let fold_button = sheet.get(fold_button_id);
    let unfold_button = sheet.get(unfold_button_id);
    let extra = sheet.get(extra_id);
    fold_button.on("click", function () {
        toggleExtra(extra, fold_button, unfold_button, false);
    });
    unfold_button.on("click", function () {
        toggleExtra(extra, fold_button, unfold_button, true);
    });
    log("END initGlobalHideShow");
}
const toggleExtra = function (extra_comp, fold, unfold, visibility) {
    log("START toggleExtra");
    if (visibility) {
        extra_comp.show();
        fold.show();
        unfold.hide();
    } else {
        extra_comp.hide();
        fold.hide();
        unfold.show();
    }
    log("END toggleExtra");
}
This simple method is easy to build but has some limitations. The first one is that if the page is reloaded, all the content will return to its original state.

Version du 10 septembre 2025 à 19:25

In a character sheet, you may sometimes want to hide certain parts of the sheet to make navigation easier.

To hide and show components, you need to use scripts and manage Bootstrap classes.

This page presents a basic method to toggle the visibility of content, followed by an improved version with a small update.

Basic Hide / Show Components

Components can be hidden using Bootstrap classes:

This allows you to hide parts of a character sheet in order to keep it more compact and readable.

Creating a collapsible container

When your character sheet contains large amounts of text, it can be useful to hide some of it to keep the sheet clear.

This is especially useful for Repeater views, which may contain a lot of text.

This tutorial methods show how to create a button that hide or display contente.


To do this, you will need to use scripting.

Define the content to hide

Place the content you want to hide inside a Column component, itself inside a Row component.

This way, you can easily hide or display the content.

To test, add or remove the d-none class from the Row component. Its child elements should show or hide accordingly.

Prepare the buttons

To control the hidden content, create two buttons:

  • a Show button
  • a Hide button

These two buttons will alternate to control the content display.

  • If the content is visible by default → apply d-none to the "Show" button.
  • If the content is hidden by default → apply d-none to the "Hide" button.

Script

The script should detect button clicks and act accordingly.

init = function(sheet) {
   if (sheet.id() === "main") {
       initMain(sheet);
   }
};
const initMain = function(sheet) {
   // initialize the main sheet
   initGlobalHideShow(sheet,"hideShow1_fold", "hideShow1_unfold", "hideShow1_extra");
};

const initGlobalHideShow = function (sheet, fold_button_id, unfold_button_id, extra_id) {
    log("START initGlobalHideShow");
    let fold_button = sheet.get(fold_button_id);
    let unfold_button = sheet.get(unfold_button_id);
    let extra = sheet.get(extra_id);
    fold_button.on("click", function () {
        toggleExtra(extra, fold_button, unfold_button, false);
    });

    unfold_button.on("click", function () {
        toggleExtra(extra, fold_button, unfold_button, true);
    });

    log("END initGlobalHideShow");
}

const toggleExtra = function (extra_comp, fold, unfold, visibility) {
    log("START toggleExtra");
    if (visibility) {
        extra_comp.show();
        fold.show();
        unfold.hide();
    } else {
        extra_comp.hide();
        fold.hide();
        unfold.show();
    }
    log("END toggleExtra");
}

This simple method is easy to build but has some limitations. The first one is that if the page is reloaded, all the content will return to its original state.