SofianeLasri

uiHandler.js

Dec 14th, 2022
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function UIWindow() {
  2.     this.id="uiWindow-"+generateUniqueId();
  3.     this.closable = true;
  4.     this.acceptable = false; // Ça n'a pas été implémenté au début, alors on va laisser false
  5.     this.preferHide = false;
  6.     this.waitAssetsLoading = true;
  7.     this.showAnimation = null;
  8.     this.hideAnimation = null;
  9.     this.debug = false;
  10.     this.closeAction = null;
  11.     this.acceptAction = null;
  12.     //this.init();
  13.  
  14.     // Détecter les touches du clavier appuyées
  15.     //var preferHide = this.preferHide;
  16.     document.addEventListener('keydown', (event) => {
  17.         if(event.key == "Escape"){
  18.             if(this.preferHide == true){
  19.                 this.hide();
  20.             }else{
  21.                 this.close();
  22.             }
  23.         }else if(event.key == "Enter"){
  24.             if(this.acceptable == true){
  25.                 this.accept();
  26.             }
  27.         }
  28.     }, false);
  29. }
  30.  
  31. UIWindow.prototype.init = function() {
  32.     if(this.debug == true) console.log("UIWindow created with id " + this.id);
  33.  
  34.     // On va vérifier qu'aucune window existe déjà
  35.     if(document.getElementById(this.id) != null){
  36.         throw "The id " + this.id + " is already used";
  37.     }
  38.     // Ici on va récréer le code html de la fenêtre
  39.     var mainContainer = document.createElement("div");
  40.    
  41.     mainContainer.className = "window";
  42.  
  43.     if(this.showAnimation != null || this.hideAnimation != null){
  44.         mainContainer.className += " animate__animated ";
  45.  
  46.         if(this.showAnimation != null){
  47.             mainContainer.className += " " + this.showAnimation;
  48.         }
  49.         if(this.hideAnimation != null){
  50.             mainContainer.className += " " + this.hideAnimation;
  51.         }
  52.     }
  53.  
  54.     mainContainer.id = this.id;
  55.     mainContainer.style.display = "none";
  56.  
  57.     var contentContainer = document.createElement("div");
  58.     contentContainer.className = "content";
  59.     contentContainer.id = "uiWindowContent-" + this.id;
  60.  
  61.     var actionContainer = document.createElement("div");
  62.     actionContainer.className = "actionsBar";
  63.  
  64.     if(this.closable == true){
  65.         var closeButton = document.createElement("div");
  66.         closeButton.className = "item";
  67.         var closeIconDiv = document.createElement("div");
  68.         closeIconDiv.className = "icon";
  69.         var closeIcon = document.createElement("icon");
  70.         closeIcon.className = "keyboard-key esc dark";
  71.  
  72.         var text = document.createElement("span");
  73.         text.innerHTML = "Fermer";
  74.  
  75.         closeIconDiv.appendChild(closeIcon);
  76.         closeButton.appendChild(closeIconDiv);
  77.         closeButton.appendChild(text);
  78.         if(this.preferHide == true){
  79.             closeButton.onclick = function(){
  80.                 this.hide();
  81.             }.bind(this);
  82.         }else{
  83.             closeButton.addEventListener("click", function(){
  84.                 this.close();
  85.             }.bind(this));
  86.         }
  87.         actionContainer.appendChild(closeButton);
  88.     }
  89.  
  90.     if(this.acceptable == true){
  91.         var acceptButton = document.createElement("div");
  92.         acceptButton.className = "item";
  93.         var acceptIconDiv = document.createElement("div");
  94.         acceptIconDiv.className = "icon";
  95.         var acceptIcon = document.createElement("icon");
  96.         acceptIcon.className = "keyboard-key enter dark";
  97.  
  98.         var text = document.createElement("span");
  99.         text.innerHTML = "Valider";
  100.  
  101.         acceptIconDiv.appendChild(acceptIcon);
  102.         acceptButton.appendChild(acceptIconDiv);
  103.         acceptButton.appendChild(text);
  104.         acceptButton.addEventListener("click", function(){
  105.             this.accept();
  106.         }.bind(this));
  107.         actionContainer.appendChild(acceptButton);
  108.     }
  109.    
  110.     mainContainer.appendChild(contentContainer);
  111.     mainContainer.appendChild(actionContainer);
  112.  
  113.     // Et on l'insère
  114.     document.body.appendChild(mainContainer);
  115.     this.elem = mainContainer;
  116. }
  117.  
  118. UIWindow.prototype.show = function() {
  119.     document.getElementById(this.id).style.display = "flex";
  120.     if(this.debug == true) console.log("UIWindow with id " + this.id + " showed up");
  121. }
  122. UIWindow.prototype.close = function() {
  123.     PlaySound("clickQuit");
  124.     document.getElementById(this.id).remove();
  125.     if(this.closeAction != null){
  126.         eval(this.closeAction);
  127.     }
  128.     if(this.debug == true) console.log("UIWindow with id " + this.id + " closed");
  129. }
  130. UIWindow.prototype.hide = function() {
  131.     PlaySound("clickQuit");
  132.     document.getElementById(this.id).style.display = "none";
  133.     if(this.closeAction != null){
  134.         eval(this.closeAction);
  135.     }
  136.     if(this.debug == true) console.log("UIWindow with id " + this.id + " hided");
  137. }
  138.  
  139. UIWindow.prototype.accept = function() {
  140.     if(this.acceptAction != null){
  141.         eval(this.acceptAction);
  142.     }
  143.     if(this.debug == true) console.log("UIWindow with id " + this.id + " accepted");
  144. }
  145.  
  146. // Cette fonction servira à ajouter des élément selon les règles de l'interface
  147. UIWindow.prototype.addElement = function(type, content, parent) {
  148.     if(parent == null){
  149.         parent = document.getElementById("uiWindowContent-" + this.id);
  150.     }else{  
  151.         parent = document.getElementById(parent);
  152.     }
  153.     if(type == null){
  154.         throw "Type is null";
  155.     }
  156.  
  157.     if(type == "title"){
  158.         var titleDiv = document.createElement("div");
  159.         titleDiv.className = "title";
  160.         titleDiv.id=generateUniqueId();
  161.  
  162.         if(content != null){
  163.             var titleH = document.createElement("h2");
  164.             titleH.innerHTML = content;
  165.             titleDiv.appendChild(titleH);
  166.         }  
  167.  
  168.         parent.appendChild(titleDiv);
  169.  
  170.         if(this.debug == true) console.log("Added pre-defined element of type titleDiv with id " + titleDiv.id +" to UIWindow with id " + this.id);
  171.  
  172.         return titleDiv.id;
  173.     }else if(type == "textBox"){
  174.         var textBoxDiv = document.createElement("div");
  175.         textBoxDiv.className = "textBox";
  176.         textBoxDiv.id=generateUniqueId();
  177.         textBoxDiv.innerHTML = content;
  178.  
  179.         parent.appendChild(textBoxDiv);
  180.  
  181.         if(this.debug == true) console.log("Added pre-defined element of type textBoxDiv with id " + textBoxDiv.id +" to UIWindow with id " + this.id);
  182.  
  183.         return textBoxDiv.id;
  184.     }else if(type == "listContainer"){
  185.         var listContainerDiv = document.createElement("div");
  186.         listContainerDiv.className = "listContainer";
  187.         listContainerDiv.id=generateUniqueId();
  188.  
  189.         parent.appendChild(listContainerDiv);
  190.  
  191.         if(this.debug == true) console.log("Added pre-defined element of type listContainerDiv with id " + listContainerDiv.id +" to UIWindow with id " + this.id);
  192.  
  193.         return listContainerDiv.id;
  194.     }else if(type == "listContainerItem"){
  195.         // Ici content est un objet
  196.         var listContainerItemDiv = document.createElement("div");
  197.         listContainerItemDiv.className = "item";
  198.         listContainerItemDiv.id=generateUniqueId();
  199.         if (content.hasOwnProperty("onClick") && content.onClick != null){
  200.             // On créé l'action
  201.             listContainerItemDiv.onclick = function(){
  202.                 eval(content.onClick);
  203.             }
  204.         }
  205.  
  206.         if(content.hasOwnProperty("icon") && content.icon != null){
  207.             // On créé l'icône
  208.             var listContainerIcon = document.createElement("div");
  209.             listContainerIcon.className = "icon";
  210.             var img = document.createElement("img");
  211.  
  212.             if(content.icon == "create-icon"){
  213.                 img.src = "images/icons/create-icon.png";
  214.                 img.alt = "create-icon";
  215.             }else if(content.icon == "world-icon"){
  216.                 img.src = "images/icons/world-icon.png";
  217.                 img.alt = "world-icon";
  218.             }else{
  219.                 throw 'Unknown icon ' + content.icon;
  220.             }
  221.  
  222.             listContainerItemDiv.addEventListener("mouseenter", function(){
  223.                 PlaySound("changeSelection");
  224.             });
  225.  
  226.             listContainerIcon.appendChild(img);
  227.             listContainerItemDiv.appendChild(listContainerIcon);
  228.         }
  229.  
  230.         // On créé la description
  231.         var listContainerDescription = document.createElement("div");
  232.         listContainerDescription.className = "description";
  233.  
  234.         var listContainerItemTitle = document.createElement("h4");
  235.         listContainerItemTitle.innerHTML = content.title;
  236.  
  237.         var listContainerSpan = document.createElement("span");
  238.         listContainerSpan.innerHTML = content.description;
  239.  
  240.         listContainerDescription.appendChild(listContainerItemTitle);
  241.         listContainerDescription.appendChild(listContainerSpan);
  242.         listContainerItemDiv.appendChild(listContainerDescription);
  243.  
  244.         parent.appendChild(listContainerItemDiv);
  245.        
  246.         if(this.debug == true) console.log("Added pre-defined element of type listContainer with id " + listContainerItemDiv.id +" to UIWindow with id " + this.id);
  247.         return listContainerItemDiv.id;
  248.     }else if(type == "horizontalContainer"){
  249.         var horizontalContainerDiv = document.createElement("div");
  250.         horizontalContainerDiv.className = "horizontalContainer";
  251.         horizontalContainerDiv.id=generateUniqueId();
  252.  
  253.         parent.appendChild(horizontalContainerDiv);
  254.  
  255.         if(this.debug == true) console.log("Added pre-defined element of type horizontalContainer with id " + horizontalContainerDiv.id +" to UIWindow with id " + this.id);
  256.  
  257.         return horizontalContainerDiv.id;
  258.     } else if(type == "presentationImage"){
  259.         var presentationImageDiv = document.createElement("div");
  260.         presentationImageDiv.className = "presentationImage";
  261.         presentationImageDiv.id=generateUniqueId();
  262.         presentationImageDiv.style = "background-image: url('" + content + "')";
  263.  
  264.         parent.appendChild(presentationImageDiv);
  265.  
  266.         if(this.debug == true) console.log("Added pre-defined element of type presentationImage with id " + presentationImageDiv.id +" to UIWindow with id " + this.id);
  267.  
  268.         return presentationImageDiv.id;
  269.     }
  270. }
  271. // Cette fonction servira à ajouter des élément HTML aux attribus personalisés
  272. UIWindow.prototype.addHTMLElement = function(UIElement, parent) {
  273.     if(parent == null){
  274.         parent = document.getElementById("uiWindowContent-" + this.id);
  275.     }else{
  276.         parent = document.getElementById(parent);
  277.     }
  278.     parent.appendChild(UIElement);
  279.     return UIElement.id;
  280. }
  281.  
  282. function HTMLUIElement(type, attributes, content) {
  283.     var element = document.createElement(type);
  284.  
  285.     // On vérifie qu'il y a bien des attributs
  286.     if(attributes != null){
  287.         for(var key in attributes){
  288.             element[key] = attributes[key];
  289.         }
  290.     }
  291.     // Pareil pour l'id
  292.     if(!element.hasOwnProperty('id') || element.id == null){
  293.         element.id = generateUniqueId();
  294.     }
  295.     // Et le content
  296.     if(content != null){
  297.         element.innerHTML = content;
  298.     }
  299.     return element;
  300. }
  301.  
  302. // A insérer peut-être ailleurs
  303. function generateUniqueId(){
  304.     // https://stackoverflow.com/a/1349426
  305.     var length = 6;
  306.     var result = '';
  307.     var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  308.     var charactersLength = characters.length;
  309.     for ( var i = 0; i < length; i++ ) {
  310.       result += characters.charAt(Math.floor(Math.random() *
  311.         charactersLength));
  312.     }
  313.     if(document.getElementById(result)){
  314.         return generateUniqueId();
  315.     }else{
  316.         return result;
  317.     }
  318. }
  319.  
Advertisement
Add Comment
Please, Sign In to add comment