eCCO TUTTA LA PARTE CHE HO OMESSO, HO CAPITO COSA INTENDI MA ESSENDO TANTISSIME RIGHE DI CODICE NON SCRITTE DA ME HO PAURA DI FARE DANNI!

codice:
  _createClass(Blocks, [{
    key: "getBlockById",
    //Ritorna il blocco corrispondente all'id fornito	
    value: function getBlockById(id) {
      for (var i = 0; i < this.blocchi.length; i++) {
        if (this.blocchi[i]["id"] == id) {
          return new Block(this.blocchi[i]);
        }
      }
    }
  }, {
    key: "getblockbyblocco",
    value: function getblockbyblocco(numeroDiBlocco) {
      for (var i = 0; i < this.blocchi.length; i++) {
        var blocco = this.blocchi[i]["blocco"].split(",", [1]);


        if (blocco == numeroDiBlocco) {
          return new Block(this.blocchi[i]);
			
        }
      }
    } //Ritorna la posizione in base all'id nell'intenro del vettore


  }, {
    key: "getPosById",
    value: function getPosById(id) {
      var length = this.blocchi.length;
      var rt = "";


      for (var i = 0; i < length; i++) {
        if (this.blocchi[i]["id"] == id) {
          rt = i;
        }
      }


      return rt;
      console.log("gpbid:", rt);
    } //Prende il blocco dopo quello passato


  }, {
    key: "getNextBlock",
    value: function getNextBlock(currentBlock) {
      var posCurrentBlock = this.getPosById(currentBlock.id);
      var posNextBlock = posCurrentBlock + 1;
      return this.blocchi[posNextBlock];
		
    } //Prende quello precedente a quello passato


  }, {
    key: "getPreviusBlock",
    value: function getPreviusBlock(currentBlock) {
      //debugger;
      var posCurrentBlock = this.getPosById(currentBlock.id);
      var posPreviusBlock = posCurrentBlock - 1;
      return this.blocchi[posPreviusBlock];
    } //Prende il blocco tramite la posizione del vettore


  }, {
    key: "getBlockByIndex",
    value: function getBlockByIndex(index) {
      return this.blocchi[index];
    }
  }, {
    key: "resetSezioni",
    value: function resetSezioni(xs) {
      this.sezioni = xs;
    } //Prepara i titoli nella pagina


  }, {
    key: "prepTitoli",
    value: function prepTitoli(id, retBlock) {
      console.log("ret:", retBlock);
      retBlock = undefined ? false : retBlock; //debugger;


      console.log("preptitid");
      var tmpBlock = {};
      var avalaibleTitle = [];


      for (var i = 0; i < allBlocks.size; i++) {
        tmpBlock = allBlocks.getBlockByIndex(i);


        if (tmpBlock.isTitle) {
          avalaibleTitle.push(tmpBlock);
        }
      }


      var passedBlock = allBlocks.getBlockById(id);
      var neededTitles = [];
      var titolo = "";
      console.log("blocco passato", passedBlock);
      console.log("blocco a disposizione: ", avalaibleTitle);


      do {
        for (var i = 0; i < avalaibleTitle.length; i++) {
          if (passedBlock.titolo == avalaibleTitle[i].id) {
            neededTitles.push(avalaibleTitle[i]);
            passedBlock = avalaibleTitle[i];
            titolo = passedBlock.titolo;
          }
        }
      } while (titolo != "");


      if (retBlock) {
        return neededTitles;
      } else {
        var html = "";


        for (var i = neededTitles.length - 1; i >= 0; i--) {
          $("#genera").append(neededTitles[i].contenuti);
          $("#" + neededTitles[i].id).addClass("titolo"); //console.log(neededTitles[i].contenuti);


          var idstmp = neededTitles[i].id; //console.log("primo blocco: r 212 " + firstblock);
        }
      }
    } //riempe la pagina di blocchi fino a riempirla


  }, {
    key: "fillPage",
    value: function fillPage(neededBlocks) {
      var titoli = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "no";
      //debugger;
      var html = "";
      var counter = 0;


      if (titoli == "yes") {
        counter = 0;


        while ($(document).height() <= $(window).height()) {
          if (neededBlocks[counter] != "" && neededBlocks[counter] != undefined) {
            //   console.log("blocco generato");
            //console.log(neededBlocks[counter]);
            $("#genera").append(neededBlocks[counter].contenuti);
            if (neededBlocks[counter].tipo_sez == "T") {
              $("#" + neededBlocks[counter].id).addClass("titolo");
            }
          }


          counter++;


          if (counter >= neededBlocks.length) {
            break;
          }
        }
      } else if (titoli == "no") {
        counter = 0;


        while ($(document).height() == $(window).height()) {
          if (neededBlocks[counter] != "") {
            if (neededBlocks[counter].tipo_sez != "T") {
              $("#genera").append(neededBlocks[counter].contenuti); //console.log(neededBlocks[counter].id);
              //console.log(counter);


              counter++;
            } else {
              counter++;
            }
          }
        }
      }
    } //Carica la pagina a partire dal blocco fornito


  }, {
    key: "loadPage",
    value: function loadPage(blockid) {
      //debugger;
      console.log("loadpagecalled");
      var neededBlocks = [];
      var lastBlockID = blockid;
      var indexOfLastBlock = this.getPosById(lastBlockID);


      for (var i = indexOfLastBlock; i < allBlocks.size; i++) {
        neededBlocks.push(allBlocks.blocchi[i]);
      }


      var html = ""; //console.log("254, loadpagecalled for eseguito, titoli: "+titoli);


      if (titoli == "once" || titoli == "yes") {
        if (this.getBlockById(blockid).tipo_sez == "T") {
          console.log("tipo T");
          this.prepTitoli(lastBlockID);
          this.fillPage(neededBlocks, titoli);
        } else {
          console.log("boh");
          this.prepTitoli(lastBlockID);
          this.fillPage(neededBlocks, titoli);
        }
      } else if (titoli == "no") {
        console.log("no titoli");
        this.fillPage(neededBlocks);
      }


      console.log("needed", neededBlocks);
    } //carica il prossimo blocco partendo dall'ultimo


  }, {
    key: "loadNext",
    value: function loadNext() {
      var thisBlockid = $("section").last().attr("id");
      var thisBlock = this.getBlockById(thisBlockid);
      var nextBlock = this.getNextBlock(thisBlock);
      $("#genera").append(nextBlock.contenuti);
	
      $("section:last").css({
        opacity: 0,
        visibility: "visible"
      }).animate({
        opacity: 1
      }, 'slow');
		
    } //Ottiene l'index di un blocco


  }, {
    key: "getIndex",
    value: function getIndex(block) {
      for (var i = 0; i < this.size; i++) {
        if (block.id = this.blocchi[i].id) {
          return i;
        }
      }
    }
  }, {
    key: "getAlltypeBBlock",
    value: function getAlltypeBBlock(from) {
      var startingPos = this.getIndex(from);
      var ndBlocks = [];


      for (var i = startingPos; i <= this.size; i++) {
        ndBlocks.push(this.blocchi[i]);
      }


      return ndBlocks;
    } //Carica il blocco precedente 


  }, {
    key: "loadPrevius",
    value: function loadPrevius() {
      //Prendo il primo blocco che non è un titolo
      var primoBloccoNT = {};
      var noTitleBlocks = [];
      $("#genera section").each(function (numElem, Elem) {
        if (!$(Elem).hasClass("titolo")) {
          noTitleBlocks.push($(Elem)); //noTitleBlocks = vettore di selettori a tutti i blocchi senza classe titolo
        }
      });
      primoBloccoNT = noTitleBlocks[0]; //Primo blocco non titolo della pagina


      primoBloccoNT = this.getBlockById($(primoBloccoNT).attr("id"));
      var bloccoPrecedente = this.getPreviusBlock(primoBloccoNT);
      var titBloccoPrec = this.getBlockById(bloccoPrecedente.titolo);
      console.log(bloccoPrecedente);
      var quit = false;
      var tipoBlocco; //debugger;
		


      do {
        tipoBlocco = bloccoPrecedente.tipo_sez;
		  
        if ($("#" + bloccoPrecedente.id).length <= 0) {
          //Se il BP non è nel DOM
          if (!(titBloccoPrec === undefined) && $("#" + titBloccoPrec.id).length > 0) {
            //Se è presente il titolo del blocco precedente stampo il BP sotto il TBP ed esco dal ciclo
           $("#genera").prepend(bloccoPrecedente.contenuti);
            quit = true;
          } else {
            //Se invece non è presente il titolo del TBP stampo il BP in cima
            $("#genera").prepend(bloccoPrecedente.contenuti);
            quit = true;
          }


          switch (tipoBlocco) {
            case "T":
              $("#" + bloccoPrecedente.id).addClass("titolo");
              break;


            case "B":
              $("#" + bloccoPrecedente.id).addClass("block");
              break;


            case "I":
              $("#" + bloccoPrecedente.id).addClass("intro");
              break;
          }
        } else {
          bloccoPrecedente = this.getPreviusBlock(bloccoPrecedente); //Rieseguo il ciclo dando a BP il blocco che lo precede


          quit = false;
        }
      } while (quit == false); //  $("body").scrollTo("section:first");




      $("body").scrollTop = $("section:first").offset().top;
    }
  }, {
    key: "hasFather",
    value: function hasFather(block, ret) {
      if (ret != undefined && ret == "ret") {
        if (this.getBlockById(block.titolo) != undefined) {
          return this.getBlockById(block.titolo);
        } else {
          return false;
        }
      } else {
        if (this.getBlockById(block.titolo) != undefined) {
          return true;
        } else {
          return false;
        }
      }
    }
  }, {
    key: "getBlockLevel",
    value: function getBlockLevel(block) {
      var hasFatherRes = false;
      var currBlock = block;
      var levelCounter = 0;


      do {
        if (this.hasFather(currBlock)) {
          levelCounter++;
          currBlock = this.hasFather(currBlock, "res");
          hasFatherRes = true;
        } else {
          hasFatherRes = false;
        }
      } while (hasFatherRes);


      return levelCounter;
    }
  }, {
    key: "size",
    get: function get() {
      return this.blocchi.length;
    }
  }]);


  return Blocks;
}();