Commit ae5096d2 authored by Iker Hurtado's avatar Iker Hurtado
Browse files

Complex search logic implementation (6)

parent 914ec763
......@@ -7699,6 +7699,11 @@
</div>
<input type="checkbox" id="multiples-of-formula" value="">
Include multiples of formula
<br>
<div class="add-buttons" >
<div class="tab-buttons" style="width: 70%; display: inline-block">
......@@ -7961,13 +7966,12 @@
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F'){
// ** PENDING: search Not allowing other elements
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item));
}else
esMatchQuery = this._getESOperatorMatch('formula', this._tokenizeFormula(item));
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){
......@@ -8116,7 +8120,7 @@
if ( this.searchQuery.length > 0 && isOpen)
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'P');
this.updateSearchQuery();
//this._showSearchBox();
}
......@@ -8141,46 +8145,64 @@
}*/
removeElementORFormulaInSearchQuery(element){
//console.log(" removeElementORFormulaInSearchQuery element: ",element);
let index = this.searchQuery.indexOf(element);
if (index >= 0){
this.searchQuery.splice(index, 1);
this.queryTypes.splice(index, 1);
if ( index > 0 ){
let prevSymbol = this.searchQuery[index-1];
if (prevSymbol === 'AND' || prevSymbol === 'OR') {
this.searchQuery.splice(index-1, 1);
this.queryTypes.splice(index-1, 1);
}
}
// Check if after the removal a AND or OR symbol remains as the first in query
if (this.queryTypes[0] === 'S'){
this.searchQuery.splice(0, 1);
this.queryTypes.splice(0, 1);
removeElementORFormulaInSearchQuery(item){
//console.log(" removeElementORFormulaInSearchQuery item: ",item, this.searchQuery.indexOf(item));
// Travese the array removing the item and the bool operator related
let itemIndex = this.searchQuery.indexOf(item);
if (itemIndex >= 0){
let i, elementsToRemove;
if (this.queryTypes[itemIndex+1] === 'S'){ // bool operator on the left
i = itemIndex; elementsToRemove = 2;
}else if (this.queryTypes[itemIndex-1] === 'S'){ // bool operator on the right
i = itemIndex-1; elementsToRemove = 2;
}else{ // case: (item)
i = itemIndex; elementsToRemove = 1;
}
this.searchQuery.splice(i, elementsToRemove);
this.queryTypes.splice(i, elementsToRemove);
}
this.updateSearchQuery();
// Travese the array removing the unnecessary parethesis (only tested for one level nested)
if ( searchQuery.indexOf('(') >= 0){ // Recursion
if (util.ELEMENTS.indexOf(element) >= 0){ // It's element (being removed)
this.elementTable.deselectElement(element);
if (this.queryTypes.indexOf('E') < 0){ // It's the last element
this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
for (let i = 0; i < searchQuery.length; i++) { // dangerous: modifing a array being traversed
if ( searchQuery[i] === '(' ){
if (searchQuery[i+1] === ')'){ // '()' case
this.searchQuery.splice(i, 2);
this.queryTypes.splice(i, 2);
}else if (searchQuery[i+2] === ')'){ // '(item)' case
this.searchQuery.splice(i, 3, searchQuery[i+1]);
this.queryTypes.splice(i, 3, queryTypes[i+1]);
}
}
}
}
}else{ // It's other than element (formula, material name ¿or prop???)
this.addElementButton.disabled = false;
this.formulaBox.disable(false);
this.materialNameBox.disable(false);
// Added ¿?
this.addFormulaButton.disabled = false;
this.updateSearchQuery();
if (util.ELEMENTS.indexOf(item) >= 0){ // It's an element (being removed)
this.elementTable.deselectElement(item);
if (this.queryTypes.indexOf('E') < 0){ // It's the last element
//this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
}
//console.log(" final searchQuery: ",this.searchQuery);
}else{ // It's other than element (formula, material name ¿or prop???)
this.addElementButton.disabled = false;
this.formulaBox.disable(false);
this.materialNameBox.disable(false);
// Added ¿?
this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
}
//console.log(" final searchQuery: ",this.searchQuery);
//}
return true;
}
......@@ -8190,7 +8212,7 @@
for (let i = 0; i < this.searchQuery.length; i++) {
let type = this.queryTypes[i];
if (type === 'S')
if (type === 'S' || type === 'P')
html+= `<span class="search-query-symbol" > ${this.searchQuery[i]} </span>`;
else
html+= getTagHtml(this.searchQuery[i], ( type === 'F' ? true : false));
......@@ -8303,6 +8325,48 @@
console.log('_reduceFormula RETURN: ', map, query);
return query;
}
_tokenizeFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
}
let query = [];
map.forEach( (value, key) => {
query.push( value === 1 ? key : key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_tokenizeFormula RETURN: ', map, query);
return query;
}
}
......@@ -8318,9 +8382,11 @@
<input type="checkbox" class="allow-other-elements" value="">
Allow other elements
<br>
<!--
<input type="checkbox" class="multiples-of-formula" value="">
Include multiples of formula
<br>
<br> -->
<input type="text" class="formula-text-field"
placeholder="Add formula to the search query above" >
......@@ -8358,10 +8424,10 @@
return this.element.querySelector('.allow-other-elements').checked;
}
/*
getMultiplesOfFormula(){
return this.element.querySelector('.multiples-of-formula').checked;
}
}*/
}
......
......@@ -92,6 +92,11 @@ class NewSearchMod {
</div>
<input type="checkbox" id="multiples-of-formula" value="">
Include multiples of formula
<br>
<div class="add-buttons" >
<div class="tab-buttons" style="width: 70%; display: inline-block">
......@@ -354,13 +359,12 @@ class NewSearchMod {
searchQuery.forEach( (item, i) => {
if (queryTypes[i] === 'F'){
// ** PENDING: search Not allowing other elements
let esMatchQuery;
// *reduced* search if (this.formulaBox.getMultiplesOfFormula()){
//esMatchQuery = this._getESSimpleMatch('formula_reduced', this._reduceFormula(item));
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item), true);
// *cell* search }else esMatchQuery = this._getESSimpleMatch('formula', item);
if ( this.element.querySelector('#multiples-of-formula').checked ){ // reduced search
esMatchQuery = this._getESOperatorMatch('formula_reduced', this._reduceFormula(item));
}else
esMatchQuery = this._getESOperatorMatch('formula', this._tokenizeFormula(item));
queryObj.push(esMatchQuery);
}else if (queryTypes[i] === 'E'){
......@@ -509,7 +513,7 @@ class NewSearchMod {
if ( this.searchQuery.length > 0 && isOpen)
this._addItemInSearchQuery(this.currentOperator, 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'S');
this._addItemInSearchQuery( (isOpen ? '(' : ')'), 'P');
this.updateSearchQuery();
//this._showSearchBox();
}
......@@ -534,46 +538,64 @@ class NewSearchMod {
}*/
removeElementORFormulaInSearchQuery(element){
//console.log(" removeElementORFormulaInSearchQuery element: ",element);
let index = this.searchQuery.indexOf(element);
if (index >= 0){
this.searchQuery.splice(index, 1);
this.queryTypes.splice(index, 1);
if ( index > 0 ){
let prevSymbol = this.searchQuery[index-1];
if (prevSymbol === 'AND' || prevSymbol === 'OR') {
this.searchQuery.splice(index-1, 1);
this.queryTypes.splice(index-1, 1);
}
}
// Check if after the removal a AND or OR symbol remains as the first in query
if (this.queryTypes[0] === 'S'){
this.searchQuery.splice(0, 1);
this.queryTypes.splice(0, 1);
removeElementORFormulaInSearchQuery(item){
//console.log(" removeElementORFormulaInSearchQuery item: ",item, this.searchQuery.indexOf(item));
// Travese the array removing the item and the bool operator related
let itemIndex = this.searchQuery.indexOf(item);
if (itemIndex >= 0){
let i, elementsToRemove;
if (this.queryTypes[itemIndex+1] === 'S'){ // bool operator on the left
i = itemIndex; elementsToRemove = 2;
}else if (this.queryTypes[itemIndex-1] === 'S'){ // bool operator on the right
i = itemIndex-1; elementsToRemove = 2;
}else{ // case: (item)
i = itemIndex; elementsToRemove = 1;
}
this.searchQuery.splice(i, elementsToRemove);
this.queryTypes.splice(i, elementsToRemove);
}
this.updateSearchQuery();
// Travese the array removing the unnecessary parethesis (only tested for one level nested)
if ( searchQuery.indexOf('(') >= 0){ // Recursion
if (util.ELEMENTS.indexOf(element) >= 0){ // It's element (being removed)
this.elementTable.deselectElement(element);
if (this.queryTypes.indexOf('E') < 0){ // It's the last element
this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
for (let i = 0; i < searchQuery.length; i++) { // dangerous: modifing a array being traversed
if ( searchQuery[i] === '(' ){
if (searchQuery[i+1] === ')'){ // '()' case
this.searchQuery.splice(i, 2);
this.queryTypes.splice(i, 2);
}else if (searchQuery[i+2] === ')'){ // '(item)' case
this.searchQuery.splice(i, 3, searchQuery[i+1]);
this.queryTypes.splice(i, 3, queryTypes[i+1]);
}
}
}
}
this.updateSearchQuery();
}else{ // It's other than element (formula, material name ¿or prop???)
this.addElementButton.disabled = false;
this.formulaBox.disable(false);
this.materialNameBox.disable(false);
// Added ¿?
this.addFormulaButton.disabled = false;
if (util.ELEMENTS.indexOf(item) >= 0){ // It's an element (being removed)
this.elementTable.deselectElement(item);
if (this.queryTypes.indexOf('E') < 0){ // It's the last element
//this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
}
//console.log(" final searchQuery: ",this.searchQuery);
}else{ // It's other than element (formula, material name ¿or prop???)
this.addElementButton.disabled = false;
this.formulaBox.disable(false);
this.materialNameBox.disable(false);
// Added ¿?
this.addFormulaButton.disabled = false;
this.addMatNameButton.disabled = false;
}
//console.log(" final searchQuery: ",this.searchQuery);
//}
return true;
}
......@@ -583,7 +605,7 @@ class NewSearchMod {
for (let i = 0; i < this.searchQuery.length; i++) {
let type = this.queryTypes[i];
if (type === 'S')
if (type === 'S' || type === 'P')
html+= `<span class="search-query-symbol" > ${this.searchQuery[i]} </span>`;
else
html+= getTagHtml(this.searchQuery[i], ( type === 'F' ? true : false));
......@@ -696,6 +718,48 @@ class NewSearchMod {
console.log('_reduceFormula RETURN: ', map, query);
return query;
}
_tokenizeFormula(formula){
let index = 0;
let map = new Map();
let key;
while ( index < formula.length ){
//console.log('_reduceFormula index', index);
let el2 = formula.substring(index, index+2);
let el1 = formula.substring(index, index+1);
if (util.ELEMENTS.indexOf(el2) >= 0){
map.set(el2, 1); // 1 default value
index += 2;
key = el2;
//console.log('eleemnt 2chars', key);
}else if (util.ELEMENTS.indexOf(el1) >= 0){
map.set(el1, 1); // 1 default value
index++;
key = el1;
//console.log('eleemnt 1chars', key);
}else{ // It's a number
let num = parseInt(el2);
if (num >= 10) index += 2; // 2 figures number
else index++;// 1 figure number
//console.log('number ', num, key);
map.set(key, num);
}
// console.log('FINAL LOOP', map, index);
}
let query = [];
map.forEach( (value, key) => {
query.push( value === 1 ? key : key+value);
//newFormula += key+(value === 1 ? '' : value);
});
console.log('_tokenizeFormula RETURN: ', map, query);
return query;
}
}
......@@ -711,9 +775,11 @@ class FormulaBox{
<input type="checkbox" class="allow-other-elements" value="">
Allow other elements
<br>
<!--
<input type="checkbox" class="multiples-of-formula" value="">
Include multiples of formula
<br>
<br> -->
<input type="text" class="formula-text-field"
placeholder="Add formula to the search query above" >
......@@ -751,10 +817,10 @@ class FormulaBox{
return this.element.querySelector('.allow-other-elements').checked;
}
/*
getMultiplesOfFormula(){
return this.element.querySelector('.multiples-of-formula').checked;
}
}*/
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment