Skip to content

Commit

Permalink
Fix when the filter has quotes (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
adri9valle authored and Jesús Ángel committed Mar 24, 2019
1 parent 7c4022c commit 8af0457
Showing 1 changed file with 59 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
define(['../module', 'jquery'], function(app, $) {
define(['../module', 'jquery'], function (app, $) {
'use strict'

class Discover {
/**
* Class Discover
*/
constructor($scope, $state, $stateParams, $document, $currentDataService) {
constructor($scope, $state, $stateParams, $document, $currentDataService, $notificationService) {
this.scope = $scope
this.state = $state
this.stateParams = $stateParams
this.iframe = $($document[0]).find('#searchAndReporting')
this.scope.loadingRing = true
this.currentDataService = $currentDataService
this.notification = $notificationService
}

/**
Expand All @@ -31,7 +32,7 @@ define(['../module', 'jquery'], function(app, $) {
}
})
} catch (error) {
this.toast('Cannot load discover.')
this.notification.showErrorToast('Cannot load discover.')
}
}

Expand All @@ -56,37 +57,64 @@ define(['../module', 'jquery'], function(app, $) {
}

backToDashboard() {
//Get the filters
const filters = this.fetchWrittenFilters()
//Add the filters
filters.map(fil => this.currentDataService.addFilter(fil))
//Back to the dashboard
this.state.go(this.stateParams.previousState)
try {
//Get the filters
const filters = this.fetchWrittenFilters()
//Add the filters
filters.map(fil => {
this.currentDataService.addFilter(fil)
})
//Back to the dashboard
this.state.go(this.stateParams.previousState)
} catch (error) {
this.notification.showErrorToast(error)
}
}

fetchWrittenFilters() {
let filtersFormatted = []
//Delete the last div of the input, this div contains a hidden string that is not needed
const divX = this.iframe
.contents()
.find('.search-field-wrapper pre div:last')
divX.remove()
//Get the filters
let filtersStr = this.iframe
.contents()
.find('.search-field-wrapper')
.text()
filtersStr = filtersStr
.split('|', 1)
.toString()
.trim()
const filtersArr = filtersStr.split(' ')
//Format the filters
filtersArr.map(fil => {
const f = fil.split('=')
filtersFormatted.push(`{"${f[0]}":"${f[1]}"}`)
})
return filtersFormatted
try {
let filtersFormatted = []
//Delete the last div of the input, this div contains a hidden string that is not needed
const divX = this.iframe
.contents()
.find('.search-field-wrapper pre div:last')
divX.remove()
//Get the filters
let filtersStr = this.iframe
.contents()
.find('.search-field-wrapper')
.text()
filtersStr = filtersStr
.split('|', 1)
.toString()
.trim()
const filtersArr = filtersStr.split(' ')
//Format the filters
filtersArr.map(fil => {
const f = fil.split('=')
const key = this.cleanQuotes(f[0])
const value = this.cleanQuotes(f[1])
filtersFormatted.push(`{"${key}":"${value}"}`)
})
return filtersFormatted
} catch (error) {
return []
}
}

cleanQuotes(str) {
try {
const firstChar = str.substring(0, 1)
const lastChar = str.substring(str.length - 1)
if (firstChar === '"' || firstChar === "'" && lastChar === '"' || lastChar === "'") {
const cleanStr = str.substring(1, str.length - 1)
return cleanStr
} else {
return str
}
} catch (error) {
return str
}
}
}
app.controller('discoverCtrl', Discover)
Expand Down

0 comments on commit 8af0457

Please sign in to comment.