import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ContentChild, TemplateRef} from '@angular/core';
import { CfListComponent } from '../list/list.component';
import { SelectListModel } from '../../models/list/select-list';
/**
*
Select-list is creating using ListModel and SelectListModel Classes/Components and based on Angular Material List. Component must be provided with template for each item and has selectable functionality.
* Example of using Select-list:
*
* <cf-select-list [settings]="myList" (cfItemAction)="log($event)"></cf-select-list>
*
*/
@Component({
selector: 'cf-select-list',
template: " {{cfSelectList.title}}
",
styles: [":host /deep/ md-nav-list { width: 100%; } :host /deep/ md-nav-list .cf-list-item-action { width: 100%; display: flex; flex-direction: row; align-items: center; font-family: Roboto,\"Helvetica Neue\",sans-serif; box-sizing: border-box; font-size: 16px; padding: 8px 0; } :host /deep/ .mat-list-item-content { height: auto !important; flex-direction: column !important; } [hidden] { display: none !important; } "]
})
export class CfSelectListComponent extends CfListComponent implements OnInit {
/**
* Component property to set template of standard view for each list item. Template must be inside cf-select-list html tags.
* Example:
* <cf-select-list>
* <template #itemTpl let-i="cfIndex" let-it="cfItem">
* <img md-list-avatar src="{{it.avatar}}">
* <div class="mat-list-text">
* <h4 md-line>{{it.name}}</h4>
* <p md-line>{{it.city}}</p>
* </div>
* </template>
* </cf-select-list>
* Where:
* - #itemTpl - it is an identifier of template for each row
* - let-i - it is registering template variable 'i'
* - cfIndex - it is index of row
* - let-it - it is registering template variable 'it'
* - cfItem - it is object with row json data
*/
cfSelectList: SelectListModel;
/**
* It is temprorary array for selected items. Used only inside component logic.
*/
selected_items = [];
/**
* It is temprorary array for selected items indexes. Used only inside component logic.
*/
selected_indexes = [];
/**
* It is Object which will recive data from SelectListModel Class. It is used in cf-select-list template.
* Example of SelectListModel Class:
* {
* title: 'Select-list Component Demo',
* items: [
* { name: 'Austin', city: 'London', avatar: 'http://wfarm2.dataknet.com/static/resources/icons/set3/c9f1cdf473a8.png' },
* { name: 'Molly', city: 'Denver', avatar: 'http://findicons.com/files/icons/1072/face_avatars/300/fh04.png' },
* { name: 'Peter', city: 'Tokyo', avatar: 'http://static.tumblr.com/c172fd03bf2bedb77e13107cb22cdcc1/3rxa3xq/asBo7l3wr/tumblr_static_6fbdkgohxjgoc4c8o8kwoso4k.png' }
* ],
* selected: this.mySelectedItems,
* color: {
* checked: 'lightseagreen',
* unchecked: 'darkorchid'
* }
* }
* title: string
* It is title for the List.
* items: array
* It is an array with items of any type.
* selected: array
* It is an array with selected items.
* color: object
* It is an object with two sting values for item background colors in checked/unchecked states. Optional.
* checked: string
* It is item background color in checked state.
* unchecked: string
* It is item background color in unchecked state.
*/
@Input()
public settings: SelectListModel;
/**
* It is function to check if item is selected. Used only inside component logic.
*/
cfItemSelected = function(item: Object){
return this.cfSelectList.selected.indexOf(item) > -1;
}
/**
* It is function to select / unselect item. Used only inside component logic.
* @param e event on click
* @param index index of List item
* @param item json object of List item
*/
/*cfToggleItem(e: any, item: Object, index: number){
const it_is = this.cfSelectList.selected.indexOf(item),
min_ind = Math.min(...this.selected_indexes),
max_ind = Math.max(...this.selected_indexes);
if(e.ctrlKey && this.cfSelectList.selected.length>0) {
this.cfSelectList.selected.splice(0, this.cfSelectList.selected.length);
this.selected_indexes.splice(0, this.selected_indexes.length);
if(index > min_ind){
for (var i = min_ind; i < index + 1; ++i) {
this.cfSelectList.selected.push(this.cfSelectList.items[i]);
}
}else if(index < max_ind){
for (var i = index; i < max_ind + 1; ++i) {
this.cfSelectList.selected.push(this.cfSelectList.items[i]);
}
}
this.cfSelectList.selected.forEach((obj, index) => {
const tempIndex = this.cfSelectList.items.indexOf(obj);
this.selected_indexes.push(tempIndex);
});
}else{
if (it_is === -1) {
this.cfSelectList.selected.push(item);
this.selected_indexes.push(index);
} else {
const delIndex = this.selected_indexes.indexOf(index);
this.cfSelectList.selected.splice(it_is, 1);
this.selected_indexes.splice(delIndex, 1);
}
}
}*/
/**
* It is function to set background color for checked/unchecked item states. Used only inside component logic.
* @param state name of item state (checked / unchecked)
*/
cfSetColor(state: string) {
if(state === 'checked') {
return this.cfSelectList.color && this.cfSelectList.color.checked ? this.cfSelectList.color.checked : '#f0f0f0';
}else{
return this.cfSelectList.color && this.cfSelectList.color.unchecked ? this.cfSelectList.color.unchecked : 'transparent';
}
}
constructor(public elementRef: ElementRef) {
super(elementRef);
}
/**
* It will be generated cfSelectList object and rendered inside component template.
*/
ngOnInit() {
this.cfSelectList = this.settings;
}
}