ngOnInit() {
    this.projectId = this.route.snapshot.paramMap.get('projectId');

    if(!this.projectId) {
      this.router.navigate(['../']);
      return;
    }

    this.projectData = this.currentProjectService.getContent(this.projectId);
    this.projectData.valueChanges().subscribe(value => {

      this.project = value;

      const ownsProject = this.auth.owns(value);
      const isPrivate = value ? value.visibility === 'Private' : true;

      if(!value || (!ownsProject && isPrivate)) {
        return this.router.navigate(['create', this.projectId, '404']);
      }
    });

    this.scriptList = this.currentProjectService.getScriptList(this.projectId);
    this.resourceList = this.currentProjectService.getResourceList(this.projectId);

    this.api = {
      changeActiveScript: (index)               => {
        return this.projectData.update({ activeScript: index });
      },
      writeFile:          ({ contents, index }) => {
        const script = this.currentProjectService.getScript(this.projectId, index);
        return script.update({ contents });
      },
      newFile:            (name)                => this.scriptList.push({ name, contents: '' }),
      deleteFile:         (index)               => {
        const script = this.currentProjectService.getScript(this.projectId, index);
        return script.remove();
      },
      editFile:           ({ index, newName })  => {
        const script = this.currentProjectService.getScript(this.projectId, index);
        return script.update({ name: newName });
      },
      ownProject:         (newOwner)            => this.projectData.update({ owner: newOwner }),
      newResource:        (resource)            => this.resourceList.push(resource),
      deleteResource:     (index)               => {
        const res = this.currentProjectService.getResource(this.projectId, index);
        return res.remove();
      }
    };
  }
 ownProject:         (newOwner)            => this.projectData.update({ owner: newOwner }),
 changeActiveScript: (index)               => {
   return this.projectData.update({ activeScript: index });
 },