/**
 * Copyright 2017 - 2019  The Hyve B.V.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */ import { Component , Input , OnInit } from '@angular/core' ; import { Constraint } from '../../../../models/constraint-models/constraint' ; import { CrossTableService } from '../../../../services/cross-table.service' ; import { MessageHelper } from '../../../../utilities/message-helper' ; import { AxisType } from '../../../../models/table-models/axis-type' ; import { ConceptConstraint } from '../../../../models/constraint-models/concept-constraint' ; import { VariableService } from '../../../../services/variable.service' ; import { ConstraintHelper } from '../../../../utilities/constraint-utilities/constraint-helper' ; import { ConstraintSerialiser } from '../../../../utilities/constraint-utilities/constraint-serialiser' ; @ Component ( { selector : 'gb-droppable-zone' , templateUrl : './gb-droppable-zone.component.html' , styleUrls : [ './gb-droppable-zone.component.css' ] } ) export class GbDroppableZoneComponent implements OnInit { @ Input ( ) constraints : Constraint [ ] = [ ] ; @ Input ( ) axis : AxisType = null ; @ Input ( ) disabled = false ; public dragCounter = 0 ; constructor ( private crossTableService , private variableService ) { } ngOnInit ( ) { } onDragEnter ( e ) { if ( this . dragCounter < 0 ) { this . dragCounter = 0 ; } const selectedConstraintCell = this . crossTableService . selectedConstraintCell ; const constraint = selectedConstraintCell ? selectedConstraintCell . constraint : null ; if ( constraint ) { const index = this . constraints . indexOf ( constraint ) ; if ( index === - 1 ) { this . dragCounter ++ ; } } else { this . dragCounter ++ ; } } onDragLeave ( e ) { e . preventDefault ( ) ; this . dragCounter -- ; } onDrop ( e ) { e . preventDefault ( ) ; const selectedConstraintCell = this . crossTableService . selectedConstraintCell ; let constraint = selectedConstraintCell ? selectedConstraintCell . constraint : null ; if ( this . disabled ) { } else if ( ! constraint ) { let variable = this . variableService . identifyDraggedElement ( ) ; if ( variable ) { constraint = new ConceptConstraint ( ) ; ( < ConceptConstraint > constraint ) . concept = variable ; if ( constraint && this . crossTableService . isValidConstraint ( constraint ) ) { constraint . textRepresentation = ConstraintHelper . brief ( constraint ) ; this . constraints . push ( constraint ) ; this . crossTableService . update ( this . constraints ) ; } else { const summary = 'Not a valid constraint, please choose a categorical concept!' ; MessageHelper . alert ( 'error' , summary ) ; console . error ( summary , ConstraintSerialiser . serialise ( constraint ) ) ; } } } else { const index = this . constraints . indexOf ( constraint ) ; if ( index === - 1 ) { this . constraints . push ( constraint ) ; selectedConstraintCell . remove ( ) ; } else { } } this . dragCounter = 0 ; this . crossTableService . selectedConstraintCell = null ; } param { Constraint } constraint onConstraintCellRemoved ( constraint ) { const index = this . constraints . indexOf ( constraint ) ; if ( index > - 1 ) { this . constraints . splice ( index , 1 ) ; } this . crossTableService . updateCells ( ) ; } get variablesDragDropScope ( ) : string { return this . variableService . variablesDragDropScope ; } }