<commit_before>import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Observable, Subject } from 'rxjs';

import { CmsService, ModalService, ToastrService } from '../core';
import { StoryModel } from '../shared';

@Component({
  selector: 'publish-story',
  styleUrls: ['story.component.css'],
  template: `
    <publish-story-hero [id]="id" [story]="story"></publish-story-hero>

    <publish-tabs [model]="story">
      <nav>
        <a routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}" [routerLink]="base">Basic Info</a>
        <a *ngIf="distPodcast" routerLinkActive="active" [routerLink]="[base, 'podcast']">Podcast Episode Info</a>
        <a *ngIf="distPlayer" routerLinkActive="active" [routerLink]="[base, 'player']">Embeddable Player</a>
        <a *ngIf="id" routerLinkActive="active" [routerLink]="[base, 'downloads']">Download Metrics</a>
      </nav>
      <publish-story-status [id]="id" [story]="story"></publish-story-status>
      <button *ngIf="id" class="delete" (click)="confirmDelete($event)">Delete</button>
    </publish-tabs>
  `
})

export class StoryComponent implements OnInit {

  id: number;
  base: string;
  seriesId: number;
  story: StoryModel;

  // distribution specific tabs
  distPodcast = false;
  distPlayer = false;

  constructor(
    private cms: CmsService,
    private route: ActivatedRoute,
    private router: Router,
    private modal: ModalService,
    private toastr: ToastrService
  ) {}

  ngOnInit() {
    this.route.params.forEach(params => {
      this.id = +params['id'];
      this.seriesId = +params['seriesId'];
      this.base = '/story/' + (this.id || 'new');
      if (this.seriesId) {
        this.base += `/${this.seriesId}`;
      }
      this.loadStory();
      this.showDistributionTabs();
    });
  }

  loadStory() {
    if (this.id) {
      this.cms.auth.follow('prx:story', {id: this.id}).subscribe(story => {
        if (story.has('prx:series')) {
          story.follow('prx:series').subscribe(series => this.setStory(series, story));
        } else {
          this.setStory(null, story);
        }
      });
    } else if (this.seriesId) {
      this.cms.auth.follow('prx:series', {id: this.seriesId}).subscribe(s => this.setStory(s, null));
    } else {
      this.cms.defaultAccount.subscribe(a => this.setStory(a, null));
    }
  }

  setStory(parent: any, story: any) {
    this.story = new StoryModel(parent, story);
    this.showDistributionTabs();
    this.checkStoryVersion();
  }

  checkStoryVersion() {
    if (!this.story.isV4()) {
      let oldLink = `https://www.prx.org/pieces/${this.id}`;
      this.modal.alert(
        'Cannot Edit Episode',
        `This episode was created in the older PRX.org app, and must be
        edited there. <a target="_blank" href="${oldLink}">Click here</a> to view it.`,
        () => { window.history.back(); }
      );
    }
  }

  showDistributionTabs() {
    this.distPlayer = this.id ? true : false;
    if (this.story && this.story.isNew) {
      this.story.getSeriesDistribution('podcast').subscribe(dist => {
        this.distPodcast = dist ? true : false;
      });
    } else if (this.story) {
      this.story.loadRelated('distributions').subscribe(() => {
        this.distPodcast = this.story.distributions.some(d => d.kind === 'episode');
        if (this.distPodcast) {
          this.story.distributions.find(d => d.kind === 'episode').loadRelated('episode');
        }
      });
    }
  }

  canDeactivate(next: any, prev: any): boolean | Observable<boolean> {
    if (this.story && this.story.changed() && !this.story.isDestroy) {
      let thatsOkay = new Subject<boolean>();
      this.modal.confirm(
        'Unsaved changes',
        `This episode has unsaved changes. You may discard the changes and
          continue or click 'Cancel' to complete and save the episode.`,
        (confirm: boolean) => {
          if (confirm) {
            this.story.discard();
          }
          thatsOkay.next(confirm);
          thatsOkay.complete();
        },
        'Discard'
      );
      return thatsOkay;<commit_msg>Hide downloads tab for new-stories or non-podcasts<commit_after>import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { Observable, Subject } from 'rxjs';

import { CmsService, ModalService, ToastrService } from '../core';
import { StoryModel } from '../shared';

@Component({
  selector: 'publish-story',
  styleUrls: ['story.component.css'],
  template: `
    <publish-story-hero [id]="id" [story]="story"></publish-story-hero>

    <publish-tabs [model]="story">
      <nav>
        <a routerLinkActive="active" [routerLinkActiveOptions]="{exact:true}" [routerLink]="base">Basic Info</a>
        <a *ngIf="distPodcast" routerLinkActive="active" [routerLink]="[base, 'podcast']">Podcast Episode Info</a>
        <a *ngIf="distPlayer" routerLinkActive="active" [routerLink]="[base, 'player']">Embeddable Player</a>
        <a *ngIf="distDownloads" routerLinkActive="active" [routerLink]="[base, 'downloads']">Download Metrics</a>
      </nav>
      <publish-story-status [id]="id" [story]="story"></publish-story-status>
      <button *ngIf="id" class="delete" (click)="confirmDelete($event)">Delete</button>
    </publish-tabs>
  `
})

export class StoryComponent implements OnInit {

  id: number;
  base: string;
  seriesId: number;
  story: StoryModel;

  // distribution specific tabs
  distPodcast = false;
  distPlayer = false;
  distDownloads = false;

  constructor(
    private cms: CmsService,
    private route: ActivatedRoute,
    private router: Router,
    private modal: ModalService,
    private toastr: ToastrService
  ) {}

  ngOnInit() {
    this.route.params.forEach(params => {
      this.id = +params['id'];
      this.seriesId = +params['seriesId'];
      this.base = '/story/' + (this.id || 'new');
      if (this.seriesId) {
        this.base += `/${this.seriesId}`;
      }
      this.loadStory();
      this.showDistributionTabs();
    });
  }

  loadStory() {
    if (this.id) {
      this.cms.auth.follow('prx:story', {id: this.id}).subscribe(story => {
        if (story.has('prx:series')) {
          story.follow('prx:series').subscribe(series => this.setStory(series, story));
        } else {
          this.setStory(null, story);
        }
      });
    } else if (this.seriesId) {
      this.cms.auth.follow('prx:series', {id: this.seriesId}).subscribe(s => this.setStory(s, null));
    } else {
      this.cms.defaultAccount.subscribe(a => this.setStory(a, null));
    }
  }

  setStory(parent: any, story: any) {
    this.story = new StoryModel(parent, story);
    this.showDistributionTabs();
    this.checkStoryVersion();
  }

  checkStoryVersion() {
    if (!this.story.isV4()) {
      let oldLink = `https://www.prx.org/pieces/${this.id}`;
      this.modal.alert(
        'Cannot Edit Episode',
        `This episode was created in the older PRX.org app, and must be
        edited there. <a target="_blank" href="${oldLink}">Click here</a> to view it.`,
        () => { window.history.back(); }
      );
    }
  }

  showDistributionTabs() {
    this.distPlayer = this.id ? true : false;
    if (this.story && this.story.isNew) {
      this.story.getSeriesDistribution('podcast').subscribe(dist => {
        this.distPodcast = dist ? true : false;
        this.distDownloads = false;
      });
    } else if (this.story) {
      this.story.loadRelated('distributions').subscribe(() => {
        this.distPodcast = this.story.distributions.some(d => d.kind === 'episode');
        this.distDownloads = this.distPodcast;
        if (this.distPodcast) {
          this.story.distributions.find(d => d.kind === 'episode').loadRelated('episode');
        }
      });
    }
  }

  canDeactivate(next: any, prev: any): boolean | Observable<boolean> {
    if (this.story && this.story.changed() && !this.story.isDestroy) {
      let thatsOkay = new Subject<boolean>();
      this.modal.confirm(
        'Unsaved changes',
        `This episode has unsaved changes. You may discard the changes and
          continue or click 'Cancel' to complete and save the episode.`,
        (confirm: boolean) => {
          if (confirm) {
            this.story.discard();
          }
          thatsOkay.next(confirm);
          thatsOkay.complete();
        },
        'Discard'
      );
      return thatsOkay;
{'fraction_non_alphanumeric': 0.16473235433443864, 'fraction_numerical': 0.00023685457129322596, 'mean_word_length': 2.9006928406466512, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6416863', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2629, 'n_words': 541}