File
Description
Manage Roles component
Author: Shilo Banihit
Extends
Metadata
moduleId |
module.id |
selector |
transfer-owner |
templateUrl |
./transfer_owner.html |
Methods
canTransfer
|
canTransfer()
|
|
|
clearSelResearcher
|
clearSelResearcher()
|
|
|
getSelResearcher
|
getSelResearcher()
|
|
|
Protected initUserlookup
|
initUserlookup()
|
|
|
Protected loadPlans
|
loadPlans()
|
|
|
onFilterChange
|
onFilterChange()
|
|
|
resetFilter
|
resetFilter()
|
|
|
toggleSelect
|
toggleSelect(plan: any)
|
|
Parameters :
Name |
Type |
Optional |
Description |
plan |
any
|
|
|
|
transferOwnership
|
transferOwnership(event: any)
|
|
Parameters :
Name |
Type |
Optional |
Description |
event |
any
|
|
|
|
initSubs
|
initSubs: any
|
Type : any
|
|
searchFilterName
|
searchFilterName: any
|
Type : any
|
|
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">×</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 with directive