github.com/argoproj/argo-cd@v1.8.7/ui/src/app/shared/services/view-preferences-service.ts (about)

     1  import {BehaviorSubject, Observable} from 'rxjs';
     2  
     3  export type AppsDetailsViewType = 'tree' | 'network' | 'list';
     4  
     5  export interface AppDetailsPreferences {
     6      resourceFilter: string[];
     7      view: AppsDetailsViewType;
     8      resourceView: 'manifest' | 'diff' | 'desiredManifest';
     9      inlineDiff: boolean;
    10      compactDiff: boolean;
    11      orphanedResources: boolean;
    12  }
    13  
    14  export type AppsListViewType = 'tiles' | 'list' | 'summary';
    15  
    16  export class AppsListPreferences {
    17      public static countEnabledFilters(pref: AppsListPreferences) {
    18          // tslint:disable-next-line: prettier
    19          return [
    20              pref.clustersFilter,
    21              pref.healthFilter,
    22              pref.labelsFilter,
    23              pref.namespacesFilter,
    24              pref.projectsFilter,
    25              pref.reposFilter,
    26              pref.syncFilter
    27          ].reduce((count, filter) => {
    28                  if (filter && filter.length > 0) {
    29                      return count + 1;
    30                  }
    31                  return count;
    32              },
    33              0
    34          );
    35      }
    36  
    37      public static clearFilters(pref: AppsListPreferences) {
    38          pref.clustersFilter = [];
    39          pref.healthFilter = [];
    40          pref.labelsFilter = [];
    41          pref.namespacesFilter = [];
    42          pref.projectsFilter = [];
    43          pref.reposFilter = [];
    44          pref.syncFilter = [];
    45      }
    46  
    47      public labelsFilter: string[];
    48      public projectsFilter: string[];
    49      public reposFilter: string[];
    50      public syncFilter: string[];
    51      public healthFilter: string[];
    52      public namespacesFilter: string[];
    53      public clustersFilter: string[];
    54      public view: AppsListViewType;
    55  }
    56  
    57  export interface ViewPreferences {
    58      version: number;
    59      appDetails: AppDetailsPreferences;
    60      appList: AppsListPreferences;
    61      pageSizes: {[key: string]: number};
    62  }
    63  
    64  const VIEW_PREFERENCES_KEY = 'view_preferences';
    65  
    66  const minVer = 4;
    67  
    68  const DEFAULT_PREFERENCES: ViewPreferences = {
    69      version: 1,
    70      appDetails: {
    71          view: 'tree',
    72          resourceFilter: ['kind:Deployment', 'kind:Service', 'kind:Pod', 'kind:StatefulSet', 'kind:Ingress', 'kind:ConfigMap', 'kind:Job', 'kind:DaemonSet', 'kind:Workflow'],
    73          inlineDiff: false,
    74          compactDiff: false,
    75          resourceView: 'manifest',
    76          orphanedResources: false
    77      },
    78      appList: {
    79          view: 'tiles' as AppsListViewType,
    80          labelsFilter: new Array<string>(),
    81          projectsFilter: new Array<string>(),
    82          namespacesFilter: new Array<string>(),
    83          clustersFilter: new Array<string>(),
    84          reposFilter: new Array<string>(),
    85          syncFilter: new Array<string>(),
    86          healthFilter: new Array<string>()
    87      },
    88      pageSizes: {}
    89  };
    90  
    91  export class ViewPreferencesService {
    92      private preferencesSubj: BehaviorSubject<ViewPreferences>;
    93  
    94      public init() {
    95          if (!this.preferencesSubj) {
    96              this.preferencesSubj = new BehaviorSubject(this.loadPreferences());
    97              window.addEventListener('storage', () => {
    98                  this.preferencesSubj.next(this.loadPreferences());
    99              });
   100          }
   101      }
   102  
   103      public getPreferences(): Observable<ViewPreferences> {
   104          return this.preferencesSubj;
   105      }
   106  
   107      public updatePreferences(change: Partial<ViewPreferences>) {
   108          const nextPref = Object.assign({}, this.preferencesSubj.getValue(), change, {version: minVer});
   109          window.localStorage.setItem(VIEW_PREFERENCES_KEY, JSON.stringify(nextPref));
   110          this.preferencesSubj.next(nextPref);
   111      }
   112  
   113      private loadPreferences(): ViewPreferences {
   114          let preferences: ViewPreferences;
   115          const preferencesStr = window.localStorage.getItem(VIEW_PREFERENCES_KEY);
   116          if (preferencesStr) {
   117              try {
   118                  preferences = JSON.parse(preferencesStr);
   119              } catch (e) {
   120                  preferences = DEFAULT_PREFERENCES;
   121              }
   122              if (!preferences.version || preferences.version < minVer) {
   123                  preferences = DEFAULT_PREFERENCES;
   124              }
   125          } else {
   126              preferences = DEFAULT_PREFERENCES;
   127          }
   128          return Object.assign({}, DEFAULT_PREFERENCES, preferences);
   129      }
   130  }