Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter in a query for a whole dashboard #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/factories/baseWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
_this.requestData();
}

function actionNewWindow(action) {
function actionNavigate(action, newWindow = false) {
let url = action.targetProperty;
let idx = url.toUpperCase().indexOf('DASHBOARD=');
if (idx !== -1) {
Expand Down Expand Up @@ -385,14 +385,24 @@
}
}

window.open(url, '_blank');
if (!!$location.search().embed) {
url += '&embed=1';
}

if (newWindow) {
window.open(url, '_blank');
} else {
window.location = url;
}
}

function performAction(action) {
let a = action.action.toLowerCase();

if (a === 'newwindow') {
actionNewWindow(action);

if (a === 'navigate') {
actionNavigate(action);
} else if (a === 'newwindow') {
actionNavigate(action, true);
} else if (a === 'setColumnSpec') {
_this.customColSpec = action.targetProperty;
_this.requestData();
Expand Down Expand Up @@ -1107,15 +1117,16 @@
filters = parts.slice(1).join(':');
}
}
if (widgetName && _this.desc.name === widgetName && filters) {
if (widgetName && _this.desc.name !== widgetName) return mdx;
if (filters) {
let f = filters.split('~');
for (let i = 0; i < f.length; i++) {
let s = f[i];
let isExclude = s.indexOf('%NOT') !== -1;
if (s.indexOf('{') !== -1) {
if (/\{([^}]+)\}/.test(s)) {
// Many values
let path = s.substring(0, s.indexOf('{')).replace('%NOT ', '');
let values = s.match(/\{([^)]+)\}/)[1].split(',');
let values = s.match(/\{([^}]+)\}/)[1].split(',');
mdx += ' %FILTER %OR({';
mdx += values.map(v => path + v + (isExclude ? '.%NOT' : '')).join(',');
mdx += '})';
Expand Down
116 changes: 86 additions & 30 deletions src/services/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(function() {
'use strict';

function FiltersSvc($rootScope, Connector, Storage, Lang) {
function FiltersSvc($rootScope, Connector, Storage, Lang, $location) {
var _this = this;
this.items = [];
this.isFiltersOnToolbarExists = false;
Expand Down Expand Up @@ -52,7 +52,6 @@
if (parts && parts.length !== 0) {
var params = parts[0].substring(2, parts[0].length - 2);
flt.additionalParams = params.toLowerCase().trim().split(',');
console.log(params);

if (flt.additionalParams.indexOf('inverseorder') !== -1) flt.values = flt.values.reverse();
if (flt.additionalParams.indexOf('ignorenow') !== -1) flt.values = flt.values.filter(function(v) { return v.path.toLowerCase() !== '&[now]';});
Expand All @@ -64,6 +63,8 @@
}

loadFiltersFromSettings();

loadFilterFromQuery();
}

/**
Expand All @@ -77,37 +78,92 @@
for (var i = 0; i < widgets._filters.length; i++) {
var flt = widgets._filters[i];

var exists = _this.items.filter(function(el) { return el.targetProperty === flt.targetProperty; })[0];
if (exists) {
// Check for single value
exists.value = flt.value;
exists.isExclude = flt.isExclude;
exists.isInterval = flt.isInterval;

if (exists.isInterval) {
exists.fromIdx = flt.fromIdx;
exists.toIdx = flt.toIdx;
exists.valueDisplay = exists.values[exists.fromIdx].name + ':' + exists.values[exists.toIdx].name;
} else {
var values = flt.value.split('|');

// Multiple values was selected
exists.values.forEach(function (v) {
if (values.indexOf(v.path) !== -1) v.checked = true;
});

exists.valueDisplay = flt.value.split('|').map(el => {
let v = exists.values.find(e => e.path == el);
return (v.name || '').toString();
}).join(',');
}

found = true;
}
applyFilterValue(flt);
}
}
}

function applyFilterValue(flt) {
var exists = _this.items.filter(function(el) { return el.targetProperty === flt.targetProperty; })[0];
if (!exists) return;
// Check for single value
exists.value = flt.value;
exists.isExclude = flt.isExclude;
exists.isInterval = flt.isInterval;

if (exists.isInterval) {
if (!flt.fromIdx && !flt.toIdx) {
let ranges = flt.value.match(/(&\[[^\]]+\]):(&\[[^\]]+\])/);
const findIndex = (path) => { return exists.values.findIndex(val => val.path === path) }
flt.fromIdx = findIndex(ranges[1]);
flt.toIdx = findIndex(ranges[2]);
}
exists.fromIdx = flt.fromIdx;
exists.toIdx = flt.toIdx;
exists.valueDisplay = exists.values[exists.fromIdx].name + ':' + exists.values[exists.toIdx].name;
} else {
var values = flt.value.split('|');

// Multiple values was selected
exists.values.forEach(function (v) {
if (values.indexOf(v.path) !== -1) v.checked = true;
});

exists.valueDisplay = flt.value.split('|').map(el => {
let v = exists.values.find(e => e.path == el);
return v === undefined ? null : (v.name || '').toString();
}).filter(v => v !== null).join(',');
}

return exists;
}

/**
* Load settings from query
*/
function loadFilterFromQuery() {
let querySettings = $location.search().SETTINGS;
if (!querySettings) return;

let params = querySettings.split(';');
let filters = '';
for (let i in params) {
let parts = params[i].split(':');
if (parts[0].toLowerCase() === 'filter') {
filters = parts.slice(1).join(':');
}
}
if (filters) {
let f = filters.split('~');
for (let i in f) {
let s = f[i];
let targetProperty = s.match(/^(\[([^\]]+)\].)*/)[0].slice(0, -1);
let flt = {
targetProperty: targetProperty,
isExclude: false,
isInterval: false
};

let filterValue = s.substr(targetProperty.length + 1);
if (filterValue.startsWith('%NOT')) {
flt.isExclude = true;
filterValue = filterValue.substr(5);
}
const re = /\{([^}]+)\}/;
if (re.test(filterValue)) {
let values = filterValue.match(re)[1].split(',');
filterValue = values.join('|');
} else if (/&\[[^\]]+\]:&\[[^\]]+\]/.test(filterValue)) {
flt.isInterval = true;
}

flt.value = filterValue;

applyFilterValue(flt);
}
}
}

function getClickFilterTarget(widgetName) {
for (var i = 0; i < _this.items.length; i++) {
var flt = _this.items[i];
Expand Down Expand Up @@ -273,6 +329,6 @@
}

angular.module('app')
.service('Filters', ['$rootScope', 'Connector', 'Storage', 'Lang', FiltersSvc]);
.service('Filters', ['$rootScope', 'Connector', 'Storage', 'Lang', '$location', FiltersSvc]);

})();