All files / app/component/plugin plugin.component.ts

31.57% Statements 42/133
22.41% Branches 13/58
22.85% Functions 8/35
33.04% Lines 38/115

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256  79x           79x 79x   79x 79x         79x 79x 79x   79x 79x 79x                 79x 1x 1x                       1x   1x 1x 1x       1x 1x 1x 1x 1x         1x 1x 1x 1x 1x   1x                                                     2x             6x       2x       12x       8x       2x                                                                                                                                                                                                       1x                       1x                                 1x                
import { HttpErrorResponse } from '@angular/common/http';
import { FakeLinkDirective } from '../../directive/fake-link.directive';
import { Component, HostBinding, Input, OnChanges, QueryList, SimpleChanges, ViewChild, ViewChildren, ChangeDetectionStrategy } from '@angular/core';
import { ReactiveFormsModule, UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
import { RouterLink } from '@angular/router';
import { catchError, of, Subscription, switchMap, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { pluginForm, PluginFormComponent } from '../../form/plugin/plugin.component';
import { DiffComponent } from '../../form/diff/diff.component';
import { HasChanges } from '../../guard/pending-changes.guard';
import { Plugin, writePlugin } from '../../model/plugin';
import { isDeletorTag, tagDeleteNotice } from '../../mods/delete';
import { AdminService } from '../../service/admin.service';
import { PluginService } from '../../service/api/plugin.service';
import { ModService } from '../../service/mod.service';
import { Store } from '../../store/store';
import { downloadPluginExport, downloadTag } from '../../util/download';
import { scrollToFirstInvalid } from '../../util/form';
import { printError } from '../../util/http';
import { ActionComponent } from '../action/action.component';
import { ConfirmActionComponent } from '../action/confirm-action/confirm-action.component';
import { InlineButtonComponent } from '../action/inline-button/inline-button.component';
import { LoadingComponent } from '../loading/loading.component';
 
@Component({
  selector: 'app-plugin',
  templateUrl: './plugin.component.html',
  styleUrls: ['./plugin.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [FakeLinkDirective, RouterLink, ConfirmActionComponent, InlineButtonComponent, ReactiveFormsModule, PluginFormComponent, LoadingComponent, DiffComponent]
})
export class PluginComponent implements OnChanges, HasChanges {
  css = 'plugin list-item';
  @HostBinding('attr.tabindex') tabIndex = 0;
 
  @ViewChildren('action')
  actionComponents?: QueryList<ActionComponent>;
 
  @ViewChild('diffEditor')
  diffEditor?: DiffComponent<Plugin>;
 
  @Input()
  plugin!: Plugin;
 
  editForm: UntypedFormGroup;
  submitted = false;
  @HostBinding('class.editing')
  editing = false;
  viewSource = false;
  diffing = false;
  diffLocal?: Plugin;
  diffRemote?: Plugin;
  @HostBinding('class.deleted')
  deleted = false;
  serverError: string[] = [];
  configErrors: string[] = [];
  defaultsErrors: string[] = [];
  schemaErrors: string[] = [];
  saving?: Subscription;
  loadingDiff?: Subscription;
 
  constructor(
    private mod: ModService,
    public admin: AdminService,
    public store: Store,
    private plugins: PluginService,
    private fb: UntypedFormBuilder,
  ) {
    this.editForm = pluginForm(fb);
  }
 
  saveChanges() {
    return !this.editing || !this.editForm.dirty;
  }
 
  init(): void {
    this.actionComponents?.forEach(c => c.reset());
    this.submitted = false;
    this.editForm.reset();
    this.editForm.patchValue({
      ...this.plugin,
      config: this.plugin.config ? JSON.stringify(this.plugin.config, null, 2) : undefined,
      defaults: this.plugin.defaults ? JSON.stringify(this.plugin.defaults, null, 2) : undefined,
      schema: this.plugin.schema ? JSON.stringify(this.plugin.schema, null, 2) : undefined,
    });
  }
 
  ngOnChanges(changes: SimpleChanges) {
    if (changes.plugin) {
      this.init();
    }
  }
 
  @HostBinding('class')
  get pluginClass() {
    return this.css + ' ' + this.plugin.tag
      .replace(/[+_]/g, '')
      .replace(/\//g, '_')
      .replace(/\./g, '-');
  }
 
  get created() {
    return !!this.plugin.modified;
  }
 
  get qualifiedTag() {
    return this.plugin.tag + this.origin;
  }
 
  get origin() {
    return this.plugin.origin || '';
  }
 
  get local() {
    return this.origin === this.store.account.origin;
  }
 
  get canDiff() {
    return !this.local && this.created && !!this.admin.getTemplate('config/diff');
  }
 
  toggleDiff() {
    if (this.diffing || this.loadingDiff) {
      this.loadingDiff?.unsubscribe();
      delete this.loadingDiff;
      this.diffing = false;
      return;
    }
    this.serverError = [];
    this.viewSource = false;
    this.loadingDiff = this.plugins.get(this.plugin.tag + this.store.account.origin).pipe(
      catchError((err: HttpErrorResponse) => {
        delete this.loadingDiff;
        this.serverError = err.status === 404
          ? [$localize`No local version found.`]
          : printError(err);
        return throwError(() => err);
      }),
    ).subscribe(local => {
      delete this.loadingDiff;
      this.diffLocal = local;
      this.diffRemote = this.plugin;
      this.editing = false;
      this.viewSource = false;
      this.diffing = true;
    });
  }
 
  saveDiff() {
    const merged = this.diffEditor?.getModifiedContent();
    if (!merged || !this.diffLocal) return;
    this.saving = this.plugins.update({
      ...merged,
      tag: this.diffLocal.tag,
      origin: this.store.account.origin,
      modifiedString: this.diffLocal.modifiedString,
    }).pipe(
      catchError((err: HttpErrorResponse) => {
        delete this.saving;
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    ).subscribe(() => {
      delete this.saving;
      this.serverError = [];
      this.diffing = false;
    });
  }
 
  save() {
    this.submitted = true;
    this.editForm.markAllAsTouched();
    if (!this.editForm.valid) {
      scrollToFirstInvalid();
      return;
    }
    const plugin = {
      ...this.plugin,
      ...this.editForm.value,
    };
    this.configErrors = [];
    this.defaultsErrors = [];
    this.schemaErrors = [];
    try {
      if (!plugin.config) delete plugin.config;
      if (plugin.config) plugin.config = JSON.parse(plugin.config);
    } catch (e: any) {
      this.configErrors.push(e.message);
    }
    try {
      if (!plugin.defaults) delete plugin.defaults;
      if (plugin.defaults) plugin.defaults = JSON.parse(plugin.defaults);
    } catch (e: any) {
      this.defaultsErrors.push(e.message);
    }
    try {
      if (!plugin.schema) delete plugin.schema;
      if (plugin.schema) plugin.schema = JSON.parse(plugin.schema);
    } catch (e: any) {
      this.schemaErrors.push(e.message);
    }
    if (this.configErrors.length || this.defaultsErrors.length || this.schemaErrors.length) return;
    this.saving = this.plugins.update(plugin).pipe(
      switchMap(() => this.plugins.get(this.qualifiedTag)),
      catchError((err: HttpErrorResponse) => {
        delete this.saving;
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    ).subscribe(tag => {
      delete this.saving;
      this.serverError = [];
      this.editing = false;
      this.plugin = tag;
      this.init();
    });
  }
 
  copy$ = () => {
    return this.plugins.create({
      ...this.plugin,
      origin: this.store.account.origin,
    }).pipe(
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    );
  }
 
  delete$ = () => {
    const deleteNotice = !isDeletorTag(this.plugin.tag) && this.admin.getPlugin('plugin/delete')
      ? this.plugins.create(tagDeleteNotice(this.plugin))
      : of(null);
    return this.plugins.delete(this.qualifiedTag).pipe(
      switchMap(() => deleteNotice),
      tap(() => {
        this.serverError = [];
        this.deleted = true;
      }),
      catchError((err: HttpErrorResponse) => {
        this.serverError = printError(err);
        return throwError(() => err);
      }),
    );
  }
 
  download = () => {
    downloadTag(writePlugin(this.plugin));
  }
 
  export() {
    downloadPluginExport(this.plugin, this.mod.exportHtml(this.plugin));
  }
}