transition between main container for a new container in Javascript using DOM

1

Hello, I have a function in Javascript using DOM to create a page and then I want to display this same page in the container and for this the data displayed would have to be only the intended one and only that container does not clear to delete the content as it shows in figura 2 .

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

andwhenIselectpisoandIwanttoseethecontentthroughbuttonviewFloorandIhavetheeventscript

/***Funçãoparavisualizardadosnopiso**/Consola.prototype.onVerClick=function(event){this.pisos.forEach(function(piso,indice){if((piso.selected)&&(pisoinstanceofPiso)){//chamaafunçãopiso.criarCompartimento();}});}

andIwanttoseethispagewithinthesamecontainer,asshownbyfigura2

funçãohasbeenimplementedinthisscript

/***Funçãoparacriarcompartimento*/Piso.prototype.criarCompartimento=function(){varh1=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 just that, as shown by figura 3

AndanotherquestionaroseisthatifIwanttoreturntocontainermainconsola1,itispossiblewithoutlosingdata,soIwillhavetocreateanewfunção,orcreateeventoforonenewbuttonretrocederoristhereanothersolution?

Thefinalgoalistoreachatleastthisstructure,asshowninfigura4,somepageshavealreadybeenimplementedseparatelyandonlyneedtosynchronize

    
asked by anonymous 08.05.2016 / 02:32

1 answer

1

Renata, follows the complete example, as I said in the comments, document.importNode does not make use of innerHTML, you're going to be creating and manipulating DOM objects directly, unlike innerHTML, where you go is manipulating a string, and the browser will need to make use of DOMParse and cause 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><divid="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>
    
08.05.2016 / 14:10