File

assets/angular/transfer_owner/transfer_owner.component.ts

Description

Manage Roles component

Author: Shilo Banihit

Extends

LoadableComponent

Metadata

moduleId module.id
selector transfer-owner
templateUrl ./transfer_owner.html

Index

Properties
Methods

Constructor

constructor(dashboardService: DashboardService, translationService: TranslationService, emailService: EmailNotificationService, vocabFieldLookupService: , completerService: CompleterService, recordService: RecordsService, app: ApplicationRef)
Parameters :
Name Type Optional Description
dashboardService DashboardService
translationService TranslationService
emailService EmailNotificationService
vocabFieldLookupService
completerService CompleterService
recordService RecordsService
app ApplicationRef

Methods

canTransfer
canTransfer()
Returns : boolean
clearSelResearcher
clearSelResearcher()
Returns : void
getSelResearcher
getSelResearcher()
Returns : any
Protected initUserlookup
initUserlookup()
Returns : void
Protected loadPlans
loadPlans()
Returns : void
onFilterChange
onFilterChange()
Returns : void
resetFilter
resetFilter()
Returns : void
toggleSelect
toggleSelect(plan: any)
Parameters :
Name Type Optional Description
plan any
Returns : void
transferOwnership
transferOwnership(event: any)
Parameters :
Name Type Optional Description
event any
Returns : void

Properties

filteredPlans
filteredPlans: Plan[]
Type : Plan[]
formGroup
formGroup: FormGroup
Type : FormGroup
initSubs
initSubs: any
Type : any
plans
plans: PlanTable
Type : PlanTable
saveMsg
saveMsg: string
Type : string
saveMsgType
saveMsgType: string
Type : string
searchFilterName
searchFilterName: any
Type : any
Public translationService
translationService: TranslationService
Type : TranslationService
userLookupMeta
userLookupMeta: VocabField
Type : VocabField
import { Component, Injectable, Inject, ApplicationRef } from '@angular/core';
import { FormArray, FormGroup, FormControl, Validators } from '@angular/forms';
import { DashboardService } from '../shared/dashboard-service';
import * as _ from "lodash-lib";
import { LoadableComponent } from '../shared/loadable.component';
import { TranslationService } from '../shared/translation-service';
import { PlanTable, Plan } from '../shared/dashboard-models';
import { VocabField, VocabFieldComponent, VocabFieldLookupService } from '../shared/form/field-vocab.component';
import { CompleterService } from 'ng2-completer';
import { RecordsService } from '../shared/form/records.service';
import { EmailNotificationService} from '../shared/email-service';

declare var pageData :any;
declare var jQuery: any;
/**
 * Manage Roles component
 *
 *
 * Author: <a href='https://github.com/shilob' target='_blank'>Shilo Banihit</a>
 */
@Component({
  moduleId: module.id,
  selector: 'transfer-owner',
  templateUrl: './transfer_owner.html'
})
export class TransferOwnerComponent extends LoadableComponent {
  plans: PlanTable;
  initSubs: any;
  searchFilterName: any;
  filteredPlans: Plan[];
  userLookupMeta: VocabField;
  formGroup: FormGroup;
  saveMsg: string;
  saveMsgType: string;

  constructor(@Inject(DashboardService) protected dashboardService: DashboardService,
   public translationService:TranslationService,
   protected emailService: EmailNotificationService,
   @Inject(VocabFieldLookupService) private vocabFieldLookupService,
   @Inject(CompleterService) private completerService: CompleterService,
   @Inject(RecordsService) private recordService: RecordsService,
   private app: ApplicationRef ) {
    super();
    this.initTranslator(translationService);
    this.plans = new PlanTable();
    this.initSubs = dashboardService.waitForInit((initStat:boolean) => {
      this.initSubs.unsubscribe();
      this.loadPlans();
    });
  }

  protected loadPlans() {
    this.translationService.isReady(tService => {
      this.dashboardService.getAlllDraftPlansCanEdit().then((draftPlans: PlanTable) => {
        this.dashboardService.setDashboardTitle(draftPlans);
        this.plans = draftPlans;
        this.filteredPlans = this.plans.items;
        if (!this.userLookupMeta) {
          this.initUserlookup();
        }
        this.saveMsg = "";
        this.saveMsgType = "";
        this.setLoading(false);
      });
    });
  }

  protected initUserlookup() {
    // create meta object for vocab
    const userLookupOptions = {
      sourceType: 'user',
      fieldNames: ['name', 'id', 'username'],
      searchFields: 'name',
      titleFieldArr: ['name'],
      editMode: true,
      placeHolder: this.translationService.t('transfer-ownership-researcher-name')
    };
    this.userLookupMeta = new VocabField(userLookupOptions, this.app['_injector']);
    this.userLookupMeta.completerService = this.completerService;
    this.userLookupMeta.lookupService = this.vocabFieldLookupService;
    this.userLookupMeta.createFormModel();
    this.userLookupMeta.initLookupData();
    this.formGroup = new FormGroup({researcher_name: this.userLookupMeta.formModel});
  }

  onFilterChange() {
    if (this.searchFilterName) {
      this.filteredPlans = _.filter(this.plans.items, (plan: any)=> {
        plan.selected = false;
        return _.toLower(plan.dashboardTitle).includes(_.toLower(this.searchFilterName));
      });
    } else {
      this.filteredPlans = this.plans.items;
    }
  }

  resetFilter() {
    this.searchFilterName = null;
    this.onFilterChange();
  }

  toggleSelect(plan: any) {
    plan.selected = !plan.selected;
  }

  transferOwnership(event: any) {
    const records = [];
    const recordMeta = [];
    _.forEach(this.filteredPlans, (plan: any) => {
      if (plan.selected) {
        records.push(plan);
        var record = {}
        var title = plan.dashboardTitle;
        record['url'] = this.emailService.getBrandingAndPortalUrl + "/record/view/" + plan.oid;
        record['title'] = title.toString();
        recordMeta.push(record);
      }
    });
    const username = this.getSelResearcher()['username'];
    const name = this.getSelResearcher()['name'];
    const email = this.getSelResearcher()['email'];
    this.saveMsg = `${this.translationService.t('transfer-ownership-transferring')}${this.spinnerElem}`;
    this.saveMsgType = "info";
    this.clearSelResearcher();
    this.recordService.modifyEditors(records, username).then((res:any)=>{
      this.saveMsg = this.translationService.t('transfer-ownership-transfer-ok');
      this.saveMsgType = "success";

      // ownership transfer ok, send notification email
      if (email) { // email address isn't a required property for the user model!
        var data = {};
        data['name'] = name;
        data['records'] = recordMeta;
        this.emailService.sendNotification(email, 'transferOwnerTo', data)
        .then(function (res) {console.log(`Email result: ${JSON.stringify(res)}`)}); // what should we do with this?
      }

      this.setLoading(true);
      this.loadPlans();
    }).catch((err:any)=>{
      this.saveMsg = err;
      this.saveMsgType = "danger";
    });
    jQuery('#myModal').modal('hide');
  }

  getSelResearcher() {
    return this.formGroup.value.researcher_name;
  }

  clearSelResearcher() {
    this.formGroup.value.researcher_name = "";
  }

  canTransfer() {
    let hasSelectedPlan = false;
    _.forEach(this.filteredPlans, (plan: any) => {
      if (plan.selected) {
        hasSelectedPlan = true;
        return false;
      }
    });
    return hasSelectedPlan && this.getSelResearcher();
  }
}
<div class="col-md-offset-2 col-md-8" *ngIf="!isLoading">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3 class="panel-title">
        {{ 'transfer-ownership-title' | translate }}
      </h3>
    </div>
    <div class="panel-body" style="overflow:scroll" *ngIf="formGroup">
      <div>{{ 'transfer-ownership-step-1' | translate }}</div>
      <div><br/></div>
      <div class="input-group">
        <span class="input-group-addon" id="name-addon">{{ 'transfer-ownership-filter-plan-name' | translate }}</span>
        <input type="text" (keyup)="onFilterChange()" [(ngModel)]="searchFilterName" class="form-control" placeholder="{{ 'transfer-ownership-filter-plan-name-placeholder' | translate}}" aria-describedby="name-addon">
        <span (click)="resetFilter()" class="input-group-btn"><button class="btn btn-primary" type='button'>{{ 'transfer-ownership-reset' | translate }}</button></span>
      </div>
      <div><br/></div>
      <div class="table-responsive">
        <table class="table table-bordered table-striped table-hover">
          <thead>
            <tr>
              <th class="text-center" style="width: 10%;">{{ 'transfer-ownership-select' | translate }}</th>
              <th>{{ 'transfer-ownership-plan-name' | translate }}</th>
            </tr>
          </thead>
          <tbody>
            <tr *ngFor="let plan of filteredPlans" (click)="toggleSelect(plan)">
              <td class="text-center"><input type='checkbox'  [(ngModel)]="plan.selected"/></td>
              <td>{{ plan.dashboardTitle }}</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div><br/></div>
      <div>{{ 'transfer-ownership-step-2' | translate }}</div>
      <rb-vocab [field]="userLookupMeta" [form]="formGroup" [disableEditAfterSelect]="false"></rb-vocab>
      <button [disabled]="!canTransfer()" class="btn btn-primary" type='button' data-toggle="modal" data-target="#myModal">{{ 'transfer-ownership-execute' | translate }}</button>
      <div><br/></div>
      <div>{{ 'transfer-ownership-step-2-update' | translate }}</div>
      <div><br/></div>
      <div class="bg-{{saveMsgType}} text-center" [innerHtml]="saveMsg"></div>
    </div>
  </div>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog" *ngIf="!isLoading" >
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">{{ 'transfer-ownership-confirm-title' | translate }}</h4>
      </div>
      <div class="modal-body">
        <h5>{{ 'transfer-ownership-confirm-body' | translate }}</h5>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" (click)="transferOwnership($event)">{{ 'transfer-ownership-confirm-yes' | translate }}</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">{{ 'transfer-ownership-confirm-no' | translate }}</button>
      </div>
    </div>
  </div>
</div>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""