repoindex/web/js/entrymanagement.js

229 lines
8.8 KiB
JavaScript

var repoindex = (function(repoindex) {
repoindex.removeFilterButtonHtml = '<button type="button" class="close" onclick="repoindex.pageManager.applySearchTerm(null, false, true, true);" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
repoindex.Entry = function(name, info, enabled) {
// basic initialization
this.name = name;
this.info = info;
this.enabled = enabled;
// callback for changed status
this.statusChanged = function() {};
// row element
this.rowElement = document.createElement("tr");
this.rowElement.entry = this;
this.rowElement.addCell = function(text) {
var cellElement = document.createElement("td");
if(text) {
cellElement.appendChild(document.createTextNode(text));
}
this.appendChild(cellElement);
};
this.initTableRow = function() {};
this.updateTableRow = function() {
this.rowElement.wipeChildren();
this.initTableRow();
};
this.add = function(parent) {
parent.appendChild(this.rowElement);
};
this.remove = function() {
this.rowElement.parentNode.removeChild(this.rowElement);
};
this.hide = function() {
this.rowElement.style.display = "none";
};
this.show = function() {
this.rowElement.style.display = "table-row";
};
this.updateEnabled = function() {};
this.contained = function(names, searchDescription) {
for(var i = 0, len = names.length; i < len; ++i) {
if(this.name.contains(names[i])) {
return true;
} else if(searchDescription && this.info.desc && this.info.desc.contains(names[i])) {
// this doesn't work yet because only descriptions of entries which have already
// been shown are available
return true;
}
}
return false;
};
};
repoindex.EntryManager = function(EntryType, entryContainer, pagination) {
// basic initialization
this.EntryType = EntryType;
this.entryContainer = entryContainer;
this.entries = [];
this.pageName = undefined;
this.updateRequired = false;
// init pagination
if((this.pagination = pagination)) {
this.pagination.entryManager = this;
}
// stuff for the filter
this.filteredEntries = [];
this.filterName = undefined;
this.filterNames = undefined;
this.filterNameExact = false;
this.filterNameInDesc = false;
this.filterRepos = undefined;
this.filterDescr = undefined;
this.customSelection = undefined;
this.customSelectionName = undefined;
this.customSelectionNamePlural = undefined;
this.customSelectionInfoText = undefined;
// define some phrases for info text
this.entryName = "entry";
this.entryNamePlural = "entries";
this.containerName = undefined;
this.containerNamePlural = undefined;
this.getContainerQuantity = function() { return 0; };
// define default filter predicate
this.filterPred = function(entry) {
return (!this.filterNames || this.filterNames.length === 0 || (this.filterNameExact ? this.filterNames.contains(entry.name) : entry.contained(this.filterNames, this.filterNameInDesc)))
&& (!this.filterRepos || this.filterRepos.contains(entry.info.repo));
};
// provide functions to apply filter and upgrade UI
this.applyFilter = function() {
if((this.filterName || this.filterRepos) && this.filterPred) {
// split filterName into filterNames
this.filterNames = this.filterName ? this.filterName.split(" ") : undefined;
this.filteredEntries = this.relevantEntries().filter(this.filterPred, this);
} else {
this.filteredEntries = this.relevantEntries();
}
// upgrade pagination (the pageSelected method defined above will be invoked here automatically)
this.pagination.entryCount = this.filteredEntries.length;
this.pagination.update();
};
this.removeFilter = function() {
this.filterName = undefined;
this.filterDescr = undefined;
this.customSelection = undefined;
};
this.refreshInfo = function() {};
this.invalidate = function() {
this.updateRequired = true;
this.update();
};
this.update = function() {
if(this.updateRequired && (!this.pageName || this.pageName === repoindex.pageManager.currentPage)) {
this.applyFilter();
this.infoBox.innerHTML = this.infoText();
this.updateRequired = false;
}
};
this.removeEntries = function() {
this.entries = [];
this.filteredEntries = [];
this.entryContainer.wipeChildren();
};
this.updateEnabledForAll = function(enabled) {
for(var i = 0; i < this.entries.length; ++i) {
this.entries[i].updateEnabled(enabled);
}
};
this.infoText = function() {
var containerText = "";
if(this.containerName && this.containerNamePlural && !this.customSelection) {
var containerQuantity = this.getContainerQuantity();
containerText = containerQuantity === 1 ? " in one " + this.containerName : " in " + containerQuantity + " " + this.containerNamePlural;
}
if(this.customSelection && this.customSelectionInfoText) {
return this.customSelectionInfoText;
} else {
var entryText = this.customSelection ? (this.filteredEntries.length === 1 ? this.customSelectionName : this.customSelectionNamePlural) : (this.filteredEntries.length === 1 ? this.entryName : this.entryNamePlural);
if(this.filterDescr) {
if(this.filterName) {
return "<b>" + this.filteredEntries.length + "</b> " + entryText
+ containerText + " "
+ repoindex.escapeHtml(this.filterDescr) + " <i>"+ repoindex.escapeHtml(this.filterName) + "</i>."
+ repoindex.removeFilterButtonHtml;
} else {
return "<b>" + this.filteredEntries.length + "</b> " + entryText
+ containerText + " "
+ repoindex.escapeHtml(this.filterDescr) + "."
+ repoindex.removeFilterButtonHtml;
}
} else {
return "Showing <b>"
+ this.filteredEntries.length + "</b> "
+ entryText
+ containerText + ".";
}
}
};
this.relevantEntries = function() {
return this.customSelection ? this.customSelection : this.entries;
};
this.entryByIndex = function(entryIndex) {
if(entryIndex >= 0) {
if(this.customSelection) {
if(entryIndex < this.customSelection.length) {
return this.customSelection[entryIndex];
}
} else {
if(entryIndex < this.entries.length) {
return this.entries[entryIndex];
}
}
}
};
this.entryByName = function(entryName) {
if(this.customSelection) {
for(var i = 0; i < this.customSelection.length; ++i) {
var entry = this.customSelection[i];
if(entry.name === entryName) {
return entry;
}
}
} else {
for(var i = 0; i < this.entries.length; ++i) {
var entry = this.entries[i];
if(entry.name === entryName) {
return entry;
}
}
}
};
// function called by the ALPM client when requested data is available
this.useRequestedData = function() {
this.invalidate();
};
};
// useful helper methods in the context of entry management
repoindex.entryManagerGetRepoQuantity = function() {
return this.filterRepos ? this.filterRepos.length : repoindex.pageManager.repoManager.entries.length;
};
return repoindex;
})(repoindex || {});