Skip to content

Commit

Permalink
add filter negation
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-deboer committed Nov 3, 2017
1 parent ab26aba commit 979a232
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
3 changes: 2 additions & 1 deletion pkg/ui/src/components/FilterBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import PropTypes from 'prop-types'
import {blueA400, blueA100 } from 'material-ui/styles/colors'
import ChipInput from 'material-ui-chip-input'
import { normalizeFilter } from '../utils/filter-utils'
import Chip from 'material-ui/Chip'
import './FilterBox.css'

Expand Down Expand Up @@ -73,7 +74,7 @@ export default class FilterBox extends React.PureComponent {
}

if (filterNames.length < this.props.maxFilters) {
filterNames.push(filter)
filterNames.push(normalizeFilter(filter))

this.setState({
filterNames: filterNames,
Expand Down
44 changes: 28 additions & 16 deletions pkg/ui/src/utils/filter-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ export function applyFilters(globalFilters, dynamicFilters, resource) {
}
}

function isNamespaced(resource) {
return resource.metadata.namespace && resource.metadata.namespace !== '~'
}

/**
* Applies the provided filters to the resource, modifying
* the 'isFiltered' attribute of the resource accordingly.
Expand All @@ -39,6 +35,12 @@ function applyFiltersToResource(filters, resource) {
for (var field in filters) {
var values = filters[field]
let match = false
let negated = false
if (field.startsWith('!')) {
negated = true
field = field.substr(1)
}

if (field === '*') {
let matched = false
for (let m in resource.metadata) {
Expand Down Expand Up @@ -119,32 +121,42 @@ function applyFiltersToResource(filters, resource) {

// }
} else if (field === 'namespace') {
if (isNamespaced(resource) && !(resource.metadata.namespace in values)) {
return resource.isFiltered = true
} else {
match = true
}
match = true
} else if ( (resource.metadata[field] in values)
|| (resource[field] in values)
|| ('labels' in resource.metadata && resource.metadata.labels[field] in values)) {
match = true
} else if ( `!${resource.metadata[field]}` ) {
return resource.isFiltered = true
}

if (match) {
resource.isFiltered = false
} else {
// at least one filter field did not match; exit
return resource.isFiltered = true
if (negated) {
match = !match
}
resource.isFiltered = !match
if (resource.isFiltered) {
return true
}
}
}

export function normalizeFilter(parts) {
if (typeof parts === 'string') {
parts = splitFilter(parts)
}

if (parts[0].startsWith('!')) {
return `${parts[0].substr(1)}:!${parts[1]}`
} else {
return parts.join(':')
}
}

export function splitFilter(filter) {
var parts=filter.split(":")
if (parts.length === 1) {
parts = ["*", parts[0]]
} else if (parts[1].startsWith('!')) {
parts[0] = '!' + parts[0]
parts[1] = parts[1].substr(1)
}
return parts
}
Expand Down

0 comments on commit 979a232

Please sign in to comment.