github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/app/routes/application.js (about)

     1  import { inject as service } from '@ember/service';
     2  import { next } from '@ember/runloop';
     3  import Route from '@ember/routing/route';
     4  import { AbortError } from '@ember-data/adapter/error';
     5  import RSVP from 'rsvp';
     6  import { action } from '@ember/object';
     7  import classic from 'ember-classic-decorator';
     8  
     9  @classic
    10  export default class ApplicationRoute extends Route {
    11    @service config;
    12    @service system;
    13    @service store;
    14    @service token;
    15  
    16    queryParams = {
    17      region: {
    18        refreshModel: true,
    19      },
    20    };
    21  
    22    resetController(controller, isExiting) {
    23      if (isExiting) {
    24        controller.set('error', null);
    25      }
    26    }
    27  
    28    beforeModel(transition) {
    29      const fetchSelfTokenAndPolicies = this.get('token.fetchSelfTokenAndPolicies')
    30        .perform()
    31        .catch();
    32  
    33      const fetchLicense = this.get('system.fetchLicense')
    34        .perform()
    35        .catch();
    36  
    37      return RSVP.all([
    38        this.get('system.regions'),
    39        this.get('system.defaultRegion'),
    40        fetchLicense,
    41        fetchSelfTokenAndPolicies,
    42      ]).then(promises => {
    43        if (!this.get('system.shouldShowRegions')) return promises;
    44  
    45        const queryParam = transition.to.queryParams.region;
    46        const defaultRegion = this.get('system.defaultRegion.region');
    47        const currentRegion = this.get('system.activeRegion') || defaultRegion;
    48  
    49        // Only reset the store if the region actually changed
    50        if (
    51          (queryParam && queryParam !== currentRegion) ||
    52          (!queryParam && currentRegion !== defaultRegion)
    53        ) {
    54          this.system.reset();
    55          this.store.unloadAll();
    56        }
    57  
    58        this.set('system.activeRegion', queryParam || defaultRegion);
    59  
    60        return promises;
    61      });
    62    }
    63  
    64    // Model is being used as a way to transfer the provided region
    65    // query param to update the controller state.
    66    model(params) {
    67      return params.region;
    68    }
    69  
    70    setupController(controller, model) {
    71      const queryParam = model;
    72  
    73      if (queryParam === this.get('system.defaultRegion.region')) {
    74        next(() => {
    75          controller.set('region', null);
    76        });
    77      }
    78  
    79      return super.setupController(...arguments);
    80    }
    81  
    82    @action
    83    didTransition() {
    84      if (!this.get('config.isTest')) {
    85        window.scrollTo(0, 0);
    86      }
    87    }
    88  
    89    @action
    90    willTransition() {
    91      this.controllerFor('application').set('error', null);
    92    }
    93  
    94    @action
    95    error(error) {
    96      if (!(error instanceof AbortError)) {
    97        this.controllerFor('application').set('error', error);
    98      }
    99    }
   100  }