<commit_before>
# This file extends the namespace module with functionality to present
# the index of namespaces.
module 'NamespaceView', ->
    # Show a list of databases
    class @DatabaseList extends UIComponents.AbstractList
        # Use a namespace-specific template for the namespace list
        template: Handlebars.compile $('#database_list-template').html()
        className: 'databases_list-container'
        alert_message_template: Handlebars.compile $('#alert_message-template').html()

        events:
            'click .add-database': 'add_database'
            'click .add-namespace': 'add_namespace'
            'click .remove-namespace': 'remove_namespace'
            'click .close': 'remove_parent_alert'

        initialize: ->
            log_initial '(initializing) namespace list view'

            @add_database_dialog = new NamespaceView.AddDatabaseModal
            @add_namespace_dialog = new NamespaceView.AddNamespaceModal
            @remove_namespace_dialog = new NamespaceView.RemoveNamespaceModal

            super databases, NamespaceView.DatabaseListElement, '.collapsible-list', {}, 'database', 'cluster'

            @datacenters_length = -1
            @databases_length = -1
            datacenters.on 'all', @update_button_create_namespace
            databases.on 'all', @update_button_create_namespace
            @can_create_namespace = true

        update_button_create_namespace: =>
            if databases.length is 0 and @can_create_namespace is true
                @.$('.add-namespace').prop 'disabled', 'disabled'
                @.$('.user_alert_space-cannot_create_namespace').show()
            else if databases.length > 0 and @can_create_namespace is false
                @.$('.add-namespace').removeProp 'disabled'
                @.$('.user_alert_space-cannot_create_namespace').hide()

        render: (message) =>
            super
            @update_toolbar_buttons()

            if message?
                @.$('#user-alert-space').append @alert_message_template
                    message: message
            @update_button_create_namespace()
            return @

        remove_parent_alert: (event) ->
            event.preventDefault()
            element = $(event.target).parent()
            element.slideUp 'fast', -> element.remove()

        rename_namespace: (event) ->
            event.preventDefault()
            rename_modal = new UIComponents.RenameItemModal @model.get('id'), 'namespace'
            rename_modal.render()

        add_database: (event) =>
            @add_database_dialog.render()

        add_namespace: (event) =>
            event.preventDefault()
            @add_namespace_dialog.render()
            $('#focus_namespace_name').focus()

        remove_namespace: (event) =>
            log_action 'remove namespace button clicked'
            # Make sure the button isn't disabled, and pass the list of namespace UUIDs selected
            if not $(event.currentTarget).hasClass 'disabled'
                @remove_namespace_dialog.render @get_selected_namespaces()
            event.preventDefault()

        # Extend the AbstractList.add_element method to bind a callback to each namespace added to the list
        add_element: (element) =>
            namespaces_list_element = super element
            namespaces_list_element.register_namespace_callback [@update_toolbar_buttons]
            #TODO destroy this listener

        # Count up the number of namespaces checked off across all machine lists
        get_selected_namespaces: =>
            namespaces_lists = _.map @element_views, (database_list_element) ->
                database_list_element.namespace_list

            selected_namespaces = []
            for namespaces_list in namespaces_lists
                selected_namespaces = selected_namespaces.concat namespaces_list.get_selected_elements()

            return selected_namespaces


        # Callback that will be registered: updates the toolbar buttons based on how many namespaces have been selected
        update_toolbar_buttons: =>
            if @get_selected_namespaces().length < 1 and @.$('.btn.remove-namespace').prop('disabled') is false
                @.$('.btn.remove-namespace').prop 'disabled', 'disabled'
                @.$('.btn.remove-namespace').addClass 'disabled'
            else if @get_selected_namespaces().length > 0 and @.$('.btn.remove-namespace').prop('disabled') is true
                @.$('.btn.remove-namespace').prop 'disabled', 'disabled'
                @.$('.btn.remove-namespace').removeClass 'disabled'

        destroy: =>
            super
            datacenters.off 'all', @update_button_create_namespace
            databases.off 'all', @update_button_create_namespace
            @add_database_dialog.destroy()
            @add_namespace_dialog.destroy()
            @remove_namespace_dialog.destroy()

    class @DatabaseListElement extends UIComponents.CollapsibleListElement
        template: Handlebars.compile $('#database_list_element-template').html()
        summary_template: Handlebars.compile $('#database_list_element-summary-template').html()

        className: 'element-container'

        events: ->
            _.extend super,
               'click button.remove-database': 'remove_database'

        initialize: ->
            super

            @delegateEvents()

            @namespace_list = new NamespaceView.NamespaceList @model.get('id')
            @callbacks = []
            @no_namespace = true

            @model.on 'change', @render_summary<commit_msg>Remove stupid bug (replace a prop() with a removeProp())<commit_after>
# This file extends the namespace module with functionality to present
# the index of namespaces.
module 'NamespaceView', ->
    # Show a list of databases
    class @DatabaseList extends UIComponents.AbstractList
        # Use a namespace-specific template for the namespace list
        template: Handlebars.compile $('#database_list-template').html()
        className: 'databases_list-container'
        alert_message_template: Handlebars.compile $('#alert_message-template').html()

        events:
            'click .add-database': 'add_database'
            'click .add-namespace': 'add_namespace'
            'click .remove-namespace': 'remove_namespace'
            'click .close': 'remove_parent_alert'

        initialize: ->
            log_initial '(initializing) namespace list view'

            @add_database_dialog = new NamespaceView.AddDatabaseModal
            @add_namespace_dialog = new NamespaceView.AddNamespaceModal
            @remove_namespace_dialog = new NamespaceView.RemoveNamespaceModal

            super databases, NamespaceView.DatabaseListElement, '.collapsible-list', {}, 'database', 'cluster'

            @datacenters_length = -1
            @databases_length = -1
            datacenters.on 'all', @update_button_create_namespace
            databases.on 'all', @update_button_create_namespace
            @can_create_namespace = true

        update_button_create_namespace: =>
            if databases.length is 0 and @can_create_namespace is true
                @.$('.add-namespace').prop 'disabled', 'disabled'
                @.$('.user_alert_space-cannot_create_namespace').show()
            else if databases.length > 0 and @can_create_namespace is false
                @.$('.add-namespace').removeProp 'disabled'
                @.$('.user_alert_space-cannot_create_namespace').hide()

        render: (message) =>
            super
            @update_toolbar_buttons()

            if message?
                @.$('#user-alert-space').append @alert_message_template
                    message: message
            @update_button_create_namespace()
            return @

        remove_parent_alert: (event) ->
            event.preventDefault()
            element = $(event.target).parent()
            element.slideUp 'fast', -> element.remove()

        rename_namespace: (event) ->
            event.preventDefault()
            rename_modal = new UIComponents.RenameItemModal @model.get('id'), 'namespace'
            rename_modal.render()

        add_database: (event) =>
            @add_database_dialog.render()

        add_namespace: (event) =>
            event.preventDefault()
            @add_namespace_dialog.render()
            $('#focus_namespace_name').focus()

        remove_namespace: (event) =>
            log_action 'remove namespace button clicked'
            # Make sure the button isn't disabled, and pass the list of namespace UUIDs selected
            if not $(event.currentTarget).hasClass 'disabled'
                @remove_namespace_dialog.render @get_selected_namespaces()
            event.preventDefault()

        # Extend the AbstractList.add_element method to bind a callback to each namespace added to the list
        add_element: (element) =>
            namespaces_list_element = super element
            namespaces_list_element.register_namespace_callback [@update_toolbar_buttons]
            #TODO destroy this listener

        # Count up the number of namespaces checked off across all machine lists
        get_selected_namespaces: =>
            namespaces_lists = _.map @element_views, (database_list_element) ->
                database_list_element.namespace_list

            selected_namespaces = []
            for namespaces_list in namespaces_lists
                selected_namespaces = selected_namespaces.concat namespaces_list.get_selected_elements()

            return selected_namespaces


        # Callback that will be registered: updates the toolbar buttons based on how many namespaces have been selected
        update_toolbar_buttons: =>
            if @get_selected_namespaces().length < 1 and @.$('.btn.remove-namespace').prop('disabled') is false
                @.$('.btn.remove-namespace').prop 'disabled', 'disabled'
                @.$('.btn.remove-namespace').addClass 'disabled'
            else if @get_selected_namespaces().length > 0 and @.$('.btn.remove-namespace').prop('disabled') is true
                @.$('.btn.remove-namespace').removeProp 'disabled'
                @.$('.btn.remove-namespace').removeClass 'disabled'

        destroy: =>
            super
            datacenters.off 'all', @update_button_create_namespace
            databases.off 'all', @update_button_create_namespace
            @add_database_dialog.destroy()
            @add_namespace_dialog.destroy()
            @remove_namespace_dialog.destroy()

    class @DatabaseListElement extends UIComponents.CollapsibleListElement
        template: Handlebars.compile $('#database_list_element-template').html()
        summary_template: Handlebars.compile $('#database_list_element-summary-template').html()

        className: 'element-container'

        events: ->
            _.extend super,
               'click button.remove-database': 'remove_database'

        initialize: ->
            super

            @delegateEvents()

            @namespace_list = new NamespaceView.NamespaceList @model.get('id')
            @callbacks = []
            @no_namespace = true

            @model.on 'change', @render_summary
{'fraction_non_alphanumeric': 0.09695290858725762, 'fraction_numerical': 0.0010722902332231258, 'mean_word_length': 2.395631067961165, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '2878843', 'n_tokens_mistral': 2771, 'n_tokens_neox': 2688, 'n_words': 638}