ExplorerModule.js 8.8 KB
/**
 * Project  : AMDA-NG
 * Name     : ExplorerModule.js
 * @class   amdaDesktop.ExplorerModule
 * @extends amdaDesktop.AmdaModule
 * @brief   Explorer Module controller definition
 * @author  CDA
 */

'use strict'

/* global AmdaAction, myDesktopApp */

/**
 * @typedef {AmdaApp} myDesktopApp
 * @typedef {Object} AmdaAction
 * @typedef {Object} Ext
 */

Ext.define('amdaDesktop.ExplorerModule',
  {
    extend: 'amdaDesktop.AmdaModule',
    requires: [
      'amdaUI.ExplorerUI',
      'amdaUI.AliasUI',
      // Node Models
      'amdaModel.BkgJobNode',
      'amdaModel.DerivedParamNode',
      'amdaModel.LocalParamNode',
      'amdaModel.RemoteParamNode',
      'amdaModel.RemoteSimuParamNode',
      'amdaModel.AliasNode',
      'amdaModel.TimeTableNode',
      'amdaModel.CatalogNode',
      // StatisticsNode: singleton, not shown in the tree
      'amdaModel.StatisticsNode',
      'amdaModel.sharedTimeTableNode',
      'amdaModel.sharedCatalogNode',
      'amdaModel.MyDataParamNode',
      'amdaModel.MyDataNode',
      'amdaModel.PlotNode',
      // DownloadNode: singleton, not shown in the tree
      'amdaModel.DownloadNode',
      'amdaModel.SearchNode',
      // Object Models
      'amdaModel.Parameter',
      'amdaModel.Search',
      'amdaModel.Download',
      'amdaModel.TimeTable',
      'amdaModel.Catalog',
      'amdaModel.Stats',
      'amdaModel.FileObject',
      'amdaModel.FileParamObject',
      'amdaModel.FilterInfo'
    ],

    contentId: 'explorerUI',
    filter: null,
    filtersStore: null,
    paramInfoRegistry: {},

    constructor: function () {
      this.superclass.constructor.apply(this, arguments)
      if (!this.filtersStore) {
        this.filtersStore = Ext.create('Ext.data.Store', {model: 'amdaModel.FilterList'})
        this.filtersStore.load()
        // Note: amdaDesktop.ExplorerModule.filtersStore = this.filtersStore;
      }
    },

    createWindow: function () {
      var desktop, win

      desktop = this.app.getDesktop()
      win = desktop.getWindow(this.id)
      if (!win) {
        win = desktop.createWindow({
          id: this.id,
          title: this.title,
          width: 340,
          height: 500,
          iconCls: this.icon,
          animCollapse: false,
          border: false,
          closable: false,
          stateful: true,
          stateId: this.id,
          stateEvents: [
            'move',
            'show',
            'resize'
          ],
          constrain: true,
          layout:
            {
              type: 'border',
              align: 'stretch'
            },
          x: 5,
          y: 5,
          items: [
            {
              xtype: 'panelExplorer',
              id: this.contentId,
              region: 'center'
            },
            {
              xtype: 'panel',
              title: 'Log',
              id: 'LogPanel',
              collapsible: true,
              hideCollapseTool: true,
              region: 'south',
              split: true,
              height: 150,
              stateful: true,
              stateId: 'LogPanel',
              tools: [{
                type: 'trash',
                tooltip: 'Clear logs',
                handler: function () {
                  var logPanel = win.items.get('LogPanel')
                  logPanel.removeAll()
                }
              }]
            }
          ]
        })
      }

      win.on('activate', function () {
        var manager
        myDesktopApp.getLoadedModule(this.id, true, function (module) {
          // On activation when there's a pinedModule 'null'
          if (module.pinedModule === null) {
            // Get the ZIndex manager
            manager = myDesktopApp.desktop.getDesktopZIndexManager()
            // Ordering to send back this window
            manager.sendToBack(win)
          }
        })
      }, this)

      win.on('show', function () {
        var desktopHeight
        // Set current filter
        this.setCrtFilter()
        // Resize the explorer in relation with the desktop size
        if (win.y + win.height > myDesktopApp.desktop.el.getHeight()) {
          desktopHeight = myDesktopApp.desktop.el.getHeight()
          win.setHeight(desktopHeight - win.y)
        }
      }, this)

      win.addCls('window-active')
      win.show()
      return win
    },

    pinedModule: null,

    /**
     * Getter of pinedModule
     * @return {amdaDesktop.InteractiveModule} The pined module
     */
    getPinedModule: function () {
      // Get the pined Module
      return this.pinedModule
    },

    /**
     * Setter of pinedModule
     * @param {amdaDesktop.InteractiveModule} amdaModule The pined module
     * @returns {void}
     */
    setPinedModule: function (amdaModule) {
      // Set the pined Module
      this.pinedModule = amdaModule
    },

    /**
     * Module Attachment Method
     * @param {amdaDesktop.InteractiveModule} amdaModule The module to attach
     * @returns {void}
     */
    pinMechanism: function (amdaModule) {
      var loadedAmdaModule, loadedPinedModule, oldPinedModule
      // If there is an old pined module
      if (this.getPinedModule() !== null) {
        // Get old pined module :
        oldPinedModule = this.getPinedModule()
        loadedPinedModule = myDesktopApp.getLoadedModule(oldPinedModule)
        // Call uncolor method on the old pined Module
        loadedPinedModule.uncolorHeaderModule()
      }
      // Set pined module
      this.setPinedModule(amdaModule)
      // Call color method on pined Module
      loadedAmdaModule = myDesktopApp.getLoadedModule(amdaModule)
      loadedAmdaModule.colorHeaderModule()
    },

    addLogMessage: function (message) {
      var height, msg, panel, win
      height = 40
      msg = Ext.create('Ext.Component', {
        height: height,
        html: message,
        style: {color: '#555555'}
      })
      win = myDesktopApp.getDesktop().getWindow(this.id)
      panel = win.items.get('LogPanel')
      panel.insert(0, msg)
    },

    //
    setCrtFilter: function () {
      // Get current filter to apply
      AmdaAction.getCrtFilterResult(function (result, e) {
        var explorerUI, win
        if (!e.status) {
          Ext.Msg.show({
            title: 'Error System',
            msg: e.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
          })
        } else if (result) {
          // Apply current filter
          this.filter = result
          // Add saved filter to Explorer List
          if (!this.filtersStore.getById(this.filter.id)) {
            this.filtersStore.add({
              id: this.filter.id,
              name: this.filter.name
            })
          }

          // Apply filter to the tree
          win = myDesktopApp.getDesktop().getWindow(this.id)
          if (win) {
            explorerUI = win.query('#' + this.contentId)
            explorerUI[0].updateFilter()
          }
        } else {
          Ext.Msg.show({
            title: 'Filter',
            msg: 'Cannot apply filter',
            modal: true,
            icon: Ext.Msg.ERROR,
            buttons: Ext.Msg.OK
          })
        }
      }, this)
    },

    resetFilter: function () {
      AmdaAction.resetFilter(function (result, e) {
        var filterUI, win
        if (!e.status) {
          Ext.Msg.show({
            title: 'Error System',
            msg: e.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
          })
        } else if (result) {
          this.setCrtFilter()
          // Update filter win
          win = myDesktopApp.getDesktop().getWindow(myDesktopApp.dynamicModules.filters.id)
          if (win) {
            filterUI = win.query('panelFilters')
            filterUI[0].reloadFilter()
          }
        } else {
          Ext.Msg.show({
            title: 'Filter',
            msg: 'Cannot reset filter',
            modal: true,
            icon: Ext.Msg.ERROR,
            buttons: Ext.Msg.OK
          })
        }
      }, this)
    },

    getParamInfo: function (paramId, onReady) {
      var me = this

      if (this.paramInfoRegistry[paramId]) {
        if (onReady) {
          onReady(this.paramInfoRegistry[paramId])
        }
        return this.paramInfoRegistry[paramId]
      }

      AmdaAction.getParamInfo({paramId: paramId}, function (result, e) {
        var paramInfo = null
        if (e.status === true) {
          // No available info for this parameter, do not display an error message if no success
          if (result.success) {
            // Store parameter info in registry
            me.paramInfoRegistry[paramId] = result
            paramInfo = result
          }
        } else {
          Ext.Msg.show({
            title: 'Error System',
            msg: e.message,
            icon: Ext.MessageBox.ERROR,
            buttons: Ext.Msg.OK
          })
        }

        if (onReady) {
          onReady(paramInfo)
        }
      })

      return null
    }

  })