- md5
- 635502a1a3918ac7f6885eeaa7ae0530
- sha1
- 9e750fad9130a7148a918547c066d167193488db
- sha256
- 00888b5616ff1e9db16676a36f3a2e6458de4e0128502b75b8c1cfd17bef5d5c
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
(function () {
"use strict";
// The actual plugin constructor
var Searchtools = function(element, options) {
var defaults = {
// Form options
formSelector : '.js-stools-form',
// Search
searchFieldSelector : '.js-stools-field-search',
clearBtnSelector : '.js-stools-btn-clear',
// Global container
mainContainerSelector : '.js-stools',
// Filter fields
searchBtnSelector : '.js-stools-btn-search',
filterBtnSelector : '.js-stools-btn-filter',
filterContainerSelector : '.js-stools-container-filters',
filtersHidden : true,
// List fields
listBtnSelector : '.js-stools-btn-list',
listContainerSelector : '.js-stools-container-list',
listHidden : true,
// Ordering specific
orderColumnSelector : '.js-stools-column-order',
orderBtnSelector : '.js-stools-btn-order',
orderFieldSelector : '.js-stools-field-order',
orderFieldName : 'list[fullordering]',
limitFieldSelector : '.js-stools-field-limit',
defaultLimit : 20,
activeOrder : null,
activeDirection : 'ASC',
// Extra
clearListOptions : false
};
this.element = element;
this.options = Joomla.extend(defaults, options);
// Initialise selectors
this.theForm = document.querySelector(this.options.formSelector);
// Filters
this.filterButton = document.querySelector(this.options.formSelector + ' ' + this.options.filterBtnSelector);
this.filterContainer = document.querySelector(this.options.formSelector + ' ' + this.options.filterContainerSelector) ? document.querySelector(this.options.formSelector + ' ' + this.options.filterContainerSelector) : '';
this.filtersHidden = this.options.filtersHidden;
// List fields
this.listButton = document.querySelector(this.options.listBtnSelector);
this.listContainer = document.querySelector(this.options.formSelector + ' ' + this.options.listContainerSelector);
this.listHidden = this.options.listHidden;
// Main container
this.mainContainer = document.querySelector(this.options.mainContainerSelector);
// Search
this.searchButton = document.querySelector(this.options.formSelector + ' ' + this.options.searchBtnSelector);
this.searchField = document.querySelector(this.options.formSelector + ' ' + this.options.searchFieldSelector);
this.searchString = null;
this.clearButton = document.querySelector(this.options.clearBtnSelector);
// Ordering
this.orderCols = Array.prototype.slice.call(document.querySelectorAll(this.options.formSelector + ' ' + this.options.orderColumnSelector));
this.orderField = document.querySelector(this.options.formSelector + ' ' + this.options.orderFieldSelector);
// Limit
this.limitField = document.querySelector(this.options.formSelector + ' ' + this.options.limitFieldSelector);
// Init trackers
this.activeColumn = null;
this.activeDirection = this.options.activeDirection;
this.activeOrder = this.options.activeOrder;
this.activeLimit = null;
// Extra options
this.clearListOptions = this.options.clearListOptions;
this.init();
};
Searchtools.prototype = {
init: function () {
var self = this;
// IE < 9 - Avoid to submit placeholder value
if(!document.addEventListener ) {
if (this.searchField.value === this.searchField.getAttribute('placeholder')) {
this.searchField.value = '';
}
}
// Get values
this.searchString = this.searchField.value;
if (this.filterContainer && this.filterContainer.classList.contains('js-stools-container-filters-visible')) {
this.showFilters();
this.showList();
} else {
this.hideFilters();
this.hideList();
}
if (self.filterButton) {
self.filterButton.addEventListener('click', function(e) {
self.toggleFilters();
e.stopPropagation();
e.preventDefault();
});
}
if (self.listButton) {
self.listButton.addEventListener('click', function(e) {
self.toggleList();
e.stopPropagation();
e.preventDefault();
});
}
// Do we need to add to mark filter as enabled?
if (self.getFilterFields()) {
self.getFilterFields().forEach(function(i) {
self.checkFilter(i);
i.addEventListener('change', function () {
self.checkFilter(i);
});
});
}
if (self.clearButton) {
self.clearButton.addEventListener('click', function () {
self.clear();
});
}
// Check/create ordering field
this.createOrderField();
self.orderCols.forEach(function(item) {
item.addEventListener('click', function () {
// Order to set
var newOrderCol = this.getAttribute('data-order');
var newDirection = this.getAttribute('data-direction');
var newOrdering = newOrderCol + ' ' + newDirection;
// The data-order attrib is required
if (newOrderCol.length) {
self.activeColumn = newOrderCol;
if (newOrdering !== self.activeOrder) {
self.activeDirection = newDirection;
self.activeOrder = newOrdering;
// Update the order field
self.updateFieldValue(self.orderField, newOrdering);
}
else {
self.toggleDirection();
}
self.theForm.submit();
}
});
});
self.checkActiveStatus(self);
document.body.addEventListener('click', function(e) {
if (document.body.classList.contains('filters-shown')) {
self.hideFilters();
}
});
self.filterContainer.addEventListener('click',function(e) {
e.stopPropagation();
}, true);
},
checkFilter: function (element) {
var self = this;
var option = element.querySelector('option:checked');
if (option) {
if (option.value !== '') {
self.activeFilter(element, self);
} else {
self.deactiveFilter(element, self);
}
}
},
clear: function () {
var self = this;
if (self.getFilterFields()) {
self.getFilterFields().forEach(function(i) {
i.value = '';
self.checkFilter(i);
if (window.jQuery && jQuery.chosen) {
jQuery(i).trigger('liszt:updated');
}
});
}
if (self.clearListOptions) {
self.getListFields().forEach(function(i) {
i.value = '';
self.checkFilter(i);
if (window.jQuery && jQuery.chosen) {
jQuery(i).trigger('liszt:updated');
}
});
// Special case to limit box to the default config limit
document.querySelector('#list_limit').value = self.options.defaultLimit;
if (window.jQuery && jQuery.chosen) {
jQuery('#list_limit').trigger('liszt:updated');
}
}
self.theForm.submit();
},
checkActiveStatus: function(cont) {
var el = cont.mainContainer;
var els = [].slice.call(el.querySelectorAll('.js-stools-field-filter select'));
els.forEach(function(item) {
if (item.classList.contains('active')) {
cont.filterButton.classList.remove('btn-secondary');
cont.filterButton.classList.add('btn-primary');
return '';
}
});
},
activeFilter: function (element, cont) {
element.classList.add('active');
var chosenId = '#' + element.getAttribute('id');
var tmpEl = element.querySelector(chosenId);
if (tmpEl) {
tmpEl.classList.add('active');
}
},
deactiveFilter: function (element, cont) {
element.classList.remove('active');
var chosenId = '#' + element.getAttribute('id');
var tmpEl = element.querySelector(chosenId);
if (tmpEl) {
tmpEl.classList.remove('active');
}
},
getFilterFields: function () {
if (this.filterContainer) {
return Array.prototype.slice.call(this.filterContainer.querySelectorAll('select,input'));
}
},
getListFields: function () {
return Array.prototype.slice.call(this.listContainer.querySelectorAll('select'));
},
// Common container functions
hideContainer: function (container) {
if (container) {
container.classList.remove('js-filters-show');
document.body.classList.remove('filters-shown');
}
},
showContainer: function (container) {
container.classList.add('js-filters-show');
document.body.classList.add('filters-shown');
},
toggleContainer: function (container) {
if (container.classList.contains('js-filters-show')) {
this.hideContainer(container);
} else {
this.showContainer(container);
}
},
// List container management
hideList: function () {
this.hideContainer(this.filterContainer);
},
showList: function () {
this.showContainer(this.filterContainer);
},
toggleList: function () {
this.toggleContainer(this.filterContainer);
},
// Filters container management
hideFilters: function () {
this.hideContainer(this.filterContainer);
},
showFilters: function () {
this.showContainer(this.filterContainer);
},
toggleFilters: function () {
this.toggleContainer(this.filterContainer);
},
toggleDirection: function () {
var self = this;
var newDirection = 'ASC';
if (self.activeDirection.toUpperCase() == 'ASC')
{
newDirection = 'DESC';
}
self.activeDirection = newDirection;
self.activeOrder = self.activeColumn + ' ' + newDirection;
self.updateFieldValue(self.orderField, self.activeOrder);
},
createOrderField: function () {
var self = this;
if (!this.orderField.length)
{
this.orderField = createElement('<input>');
this.orderField.setAttribute('type', 'hidden');
this.orderField.setAttribute('id', 'js-stools-field-order');
this.orderField.setAttribute('class', 'js-stools-field-order');
this.orderField.setAttribute('name', self.options.orderFieldName);
this.orderField.setAttribute('value', self.activeOrder + ' ' + this.activeDirection);
this.theForm.innerHTML+= this.orderField;
}
// Add missing columns to the order select
if (this.orderField.tagName.toLowerCase() == 'select')
{
var allOptions = this.orderField.options;
for (var i = 0, l = allOptions.length; l>i; i++) {
var value = allOptions[i].getAttribute('data-order');
var name = allOptions[i].getAttribute('data-name');
var direction = allOptions[i].getAttribute('data-direction');
if (value && value.length)
{
value = value + ' ' + direction;
var $option = self.findOption(self.orderField, value);
if (!$option.length)
{
var $option = document.createElement('option');
$option.text = name;
$option.value = value;
// If it is the active option select it
if (allOptions[i].classList.contains('active'))
{
$option.setAttribute('selected', 'selected');
}
// Append the option an repopulate the chosen field
self.orderFieldName.innerHTML += $option;
}
}
}
if (window.jQuery && jQuery.chosen) {
jQuery(this.orderField).trigger('liszt:updated');
}
}
this.activeOrder = this.orderField.value;
},
updateFieldValue: function (field, newValue) {
var self = this,
type = field.getAttribute('type');
if (type === 'hidden' || type === 'text')
{
field.setAttribute('value', newValue);
}
else if (field.tagName.toLowerCase() === 'select')
{
// Select the option result
var allOptions = field.options;
for (var i = 0, l = allOptions.length; l>i; i++) {
if (allOptions[i].value == newValue) {
var desiredOption = allOptions[i];
}
}
if (desiredOption && desiredOption.length)
{
desiredOption.setAttribute('selected', 'selected');
}
// If the option does not exist create it on the fly
else
{
var option = document.createElement('option');
option.text = name;
option.value = newValue;
option.setAttribute('selected','selected');
// Append the option an repopulate the chosen field
field.appendChild(option);
}
field.value = newValue;
// Trigger the chosen update
if (window.jQuery && jQuery.chosen) {
field.trigger('liszt:updated');
}
}
},
findOption: function(select, value) {
for (var i = 0, l = select.length; l>i; i++) {
if (select[i].value == value) {
return select[i];
}
}
}
};
// Execute on DOM Loaded Event
document.addEventListener('DOMContentLoaded', function(){
if (Joomla.getOptions('searchtools')) {
var options = Joomla.getOptions('searchtools'),
element = document.querySelector(options.selector);
new Searchtools(element, options);
}
});
})();
No other release has this exact content. See every version of this file.