-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueryBuilder.js
767 lines (664 loc) · 36.7 KB
/
QueryBuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// This file is part of DQX - (C) Copyright 2014, Paul Vauterin, Ben Jeffery, Alistair Miles <[email protected]>
// This program is free software licensed under the GNU Affero General Public License.
// You can find a copy of this license in LICENSE in the top directory of the source code or at <http://opensource.org/licenses/AGPL-3.0>
define(["jquery", "DQX/SQL", "DQX/Utils", "DQX/DocEl", "DQX/Msg", "DQX/FramePanel"],
function ($, SQL, DQX, DocEl, Msg, FramePanel) {
var QueryBuilder = {};
//////////////////////////////////////////////////////////////////////////////////////////
// Interactive query builder FramePanel
//////////////////////////////////////////////////////////////////////////////////////////
QueryBuilder.Panel = function (iParentRef, settings) {
var that = QueryBuilder.Builder(iParentRef, settings);
that.handleResize = function () {
if ((that.getRootElem().width() > 5) && (that.getRootElem().height() > 5))
this._reRender();
}
return that;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// The query builder class
///////////////////////////////////////////////////////////////////////////////////////////////////
// iDivID = the id of the div that serves as a container for the htnl elements
// settings.noUpdate
// NOTE: this should not be called directly. Use QueryBuilder.Panel instead
QueryBuilder.Builder = function (iParentRef, settings) {
var that = FramePanel(iParentRef);
if (!settings)
settings = {};
DQX.ObjectMapper.Add(that);
that.myColumns = []; //list of SQL.TableColInfo objects
that.bSepX = 20;
that.spacerH1 = 20; // 30;
that.spacerH2 = 15; // 20;
that.borderSize = 0;
that._globalcontentnr = 10;
that._compid = 0;
that.subsetList = settings.subsetList;
that.primKey = settings.primKey;
that.subsetTableName = settings.subsetTableName;
that.tableName = settings.tableName;
that.notifyModified = function () {
Msg.broadcast({ type: "QueryModified", id: this.getDivID() });
}
//create an individual statement
//this is a wrapper object that contains a SQL.WhereClause - type thing
that._createNewStatement = function (parent) {
this._globalcontentnr++;
var newcomp = {};
newcomp.isCompound = false;
newcomp.myOperator = SQL.WhereClause.CompareFixed(this.myColumns[0].ID, "=", "");
parent.myComponents.push(newcomp);
}
//create an OR branch
that._createCompOR = function () {
var comp = {};
comp.isCompound = true;
comp.Tpe = "OR";
comp.myComponents = [];
return comp;
}
//create an AND branch
that._createCompAND = function () {
var comp = {};
comp.isCompound = true;
comp.Tpe = "AND";
comp.myComponents = [];
return comp;
}
//the one and only root element
//The query is maintained as a tree from this root
that.root = that._createCompAND();
//adds a new column definition to the query tool. this should be of type SQL.TableColInfo
that.addTableColumn = function (icolinfo) {
this.myColumns.push(icolinfo);
}
//determines if a column with a specific id is present
that.hasColumn = function (icolid) {
for (var i in this.myColumns)
if (this.myColumns[i].ID == icolid)
return true;
return false;
}
//returns a column by id
that.getColumn = function (icolid) {
for (var i in this.myColumns)
if (this.myColumns[i].ID == icolid)
return this.myColumns[i];
DQX.reportError("Invalid column id " + icolid);
}
//Internal function: this removes all unnecessary items from the tree
that._cleanUp = function (comp) {
var modified = false;
if (comp.isCompound) {
for (var compnr in comp.myComponents) {
if (this._cleanUp(comp.myComponents[compnr]))
modified = true;
}
var compnr = 0;
while (compnr < comp.myComponents.length) {
var trycomp = comp.myComponents[compnr];
var todelete = false;
if (trycomp.isCompound) {
if (trycomp.Tpe == comp.Tpe) {
for (var subcompnr in trycomp.myComponents)
comp.myComponents.splice(compnr + 1 + subcompnr, 0, trycomp.myComponents[subcompnr]);
trycomp.myComponents = [];
modified = true;
}
if (trycomp.myComponents.length == 0) {
todelete = true;
modified = true;
}
if (trycomp.myComponents.length == 1) {
comp.myComponents[compnr] = trycomp.myComponents[0];
modified = true;
}
}
if (todelete)
comp.myComponents.splice(compnr, 1);
else
compnr++;
}
}
return modified;
}
//Called when, for a a field comparison statement, the field control was modified
that._ReactChangeField = function (id) {
this._reRender(); //todo: optimise this by only building single statement?
this.notifyModified();
}
//Called when, for a a field comparison statement, the comparison type control was modified
that._ReactChangeCompType = function (id) {
this._reRender(); //todo: optimise this by only building single statement?
this.notifyModified();
}
//Called when, for a a field comparison statement, a content control was modified
that._ReactStatementModified = function (id) {
this.notifyModified();
}
//Called when a statement should be deleted
that._ReactDel = function (id) {
if (compmap[id].myParent == null)
DQX.reportError("no parent");
var parentcomp = compmap[id].myParent;
var childid = -1;
for (var i in parentcomp.myComponents)
if (parentcomp.myComponents[i].ID == id)
childid = i;
if (childid < 0) DQX.reportError("???");
parentcomp.myComponents.splice(childid, 1);
this._reRender();
this.notifyModified();
}
//Add a new components in an existing and chain
that._ReactAddAnd = function (id) {
this._createNewStatement(compmap[id]);
this._reRender();
this.notifyModified();
}
//Add a nw component in an existing or chain
that._ReactAddOr = function (id) {
this._createNewStatement(compmap[id]);
this._reRender();
this.notifyModified();
}
//Create a new or chain at the point of an individual statement
that._ReactCreateOr = function (id) {
var parentcomp = compmap[id].myParent;
var childid = -1;
for (var i in parentcomp.myComponents)
if (parentcomp.myComponents[i].ID == id)
childid = i;
if (childid < 0) DQX.reportError("???");
var orcomp = this._createCompOR();
parentcomp.myComponents[childid] = orcomp;
orcomp.myComponents.push(compmap[id]);
this._createNewStatement(orcomp);
this._reRender();
this.notifyModified();
}
//Create new or chain at the root of the query
that._ReactCreateRootOr = function () {
var oldroot = this.root;
this.root = this._createCompAND();
var orcomp = this._createCompOR();
this.root.myComponents.push(orcomp);
orcomp.myComponents.push(oldroot);
this._createNewStatement(orcomp);
this._reRender();
this.notifyModified();
}
//Create a new and chain at the point of an individual statement
that._ReactCreateAnd = function (id) {
var parentcomp = compmap[id].myParent;
var childid = -1;
for (var i in parentcomp.myComponents)
if (parentcomp.myComponents[i].ID == id)
childid = i;
if (childid < 0) DQX.reportError("???");
var orcomp = this._createCompAND();
parentcomp.myComponents[childid] = orcomp;
orcomp.myComponents.push(compmap[id]);
this._createNewStatement(orcomp);
this._reRender();
this.notifyModified();
}
that._ReactUpdateQuery = function (id) {
Msg.broadcast({ type: "RequestUpdateQuery", id: this.getDivID() });
}
//Calculates the minimum required horizontal space for a component
that._calcMinSizeX = function (comp) {
if (comp.isCompound) {
if (comp.Tpe == 'AND') {
var minsizex = 0;
for (var compnr in comp.myComponents) {
this._calcMinSizeX(comp.myComponents[compnr]);
minsizex = Math.max(minsizex, comp.myComponents[compnr].MinSizeX);
}
comp.MinSizeX = minsizex;
}
if (comp.Tpe == 'OR') {
var minsizex = 0;
for (var compnr in comp.myComponents) {
this._calcMinSizeX(comp.myComponents[compnr]);
minsizex += comp.myComponents[compnr].MinSizeX;
}
comp.MinSizeX = minsizex + (comp.myComponents.length - 1) * this.bSepX;
}
}
else {
comp.MinSizeX = 200;
}
}
//determines the actual horizontal size of a component, adapting to the given available siwe
that._calcSizeX = function (comp, availsizex) {
if (comp.isCompound) {
if (comp.Tpe == 'AND') {
for (var compnr in comp.myComponents)
this._calcSizeX(comp.myComponents[compnr], availsizex);
comp.sizeX = availsizex;
}
if (comp.Tpe == 'OR') {
var extrasizex = availsizex - (comp.myComponents.length - 1) * this.bSepX;
for (var compnr in comp.myComponents)
extrasizex -= comp.myComponents[compnr].MinSizeX;
extrasizex = Math.floor(extrasizex / comp.myComponents.length);
for (var compnr in comp.myComponents) {
this._calcSizeX(comp.myComponents[compnr], comp.myComponents[compnr].MinSizeX + extrasizex);
}
comp.sizeX = availsizex;
}
}
else {
comp.sizeX = availsizex;
}
}
//creates a string literal that calls a react calback function
that._createReactFunctionString = function (itype, iid) {
return DQX.ObjectMapper.CreateCallBackFunctionString(this, itype, iid);
}
//or blocks get a color variation to stand them out more. this calculates the color based on the level of nested or's
that._createBlockColor = function (level) {
return DQX.parseColorString($('#' + this.getDivID()).css("background-color"), DQX.Color(0.9, 0.9, 0.9)).lighten(0.1 * level);
}
//returns the identifier for a control in an individual statement
that.getControlID = function (statementID, aspect) {
return "DQXQbldStmnt" + this.getDivID() + statementID + aspect;
}
//adds some extra required stuff around every control that appears in an query statement
that.decorateQueryStatementControl = function (ctrl, ID) {
ctrl.setOnChange(this._createReactFunctionString('_ReactStatementModified', ID));
ctrl.setOnKeyUp(this._createReactFunctionString('_ReactStatementModified', ID));
}
//render an individual comparison statement
//theComponentStatement= query statement, containing a SQL.WhereClause - thing
//theComponentElement= html element to render it in
that._buildStatement = function (theComponentStatement, theComponentElement) {
var myOperator = theComponentStatement.myOperator; //the SQL.WhereClause - thing
var sizex = theComponentStatement.sizeX;
var addor = (theComponentStatement.myParent.Tpe != 'OR');
var addand = (theComponentStatement.myParent.Tpe != 'AND');
theComponentElement.setCssClass('DQXQBQuerybox');
theComponentElement.addStyle('float', 'left');
theComponentElement.setWidthPx(sizex - 2 * this.borderSize);
theComponentElement.addStyle('position', 'relative');
var elem0 = DocEl.Div({ parent: theComponentElement });
elem0.addStyle('text-align', 'center').addStyle('padding-top', '10px').addStyle('padding-bottom', '10px').addStyle('padding-left', '8px').addStyle('padding-right', '8px');
var elem = DocEl.Span({ parent: elem0 });
elem.addElem(" ");
var thecols = [];
for (var colnr in this.myColumns) {
thecols.push({ id: this.myColumns[colnr].ID, name: this.myColumns[colnr].name, group: this.myColumns[colnr].GroupName });
}
thecols.push({ id:'_note_', name: 'has note containing', group: 'Other'});
if (that.subsetList && (that.subsetList.length>0))
thecols.push({ id:'_subset_', name: 'in subset', group: 'Other'});
var wrapper = DocEl.Div();
wrapper.addStyle('display', 'inline-block');
wrapper.setCssClass('DQXSelectWrapper');
var fieldlist = DocEl.Select(thecols, myOperator.ColName, {parent:wrapper});
fieldlist.setID(this.getControlID(theComponentStatement.ID, "Field"));
fieldlist.setWidthPx(150);
fieldlist.setCssClass('DQXQBQueryboxControl');
fieldlist.SetChangeEvent(this._createReactFunctionString('_ReactChangeField', theComponentStatement.ID));
elem.addElem(wrapper);
elem.addElem(" ");
if ((myOperator.Tpe == '_subset_') || (myOperator.Tpe == '_note_')) {
}
else {
var compatops = SQL.WhereClause.getCompatibleFieldComparisonOperators(this.getColumn(myOperator.ColName).datatype);
var cmpselectlist = [];
var foundinlist = false;
for (var operatornr in compatops) {
var op = compatops[operatornr];
cmpselectlist.push({ id: op.ID, name: op.name });
if (myOperator.Tpe == op.ID) foundinlist = true;
}
if (!foundinlist) {
myOperator.Tpe = cmpselectlist[0].id;
this._needRebuild = true;
}
var wrapper = DocEl.Div();
wrapper.addStyle('display', 'inline-block');
wrapper.setCssClass('DQXSelectWrapper');
var comptype = DocEl.Select(cmpselectlist, myOperator.Tpe, {parent:wrapper});
comptype.setID(this.getControlID(theComponentStatement.ID, "Type"));
comptype.setWidthPx(150);
comptype.setCssClass('DQXQBQueryboxControl');
comptype.SetChangeEvent(this._createReactFunctionString('_ReactChangeCompType', theComponentStatement.ID));
elem.addElem(wrapper);
}
elem.addElem(" ");
myOperator._buildStatement(theComponentStatement.ID, elem, this);
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("close.png"), "Delete this condition", this._createReactFunctionString('_ReactDel', theComponentStatement.ID));
subel.addStyle('position', 'absolute');
subel.addStyle('left', '-8px');
subel.addStyle('top', '-12px');
elem0.addElem(subel);
if (addor) {
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("addright.png"), "Create an alternative condition (OR)", this._createReactFunctionString('_ReactCreateOr', theComponentStatement.ID));
subel.addStyle('position', 'absolute');
subel.addStyle('right', '-10px');
subel.addStyle('top', '-14px');
elem0.addElem(subel);
}
if (addand) {
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("adddown.png"), "Create an extra condition (AND)", this._createReactFunctionString('_ReactCreateAnd', theComponentStatement.ID))
subel.addStyle('position', 'absolute');
subel.addStyle('left', (sizex / 2 + 8) + 'px');
subel.addStyle('bottom', '-16px');
elem0.addElem(subel);
}
}
that._buildElement = function (theQueryComponent, theQueryParentComponent, orlevel) {
var sizex = theQueryComponent.sizeX;
theQueryComponent.myParent = theQueryParentComponent;
this._compid++;
var mycompid = this._compid;
var theComponentElement = DocEl.Div();
theQueryComponent.ID = this._compid;
compmap[this._compid] = theQueryComponent;
theComponentElement.addAttribute('id', this.getControlID(theQueryComponent.ID, ''));
theComponentElement.addStyle('float', 'left');
theComponentElement.setWidthPx(sizex);
theComponentElement.setBackgroundColor(this._createBlockColor(orlevel));
if (theQueryComponent.isCompound) {
if (theQueryComponent.Tpe == 'AND') {
theComponentElement.addStyle('background-image', 'url('+DQX.BMP('arrowdown.png')+')');
theComponentElement.addStyle('background-position', 'center');
theComponentElement.addStyle('background-repeat', 'repeat-y');
theComponentElement.addStyle('position', 'relative');
for (var compnr in theQueryComponent.myComponents) {
var needspacer = (compnr > 0); // && (compnr < theQueryComponent.myComponents.length-1);
if (needspacer) {
var spacer = DocEl.Div();
spacer.addStyle('float', 'left');
spacer.setWidthPx(sizex);
spacer.setHeightPx(this.spacerH1);
theComponentElement.addElem(spacer);
}
var subcomp = this._buildElement(theQueryComponent.myComponents[compnr], theQueryComponent, orlevel);
theComponentElement.addElem(subcomp);
}
//create the "add" button
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("adddown.png"), "Create an extra condition (AND)", this._createReactFunctionString('_ReactAddAnd', mycompid));
subel.addStyle('position', 'absolute');
subel.addStyle('left', (sizex / 2 + 10) + 'px');
subel.addStyle('bottom', '-16px');
theComponentElement.addElem(subel);
}
if (theQueryComponent.Tpe == 'OR') {
orlevel++;
var subsize = (sizex - (theQueryComponent.myComponents.length - 1) * QueryBuilder.bSepX) / theQueryComponent.myComponents.length - 2;
var subcomps = [];
for (var compnr in theQueryComponent.myComponents) {
var subcomp = this._buildElement(theQueryComponent.myComponents[compnr], theQueryComponent, orlevel)
subcomps.push(subcomp);
}
//start point
var spacer = DocEl.Div();
spacer.addStyle('float', 'left');
spacer.setWidthPx(sizex);
spacer.setHeightPx(15);
spacer.setBackgroundColor('rgb(140,140,140)');
spacer.addStyle('text-align', 'center');
spacer.addStyle('position', 'relative');
spacer.addStyle('border-top-left-radius', '15px');
spacer.addStyle('border-top-right-radius', '15px');
spacer.addElem("Alternative paths (OR)")
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("addright.png"), "Add another alternative condition (OR)", this._createReactFunctionString('_ReactAddOr', mycompid));
subel.addStyle('position', 'absolute');
subel.addStyle('right', '15px');
subel.addStyle('top', '-8px');
spacer.addElem(subel);
theComponentElement.addElem(spacer);
var subcompcontainer = DocEl.Div();
subcompcontainer.setBackgroundColor(this._createBlockColor(orlevel));
subcompcontainer.addStyle('float', 'left');
subcompcontainer.setCssClass('DQXOrContainer'); //###
for (compnr in subcomps) {
if (compnr > 0) {
var spacer = DocEl.Div();
spacer.addStyle('float', 'left');
spacer.setHeightPx(3);
spacer.setWidthPx(this.bSepX);
subcompcontainer.addElem(spacer);
}
var subcompholder = DocEl.Div();
subcompholder.addStyle('float', 'left');
subcompholder.addStyle('height', '100%');
subcompholder.addStyle('background-image', 'url(' + DQX.BMP('arrowdown.png') + ')');
subcompholder.addStyle('background-position', 'center');
subcompholder.addStyle('background-repeat', 'repeat-y');
subcompholder.addStyle('padding-top', this.spacerH2 + 'px');
subcompholder.addStyle('padding-bottom', this.spacerH2 + 'px');
subcompholder.addElem(subcomps[compnr]);
subcompcontainer.addElem(subcompholder);
}
theComponentElement.addElem(subcompcontainer);
//end point
var spacer = DocEl.Div();
spacer.addStyle('float', 'left');
spacer.setWidthPx(sizex);
spacer.setHeightPx(15);
spacer.setBackgroundColor('rgb(140,140,140)');
spacer.addStyle('border-bottom-left-radius', '15px');
spacer.addStyle('border-bottom-right-radius', '15px');
theComponentElement.addElem(spacer);
}
}
else
this._buildStatement(theQueryComponent, theComponentElement);
return theComponentElement;
}
that.render = function () {
this._compid = 0;
compmap = {};
while (this._cleanUp(this.root));
var sizex = $('#' + this.getDivID()).width() - 30;
if (sizex <= 1) sizex = 600; //an elementary safety measure to avoid silly things if this is rendered to an invisible component
this._calcMinSizeX(this.root);
if (this.root.MinSizeX < sizex) {
this.root.MinSizeX = sizex;
}
sizex = this.root.MinSizeX;
this._calcSizeX(this.root, this.root.MinSizeX);
var container = DocEl.Div();
container.addStyle('float', 'left');
container.addStyle('margin-left', '9px');
container.addStyle('margin-top', '8px');
container.setWidthPx(sizex);
var createstartendpoint = function (txt) {
var spacer = DocEl.Div();
spacer.addStyle('position', 'relative');
spacer.addStyle('float', 'left');
spacer.addStyle('text-align', 'center');
spacer.setWidthPx(sizex);
spacerel = DocEl.Span({ parent: spacer });
spacerel.setBackgroundColor(DQX.Color(0.5, 0.5, 0.5));
spacerel.setColor(DQX.Color(0.9, 0.9, 0.9));
spacerel.addStyle('border-radius', '18px');
spacerel.addStyle('text-align', 'center');
spacerel.addStyle('padding-top', '10px');
spacerel.addStyle('padding-bottom', '10px');
spacerel.addStyle('display', 'block');
spacerel.addStyle('font-weight', 'bold');
spacerel.addStyle('font-size', '9pt');
spacerel.addElem(txt);
spacerel.addStyle('margin-left', 'auto');
spacerel.addStyle('margin-right', 'auto');
spacerel.addStyle('position', 'relative');
spacerel.addStyle('color', 'rgb(200,230,255)');
spacerel.setHeightPx(16);
spacerel.setWidthPx(230);
return spacer;
}
{//start point
var spacer = createstartendpoint('Full data set');
var addor = this.root.myComponents.length > 1;
if (addor) {
var subel = DocEl.JavaScriptBitmaplinkTransparent(DQX.BMP("addright.png"), "Create an alternative condition (OR)", this._createReactFunctionString('_ReactCreateRootOr', -1));
subel.addStyle('position', 'absolute');
subel.addStyle('right', '-12px');
subel.addStyle('top', '5px');
spacer.getElem(0).addElem(subel);
}
container.addElem(spacer);
}
{//spacer with arrow
var spacer = DocEl.Div();
spacer.addStyle('background-image', 'url(' + DQX.BMP('arrowdown.png') + ')');
spacer.addStyle('background-position', 'center');
spacer.addStyle('background-repeat', 'repeat-y');
spacer.addStyle('float', 'left');
spacer.setWidthPx(sizex);
spacer.setHeightPx(this.spacerH1);
container.addElem(spacer);
}
//the actual query
container.addElem(this._buildElement(this.root, null, 0));
{//spacer with arrow
var spacer = DocEl.Div();
spacer.addStyle('background-image', 'url(' + DQX.BMP('arrowdown.png') + ')');
spacer.addStyle('background-position', 'center');
spacer.addStyle('background-repeat', 'repeat-y');
spacer.addStyle('float', 'left');
spacer.setWidthPx(sizex);
spacer.setHeightPx(this.spacerH1);
container.addElem(spacer);
}
var spacer = createstartendpoint('Filtered data set');
if (!settings.noUpdate) {
var updateBitmap = DocEl.Div();
updateBitmap.setCssClass("DQXGlowButton");
updateBitmap.addAttribute("onclick", this._createReactFunctionString('_ReactUpdateQuery', -1));
updateBitmap.setWidthPx(150);
updateBitmap.addElem("Update query results");
updateBitmap.addStyle('position', 'absolute');
updateBitmap.addStyle('left', '30px');
updateBitmap.addStyle('bottom', '-40px');
spacerel.addElem(updateBitmap);
}
container.addElem(spacer);
var rs = container.toString();
$('#' + this.getDivID()).html(rs);
//Post pass: make sure that the columns in each OR block have the same height (so hat the down arrow is shown over the entire stretch)
$('.DQXOrContainer').each(
function (idx0, rootel) {
var maxh = 1;
$(rootel).children().each(
function (idx, el) {
maxh = Math.max(maxh, $(el).height());
}
);
$(rootel).children().each(
function (idx, el) {
$(el).height(maxh);
}
);
}
);
}
//fetches the content of all comparison statements in a query component
that._fetchStatementContent = function (theQueryComponent) {
if (theQueryComponent.isCompound) {
for (var compnr in theQueryComponent.myComponents) {
this._fetchStatementContent(theQueryComponent.myComponents[compnr]);
}
}
else {
if ("ID" in theQueryComponent) {
var colName = $("#" + this.getControlID(theQueryComponent.ID, "Field")).val();
theQueryComponent.myOperator = null;
if (colName == '_subset_') {
theQueryComponent.myOperator = SQL.WhereClause.getFieldComparisonOperatorInfo('_subset_').Create();
theQueryComponent.myOperator.ColName = '_subset_';
theQueryComponent.myOperator._fetchStatementContent(theQueryComponent.ID, this);
}
if (colName == '_note_') {
theQueryComponent.myOperator = SQL.WhereClause.getFieldComparisonOperatorInfo('_note_').Create();
theQueryComponent.myOperator.ColName = '_note_';
theQueryComponent.myOperator._fetchStatementContent(theQueryComponent.ID, this);
}
if (!theQueryComponent.myOperator) {
var mytype = $("#" + this.getControlID(theQueryComponent.ID, "Type")).val();
if (!mytype)
mytype = '=';
theQueryComponent.myOperator = SQL.WhereClause.getFieldComparisonOperatorInfo(mytype).Create();
theQueryComponent.myOperator.ColName = colName;
theQueryComponent.myOperator._fetchStatementContent(theQueryComponent.ID, this);
}
}
}
}
//Forces a recreation of the UI
that._reRender = function () {
this._needRebuild = false;
//we first fetch the current status of the content
this._fetchStatementContent(this.root);
this.render();
if (this._needRebuild) {
this._fetchStatementContent(this.root);
this.render();
}
}
//returns a SQL.WhereClause - thing for a query component
that._extractQueryContent = function (theQueryComponent) {
if (theQueryComponent.isCompound) {
var rs = SQL.WhereClause.Compound(theQueryComponent.Tpe);
for (var compnr in theQueryComponent.myComponents)
rs.addComponent(this._extractQueryContent(theQueryComponent.myComponents[compnr]));
return rs;
}
else {
return theQueryComponent.myOperator;
}
}
//returns the query defined by this builder, returning a class tree defined with SQL.WhereClause stuff
that.getQuery = function () {
this._fetchStatementContent(this.root);
if (this.root.myComponents.length == 0)
return SQL.WhereClause.Trivial();
return this._extractQueryContent(this.root);
}
/*
//returns a user-friendly description of the query
that.getQueryDisplayString = function() {
if (this.root.myComponents.length == 0)
return 'All';
}
*/
that._importQuery = function (statement) {
if (statement.isCompound) {
if (statement.Tpe == 'OR')
var newcomp = this._createCompOR();
else
var newcomp = this._createCompAND();
for (var i = 0; i < statement.Components.length; i++) {
var subcomp = this._importQuery(statement.Components[i]);
newcomp.myComponents.push(subcomp);
}
return newcomp;
}
else {
var newcomp = {};
newcomp.isCompound = false;
newcomp.myOperator = statement;
return newcomp;
}
}
//sets thequery for this builder, providing a class tree defined with SQL.WhereClause stuff
that.setQuery = function (queryTree) {
this.root = this._createCompAND();
if (queryTree.Tpe != '')
this.root.myComponents.push(this._importQuery(queryTree));
/* if (queryTree.Tpe == '')
this.root=this._createCompAND();
else
this.root = this._importQuery(queryTree);*/
this.render();
}
return that;
}
return QueryBuilder;
});