EpnTapModule.js 6.04 KB
/**
 * Project  : AMDA-NG
 * Name   : EpnTapModule.js
 * @class   amdaDesktop.EpnTapModule
 * @extends amdaDesktop.AmdaModule
 * @brief   EpnTap Module controller definition
 * @author  Nathanael Jourdane
 */

'use strict'
Ext.define('amdaDesktop.EpnTapModule', {

  extend: 'amdaDesktop.AmdaModule',
  requires: ['amdaUI.EpnTapUI'],
  contentId: 'EpnTapUI',

  /** The alias name of the module view. */
  // uiType: 'panelEpnTap',
  uiType: 'panelEpnTap',

  /** The text displayed on the *help button* tooltip. */
  helpTitle: 'Help on EPN-TAP Module',

  /** The name of the documentation file related to the module. */
  helpFile: 'epnTapHelp',

  /**
  Module initialisation. Called the first time that the user open the epnTap module.
  */
  init: function () {
    this.launcher = {
      text: this.title,
      iconCls: this.icon,
      handler: this.createWindow,
      scope: this
    }
  },

  /**
   Called each time the epntap module is loaded.
   - `target`: an array of 3 values: [target_name, dataproduct_type]; or null.
   */
  loadTarget: function (filter) {
    this.acquireElements()
    this.addListeners()

    if (filter) {
      this.targetNameCB.setRawValue(filter['targetName'])
      this.productTypeCB.select(filter['productType'])
      if (this.servicesStore.count() > 0) {
        this.updateNbResults()
      }
    } else {
      this.servicesStore.each(function (record) {
        record.set('nb_results', -1)
      }, this)
      this.granulesStore.removeAll()
    }
  },

  acquireElements: function () {
    // UI elements
    this.servicesGrid = Ext.getCmp('epnTapServicesGrid')
    this.productTypeCB = Ext.getCmp('epnTapProductTypeCB')
    this.targetNameCB = Ext.getCmp('epnTapTargetNameCB')
    this.timeSelector = Ext.getCmp('epnTapTimeSelector')
    this.getBtn = Ext.getCmp('epnTapGetBtn')

    // stores elements
    this.servicesStore = Ext.data.StoreManager.lookup('servicesStore')
    this.granulesStore = Ext.data.StoreManager.lookup('granulesStore')
    this.productTypesStore = Ext.data.StoreManager.lookup('productTypesStore')
    this.targetNamesStore = Ext.data.StoreManager.lookup('targetNamesStore')
  },

  addListeners: function () {
    this.servicesStore.on('load', function () {
      this.updateNbResults()
    }, this)

    this.servicesGrid.on('cellclick', function (grid, td, cellIndex, record) {
      this.onServiceSelected(record)
    }, this)

    this.getBtn.on('click', function () {
      this.updateNbResults()
    }, this)
  },

  /*************
  *** Events ***
  *************/

  /**
   Triggered when the 'Get results' button is clicked.
   */
  updateNbResults: function () {
    /* global loadMask */
    loadMask.show()

    const targetName = this.targetNameCB.rawValue
    const productTypes = this.productTypeCB.value.join(';')
    const initTMin = this.timeSelector.getStartTime()
    const initTMax = this.timeSelector.getStopTime()
    const initTMinStr = Ext.Date.format(initTMin, 'd/m/Y H:i:s')
    const initTMaxStr = Ext.Date.format(initTMax, 'd/m/Y H:i:s')

    this.servicesStore.each(function (record) {
      record.set('nb_results', -1)
    }, this)
    this.granulesStore.removeAll()

    this.servicesStore.each(function (record) {
      const ss = this.servicesStore
      Ext.Ajax.request({
        url: 'php/epntap.php',
        method: 'GET',
        headers: {'Content-Type': 'application/json'},
        params: {
          'action': 'updateNbResults',
          'serviceId': record.data['id'],
          'url': record.data['access_url'],
          'tableName': record.data['table_name'],
          'targetNames': targetName,
          'productTypes': productTypes,
          'timeMin': initTMinStr,
          'timeMax': initTMaxStr
        },
        // timeout: 3000,
        success: function (response, options) {
          const record = ss.getById(options.params['serviceId'])
          // noinspection JSValidateTypes
          const responseObj = Ext.decode(response.responseText)
          this.updateService(record, responseObj['success'] ? responseObj['data'] : {'nbRes': -2}, responseObj['msg'], initTMin, initTMax)
        },
        failure: function (response, options) {
          const record = ss.getById(options.params['serviceId'])
          this.updateService(record, -1, response.statusText)
        },
        scope: this
      })
    }, this)
  },

  /**
   Update the nb_result field of the services store (see `EpnTapUI.servicesStore`), according to the field values in `serviceFilterPanel`.
   */
  updateService: function (record, data, info, timeMin, timeMax) {
    record.set('nb_results', data['nbRes'])
    record.set('time_min', data['tMin'])
    record.set('time_max', data['tMax'])
    record.set('info', info)

    const tMinService = Ext.Date.parse(data['tMin'], 'd/m/Y H:i:s')
    const tMaxService = Ext.Date.parse(data['tMax'], 'd/m/Y H:i:s')

    if(tMinService !== null && (this.servicesStore.tMin === null || tMinService < this.servicesStore.tMin)) {
      this.servicesStore.tMin = tMinService
      if(timeMin === null) {
        this.timeSelector.setStartTime(this.servicesStore.tMin)
      }
    }

    if(tMaxService !== null && (this.servicesStore.tMax === null || tMaxService > this.servicesStore.tMax)) {
      this.servicesStore.tMax = tMaxService
      if(timeMax === null) {
        this.timeSelector.setStopTime(this.servicesStore.tMax)
      }
    }

    this.servicesStore.sort()
    loadMask.hide()
  },

  /**
   Triggered when a row is clicked in `servicesGrid` table (see `EpnTapUI.createServicesGrid()`). Among other things,
   send a new query and fill `granulesGrid`.
   */
  onServiceSelected: function (record) {
    this.granulesStore.selectedService = record.data.id
    Ext.Ajax.suspendEvent('requestexception')
    Ext.Ajax.abortAll()

    if (record.get('nb_results') > 0) {
      this.granulesStore.removeAll()
      this.granulesStore.load({
        callback: function () {
          Ext.Ajax.resumeEvents('requestexception')
          // console.log(Ext.decode(operation.response.responseText))
        },
        start: 0,
        limit: this.granulesStore.pageSize,
        scope: this
      })
    }
  }
})