src/app/modules/content-search/components/topic-picker/topic-picker.component.ts
OnInit
AfterViewInit
OnDestroy
selector | app-topic-picker |
styleUrls | ./topic-picker.component.scss |
templateUrl | ./topic-picker.component.html |
Properties |
|
Methods |
|
Inputs |
Outputs |
constructor(resourceService: ResourceService, lazzyLoadScriptService: LazzyLoadScriptService)
|
|||||||||
Parameters :
|
formTopic | |
Type : any
|
|
selectedTopics | |
Type : any
|
|
topicPickerClass | |
Type : string
|
|
topicChange | |
Type : EventEmitter
|
|
Private formatSelectedTopics | ||||||||
formatSelectedTopics(topics, unformatted, formated)
|
||||||||
Parameters :
Returns :
void
|
Private formatTopics | |||||||||
formatTopics(topics, subTopic)
|
|||||||||
Parameters :
Returns :
Array<TopicTreeNode>
|
Private initTopicPicker | ||||||
initTopicPicker(data: Array<TopicTreeNode>)
|
||||||
Parameters :
Returns :
void
|
ngAfterViewInit |
ngAfterViewInit()
|
Returns :
void
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Public placeHolder |
Type : string
|
resourceDataSubscription |
Type : Subscription
|
Public resourceService |
Type : ResourceService
|
Public selectedNodes |
Type : any
|
import { Component, OnInit, Output, Input, EventEmitter, OnDestroy, AfterViewInit } from '@angular/core';
import * as _ from 'lodash-es';
import { Subscription, combineLatest } from 'rxjs';
import { ResourceService } from '@sunbird/shared';
import * as treePicker from './../../../../../assets/libs/semantic-ui-tree-picker/semantic-ui-tree-picker';
import { tap } from 'rxjs/operators';
import { LazzyLoadScriptService } from 'LazzyLoadScriptService';
$.fn.treePicker = treePicker;
interface TopicTreeNode {
id: string;
name: string;
selectable: string;
nodes: Array<TopicTreeNode>;
}
@Component({
selector: 'app-topic-picker',
templateUrl: './topic-picker.component.html',
styleUrls: ['./topic-picker.component.scss']
})
export class TopicPickerComponent implements OnInit, AfterViewInit, OnDestroy {
@Input() formTopic: any;
@Input() selectedTopics: any;
@Input() topicPickerClass: string;
@Output() topicChange = new EventEmitter();
public placeHolder: string;
public selectedNodes: any;
resourceDataSubscription: Subscription;
constructor(public resourceService: ResourceService, private lazzyLoadScriptService: LazzyLoadScriptService) {
this.resourceService = resourceService;
}
ngOnInit() {
this.resourceDataSubscription = this.resourceService.languageSelected$
.pipe(
tap(() => {
const selectedTopics = _.reduce(this.selectedTopics, (collector, element) => {
if (typeof element === 'string') {
collector.unformatted.push(element);
} else if (_.get(element, 'identifier')) {
collector.formated.push(element);
}
return collector;
}, { formated: [], unformatted: [] });
this.formatSelectedTopics(this.formTopic.range, selectedTopics.unformatted, selectedTopics.formated);
this.selectedTopics = selectedTopics.formated;
this.selectedNodes = { ...selectedTopics.formated };
this.topicChange.emit(this.selectedTopics);
})
)
.subscribe(item => {
this.initTopicPicker(this.formatTopics(this.formTopic.range));
this.placeHolder = this.selectedTopics.length + ' ' + this.resourceService.frmelmnts.lbl.topics +
' ' + this.resourceService.frmelmnts.lbl.selected;
}
);
}
private formatSelectedTopics(topics, unformatted, formated) {
_.forEach(topics, (topic) => {
if (unformatted.includes(topic.name) && !topic.children) {
formated.push({
identifier: topic.identifier,
name: topic.name
});
}
if (topic.children) {
this.formatSelectedTopics(topic.children, unformatted, formated);
}
});
}
ngAfterViewInit() {
this.initTopicPicker(this.formatTopics(this.formTopic.range));
}
private formatTopics(topics, subTopic = false): Array<TopicTreeNode> {
return _.map(topics, (topic) => ({
id: topic.identifier,
name: topic.name,
selectable: subTopic ? 'selectable' : 'notselectable',
nodes: this.formatTopics(topic.children, true)
}));
}
private initTopicPicker(data: Array<TopicTreeNode>) {
combineLatest(this.lazzyLoadScriptService.loadScript('fancytree-all-deps.js'),
this.lazzyLoadScriptService.loadScript('semanticModal.js')).subscribe(() => {
jQuery('.topic-picker-selector').treePicker({
data: data,
name: this.resourceService.frmelmnts.lbl.topics,
noDataMessage: this.resourceService.messages.fmsg.m0089,
submitButtonText: this.resourceService.frmelmnts.lbl.done,
cancelButtonText: this.resourceService.frmelmnts.btn.cancelCapitalize,
removeAllText: this.resourceService.frmelmnts.lbl.removeAll,
chooseAllText: this.resourceService.frmelmnts.lbl.chooseAll,
searchText: this.resourceService.frmelmnts.prmpt.search,
selectedText: this.resourceService.frmelmnts.lbl.selected,
picked: _.map(this.selectedNodes, 'identifier'),
onSubmit: (selectedNodes) => {
this.selectedNodes = selectedNodes;
this.selectedTopics = _.map(selectedNodes, node => ({
identifier: node.id,
name: node.name
}));
this.placeHolder = this.selectedTopics.length + ' topics selected';
this.topicChange.emit(this.selectedTopics);
},
nodeName: 'topicSelector',
minSearchQueryLength: 1
});
setTimeout(() =>
document.getElementById('topicSelector').classList.add(this.topicPickerClass), 100);
});
}
ngOnDestroy() {
if (this.resourceDataSubscription) {
this.resourceDataSubscription.unsubscribe();
}
}
}
<div class="treepicker-parent">
<input [(ngModel)]="placeHolder" id="treePicker" class="topic-picker-selector cursor-pointer" readonly/>
</div>
./topic-picker.component.scss
input{
background: transparent !important;
}