Skip to content

Commit

Permalink
searchfield.search option
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Heusschen committed Aug 15, 2022
1 parent 0b186a9 commit 2f2049d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 65 deletions.
14 changes: 2 additions & 12 deletions demo/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>This is a demo.</h1>
<li>
<span>The team</span>
<ul>
<li class="Selected">
<li>
<a href="#/">Management</a>
</li>
<li>
Expand All @@ -60,17 +60,7 @@ <h1>This is a demo.</h1>
<!-- mmenu scripts -->
<script src="../dist/mmenu.js"></script>
<script>
new Mmenu(document.querySelector("#menu"), {
searchfield: {
add: true,
},
// navbars: [
// {
// content: ["searchfield"],
// },
// ],
slidingSubmenus: false,
});
new Mmenu(document.querySelector("#menu"));

document.addEventListener("click", function (evnt) {
var anchor = evnt.target.closest('a[href="#/"]');
Expand Down
11 changes: 9 additions & 2 deletions dist/addons/searchfield/mmenu.searchfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ const initSearch = function (form) {
const search = () => {
/** The searchquery */
const query = input.value.toLowerCase().trim();
if (query.length) {
form.classList.add('mm-searchfield--searching');
}
else {
form.classList.remove('mm-searchfield--searching');
}
if (!options.search) {
return;
}
/** All listitems */
const listitems = [];
searchIn.forEach(panel => {
Expand All @@ -261,7 +270,6 @@ const initSearch = function (form) {
if (query.length) {
// Trigger event.
this.trigger('search:before');
form.classList.add('mm-searchfield--searching');
resultspanel.classList.add('mm-panel--searching');
// Add data attribute to the matching listitems.
listitems.forEach((listitem) => {
Expand All @@ -288,7 +296,6 @@ const initSearch = function (form) {
else {
// Trigger event.
this.trigger('clear:before');
form.classList.remove('mm-searchfield--searching');
resultspanel.classList.remove('mm-panel--searching', 'mm-panel--noresults');
// Resultspanel.
if (resultspanel.matches('.mm-panel--search')) {
Expand Down
1 change: 1 addition & 0 deletions dist/addons/searchfield/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const options = {
addTo: 'panels',
noResults: 'No results found.',
placeholder: 'Search',
search: true,
searchIn: 'panels',
splash: '',
title: 'Search',
Expand Down
25 changes: 4 additions & 21 deletions dist/core/offcanvas/mmenu.offcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ export default function () {
// Setup the menu.
this.node.menu.classList.add('mm-menu--offcanvas');
this.node.menu.setAttribute('inert', 'true');
this.position(options.position);
if (possiblePositions.includes(options.position)) {
this.node.wrpr.classList.add(`mm-wrapper--position-${options.position}`);
this.node.menu.classList.add(`mm-menu--position-${options.position}`);
}
// Open if url hash equals menu id (usefull when user clicks the hamburger icon before the menu is created)
let hash = window.location.hash;
if (hash) {
Expand Down Expand Up @@ -100,26 +103,6 @@ export default function () {
}
});
}
/**
* Get or set the postion for the menu.
*
* @param {string} [position] The position for the menu.
*/
Mmenu.prototype.position = function (position = null) {
const options = this.opts.offCanvas;
if (position) {
if (possiblePositions.includes(position)) {
this.node.wrpr.classList.remove(`mm-wrapper--position-${options.position}`);
this.node.wrpr.classList.add(`mm-wrapper--position-${position}`);
this.node.menu.classList.remove(`mm-menu--position-${options.position}`);
this.node.menu.classList.add(`mm-menu--position-${position}`);
options.position = position;
}
}
else {
return options.position;
}
};
/**
* Open the menu.
*/
Expand Down
4 changes: 2 additions & 2 deletions dist/mmenu.js

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions src/addons/searchfield/mmenu.searchfield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,16 @@ const initSearch = function (
/** The searchquery */
const query = input.value.toLowerCase().trim();

if (query.length) {
form.classList.add('mm-searchfield--searching');
} else {
form.classList.remove('mm-searchfield--searching');
}

if (!options.search) {
return;
}

/** All listitems */
const listitems = [];

Expand All @@ -355,8 +365,6 @@ const initSearch = function (
if (query.length) {
// Trigger event.
this.trigger('search:before');

form.classList.add('mm-searchfield--searching');
resultspanel.classList.add('mm-panel--searching');

// Add data attribute to the matching listitems.
Expand Down Expand Up @@ -389,7 +397,6 @@ const initSearch = function (
// Trigger event.
this.trigger('clear:before');

form.classList.remove('mm-searchfield--searching');
resultspanel.classList.remove('mm-panel--searching', 'mm-panel--noresults');

// Resultspanel.
Expand Down
1 change: 1 addition & 0 deletions src/addons/searchfield/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const options: mmOptionsSearchfield = {
addTo: 'panels',
noResults: 'No results found.',
placeholder: 'Search',
search: true,
searchIn: 'panels',
splash: '',
title: 'Search',
Expand Down
3 changes: 3 additions & 0 deletions src/addons/searchfield/typings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface mmOptionsSearchfield {
/** The placeholder text for the searchfield. */
placeholder?: string

/** Whether or not to search. */
search?: boolean

/** QuerySelector for the panels to search in. */
searchIn?: string

Expand Down
26 changes: 4 additions & 22 deletions src/core/offcanvas/mmenu.offcanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export default function (this: Mmenu) {
this.node.menu.classList.add('mm-menu--offcanvas');
this.node.menu.setAttribute('inert', 'true');

this.position(options.position);
if (possiblePositions.includes(options.position)) {
this.node.wrpr.classList.add(`mm-wrapper--position-${options.position}`);
this.node.menu.classList.add(`mm-menu--position-${options.position}`);
}

// Open if url hash equals menu id (usefull when user clicks the hamburger icon before the menu is created)
let hash = window.location.hash;
Expand Down Expand Up @@ -130,27 +133,6 @@ export default function (this: Mmenu) {
});
}

/**
* Get or set the postion for the menu.
*
* @param {string} [position] The position for the menu.
*/
Mmenu.prototype.position = function(this: Mmenu, position: mmOptionsOffcanvasPositions = null) {
const options = this.opts.offCanvas;

if (position) {
if (possiblePositions.includes(position)) {
this.node.wrpr.classList.remove(`mm-wrapper--position-${options.position}`);
this.node.wrpr.classList.add(`mm-wrapper--position-${position}`);
this.node.menu.classList.remove(`mm-menu--position-${options.position}`);
this.node.menu.classList.add(`mm-menu--position-${position}`);
options.position = position;
}
} else {
return options.position;
}
}

/**
* Open the menu.
*/
Expand Down
3 changes: 0 additions & 3 deletions src/core/oncanvas/mmenu.oncanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ export default class Mmenu {
/** Set the page HTML element. */
setPage: Function;

/** Set the menu position. */
position: Function;

/**
* Create a mobile menu.
* @param {HTMLElement|string} menu The menu node.
Expand Down

0 comments on commit 2f2049d

Please sign in to comment.