-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbravefilter.js
More file actions
656 lines (564 loc) · 23.7 KB
/
bravefilter.js
File metadata and controls
656 lines (564 loc) · 23.7 KB
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
/**
* returns x, y coordinates for absolute positioning of a span within a given text input
* at a given selection point
* @param {object} input - the input element to obtain coordinates for
* @param {number} selectionPoint - the selection point for the input
*/
function stripHtml(html) {
// Create a new div element
var temporalDivElement = document.createElement("div");
// Set the HTML content with the providen
temporalDivElement.innerHTML = html;
// Retrieve the text property of the element (cross-browser support)
return temporalDivElement.innerText;
}
var decodeHTML = function (html) {
var txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
};
const getCursorXY = (input, selectionPoint) => {
const {
offsetLeft: inputX,
offsetTop: inputY,
} = input
// create a dummy element that will be a clone of our input
const div = document.createElement('div')
// get the computed style of the input and clone it onto the dummy element
const copyStyle = getComputedStyle(input)
for (const prop of copyStyle) {
div.style[prop] = copyStyle[prop]
}
// we need a character that will replace whitespace when filling our dummy element if it's a single line <input/>
const swap = '.'
const inputValue = input.tagName === 'INPUT' ? input.value.replace(/ /g, swap) : input.value
// set the div content to that of the textarea up until selection
const textContent = inputValue.substr(0, selectionPoint)
// set the text content of the dummy element div
div.textContent = textContent
if (input.tagName === 'TEXTAREA') div.style.height = 'auto'
// if a single line input then the div needs to be single line and not break out like a text area
if (input.tagName === 'INPUT') div.style.width = 'auto'
// create a marker element to obtain caret position
const span = document.createElement('span')
// give the span the textContent of remaining content so that the recreated dummy element is as close as possible
span.textContent = inputValue.substr(selectionPoint) || '.'
// append the span marker to the div
div.appendChild(span)
// append the dummy element to the body
document.body.appendChild(div)
// get the marker position, this is the caret position top and left relative to the input
const {
offsetLeft: spanX,
offsetTop: spanY
} = span
// lastly, remove that dummy element
// NOTE:: can comment this out for debugging purposes if you want to see where that span is rendered
document.body.removeChild(div)
// return an object with the x and y of the caret. account for input positioning so that you don't need to wrap the input
return {
x: inputX + spanX,
y: inputY + spanY,
}
}
function getSelectionCoords() {
var sel = document.selection,
range, rect;
var x = 0,
y = 0;
if (sel) {
if (sel.type != "Control") {
range = sel.createRange();
range.collapse(true);
x = range.boundingLeft;
y = range.boundingTop;
}
} else if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0).cloneRange();
if (range.getClientRects) {
range.collapse(true);
if (range.getClientRects().length > 0) {
rect = range.getClientRects()[0];
x = rect.left;
y = rect.top;
}
}
// Fall back to inserting a temporary element
if (x == 0 && y == 0) {
var span = document.createElement("span");
if (span.getClientRects) {
// Ensure span has dimensions and position by
// adding a zero-width space character
span.appendChild(document.createTextNode("\u200b"));
range.insertNode(span);
rect = span.getClientRects()[0];
x = rect.left;
y = rect.top;
var spanParent = span.parentNode;
spanParent.removeChild(span);
// Glue any broken text nodes back together
spanParent.normalize();
}
}
}
}
return {
x: x,
y: y
};
}
var app = new Vue({
el: '#app',
data: {
fields: listingFields,
queryOperators: {
"string": ["=", "~", "in"],
"date": ["=", ">", "<", ">=", "<="],
"reference": ["=", "!="],
},
dateSuggestions: [
/*
(+/-)nn(y|M|w|d|h|m)
endOfDay("+1") // due by the end of tomorrow:
endOfMonth("+15d") // Find issues due by the 15th of next month:
*/
{
name: "endOfDay()",
hint: "endOfDay(inc), inc is an optional increment of (+/-)(year|month|week|day|hour|minute)"
},
{
name: "endOfMonth()",
hint: ""
},
{
name: "endOfWeek()",
hint: ""
},
{
name: "endOfYear()",
hint: ""
},
{
name: "endOfDay()",
hint: ""
},
{
name: "startOfDay()",
hint: ""
},
{
name: "startOfMonth()",
hint: ""
},
{
name: "startOfWeek()",
hint: ""
},
{
name: "startOfYear()",
hint: ""
},
{
name: "now()",
hint: ""
},
{
name: "datestring()",
hint: ""
},
{
name: "endOfDay('+1 day')",
hint: "Due End of Tomorrow"
},
{
name: "endOfMonth('+15 days')",
hint: "Due by the 15th of next month"
},
],
keywords: ["and", "or"],
switch: "field", // field, operator, value, keyword.
realQuery: filterRaw,
syntaxStatus: "correct",
queryElements: [],
queryComplete: "", // yes, no
syntaxString: "",
suggestionAr: [],
selectedIndex: 0,
sqlQuery: ""
},
watch: {
realQuery: function(val) {
// Replacting the spaces inside the quotes so that we do not break them apart.
/*
var phrase = val;
var repPharse = phrase.replace(/(<([^>]+)>)/ig,"");
//console.log(phrase + "90")
repPharse = repPharse.replace(/ /gi, " ");
var repPh2 = repPharse.replace(/(\".*?\")/gi, this.replacer);
// Exploading the phrase into array where we find the spaces.
console.log("Starts");
console.log(repPh2);
console.log(repPh2.split(" "));
console.log("ends");
this.queryElements = repPh2.split(" ");
this.checkOrder();
this.anykeyPress();
console.log(this.realQuery)
console.log(this.syntaxString);
*/
/* // Replacting the spaces inside the quotes so that we do not break them apart.
//this.realQuery = this.realQuery.replace(/ /gi, " ");
// console.log("Superb phrase");
//console.log(val+"90");
//var phrase = val.replace(/ /gi, " ");
// var phrase = stripHtml(phrase);
var phrase = val.replace(/(\".*?\")/gi, this.replacer);
// Exploading the phrase into array where we find the spaces.
//console.log(phrase+"90");
this.queryElements = phrase.split(" ");
this.checkOrder();
if(this.realQuery!=this.syntaxString)
{
console.log("matching");
console.log("-"+this.realQuery+"-");
console.log("-"+this.syntaxString+"-");
// this.realQuery = this.syntaxString;
}
this.anykeyPress();*/
}
},
updated: function() {
this.anykeyPress();
},
methods: {
onKeyUp: function(event) {
if (event.key == "ArrowDown" || event.key == "ArrowUp"|| event.key == "ArrowUp") {
return;
}
console.log(event);
var phrase = this.realQuery;
var repPharse = phrase.replace(/(<([^>]+)>)/ig, "");
//var repPharse = decodeHTML(phrase);
//var repPharse = phrase.toString();
//repPharse = repPharse.replace(/ /gi, "4321");
repPharse = repPharse.replace(/ /g, " ");
console.log(repPharse + "90")
if (repPharse.trim() != "") {
var repPh2 = repPharse.replace(/(\".*?\")/g, this.replacer);
repPh2 = repPh2.replace(new RegExp(" +","g"), function() {
return "###";
});
// Exploading the phrase into array where we find the spaces.
console.log("Starts");
console.log(repPh2 + "------");
console.log(repPh2.split("###"));
console.log("ends");
this.queryElements = repPh2.split("###");
console.log(this.queryElements);
} else {
this.queryElements = [];
console.log("beauty");
console.log(this.realQuery);
}
this.checkOrder();
this.anykeyPress();
this.realQuery = this.syntaxString;
},
onFocus: function() {
$(document.body).on('click.menuHide', function(e) {
var $body = $(this);
if ($(e.target).parents('.auto-suggester').length == 0 && !$(e.target).hasClass('auto-suggester') && !$(e.target).hasClass('editable-filter-query')) {
$(".auto-suggester").hide();
$body.off('click.menuHide');
}
});
/* this.checkOrder();
this.anykeyPress();
this.realQuery = this.syntaxString;*/
if (this.suggestionAr.length) {
$(".auto-suggester").show();
}
/*this.onKeyUp();*/
},
onBlur: function() {
//setTimeout(function(){ $(".auto-suggester").hide(); }, 1000);
},
anykeyPress: function() {
/* var input = $(".editable-filter-query")[0];
const {
offsetLeft,
offsetTop,
offsetHeight,
offsetWidth,
scrollLeft,
scrollTop,
selectionEnd,
} = input
const { lineHeight, paddingRight } = getComputedStyle(input);
const { x, y } = getSelectionCoords();
// set the marker positioning
// for the left positioning we ensure that the maximum left position is the width of the input minus the right padding using Math.min
// we also account for current scroll position of the input
const newLeft = Math.min(
x - scrollLeft,
(offsetLeft + offsetWidth) - parseInt(paddingRight, 10)
)
// for the top positioning we ensure that the maximum top position is the height of the input minus line height
// we also account for current scroll position of the input
const newTop = Math.min(
y - scrollTop,
(offsetTop + offsetHeight) - parseInt(lineHeight, 10)
)
*/
var coordinates2 = getSelectionCoords()
var newLeft2 = coordinates2.x;
var newTop2 = coordinates2.y + 30;
$(".auto-suggester").css("left", newLeft2);
$(".auto-suggester").css("top", newTop2);
var target = $(".s-item.active")[0];
if (target != undefined) {
$(".s-item.active")[0].scrollIntoView({
behavior: "auto",
block: "nearest"
});
}
},
arrowDown: function() {
if (this.suggestionAr.length - 1 > this.selectedIndex) {
this.selectedIndex++
}
},
arrowUp: function() {
if (this.selectedIndex > 0) {
this.selectedIndex--;
}
},
insertKeyWord: function(event) {
if (this.queryElements.length>0)
{
this.queryElements[this.queryElements.length - 1] = this.suggestionAr[this.selectedIndex].key + "";
}
else
{
this.queryElements.push(this.suggestionAr[this.selectedIndex].key);
}
this.queryElements.push(" ");
var test = this.queryElements.join(" ");
console.log("test");
console.log(test);
test = test.replace(new RegExp(" ", "g")," ")
console.log(test);
this.realQuery = test;
this.realQuery = this.realQuery.replace("__", " ");
this.anykeyPress();
},
isMatchItem: function(index) {
return index == this.selectedIndex;
},
parsePhase: function() {
var phrase = this.realQuery;
var phrase = phrase.replace(/(\".*?\")/gi, this.replacer);
},
replacer: function(match, offset, string) {
return match.replace(new RegExp(' ', 'g'), "__");
},
checkOrder: function() {
console.log("hello");
console.log(this.queryElements);
console.log("hello-ends");
this.selectedIndex = 0;
var syntaxStringAr = [];
this.syntaxString = ""
this.sqlQuery = ""
this.syntaxStatus = "correct";
var order = ["fields", "operators", "value", "keywords"];
var orderMarker = 0;
var error = 0;
var last_type = "";
this.queryComplete = "yes";
var referenceAr = [];
var currentElement =""
for (var i = 0; i < this.queryElements.length; i++) {
var alternateValue = "";
currentElement = this.queryElements[i];
var expectedType = order[orderMarker];
if (currentElement == "") {
syntaxStringAr.push(" ");
}
if (currentElement == "") {
continue;
}
if (currentElement == ")" || currentElement == "(") {
this.syntaxString += currentElement + " ";
continue;
}
if (expectedType == "fields") {
var fieldMatches = this.fields.filter(function(item) {
return item.key == currentElement;
});
if (fieldMatches.length > 0) {
last_type = fieldMatches[0].fieldType;
if (last_type == "single_reference") {
last_type = "reference"
}
if (last_type == "number" || last_type == "textarea") {
last_type = "string"
}
if (last_type == "datepicker" || last_type == "datetime") {
last_type = "date"
}
if (last_type == "reference") {
referenceAr = fieldMatches[0].suggestions;
alternateValue = currentElement + "";
}
}
}
if (expectedType == "operators") {
if (last_type != "") {
var fieldMatches = this.queryOperators[last_type].filter(function(item) {
return item == currentElement;
})
}
}
if (expectedType == "keywords") {
var fieldMatches = this.keywords.filter(function(item) {
return item == currentElement;
})
}
if (expectedType == "value") {
if (last_type == "reference") {
/* Finding the the Key */
var nameToCheck = currentElement.replace(new RegExp("__", 'g'), " ").replace(new RegExp('"', 'g'), "")
currentElement = currentElement.replace(new RegExp("__", 'g'), " ");
console.log("Our current element");
console.log(this.suggestionAr);
console.log(nameToCheck);
var suggestionAr = referenceAr;
var fieldMatches = suggestionAr.filter(function(item) {
return item.name == nameToCheck;
});
if (fieldMatches.length > 0) {
alternateValue = fieldMatches[0].id;
}
}
}
if (fieldMatches.length > 0) {
console.log("Match Found");
this.syntaxString += currentElement;
syntaxStringAr.push(currentElement);
if (alternateValue == "") {
this.sqlQuery += currentElement + " ";
} else {
this.sqlQuery += alternateValue + " ";
}
} else {
this.syntaxString += "<span class='text-danger'>" + currentElement + "</span>";
syntaxStringAr.push("<span class='text-danger'>" + currentElement + "</span>");
console.log("No match found");
this.syntaxStatus = "error";
}
orderMarker++;
/* ----------------------------------------*/
/* Reseting the Order back to expect Field*/
/* -----------------------------------------*/
if (orderMarker == order.length) {
orderMarker = 0;
}
}
/* ========================================*/
this.syntaxString = syntaxStringAr.join(" ");
console.log(this.syntaxString)
this.syntaxString = this.syntaxString.replace(new RegExp(" ", "g"), " ");
console.log("okasy")
console.log(syntaxStringAr)
console.log(this.syntaxString)
/* ========================================*/
/* ----------------------------------------*/
/* If query is complete */
/* -----------------------------------------*/
if (order[orderMarker - 1] != "value" && this.queryElements.length > 0) {
this.queryComplete = "no";
}
/* -----------------------------------------*/
this.suggestionAr = [];
/* --------------------------------------------------------*/
/* AUTOCOMPLETE BASED ON TYPE OF FIELD. */
/* --------------------------------------------------------*/
if (currentElement.trim() == "" || currentElement.trim() == ")" || currentElement.trim() == "(") {
orderMarker = orderMarker + 1;
}
if (order[orderMarker - 1] == "fields") {
this.suggestionAr = this.fields.map(function(item) {
return {
name: item.name,
key: item.key + "",
hint: ""
};
})
}
if (order[orderMarker - 1] == "operators") {
if (last_type != "") {
this.suggestionAr = this.queryOperators[last_type].map(function(item) {
return {
name: item,
key: item,
hint: ""
};
})
}
}
if (order[orderMarker - 1] == "keywords") {
this.suggestionAr = this.keywords.map(function(item) {
return {
name: item,
key: item + "",
hint: ""
};
})
}
if (order[orderMarker - 1] == "value") {
if (last_type == "date") {
this.suggestionAr = this.dateSuggestions.map(function(item) {
return {
name: item.name,
key: item.name + "",
hint: item.hint
};
})
}
if (last_type == "reference") {
this.suggestionAr = referenceAr.map(function(item) {
return {
id: item.id,
name: item.name,
key: '"' + item.name + '"',
hint: ""
};
})
}
}
/* ---------------------------------------------------------*/
/* FILTERING WITH FUES. */
/* ---------------------------------------------------------*/
var options = {
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ["name"]
};
var fuse = new Fuse(this.suggestionAr, options); // "list" is the item array
if (currentElement.trim() != "") {
this.suggestionAr = fuse.search(currentElement);
}
this.anykeyPress();
}
}
})
$(window).scroll(function() {
app.anykeyPress();
})