transition between main container to a new Javascript container using DOM

Asked

Viewed 186 times

1

Hello, I have a function in Javascript using DOM to create a page and then I intend to view this page on container and for this the visualized data would have to be just the intended and only that container does not make a clearto delete the content as shown in figura 2.

have this figura 1, shows the first page created, source view HTML element placed on the page using DOM - Javascript

inserir a descrição da imagem aqui

and when I select the pisoand I intend to visualize the contents through the button view Floor and I have the event script

/**
 * Função para visualizar dados no piso
 * 
 */
Consola.prototype.onVerClick = function (event) {
    this.pisos.forEach(function (piso, indice) {
        if ((piso.selected) &&
            (piso instanceof Piso)) {
             //chama a função
             piso.criarCompartimento(); 
        }
    });
}

and I intend to view this page within it container, as shown at figura 2

inserir a descrição da imagem aqui

to função was implemented in this script

/**
 * Função para criar compartimento
 */
Piso.prototype.criarCompartimento = function () {
    var h1 = document.createElement("h1")
    h1.style.color = "#ff0000";
    h1.style.textAlign = "center";

    var textoPiso = document.createTextNode("Piso " + this.id);
    h1.appendChild(textoPiso);

    var h2 = document.createElement("h2")
    h2.style.color = "#0000ff";
    h2.style.textAlign = "center";

    var textoCompartimentos = document.createTextNode("Compartimentos:");
    h2.appendChild(document.createElement("br"));
    h2.appendChild(textoCompartimentos);

    var btnCriar = document.createElement("button");
    var btnCriarText = document.createTextNode("Criar");
    btnCriar.appendChild(btnCriarText);

    var btnApagar = document.createElement("button");
    var btnApagarText = document.createTextNode("Apagar");
    btnApagar.appendChild(btnApagarText);

    var btnSistemaDom = document.createElement("button");
    var btnSistemaDomText = document.createTextNode("Sistema Domótico");
    btnSistemaDom.appendChild(btnSistemaDomText);

    container.appendChild(h1); //adicionar itens ao container
    container.appendChild(h2);
    container.appendChild(btnCriar);
    container.appendChild(btnApagar);
    container.appendChild(btnSistemaDom);
}

and I was able to do it just like this, as shown by figura 3

inserir a descrição da imagem aqui

and another question also arose is that if I intend to return to container leading consola 1, it is possible without losing the data, for this I will have to create a newfunção, or create a evento to a new button retroceder or there is another solution?

the ultimate goal is to reach at least this structure, as shown in figura 4, some pages have already been implemented separately and only need to synchronize

inserir a descrição da imagem aqui

  • I believe that the simplest in your case is to open a dialog that allows you to enter this data.

  • And just as I suggested in your other question, avoid manipulating DOM to assemble the layout. Instead set a template on your page and clone it, make this also the Compartments.

  • in fact, I have to follow the statement -> Use the methods of DOM (document.createElement and document.createTextNode) to create the elements needed for the page. Place them on the page, through appendChild and remove them through removeChild. Use of property, non-standard, innerHTML is not allowed.

  • the dialog to enter data will open when I click on button criar

  • I intend only the transition consola 1 for piso selected, without the data of the consola 1 appear, as shown in figura 2

  • and if possible the transição inversa in case I click on a button retroceder for consola, sane funções which I intend to implement and I know how to do but only need access to the intended content

  • on the compartimentos and equipamentos both will be implemented the same logic in which the consolas with the pisos, just need to access a new container with the piso selected so I can call the funções, container->console->floor

  • the page index.html shall contain only container to create consola e pisos how it was implemented in the other question and after that we will have to use funções in Javascript to place the entries on the page and on phase 2 of this project will be drawn up in Asp.Net MVC + Ajax, so I use more the DOM to generate the página through the elements placed.

  • 1

    I’m curious what you mean "property, non-standard, innerHTML"... as well as non-standard?

  • @Sergio, I think the AP didn’t understand the method I suggested, it doesn’t make use of innerHTML to create the elements, on the contrary, it makes use of the var node = document.importNode(template.content, true), that is, creates a new HTMLElement cloning a FragmentDocument, in order to avoid a reflow of html, thus obtaining a performance similar to document.createElement and a huge gain in legibilidade, and yet we’re using "DOM".

Show 5 more comments

1 answer

1


Renata, follow the full example, as I said in the comments, the document.importNode does not make use of innerHTML, you will be creating and manipulating DOM objects directly, unlike innerHTML, where you will be manipulating a string, and the browser will need to make use of Domparse and provoke an HTML reflow.

var consolas = [];
var container = null;
var pisoClose = null;
var dialog = {};
var pisoAtual = null;


document.addEventListener('readystatechange', function() {
  if (document.readyState == "interactive") {
    container = document.getElementById("consolas");
    dialog.wrapper = document.querySelector("dialog");
    dialog.close = dialog.wrapper.querySelector(".piso-close");
    dialog.add = dialog.wrapper.querySelector(".compartimento-add");
    dialog.desc = document.getElementById("piso_desc");
    dialog.lista = dialog.wrapper.querySelector(".compartimento-list");
    
    var addConsola = document.getElementById("add_consola");
    addConsola.addEventListener("click", function() {
      var consola = new Consola();;
      consolas.push(consola);
    });
    dialog.close.addEventListener("click", function() {
      pisoAtual = null;
      dialog.wrapper.close();
    });
    dialog.add.addEventListener("click", function() {
      var compartimento = new Compartimento(pisoAtual);
      pisoAtual.compartimentos.push(compartimento);
    });
    dialog.desc.addEventListener("input", function() {
      pisoAtual.texto = dialog.desc.value;
    });
  }
});

Consola = function() {
  var that = this;
  this.id = ++Consola.lastId;
  this.pisos = [];
  this.dom = {};
  this.dom.card = document.importNode(this.template.content, true);
  this.dom.titulo = this.dom.card.querySelector(".mdl-card__title-text");
  this.dom.add = this.dom.card.querySelector(".piso-add");  
  this.dom.lista = this.dom.card.querySelector(".piso-list");
  this.dom.titulo.textContent = "Consola " + this.id;

  this.dom.add.addEventListener("click", function(event) {
    that.onAddClick(event);
  });
  

  container.appendChild(this.dom.card);
};

Consola.lastId = 0;
Consola.prototype.template = document.getElementById("tmplConsola");
Consola.prototype.onAddClick = function(event) {
  this.pisos.push(new Piso(this))
}

var Piso = function(consola) {
  var that = this;
  this.id = ++Piso.lastId;
  this.consola = consola;
  this.compartimentos = [];
  this.dom = {};
  this.dom.fragment = document.importNode(this.template.content, true);
  this.dom.item = this.dom.fragment.querySelector(".piso-item");
  this.dom.label = this.dom.item.querySelector(".piso-label");  
  this.dom.edit = this.dom.item.querySelector(".piso-edit");
  this.dom.remove = this.dom.item.querySelector(".piso-remove");  
  this.dom.button = this.dom.item.querySelector(".mdl-button");
  this.dom.menu = this.dom.item.querySelector(".mdl-menu");
  
  this.dom.button.id = "piso-button-menu-" + this.id;
  this.dom.menu.setAttribute("for", "piso-button-menu-" + this.id);
  
  this.texto = "Piso " + this.id;
  this.dom.edit.addEventListener("click", function(event) {
    that.onEditClick(event);
  });
  this.dom.remove.addEventListener("click", function(event) {
    that.onRemoveClick(event);
  });
  this.consola.dom.lista.appendChild(this.dom.item);
  componentHandler.upgradeDom();
};

Object.defineProperty(Piso.prototype, "texto", {
  get: function() {
    return this.dom.label.textContent;
  },
  set: function(value) {
    this.dom.label.textContent = value;
  }
});

Piso.lastId = 0;
Piso.prototype.template = document.getElementById("tmplPiso");
Piso.prototype.onRemoveClick = function(event) {
  var index = this.consola.pisos.indexOf(this);
  this.consola.pisos.splice(index, 1);
  this.consola.dom.lista.removeChild(this.dom.item);
  this.consola = null;
}
Piso.prototype.onEditClick = function(event) {
  pisoAtual = this;
  dialog.desc.value = pisoAtual.texto;
  dialog.desc.parentElement.classList.toggle("is-dirty", pisoAtual.texto);  
  // limpando a lista.
  while (dialog.lista.firstChild) {
    dialog.lista.removeChild(dialog.lista.firstChild);
  }  
  this.compartimentos.forEach(function (compartimento, indice) {
    dialog.lista.appendChild(compartimento.dom.item);
  });
  dialog.wrapper.showModal();
}

var Compartimento = function(piso) {
  var that = this;
  this.id = ++Compartimento.lastId;
  this.piso = piso;
  this.dom = {};
  this.dom.fragment = document.importNode(this.template.content, true);
  this.dom.item = this.dom.fragment.querySelector(".compartimento-item");
  this.dom.label = this.dom.item.querySelector(".compartimento-label"); 
  this.dom.remove = this.dom.item.querySelector(".compartimento-remove"); 
  
  
  this.texto = "Compartimento " + this.id;
  this.dom.remove.addEventListener("click", function(event) {
    that.onRemoveClick(event);
  });
  dialog.lista.appendChild(this.dom.item);
  componentHandler.upgradeDom();
};

Object.defineProperty(Compartimento.prototype, "texto", {
  get: function() {
    return this.dom.label.textContent;
  },
  set: function(value) {
    this.dom.label.textContent = value;
  }
});

Compartimento.lastId = 0;
Compartimento.prototype.template = document.getElementById("tmplCompartimento");
Compartimento.prototype.onRemoveClick = function(event) {
  var index = this.piso.compartimentos.indexOf(this);
  this.piso.compartimentos.splice(index, 1);
  this.piso = null;
  dialog.lista.removeChild(this.dom.item);  
}
html,
body {
  position: relative;
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
  background-color: whitesmoke;
}

.mdl-layout__header {
  height: 40px !important;
  display: block !important;
}

.mdl-card__title {
  background-color: rgb(0, 150, 136);
}
.mdl-card__title-text, .mdl-card__menu .piso-add i {
  color: white !important;
}
.mdl-card__supporting-text, .mdl-dialog__content, .mdl-layout__content {
  overflow: auto !important;
  padding: 0px !important;
}
.mdl-card__supporting-text {  
  overflow: hidden !important;
  padding: 0px !important;
  width: 100% !important;
}
.piso-list.mdl-list, .compartimento-list.mdl-list {
  height: 200px;
  margin: 0px !important;
  overflow: auto !important;
}
.mdl-card {
  width: 100%;
}
.mdl-list__item-secondary-content div {
  float: left;
}
dialog {
  width: 320px !important;
}
#add_consola {
  margin: auto;
  position: absolute;
  bottom: 10px;
  right: 10px;
  z-index: 2;
}
#container {
  position: absolute;
  top: 0px;
  right: 0px;
  bottom: 0px;
  left: 0px;
  margin: auto;
  background-color: white;
  width: 100%;
  max-width: 1080px;
  box-shadow: 0px 0px 10px black;
}
#consolas {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: auto;
  z-index: 1;
  padding: 0px;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.teal-red.min.css" />
<script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script>

<div id="container">
  <div class="demo-layout-transparent mdl-layout mdl-js-layout">
    <header class="mdl-layout__header mdl-layout__header--scroll">
      <div class="mdl-layout__header-row">
        <span class="mdl-layout-title">Sistemas Domóticos</span>
      </div>
    </header>
    <main class="mdl-layout__content">
      <div id="consolas" class="mdl-grid">        
      </div>          
    </main>
    <button id="add_consola" class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored">
      <i class="material-icons">add</i>
    </button>
  </div>
</div>
<dialog class="mdl-dialog">
  <h4 class="mdl-dialog__title">Editar Piso</h4>
  <div class="mdl-dialog__content">
    <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
      <input class="mdl-textfield__input" type="text" id="piso_desc">
      <label class="mdl-textfield__label" for="piso_desc">Descrição</label>
    </div>
    <ul class="compartimento-list demo-list-control mdl-list">

    </ul>
  </div>
  <div class="mdl-dialog__actions">
    <button type="button" class="piso-close mdl-button">Fechar</button>
    <button type="button" class="compartimento-add mdl-button">Add Compartimento</button>
  </div>
</dialog>

<template id="tmplConsola">
  <div class="consola mdl-cell mdl-cell--4-col mdl-cell--6-col-tablet mdl-cell--12-col-phone">
    <div class="mdl-card mdl-shadow--2dp">
      <div class="mdl-card__title mdl-card--expand">
        <h2 class="mdl-card__title-text">Consola</h2>
      </div>
      <div class="mdl-card__supporting-text">
        <ul class="piso-list mdl-list">

        </ul>
      </div>
      <div class="mdl-card__menu">
        <button class="piso-add mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect">
          <i class="material-icons">add</i>
        </button>
      </div>
    </div>
  </div>
</template>

<template id="tmplPiso">
  <li class="piso-item mdl-list__item">
    <span class="mdl-list__item-primary-content">
      <span class="piso-label"></span>             
    </span>
    <span class="mdl-list__item-secondary-content">
      <button class="mdl-button mdl-js-button mdl-button--icon">
        <i class="material-icons">more_vert</i>
      </button>
      <ul class="mdl-menu mdl-menu--bottom-right mdl-js-menu mdl-js-ripple-effect">
        <li class="piso-edit mdl-menu__item">Editar</li>
        <li class="piso-remove mdl-menu__item">Excluir</li>
      </ul>
    </span>
  </li>
</template>

<template id="tmplCompartimento">
  <li class="compartimento-item mdl-list__item">
    <span class="mdl-list__item-primary-content">
      <span class="compartimento-label"></span>             
    </span>
    <span class="mdl-list__item-secondary-content">
      <button class="compartimento-remove mdl-button mdl-js-button mdl-button--icon mdl-button--colored">
        <i class="material-icons">delete_forever</i>
      </button>
    </span>
  </li>
</template>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.