<commit_before>##
# AssetManager
#
# For Managing Assets
class outpost.AssetManager
    @TemplatePath = 'outpost/asset_host/templates'

    DefaultOptions:
        jsonInput: "#asset_json"

    #----------

    constructor: (assets, el, options={}) ->
        _.extend @, Backbone.Events
        @options = _.defaults options, @DefaultOptions
        @el = $(el)

        @assets     = new outpost.AssetHost.Assets(assets)
        @assetsView = new outpost.AssetManager.Assets(collection: @assets)
        
        @el.html @assetsView.el
        
        # Register listener for AssetHost message
        window.addEventListener "message", (event) =>
            # If the data is an object (i.e. not "LOADED"), do things
            if _.isObject(event.data)
                @assets.reset(event.data)
                @assets.sort()
                @assetsView.render()
                @updateInput()
        , false

    #----------
    
    updateInput: ->
        $(@options.jsonInput).val(JSON.stringify(@assets.simpleJSON()))
        
    #----------
    
    @Asset: Backbone.View.extend
        tagName: "li"
        template: JST[AssetManager.TemplatePath + '/asset']
        
        #----------
        
        initialize: ->
            @render()
            @model.bind "change", => @render()

        #----------

        render: ->
            if @model.get('tags')
                @$el.html @template(asset: @model.toJSON())
                
            @
    
    #----------

    @Assets: Backbone.View.extend
        tagName: "ul"
        events: "click a.popup": "_popup"
        
        initialize: ->
            @_views = {}
            
            @collection.bind "reset", => 
                view.detach() for view in @_views
                @_views = {}
                
            @collection.bind 'add', (f) => 
                @_views[f.cid] = new outpost.AssetManager.Asset
                    model: f
                
                @renderCollection()

            @collection.bind 'remove', (f) => 
                $(@_views[f.cid].el).detach()
                delete @_views[f.cid]
                @renderCollection()
            
            # attach a listener to wait for the LOADED message
            window.addEventListener "message", (evt) => 
                if evt.data == "LOADED" and @chooserIsAvailable()
                    console.log @collection.toJSON()
                    # dispatch our event with the asset data
                    window.assethostChooser.postMessage @collection.toJSON(), assethost.SERVER
            , false

            @render()
        
        #----------

        chooserIsAvailable: ->
            window.assethostChooser and !window.assethostChooser.closed

        #----------
        
        _popup: (event) ->
            event.originalEvent.stopPropagation()
            event.originalEvent.preventDefault()
            
            popup =
                url:     "#{assethost.SERVER}/a/chooser"
                name:    "chooser"
                options: "height=620,width=1000,scrollbars=1"
            
            if @chooserIsAvailable()
                window.assethostChooser.focus()
            else
                window.assethostChooser = window.open(popup.url, popup.name, popup.options)
            
            false
            
        #----------
        # Render the full view. This should only be called once.
        render: ->
            @$el.html JST[AssetManager.TemplatePath + '/asset_manager']
            @collectionEl = $(".collection", @$el)
            @emptyNotification = new outpost.Notification(
                @collectionEl, 
                "info", 
                "There are no assets. Click the button to open AssetHost."
            )
            
            @renderCollection()
            @
            
        #----------
        # Render the collection. This gets called every time AssetHost send a message.
        renderCollection: ->
            if @collection.isEmpty()
                return @emptyNotification.render()<commit_msg>Add trailing slash to TemplatePath<commit_after>##
# AssetManager
#
# For Managing Assets
class outpost.AssetManager
    @TemplatePath = 'outpost/asset_host/templates/'

    DefaultOptions:
        jsonInput: "#asset_json"

    #----------

    constructor: (assets, el, options={}) ->
        _.extend @, Backbone.Events
        @options = _.defaults options, @DefaultOptions
        @el = $(el)

        @assets     = new outpost.AssetHost.Assets(assets)
        @assetsView = new outpost.AssetManager.Assets(collection: @assets)
        
        @el.html @assetsView.el
        
        # Register listener for AssetHost message
        window.addEventListener "message", (event) =>
            # If the data is an object (i.e. not "LOADED"), do things
            if _.isObject(event.data)
                @assets.reset(event.data)
                @assets.sort()
                @assetsView.render()
                @updateInput()
        , false

    #----------
    
    updateInput: ->
        $(@options.jsonInput).val(JSON.stringify(@assets.simpleJSON()))
        
    #----------
    
    @Asset: Backbone.View.extend
        tagName: "li"
        template: JST[AssetManager.TemplatePath + 'asset']
        
        #----------
        
        initialize: ->
            @render()
            @model.bind "change", => @render()

        #----------

        render: ->
            if @model.get('tags')
                @$el.html @template(asset: @model.toJSON())
                
            @
    
    #----------

    @Assets: Backbone.View.extend
        tagName: "ul"
        events: "click a.popup": "_popup"
        
        initialize: ->
            @_views = {}
            
            @collection.bind "reset", => 
                view.detach() for view in @_views
                @_views = {}
                
            @collection.bind 'add', (f) => 
                @_views[f.cid] = new outpost.AssetManager.Asset
                    model: f
                
                @renderCollection()

            @collection.bind 'remove', (f) => 
                $(@_views[f.cid].el).detach()
                delete @_views[f.cid]
                @renderCollection()
            
            # attach a listener to wait for the LOADED message
            window.addEventListener "message", (evt) => 
                if evt.data == "LOADED" and @chooserIsAvailable()
                    console.log @collection.toJSON()
                    # dispatch our event with the asset data
                    window.assethostChooser.postMessage @collection.toJSON(), assethost.SERVER
            , false

            @render()
        
        #----------

        chooserIsAvailable: ->
            window.assethostChooser and !window.assethostChooser.closed

        #----------
        
        _popup: (event) ->
            event.originalEvent.stopPropagation()
            event.originalEvent.preventDefault()
            
            popup =
                url:     "#{assethost.SERVER}/a/chooser"
                name:    "chooser"
                options: "height=620,width=1000,scrollbars=1"
            
            if @chooserIsAvailable()
                window.assethostChooser.focus()
            else
                window.assethostChooser = window.open(popup.url, popup.name, popup.options)
            
            false
            
        #----------
        # Render the full view. This should only be called once.
        render: ->
            @$el.html JST[AssetManager.TemplatePath + 'asset_manager']
            @collectionEl = $(".collection", @$el)
            @emptyNotification = new outpost.Notification(
                @collectionEl, 
                "info", 
                "There are no assets. Click the button to open AssetHost."
            )
            
            @renderCollection()
            @
            
        #----------
        # Render the collection. This gets called every time AssetHost send a message.
        renderCollection: ->
            if @collection.isEmpty()
                return @emptyNotification.render()
{'fraction_non_alphanumeric': 0.12594859241126072, 'fraction_numerical': 0.0019583843329253367, 'mean_word_length': 1.2836780324203465, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 33, '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': '5451439', 'n_tokens_mistral': 2157, 'n_tokens_neox': 2049, 'n_words': 439}