<commit_before>//=====================
// Tram
function Fiber(id, name, first_site, end_site, paths, type, m){
  this.id = id;
  this.name = name;
  this.first_site = first_site;
  this.end_site = end_site;
  this.type = (!type) ? m.type_path_default : type;
  this.polyline = null;
  this.paths = paths;
  this.sites = [];
  this.color_building = "red";
  this.color_did = "blue";
  this.color_mouseover = "green";
  this.observations = null;

  this.map_parent = m;

  //Calcule sites?
}
Fiber.prototype.setFirstSite = function(b){
  this.first_site = b.id;
  this.sites.push(b.id);
};
Fiber.prototype.setEndSite = function(b){
  this.end_site = b.id;
  this.draw();
  this.map_parent.setIconInSiteById(this.first_site);
  this.map_parent.setIconInSiteById(this.end_site);
  this.map_parent.paths.push(this);
  this.map_parent.active_path = "";
  if (this.end_site != this.first_site) {
    this.save();
  } else {
    // Esborrar link
    this.clear();
  }
};
Fiber.prototype.changeTypeFiber = function(status, type) {
  this.polyline.setStyle( { color: this.findFiberColor(status, type) });
}
Fiber.prototype.findFiberColor = function(status, type) {
  if (!type) type = this.type;
  if (!type) type = this.map_parent.type_path_default;
  type_idx = this.map_parent.type_path.indexOf(type);

  var color;
  switch( status ){
    case "over":
    case "active":
    case "grey":
      color = this.map_parent.type_path_colors[status][type_idx];
      break;
    default:
      color = (this.map_parent.layerActive == 'civil') ? this.map_parent.type_path_colors['normal'][type_idx] : this.map_parent.type_path_colors['grey'][type_idx];
  }
  return color;
}
Fiber.prototype.clear = function() {
  this.map_parent.map.removeLayer(this.polyline);
};
Fiber.prototype.getAllDots = function() {
  var that = this;

  var dots = [];
  for(var x = 0; x < this.paths.length; x++){
    var actPath = this.map_parent.getPath(this.paths[x]);
    // Si el primer site del path actual, no és el site per numero
    // que toca s'han de guardar els punts al reves
    if (actPath.first_site == this.sites[x]) {
      for(var y = 0; y < actPath.dots.length; y++){
        var actDot = actPath.dots[y];
        if (dots.length == 0){
          dots.push(actDot);
        } else {
          // Mirem que no hi hagi el punt repetit!!
          var beforeDot = dots[dots.length - 1]
          if ((beforeDot.lat != actDot.lat) || (beforeDot.lng != actDot.lng)){
            dots.push(actDot);
          }
        }
      }
    } else {
      for(var y = actPath.dots.length - 1; y >= 0; y--){
        var actDot = actPath.dots[y];
        if (dots.length == 0){
          dots.push(actDot);
        } else {
          // Mirem que no hi hagi el punt repetit!!
          var beforeDot = dots[dots.length - 1]
          if ((beforeDot.lat != actDot.lat) || (beforeDot.lng != actDot.lng)){
            dots.push(actDot);
          }
        }
      }
    }
  }
  return dots;
}

Fiber.prototype.draw = function() {
  var that = this;
  // Pintar el Tram
  if (this.polyline) {
    this.clear();
  }

  var color = (this.end_site) ? this.findFiberColor() : this.findFiberColor('active');

  var dots = this.getAllDots();
  this.polyline = new L.Polyline(dots, {
      color: color,
      weight: 5,
      opacity: 0.5,
      smoothFactor: 1
  }).on('click', function(e) { return that.onFiberClick(e); })
    .on('mouseover', function(e) { return that.onFiberMouseOver(e); })
    .on('mouseout', function(e) { return that.onFiberMouseOut(e); })
    .addTo(this.map_parent.map);

};
Fiber.prototype.addPath = function(path) {
  if (!this.end_site){
    if(this.first_site) {
      console.log(path);
      this.paths.push(path);
      this.draw();
    } else {
      console.log("Els trams comencen a una caixa.");
    }
  } else {
    console.log("Aquest tram ja està tancat.");
  }
};
Fiber.prototype.addSite = function (site){
  var countBox = site.countBox();
  var lastSiteInFiber = this.sites[this.sites.length - 1];
  // Comprovar si existeix un path entre aquest dos sites!
  var pathBetween = this.map_parent.getPathBeetwenSites(lastSiteInFiber, site.id);

  // No  hi ha path entre aquest dos i no estem dient que ja estem a l'últim site!
  if ((!pathBetween)  && (lastSiteInFiber != site.id)) {
      console.log("Aquest site no està seguit de l'altre!!!");
      return;
  }
  if ((countBox > 0 ) && (lastSiteInFiber == site.id)){
    // Sí, és el final
    console.log('Tancar fibra.');
    this.setEndSite(this);
    //this.getAllDots();
  } else {
    // No, només pot ser un intermig
    console.log('Intermedial site!');
    this.addPath(pathBetween);
    this.sites.push(site.id);
  }
}
Fiber.prototype.save = function (){
  var that = this;
  strUrl = this.map_parent.serverUrl + "/path";
  console.log('API call: ' + strUrl);
  if (this.first_site == null || this.end_site == null) {
    console.log("First or End site does not have id, please check this problem.");
    return;
  }
  $.post( strUrl, JSON.stringify({ "first": this.first_site, "last": this.end_site,
              "intermedial": JSON.stringify(this.dots), "project": this.map_parent.active_project.id,
              "type": this.type}))
    .done(function( data ) {
      that.id = data.id;
    }, "json");
};
Fiber.prototype.loadTypes = function(SelectField){
  var that = this;

  SelectField.find('option').remove().end();
  $.each(this.map_parent.type_path, function(key, value) {
    var option = $("<option></option>")
                    .attr("value",value)
                    .text(value);
    if (that.type == value) {
      option.attr("selected","selected");
    }
    SelectField.append(option);<commit_msg>Debug: Fiber save system<commit_after>//=====================
// Tram
function Fiber(id, name, first_site, end_site, paths, type, m){
  this.id = id;
  this.name = name;
  this.first_site = first_site;
  this.end_site = end_site;
  this.type = (!type) ? m.type_path_default : type;
  this.polyline = null;
  this.paths = paths;
  this.sites = [];
  this.color_building = "red";
  this.color_did = "blue";
  this.color_mouseover = "green";
  this.observations = null;

  this.map_parent = m;

  //Calcule sites?
}
Fiber.prototype.setFirstSite = function(b){
  this.first_site = b.id;
  this.sites.push(b.id);
};
Fiber.prototype.setEndSite = function(b){
  this.end_site = b.id;
  this.draw();
  this.map_parent.setIconInSiteById(this.first_site);
  this.map_parent.setIconInSiteById(this.end_site);
  this.map_parent.paths.push(this);
  this.map_parent.active_path = "";
  if (this.end_site != this.first_site) {
    this.save();
  } else {
    // Esborrar link
    this.clear();
  }
};
Fiber.prototype.changeTypeFiber = function(status, type) {
  this.polyline.setStyle( { color: this.findFiberColor(status, type) });
}
Fiber.prototype.findFiberColor = function(status, type) {
  if (!type) type = this.type;
  if (!type) type = this.map_parent.type_path_default;
  type_idx = this.map_parent.type_path.indexOf(type);

  var color;
  switch( status ){
    case "over":
    case "active":
    case "grey":
      color = this.map_parent.type_path_colors[status][type_idx];
      break;
    default:
      color = (this.map_parent.layerActive == 'civil') ? this.map_parent.type_path_colors['normal'][type_idx] : this.map_parent.type_path_colors['grey'][type_idx];
  }
  return color;
}
Fiber.prototype.clear = function() {
  this.map_parent.map.removeLayer(this.polyline);
};
Fiber.prototype.getAllDots = function() {
  var that = this;

  var dots = [];
  for(var x = 0; x < this.paths.length; x++){
    var actPath = this.map_parent.getPath(this.paths[x]);
    // Si el primer site del path actual, no és el site per numero
    // que toca s'han de guardar els punts al reves
    if (actPath.first_site == this.sites[x]) {
      for(var y = 0; y < actPath.dots.length; y++){
        var actDot = actPath.dots[y];
        if (dots.length == 0){
          dots.push(actDot);
        } else {
          // Mirem que no hi hagi el punt repetit!!
          var beforeDot = dots[dots.length - 1]
          if ((beforeDot.lat != actDot.lat) || (beforeDot.lng != actDot.lng)){
            dots.push(actDot);
          }
        }
      }
    } else {
      for(var y = actPath.dots.length - 1; y >= 0; y--){
        var actDot = actPath.dots[y];
        if (dots.length == 0){
          dots.push(actDot);
        } else {
          // Mirem que no hi hagi el punt repetit!!
          var beforeDot = dots[dots.length - 1]
          if ((beforeDot.lat != actDot.lat) || (beforeDot.lng != actDot.lng)){
            dots.push(actDot);
          }
        }
      }
    }
  }
  return dots;
}

Fiber.prototype.draw = function() {
  var that = this;
  // Pintar el Tram
  if (this.polyline) {
    this.clear();
  }

  var color = (this.end_site) ? this.findFiberColor() : this.findFiberColor('active');

  var dots = this.getAllDots();
  this.polyline = new L.Polyline(dots, {
      color: color,
      weight: 5,
      opacity: 0.5,
      smoothFactor: 1
  }).on('click', function(e) { return that.onFiberClick(e); })
    .on('mouseover', function(e) { return that.onFiberMouseOver(e); })
    .on('mouseout', function(e) { return that.onFiberMouseOut(e); })
    .addTo(this.map_parent.map);

};
Fiber.prototype.addPath = function(path) {
  if (!this.end_site){
    if(this.first_site) {
      console.log(path);
      this.paths.push(path);
      this.draw();
    } else {
      console.log("Els trams comencen a una caixa.");
    }
  } else {
    console.log("Aquest tram ja està tancat.");
  }
};
Fiber.prototype.addSite = function (site){
  var countBox = site.countBox();
  var lastSiteInFiber = this.sites[this.sites.length - 1];
  // Comprovar si existeix un path entre aquest dos sites!
  var pathBetween = this.map_parent.getPathBeetwenSites(lastSiteInFiber, site.id);

  // No  hi ha path entre aquest dos i no estem dient que ja estem a l'últim site!
  if ((!pathBetween)  && (lastSiteInFiber != site.id)) {
      console.log("Aquest site no està seguit de l'altre!!!");
      return;
  }
  if ((countBox > 0 ) && (lastSiteInFiber == site.id)){
    // Sí, és el final
    console.log('Tancar fibra.');
    this.setEndSite(this);
    //this.getAllDots();
  } else {
    // No, només pot ser un intermig
    console.log('Intermedial site!');
    this.addPath(pathBetween);
    this.sites.push(site.id);
  }
}
Fiber.prototype.save = function (){
  var that = this;
  strUrl = this.map_parent.serverUrl + "/fiber";
  console.log('API call: ' + strUrl);
  console.log(this);
  if (this.first_site == null || this.end_site == null) {
    console.log("First or End site does not have id, please check this problem.");
    return;
  }
  $.post( strUrl, JSON.stringify({ "first": this.first_site, "last": this.end_site,
              "intermedial": JSON.stringify(this.dots), "project": this.map_parent.active_project.id,
              "type": this.type}))
    .done(function( data ) {
      that.id = data.id;
    }, "json");
};
Fiber.prototype.loadTypes = function(SelectField){
  var that = this;

  SelectField.find('option').remove().end();
  $.each(this.map_parent.type_path, function(key, value) {
    var option = $("<option></option>")
                    .attr("value",value)
                    .text(value);
    if (that.type == value) {
      option.attr("selected","selected");
    }
    SelectField.append(option);
{'fraction_non_alphanumeric': 0.15969481715338069, 'fraction_numerical': 0.0024554941682013503, 'mean_word_length': 2.829415715245131, 'pattern_counts': {'":': 16, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4689927', 'n_tokens_mistral': 4063, 'n_tokens_neox': 3985, 'n_words': 843}