1+ import angular from "angular" ;
2+ import template from "./template.html" ;
3+ import "./styles.scss" ;
4+
5+ const app = angular . module ( "app.queryExpressionModalController" , [ ] ) ;
6+
7+ const AND_OPERATOR = 'AND' ;
8+ const OR_OPERATOR = 'OR' ;
9+
10+ const Controller = function ( $filter ) {
11+ const $ctrl = this ;
12+ $ctrl . submitted = false ;
13+ $ctrl . conditions = [ ] ;
14+ $ctrl . joins = [ ] ;
15+ $ctrl . tableColumns = [ ] ;
16+
17+ const defaultJoin = {
18+ columnNameOrigin : null ,
19+ columnNameTarget : null ,
20+ }
21+
22+ const defaultCondition = {
23+ columnName : null ,
24+ columnType : null ,
25+ type : null ,
26+ comparativeValue : null ,
27+ submitted : false ,
28+ logicalOperator : AND_OPERATOR ,
29+ } ;
30+
31+ $ctrl . selectColumnJoin = ( selected , attribute , index ) => {
32+ const selectedJoin = $ctrl . joins [ index ] ;
33+ $ctrl . joins [ index ] = {
34+ ...selectedJoin ,
35+ [ attribute ] : selected . name ,
36+ } ;
37+ }
38+
39+ $ctrl . saveJoin = ( index ) => {
40+ const selectedJoin = $ctrl . joins [ index ] ;
41+ $ctrl . joins [ index ] = {
42+ ...selectedJoin ,
43+ submitted : true ,
44+ } ;
45+ }
46+
47+ $ctrl . removeJoin = ( index ) => {
48+ $ctrl . joins . splice ( index , 1 ) ;
49+ }
50+
51+ $ctrl . createLabel = condition => {
52+ return `${ condition . columnName } ${ $filter ( 'translate' ) ( condition . name ) . toLowerCase ( ) } ${ condition . comparativeValue } ${ condition . comparativeValue2 ? `${ $filter ( 'translate' ) ( 'and' ) } ${ condition . comparativeValue2 } ` : '' } ` ;
53+ }
54+
55+ $ctrl . changeOperator = ( index ) => {
56+ const selectedCondition = $ctrl . conditions [ index ] ;
57+ $ctrl . conditions [ index ] = {
58+ ...selectedCondition ,
59+ logicalOperator : selectedCondition . logicalOperator === AND_OPERATOR ? OR_OPERATOR : AND_OPERATOR ,
60+ } ;
61+ }
62+
63+ $ctrl . selectComparasion = ( selected , index ) => {
64+ const selectedCondition = $ctrl . conditions [ index ] ;
65+ $ctrl . conditions [ index ] = {
66+ ...selectedCondition ,
67+ name : selected . name ,
68+ type : selected . type ,
69+ } ;
70+ } ;
71+
72+ $ctrl . removeCondition = ( index ) => {
73+ $ctrl . conditions . splice ( index , 1 ) ;
74+ }
75+
76+ $ctrl . saveCondition = ( index ) => {
77+ const selectedCondition = $ctrl . conditions [ index ] ;
78+ $ctrl . conditions [ index ] = {
79+ ...selectedCondition ,
80+ submitted : true ,
81+ } ;
82+ }
83+
84+ $ctrl . selectColumn = ( selected , index ) => {
85+ const selectedCondition = $ctrl . conditions [ index ] ;
86+ $ctrl . conditions [ index ] = {
87+ ...selectedCondition ,
88+ columnName : selected . name ,
89+ columnType : selected . type
90+ } ;
91+ }
92+
93+ $ctrl . addCondition = ( ) => {
94+ $ctrl . conditions . push ( defaultCondition ) ;
95+ } ;
96+
97+ $ctrl . addJoin = ( ) => {
98+ $ctrl . joins . push ( defaultJoin ) ;
99+ }
100+
101+ $ctrl . $onInit = ( ) => {
102+ $ctrl . showJoins = $ctrl . tables . length > 1 ;
103+ $ctrl . tables . forEach ( table => {
104+ table . columns . forEach ( column => {
105+ $ctrl . tableColumns . push ( {
106+ name : `${ table . name } .${ column . name } ` ,
107+ type : column . type ,
108+ } ) ;
109+ } ) ;
110+ } ) ;
111+ } ;
112+
113+ $ctrl . $onChanges = ( changes ) => {
114+ if ( changes . queryConditions != null && changes . queryConditions . currentValue != null ) {
115+ $ctrl . conditions = changes . queryConditions . currentValue . values || [ ] ;
116+ $ctrl . joins = changes . queryConditions . currentValue . joins || [ ] ;
117+ }
118+ }
119+
120+ $ctrl . save = function ( ) {
121+ $ctrl . close ( {
122+ result : {
123+ conditions : $ctrl . conditions . filter ( condition => condition . submitted ) ,
124+ joins : $ctrl . joins . filter ( join => join . submitted ) ,
125+ }
126+ } ) ;
127+ } ;
128+
129+ $ctrl . cancel = function ( ) {
130+ $ctrl . dismiss ( {
131+ reason : "cancel" ,
132+ } ) ;
133+ } ;
134+ } ;
135+
136+ export default app . component ( "queryExpressionModal" , {
137+ template,
138+ bindings : {
139+ close : "&" ,
140+ dismiss : "&" ,
141+ tables : "<" ,
142+ queryConditions : '<'
143+ } ,
144+ controller : Controller ,
145+ } ) . name ;
0 commit comments