<commit_before>Teikei.module("Places", function(Places, App, Backbone, Marionette, $, _) {

  Places.Controller = Backbone.Marionette.Controller.extend( {

    initialize: function(){
      this.placeMessage = new Teikei.PlaceMessage.Model();
      this.collection = new Teikei.Places.Collection();
      this.collection.bind("reset", function(collection){
        App.vent.trigger("places:change", collection);
      });

      this.mapView = new Teikei.Places.MapView({
        collection: this.collection,
        defaultBounds: this.areas.default.boundingBox
      });


      this.areaSelectView = new Teikei.Places.AreaSelectView({
        areas: this.areas
      });
      App.controlsRegion.show(this.areaSelectView);

      this.areaSelectView.bind("select:area", this.showArea, this);

      this.mapView.bind("select:details", this.showDetails, this);
      this.mapView.bind("select:network", this.showNetwork, this);

      App.vent.on("user:add:depot", this.showEntryDepotForm, this);
      App.vent.on("user:add:farm", this.showEntryFarmForm, this);
      App.vent.on("user:logout:success", this.refreshCollection, this);
      App.vent.on("user:show:entrylist", this.showEntryList, this);

      App.vent.on("edit:entry", this.editEntry, this);
      App.vent.on("delete:entry", this.deleteEntry, this);

      App.vent.on("place:deleted", this.updateMap, this);
      App.vent.on("place:added", this.placeAdded, this);

      this.refreshCollection();
    },

    placeAdded: function() {
      // quick&dirty fix to get the correct network view
      // after place has been added
      // TODO: should be fixed by using proper event handling later,
      // as part of further refactoring of frontend event handling
      this.refreshCollection();
      this.updateMap();
    },

    refreshCollection: function() {
      this.collection.fetch({reset: true, async: false});
    },

    updateMap: function() {
      this.mapView.updateMap();
    },

    editEntryById: function(id) {
      var model = this.collection.get(id);
      this.editEntry(model);
    },

    editEntry: function(model) {
      var showEntryForm = this.showEntryForm;
      model.fetch({
        success: function(model, response, options) {
          var type = model.get("type");
          Backbone.history.navigate("places/" + model.id + "/edit");
          if (type == "Farm") {
            showEntryForm(Places.EntryFarmView, "Angaben zum Betrieb editieren", model, model.collection);
          }
          else if (type == "Depot") {
            showEntryForm(Places.EntryDepotView, "Angaben zur Gruppe editieren", model, model.collection);
          }
        }
      });
    },

    deleteEntry: function(model) {
      this.deleteEntryView = new Places.DeleteEntryView({model: model});
      App.modalRegion.show(this.deleteEntryView);
    },

    submitPlaceMessage: function(data) {
      var model = this.placeMessage;
      // Wrap form data into :place_form hash to satisfy the API controller.
      var messageData = { place_form: data };

      model.save(messageData, {
        success: function(model, response, options) {
          var message = model.get("message");
          if (message === undefined) {
            message = "Deine Nachricht wurde erfolgreich versandt.";
          }
          App.vent.trigger("place:message:success", message);
        },
        error: function(model, xhr, options) {
          App.vent.trigger("place:message:failure", xhr);
        }
      });
    },

    showEntryDepotForm: function() {
      Backbone.history.navigate("places/new/depot");
      this.showEntryForm(Places.EntryDepotView, "Neues Depot eintragen", new Places.Model(), this.collection);
    },

    showEntryFarmForm: function() {
      Backbone.history.navigate("places/new/farm");
      this.showEntryForm(Places.EntryFarmView, "Neuen Betrieb eintragen", new Places.Model(), this.collection);
    },

    showEntryForm: function(EntryView, headline, model, collection) {
      this.entryView = new EntryView ({
        model: model,
        collection: collection,
        headline: headline
      });

      App.modalRegion.show(this.entryView);
    },

    showEntryList: function() {
      var currentUser = Teikei.currentUser;
      var filteredCollection;
      if (currentUser) {
        filteredCollection = this.collection.byUser(currentUser.get('id'));
        filteredCollection.comparator = function(model) {
          return [model.get("type"), model.get("name")];
        }
        filteredCollection.sort();
      }
      this.entryListView = new Places.EntryListView({
        collection: filteredCollection
      });
      App.modalRegion.show(this.entryListView);
    },

    showTip: function(id) {
      this.mapView.showTip(id);
    },

    showDetails: function(id) {
      Backbone.history.navigate('places/' + id + '/details');
      var model = this.collection.get(id);
      var detailsView = new Places.DetailsMessageFormView({ model: model });
      detailsView.bind("placeMessageForm:submit", this.submitPlaceMessage, this);
      model.fetch({
        success: function(){
          App.modalRegion.show(detailsView);
        }
      });
      this.detailsView = detailsView;
    },

    showNetwork: function(id) {
      Backbone.history.navigate('places/' + id + '/network');
      var model = this.collection.get(id);
      var mapView = this.mapView;
      model.fetch({
        reset: true,
        success: function(){
          mapView.hilightNetwork(model);
        }
      });
    },

    showArea: function(area){
      Backbone.history.navigate('region/' + area);
      var bounds = this.areas[area].boundingBox;
      this.areaSelectView.setOption(area);
      this.mapView.showArea(bounds);
    },

    areas: {
      default: {
        boundingBox: [[47.2703, 5.8667],[54.0585, 15.0419]],
        displayName: "– Region auswählen –"
      },
      bayern: {
        boundingBox: [[ 47.375413747749, 9.0138020303575 ],[ 50.52894177871, 13.778139870618 ]],
        displayName: "Bayern"
      },
      badenwuerttemberg: {
        boundingBox: [[ 47.53649305, 7.51464013 ],[ 49.79146623, 10.49316881 ]],
        displayName: "Baden-Württemberg"
      },
      berlin: {
        boundingBox: [[ 52.34036388, 13.08202030 ],[ 52.67513452, 13.75919894 ]],
        displayName: "Berlin"
      },
      brandenburg: {
        boundingBox: [[ 51.37290595, 11.52257448 ],[ 53.4422936, 14.76043039 ]],<commit_msg>Fix alphabetical order ... Bayern is not on pole position here :}<commit_after>Teikei.module("Places", function(Places, App, Backbone, Marionette, $, _) {

  Places.Controller = Backbone.Marionette.Controller.extend( {

    initialize: function(){
      this.placeMessage = new Teikei.PlaceMessage.Model();
      this.collection = new Teikei.Places.Collection();
      this.collection.bind("reset", function(collection){
        App.vent.trigger("places:change", collection);
      });

      this.mapView = new Teikei.Places.MapView({
        collection: this.collection,
        defaultBounds: this.areas.default.boundingBox
      });


      this.areaSelectView = new Teikei.Places.AreaSelectView({
        areas: this.areas
      });
      App.controlsRegion.show(this.areaSelectView);

      this.areaSelectView.bind("select:area", this.showArea, this);

      this.mapView.bind("select:details", this.showDetails, this);
      this.mapView.bind("select:network", this.showNetwork, this);

      App.vent.on("user:add:depot", this.showEntryDepotForm, this);
      App.vent.on("user:add:farm", this.showEntryFarmForm, this);
      App.vent.on("user:logout:success", this.refreshCollection, this);
      App.vent.on("user:show:entrylist", this.showEntryList, this);

      App.vent.on("edit:entry", this.editEntry, this);
      App.vent.on("delete:entry", this.deleteEntry, this);

      App.vent.on("place:deleted", this.updateMap, this);
      App.vent.on("place:added", this.placeAdded, this);

      this.refreshCollection();
    },

    placeAdded: function() {
      // quick&dirty fix to get the correct network view
      // after place has been added
      // TODO: should be fixed by using proper event handling later,
      // as part of further refactoring of frontend event handling
      this.refreshCollection();
      this.updateMap();
    },

    refreshCollection: function() {
      this.collection.fetch({reset: true, async: false});
    },

    updateMap: function() {
      this.mapView.updateMap();
    },

    editEntryById: function(id) {
      var model = this.collection.get(id);
      this.editEntry(model);
    },

    editEntry: function(model) {
      var showEntryForm = this.showEntryForm;
      model.fetch({
        success: function(model, response, options) {
          var type = model.get("type");
          Backbone.history.navigate("places/" + model.id + "/edit");
          if (type == "Farm") {
            showEntryForm(Places.EntryFarmView, "Angaben zum Betrieb editieren", model, model.collection);
          }
          else if (type == "Depot") {
            showEntryForm(Places.EntryDepotView, "Angaben zur Gruppe editieren", model, model.collection);
          }
        }
      });
    },

    deleteEntry: function(model) {
      this.deleteEntryView = new Places.DeleteEntryView({model: model});
      App.modalRegion.show(this.deleteEntryView);
    },

    submitPlaceMessage: function(data) {
      var model = this.placeMessage;
      // Wrap form data into :place_form hash to satisfy the API controller.
      var messageData = { place_form: data };

      model.save(messageData, {
        success: function(model, response, options) {
          var message = model.get("message");
          if (message === undefined) {
            message = "Deine Nachricht wurde erfolgreich versandt.";
          }
          App.vent.trigger("place:message:success", message);
        },
        error: function(model, xhr, options) {
          App.vent.trigger("place:message:failure", xhr);
        }
      });
    },

    showEntryDepotForm: function() {
      Backbone.history.navigate("places/new/depot");
      this.showEntryForm(Places.EntryDepotView, "Neues Depot eintragen", new Places.Model(), this.collection);
    },

    showEntryFarmForm: function() {
      Backbone.history.navigate("places/new/farm");
      this.showEntryForm(Places.EntryFarmView, "Neuen Betrieb eintragen", new Places.Model(), this.collection);
    },

    showEntryForm: function(EntryView, headline, model, collection) {
      this.entryView = new EntryView ({
        model: model,
        collection: collection,
        headline: headline
      });

      App.modalRegion.show(this.entryView);
    },

    showEntryList: function() {
      var currentUser = Teikei.currentUser;
      var filteredCollection;
      if (currentUser) {
        filteredCollection = this.collection.byUser(currentUser.get('id'));
        filteredCollection.comparator = function(model) {
          return [model.get("type"), model.get("name")];
        }
        filteredCollection.sort();
      }
      this.entryListView = new Places.EntryListView({
        collection: filteredCollection
      });
      App.modalRegion.show(this.entryListView);
    },

    showTip: function(id) {
      this.mapView.showTip(id);
    },

    showDetails: function(id) {
      Backbone.history.navigate('places/' + id + '/details');
      var model = this.collection.get(id);
      var detailsView = new Places.DetailsMessageFormView({ model: model });
      detailsView.bind("placeMessageForm:submit", this.submitPlaceMessage, this);
      model.fetch({
        success: function(){
          App.modalRegion.show(detailsView);
        }
      });
      this.detailsView = detailsView;
    },

    showNetwork: function(id) {
      Backbone.history.navigate('places/' + id + '/network');
      var model = this.collection.get(id);
      var mapView = this.mapView;
      model.fetch({
        reset: true,
        success: function(){
          mapView.hilightNetwork(model);
        }
      });
    },

    showArea: function(area){
      Backbone.history.navigate('region/' + area);
      var bounds = this.areas[area].boundingBox;
      this.areaSelectView.setOption(area);
      this.mapView.showArea(bounds);
    },

    areas: {
      default: {
        boundingBox: [[47.2703, 5.8667],[54.0585, 15.0419]],
        displayName: "– Region auswählen –"
      },
      badenwuerttemberg: {
        boundingBox: [[ 47.53649305, 7.51464013 ],[ 49.79146623, 10.49316881 ]],
        displayName: "Baden-Württemberg"
      },
      bayern: {
        boundingBox: [[ 47.375413747749, 9.0138020303575 ],[ 50.52894177871, 13.778139870618 ]],
        displayName: "Bayern"
      },
      berlin: {
        boundingBox: [[ 52.34036388, 13.08202030 ],[ 52.67513452, 13.75919894 ]],
        displayName: "Berlin"
      },
      brandenburg: {
        boundingBox: [[ 51.37290595, 11.52257448 ],[ 53.4422936, 14.76043039 ]],
{'fraction_non_alphanumeric': 0.1362026485987065, 'fraction_numerical': 0.030181706190329536, 'mean_word_length': 2.6764789131050097, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1083113', 'n_tokens_mistral': 4192, 'n_tokens_neox': 3865, 'n_words': 724}