﻿Ext.ux.ClientDeviceMetadataGridPanel = Ext.extend(Ext.ux.MetadataGridPanel, {

    // standard initializer
    initComponent: function() {
        Ext.ux.ClientDeviceMetadataGridPanel.superclass.initComponent.apply(this, arguments);

        // args have to go here as base class initComponent doesn't get called for some reason
        var config = {
            view: new Ext.ux.ClientDeviceMetadataGridView({ scrollOffset: g_gridScrollOffset })
        };

        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);

        this.on('contextmenu', this.onContextMenu, this);
    },


    // ------ event handler functions ------ //

    // context menu
    onContextMenu: function(e) {
        var rowIndex = this.getRowIndexFromEvent(e);

        this.updateSelections(rowIndex);
        this.contextMenu = this.createActionsContextMenu(this, rowIndex);

        Ext.ux.ClientDeviceMetadataGridPanel.superclass.onContextMenu.call(this, e);
    },


    // ------ context menu creation helper functions ------ //

    // create the context menu
    createActionsContextMenu: function(grid, rowIndex) {
        var selections  = this.getSelectionModel().getSelections();
        var contextMenu = this.createEmptyActionsContextMenu();

        contextMenu = this.addMetadataStreamingMenuItems(contextMenu, rowIndex);

        // extract the type from the first item
        var type    = selections[0].data.type;
        var idArray = idHelper_createIdArray(selections);

        if (selections.length > 0) {
            contextMenu.add(new Ext.menu.Item({
                text: g_resourceStrings['Js_TracksGrid_PopupMenu_AddToUploads'],
                handler: function() { clientDeviceMetadataGridPanel_handleAddToUploadQueueClicked(type, idArray); }
            }));
        }
        
        // add any generic menu items
        contextMenu = this.addGenericMenuItems(grid, contextMenu, rowIndex);

        return contextMenu;
    }
});

Ext.ux.ClientDeviceMetadataGridView = Ext.extend(Ext.ux.MetadataGridView, {
});

Ext.ux.ClientDeviceMetadataGridDragZone = Ext.extend(Ext.ux.MetadataGridDragZone, {
});


// ------ context menu item click handler functions ------ //

// Add menu item clicked from master
function clientDeviceMetadataGridPanel_handleAddToUploadQueueClicked(type, idArray) {

    var deviceId = clientDevicesPanelHelper.getDeviceId();

    // set the queue status
    ajax_setQueueStatus(deviceId, g_cloud_deviceId, type, idArray, true, g_lowUserPriorityDefault, false);
}