-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v4: Core: Menu - add menu events + app methods
- Loading branch information
1 parent
cff5694
commit 2510465
Showing
2 changed files
with
53 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}, | ||
}; |