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

[tree-filter] Make search case insensitve and accept RegExp #948

Merged
merged 6 commits into from
May 15, 2017
Merged
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
106 changes: 88 additions & 18 deletions src/jupyter_contrib_nbextensions/nbextensions/tree-filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ define([
'require',
'jqueryui',
'base/js/namespace',
'base/js/events',
'base/js/utils',
'services/config'
], function (
require,
$,
IPython,
events,
Jupyter,
utils,
configmod
) {
Expand All @@ -24,39 +22,111 @@ define([

config.loaded.then(function() {
if (config.data.hasOwnProperty('filter_keyword')) {
var filter_keyword = config.data.filter_keyword;
if (filter_keyword) {
console.log("filter_keyword:", filter_keyword);
$('#filterkeyword').val(filter_keyword);
filterRows(filter_keyword);
}
var filter_keyword = config.data.filter_keyword;
if (filter_keyword) {
console.log("filter_keyword:", filter_keyword);
$('#filterkeyword').val(filter_keyword);
filterRows(filter_keyword);
}
}
});

function filterRows (filterText) {
function filterRows (filterText, caseSensitive, useRegex) {
var input = $('#filterkeyword');
var btnRegex = $('#filterisreg');

filterText = filterText !== undefined ? filterText : input.val();
useRegex = useRegex !== undefined ? useRegex : btnRegex.attr('aria-pressed') === 'true';
caseSensitive = caseSensitive !== undefined ? caseSensitive : $('#filtercase').attr('aria-pressed') === 'true';

if (!useRegex) {
// escape any regex special chars
filterText = filterText.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}
var matchExpr;
try {
matchExpr = new RegExp(filterText, caseSensitive ? '' : 'i');
}
catch (err) {
// do nothing, error is handled based on undefined matchExpr
}

var invalidRegex = matchExpr === undefined;
btnRegex.toggleClass('btn-danger', invalidRegex);
btnRegex.toggleClass('btn-default', !invalidRegex);
btnRegex.closest('.form-group').toggleClass('has-error has-feedback', invalidRegex);

var rows = Array.prototype.concat.apply([], document.querySelectorAll('.list_item.row'));
rows.forEach(function (row) {
if (!filterText || row.querySelector('.item_name').innerHTML.indexOf(filterText) !== -1) {
if (!filterText || row.querySelector('.item_name').textContent.search(matchExpr) !== -1) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
}

function filterRowsDefaultParams () {
return filterRows();
}

function load_ipython_extension () {
var html = '<label id="Keyword-Filter" for="filterkeyword">Filter: </label><input type="text" id="filterkeyword">';
$('#notebook_list_header').append(html);
$('#Keyword-Filter').attr('title','Keyword for filtering tree');

$('#filterkeyword').keyup( function (e) {
filterRows($('#filterkeyword').val());
});
var form = $('<form/>')
.css('padding', '0 7px 4px')
.appendTo('#notebook_list_header');

var frm_grp = $('<div/>')
.addClass('form-group')
.css('margin-bottom', 0)
.appendTo(form);

var grp = $('<div/>')
.addClass('input-group')
.appendTo(frm_grp);

$('<input/>')
.attr('type', 'text')
.addClass('form-control input-sm')
.attr('title', 'Keyword for filtering tree')
.attr('id', 'filterkeyword')
.attr('placeholder', 'Filter')
.css('font-weight', 'bold')
.appendTo(grp);

var btns = $('<div/>')
.addClass('input-group-btn')
.appendTo(grp);

$('<button/>')
.attr('type', 'button')
.attr('id', 'filterisreg')
.addClass('btn btn-default btn-sm')
.attr('data-toggle', 'button')
.css('font-weight', 'bold')
.attr('title', 'Use regex (JavaScript regex syntax)')
.text('.*')
.on('click', function (evt) { setTimeout(filterRowsDefaultParams); })
.appendTo(btns);

$('<button/>')
.attr('type', 'button')
.attr('id', 'filtercase')
.addClass('btn btn-default btn-sm')
.attr('data-toggle', 'button')
.attr('tabindex', '0')
.attr('title', 'Match case')
.css('font-weight', 'bold')
.text('Aa')
.on('click', function (evt) { setTimeout(filterRowsDefaultParams); })
.appendTo(btns);

$('#filterkeyword').on('keyup', filterRowsDefaultParams);
config.load();
}

return {
load_ipython_extension : load_ipython_extension
};

});
});