<commit_before>define 'app/views', ['underscore', 'backbone', 'backbone.marionette', 'leaflet', 'i18next', 'TweenLite', 'app/p13n', 'app/widgets', 'app/jade', 'app/models', 'app/search', 'app/color'], (_, Backbone, Marionette, Leaflet, i18n, TweenLite, p13n, widgets, jade, models, search, colors) ->

    class SMItemView extends Marionette.ItemView
        templateHelpers:
            t: i18n.t
        getTemplate: ->
            return jade.get_template @template

    class AppView extends Backbone.View
        initialize: (options)->
            @service_sidebar = new ServiceSidebarView
                parent: this
                service_tree_collection: options.service_list
            options.map_view.addControl 'sidebar', @service_sidebar.map_control()
            @map = options.map_view.map
            @current_markers = {}

        render: ->
            return this
        remember_markers: (service_id, markers) ->
            @current_markers[service_id] = markers
        remove_service_points: (service_id) ->
            _.each @current_markers[service_id], (marker) =>
                @map.removeLayer marker
            delete @current_markers[service_id]

        add_service_points: (service, on_success, spinner_target = null) ->
            unit_list = new models.UnitList()
            unit_list.fetch
                data:
                    service: service.id
                    page_size: 1000
                    only: 'name,location'
                spinner_target: spinner_target
                success: =>
                    markers = @draw_units unit_list,
                        service: service
                    @remember_markers service.id, markers
                    on_success?()

        draw_units: (unit_list, opts) ->
            markers = []
            if opts.service?
                color = colors.service_color(opts.service)
            else
                color = null

            unit_list.each (unit) =>
                if !color?
                    color = colors.unit_color(unit)
                icon = new widgets.CanvasIcon 50, color
                location = unit.get('location')
                if location?
                    coords = location.coordinates
                    popup = L.popup(closeButton: false).setContent "<div class='unit-name'>#{unit.get_text 'name'}</div>"
                    marker = L.marker([coords[1], coords[0]], icon: icon)
                        .bindPopup(popup)
                        .addTo(@map)

                    marker.unit = unit
                    unit.marker = marker
                    markers.push marker
                    marker.on 'click', (event) =>
                        marker = event.target
                        @service_sidebar.show_details marker.unit
                    marker.on 'mouseover', (event) ->
                        event.target.openPopup()

            bounds = L.latLngBounds (m.getLatLng() for m in markers)
            bounds = bounds.pad 0.05
            # FIXME: map.fitBounds() maybe?
            if opts? and opts.zoom and unit_list.length == 1
                coords = unit_list.first().get('location').coordinates
                @map.setView [coords[1], coords[0]], 12

            return markers

        # The transitions triggered by removing the class landing from body are defined
        # in the file landing-page.less.
        # When key animations have ended a 'landing-page-cleared' event is triggered.
        clear_landing_page: () ->
            if $('body').hasClass('landing')
                $('body').removeClass('landing')
                $('.service-sidebar').on('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', (event) ->
                    if event.originalEvent.propertyName is 'top'
                        app.vent.trigger('landing-page-cleared')
                        $(@).off('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd')
                )


    class ServiceSidebarView extends Backbone.View
        tagName: 'div'
        className: 'service-sidebar'
        events:<commit_msg>Make popup stay open if unit is on details view<commit_after>define 'app/views', ['underscore', 'backbone', 'backbone.marionette', 'leaflet', 'i18next', 'TweenLite', 'app/p13n', 'app/widgets', 'app/jade', 'app/models', 'app/search', 'app/color'], (_, Backbone, Marionette, Leaflet, i18n, TweenLite, p13n, widgets, jade, models, search, colors) ->

    class SMItemView extends Marionette.ItemView
        templateHelpers:
            t: i18n.t
        getTemplate: ->
            return jade.get_template @template

    class AppView extends Backbone.View
        initialize: (options)->
            @service_sidebar = new ServiceSidebarView
                parent: this
                service_tree_collection: options.service_list
            options.map_view.addControl 'sidebar', @service_sidebar.map_control()
            @map = options.map_view.map
            @current_markers = {}
            @details_marker = null # The marker currently visible on details view.

        render: ->
            return this
        remember_markers: (service_id, markers) ->
            @current_markers[service_id] = markers
        remove_service_points: (service_id) ->
            _.each @current_markers[service_id], (marker) =>
                @map.removeLayer marker
            delete @current_markers[service_id]

        add_service_points: (service, on_success, spinner_target = null) ->
            unit_list = new models.UnitList()
            unit_list.fetch
                data:
                    service: service.id
                    page_size: 1000
                    only: 'name,location'
                spinner_target: spinner_target
                success: =>
                    markers = @draw_units unit_list,
                        service: service
                    @remember_markers service.id, markers
                    on_success?()

        draw_units: (unit_list, opts) ->
            markers = []
            if opts.service?
                color = colors.service_color(opts.service)
            else
                color = null

            unit_list.each (unit) =>
                if !color?
                    color = colors.unit_color(unit)
                icon = new widgets.CanvasIcon 50, color
                location = unit.get('location')
                if location?
                    coords = location.coordinates
                    popup = L.popup(closeButton: false).setContent "<div class='unit-name'>#{unit.get_text 'name'}</div>"
                    marker = L.marker([coords[1], coords[0]], icon: icon)
                        .bindPopup(popup)
                        .addTo(@map)

                    marker.unit = unit
                    unit.marker = marker
                    markers.push marker
                    marker.on 'click', (event) =>
                        marker = event.target
                        @service_sidebar.show_details marker.unit
                        @details_marker?.closePopup()
                        popup.addTo(@map)
                        @details_marker = marker
                    marker.on 'mouseover', (event) ->
                        event.target.openPopup()

            bounds = L.latLngBounds (m.getLatLng() for m in markers)
            bounds = bounds.pad 0.05
            # FIXME: map.fitBounds() maybe?
            if opts? and opts.zoom and unit_list.length == 1
                coords = unit_list.first().get('location').coordinates
                @map.setView [coords[1], coords[0]], 12

            return markers

        # The transitions triggered by removing the class landing from body are defined
        # in the file landing-page.less.
        # When key animations have ended a 'landing-page-cleared' event is triggered.
        clear_landing_page: () ->
            if $('body').hasClass('landing')
                $('body').removeClass('landing')
                $('.service-sidebar').on('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd', (event) ->
                    if event.originalEvent.propertyName is 'top'
                        app.vent.trigger('landing-page-cleared')
                        $(@).off('transitionend webkitTransitionEnd oTransitionEnd MSTransitionEnd')
                )


    class ServiceSidebarView extends Backbone.View
        tagName: 'div'
        className: 'service-sidebar'
        events:
{'fraction_non_alphanumeric': 0.09776470588235295, 'fraction_numerical': 0.00611764705882353, 'mean_word_length': 1.6164973838104033, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '2708773', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2118, 'n_words': 499}