From 95a7150d138ab1d4d56edabe5dd7d4bf656a7474 Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Tue, 5 May 2020 10:44:46 +0200 Subject: [PATCH] Fixes issues with AJAX and multi-site. Bump to 1.1.1 --- CHANGELOG.md | 5 ++ composer.json | 2 +- src/ChildMe.php | 1 + src/resources/childme.js | 114 +++++++++++++++++---------------------- 4 files changed, 56 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 789aaf0..f7fcee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## 1.1.1 - 2020-05-05 +### Fixed +- Fixes an issue where entry type menus would not be displayed for entries loaded in with AJAX on paginated entry indexes +- Fixes an issue where Child Me! could redirect to the wrong URL for multi-site installs + ## 1.1.0 - 2020-03-08 ### Fixed - Fixes an issue where entry type menus could be cut off on Craft 3.4+ diff --git a/composer.json b/composer.json index ab024fe..d064dce 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "mmikkel/child-me", "description": "Easily create child elements", "type": "craft-plugin", - "version": "1.1.0", + "version": "1.1.1", "keywords": [ "craft", "cms", diff --git a/src/ChildMe.php b/src/ChildMe.php index 83cdba7..c345cbd 100644 --- a/src/ChildMe.php +++ b/src/ChildMe.php @@ -121,6 +121,7 @@ function () { $data['sites']['site:' . $site->id] = $site->handle; } } + $data['isCraft34'] = \version_compare(Craft::$app->getVersion(), '3.4.0', '>='); Craft::$app->getView()->registerAssetBundle(ChildMeBundle::class); Craft::$app->getView()->registerJs('Craft.ChildMePlugin.init(' . Json::encode($data) . ')'); } catch (InvalidConfigException $e) { diff --git a/src/resources/childme.js b/src/resources/childme.js index 8f513bd..cdcac75 100644 --- a/src/resources/childme.js +++ b/src/resources/childme.js @@ -43,8 +43,6 @@ this.elementIndex = elementIndex; if (elementIndex.settings.context === 'modal') { this.hideButtons(); - } else { - this.createEntryTypeButtons(); } }, @@ -62,7 +60,6 @@ this.hideButtons(); } else { this.showButtons(); - this.createEntryTypeButtons(); } }, @@ -71,58 +68,47 @@ return this.data['sites']['site:' + siteId] || null; }, - createEntryTypeButtons: function () { + createEntryTypeMenu: function ($button) { - if (this.entryTypeButtons) { - for (var i = 0; i < this.entryTypeButtons.length; ++i) { - this.entryTypeButtons[i].destroy(); - } + var sectionHandle = $button.data('section') || null; + if (!sectionHandle) { + return null; } - var _self = this; - var entryTypeButtons = []; - var $button; - - $('[data-childmeadd]').each(function () { - - $button = $(this); - - var sectionHandle = $button.data('section') || null; - if (!sectionHandle) { - return false; - } - - var entryTypes = _self.data['entryTypes'][sectionHandle] || {}; - var entryTypeIds = Object.keys(entryTypes); - if (entryTypeIds.length <= 1) { - return false; - } + var entryTypes = this.data['entryTypes'][sectionHandle] || {}; + var entryTypeIds = Object.keys(entryTypes); + if (entryTypeIds.length <= 1) { + return null; + } - var menuHtml = ''; - entryTypeButtons.push(new Garnish.MenuBtn($button)); + $button + .data('_childmemenu', $(menuHtml).appendTo($button)) + .removeAttr('title'); - }); - - this.entryTypeButtons = entryTypeButtons; + return new Garnish.MenuBtn($button); + }, + getEntryTypeMenu: function ($button) { + var menu = $button.data('_childmemenu'); + if (menu === undefined) { + this.createEntryTypeMenu($button); + return $button.data('_childmemenu'); + } + return menu; }, onEntryTypeOptionSelect: function (e) { @@ -144,14 +130,16 @@ }, closeActiveEntryTypeMenu: function () { - if (this.openEntryTypeMenu) { - this.openEntryTypeMenu.hide(); - this.openEntryTypeMenu = null; + if (!this.openEntryTypeMenu) { + return; } + this.openEntryTypeMenu.hide(); + this.openEntryTypeMenu = null; }, + // Position a fixed menu relative to the trigger – only relevant for Craft 3.4+ positionMenu: function (menu) { - if (!menu) { + if (!menu || !this.data.isCraft34) { return; } var $menu = $(menu); @@ -164,14 +152,15 @@ var left = rect.left; $menu.css({ top: top, - left: left + left: left, + position: 'fixed' }); }, onChildMeButtonFocus: function (e) { this.closeActiveEntryTypeMenu(); - var $target = $(e.currentTarget); - var menu = $target.data('_childmemenu'); + var $button = $(e.currentTarget); + var menu = this.getEntryTypeMenu($button); if (menu) { e.preventDefault(); e.stopPropagation(); @@ -182,21 +171,16 @@ }, onChildMeButtonClick: function (e) { - e.preventDefault(); - e.stopPropagation(); - var $target = $(e.currentTarget); - var menu = $target.data('_childmemenu'); + this.closeActiveEntryTypeMenu(); + var $button = $(e.currentTarget); + var menu = this.getEntryTypeMenu($button); if (menu) { - return false; - } - var url = $target.attr('href'); - var siteId = Craft.getLocalStorage('BaseElementIndex.siteId'); - var siteHandle = this.data['sites']['site:' + siteId] || null; - if (siteHandle) { - url = url.split('?'); - url = url[0] + '/' + siteHandle + '?' + url[1]; + e.preventDefault(); + e.stopPropagation(); + this.openEntryTypeMenu = menu; + this.positionMenu(menu); + menu.show(); } - window.location.href = url; }, onDragStop: function (tableSorter) {