Skip to content

Commit

Permalink
v4: Core: Menu - add menu events + app methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Jan 29, 2019
1 parent cff5694 commit 2510465
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/core/components/menu/menu.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import Framework7, { CSSSelector, Framework7EventsClass, Framework7Plugin } from '../app/app-class';

export namespace Menu {
interface AppMethods {}
interface DomEvents {
/** Event will be triggered right after menu dropdown will be opened */
'menu:opened' : () => void
/** Event will be triggered right after menu dropdown will be closed */
'menu:closed' : () => void
}
interface AppMethods {
menu: {
/** open Menu dropdown */
open(el? : HTMLElement | CSSSelector) : void
/** close Menu dropdown */
close(el? : HTMLElement | CSSSelector) : void
}
}
interface AppParams {}
interface AppEvents {}
interface AppEvents {
/** Event will be triggered right after menu dropdown will be opened. As an argument event handler receives menu dropdown item element */
menuOpened: (el : HTMLElement) => void
/** Event will be triggered right after menu dropdown will be closed. As an argument event handler receives menu dropdown item element */
menuClosed: (el : HTMLElement) => void
}
}

declare const MenuComponent: Framework7Plugin;
Expand Down
37 changes: 33 additions & 4 deletions src/core/components/menu/menu.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,58 @@
import $ from 'dom7';

const Menu = {
open(el = '.menu-item-dropdown') {
const app = this;
if (!el) return;
const $el = $(el).closest('.menu-item-dropdown');
if (!$el.length) return;
$el.eq(0).addClass('menu-item-dropdown-opened').trigger('menu:opened');
app.emit('menuOpened', $el.eq(0)[0]);
},
close(el = '.menu-item-dropdown-opened') {
const app = this;
if (!el) return;
const $el = $(el).closest('.menu-item-dropdown-opened');
if (!$el.length) return;
$el.eq(0).removeClass('menu-item-dropdown-opened').trigger('menu:closed');
app.emit('menuClosed', $el.eq(0)[0]);
},
};

export default {
name: 'menu',
create() {
const app = this;
app.menu = {
open: Menu.open.bind(app),
close: Menu.close.bind(app),
};
},
on: {
click(e) {
const app = this;
const openedMenus = $('.menu-item-dropdown-opened');
if (!openedMenus.length) return;
openedMenus.each((index, el) => {
if (!$(e.target).closest('.menu-item-dropdown-opened').length) {
$(el).removeClass('menu-item-dropdown-opened');
app.menu.close(el);
}
});
},
},
clicks: {
'.menu-item-dropdown': function onClick($clickedEl, dataset, e) {
const app = this;
if ($clickedEl.hasClass('menu-item-dropdown-opened')) {
if ($(e.target).closest('.menu-dropdown').length) return;
$clickedEl.removeClass('menu-item-dropdown-opened');
app.menu.close($clickedEl);
} else {
$clickedEl.addClass('menu-item-dropdown-opened');
app.menu.open($clickedEl);
}
},
'.menu-close': function onClick() {
$('.menu-item-dropdown-opened').removeClass('menu-item-dropdown-opened');
const app = this;
app.menu.close();
},
},
};

0 comments on commit 2510465

Please sign in to comment.