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 option for open files #3673

Open
wants to merge 1 commit 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
89 changes: 83 additions & 6 deletions src/modules/openfiles/content/openfiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ if (typeof ko.openfiles == 'undefined')
var groupOptions = {};
var sortOptions = {};

/** Filters. Array of arrays. First dimension is an AND. Second is an OR. */
var filters = [];

/** File separator */
const system = require("sdk/system");
const sep = system.platform == "winnt" ? "\\" : "/";

/* Keep track of listeners so we can remove them if needed */
var listeners = {};

Expand Down Expand Up @@ -91,6 +98,9 @@ if (typeof ko.openfiles == 'undefined')
template.groupItem.removeAttribute('id')
tpl.parentNode.removeChild(tpl);

// Disables select handler
this._disableSelectHandler = false;

// Register built in sorting options
this.registerSortOption(
'byName',
Expand Down Expand Up @@ -449,7 +459,7 @@ if (typeof ko.openfiles == 'undefined')

/**** OpenFiles Events ******/
listbox.addEventListener('select', function(e) {
if (e.target == listbox && e.target.selectedItem)
if (e.target == listbox && e.target.selectedItem && !this._disableSelectHandler)
{
this.selectItem(e.target.selectedItem, true /*sendEditorTabClick*/ );
ko.commands.doCommandAsync('cmd_focusEditor')
Expand Down Expand Up @@ -658,6 +668,8 @@ if (typeof ko.openfiles == 'undefined')
*/
drawList: function openfiles_drawList(noDelay = false)
{
var hasRemovedItems = false;

// Prevent multiple calls in a short amount of time (10ms)
if ( ! noDelay) {
clearTimeout(timers.drawList || {});
Expand All @@ -671,14 +683,58 @@ if (typeof ko.openfiles == 'undefined')
// Iterate through open views and append them to the list
for (let uid of Object.keys(openViews))
{
// Get view
var editorView = openViews[uid];

// Check filters
var foundInFilter = true;
if (filters.length > 0)
{
var dirName = editorView.koDoc.file ? editorView.koDoc.file.dirName : '';
var file = dirName + sep + editorView.title;
file = file.toLowerCase();

// Check AND filters
for (var filterIdx = 0; filterIdx < filters.length; filterIdx++)
{
// Check OR filters
var orFilters = filters[filterIdx];
var foundInOr = false;
for (var orFiltersIdx = 0; orFiltersIdx < orFilters.length; orFiltersIdx++)
{
if (file.indexOf(orFilters[orFiltersIdx]) !== -1)
{
foundInOr = true;
break;
}
}
if (!foundInOr)
{
foundInFilter = false;
break;
}
}
}

// Skip items that are already in the list
if (listbox.querySelector('richlistitem[id="'+uid+'"]'))
var existingItem = listbox.querySelector('richlistitem[id="'+uid+'"]');
if (existingItem !== null)
{
// Remove if shouldn't be in list
if (!foundInFilter)
{
hasRemovedItems = true;
existingItem.parentNode.removeChild(existingItem);
}

continue;
}


// Skip if not found in filters
if (!foundInFilter)
continue;

// Append the item
var editorView = openViews[uid];
var listItem = listbox.appendChild(
this.createListItem(editorView));

Expand All @@ -691,7 +747,11 @@ if (typeof ko.openfiles == 'undefined')
this.selectItem(editorView);
}
}


// Remove empty groups caused by removed items
if (hasRemovedItems)
this.removeEmptyGroups();

// Render the groups and splits
this.drawGroups();
this.updateLabelCounter();
Expand Down Expand Up @@ -1906,8 +1966,25 @@ if (typeof ko.openfiles == 'undefined')
{
var groupOption = ko.prefs.getString(PREF_GROUPING_TYPE, 'byLanguage');
return groupOptions[groupOption];
},

/** Update filters */
updateFilters: function openfiles_updateFilters()
{
filters = [];
var textSearch = document.getElementById("filter-search").value.toLowerCase().trim();
if (textSearch.length > 0)
{
for (var word of textSearch.split(/\s+/))
filters.push(word.split("|"));
}

// Redraw list
var orig_disableSelectHandler = this._disableSelectHandler;
this._disableSelectHandler = true;
this.drawList(true);
this._disableSelectHandler = orig_disableSelectHandler;
}

};

ko.openfiles = new ko.openfiles();
Expand Down
7 changes: 7 additions & 0 deletions src/modules/openfiles/content/panel.xul
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@
tooltiptext="&closeFiles.tooltip;"/>

</richlistitem>
<box class="widgetToolbarWrap centered">
<textbox id="filter-search" flex="1"
emptytext="&openFiles.filterSearch;" type="search"
oninput="ko.openfiles.updateFilters();"
oncommand="ko.openfiles.updateFilters();" />
<separator flex="1"/>
</box>
</vbox>
</vbox>

Expand Down
1 change: 1 addition & 0 deletions src/modules/openfiles/locale/en-US/openfiles.dtd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!ENTITY openFiles.label "Open Files">
<!ENTITY openFiles.filterSearch "Filter Files">
<!ENTITY viewEditorTabs.label "Show Editor Tabs">
<!ENTITY viewEditorTabs.tooltip "Toggle Editor Tabs">
<!ENTITY closeFile.tooltip "Close File">
Expand Down