Skip to content

Commit

Permalink
Fixes issues with AJAX and multi-site. Bump to 1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed May 5, 2020
1 parent 2c14bbe commit 95a7150
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 66 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/ChildMe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
114 changes: 49 additions & 65 deletions src/resources/childme.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
this.elementIndex = elementIndex;
if (elementIndex.settings.context === 'modal') {
this.hideButtons();
} else {
this.createEntryTypeButtons();
}
},

Expand All @@ -62,7 +60,6 @@
this.hideButtons();
} else {
this.showButtons();
this.createEntryTypeButtons();
}
},

Expand All @@ -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 = '<div class="menu" data-align="center" style="position:fixed;"><ul>';
var menuOptions = [];
var typeId;
var menuHtml = '<div class="menu" data-align="center" style="z-index:1;"><ul>';
var menuOptions = [];
var typeId;

for (var j = 0; j < entryTypeIds.length; ++j) {
typeId = parseInt(entryTypeIds[j].split(':').pop(), 10);
if (!typeId || isNaN(typeId)) {
continue;
}
menuOptions.push('<li><a data-type="' + typeId + '" data-parent="' + $button.data('id') + '" data-section="' + sectionHandle + '">' + entryTypes[entryTypeIds[j]] + '</a></li>');
for (var j = 0; j < entryTypeIds.length; ++j) {
typeId = parseInt(entryTypeIds[j].split(':').pop(), 10);
if (!typeId || isNaN(typeId)) {
continue;
}
menuOptions.push('<li><a data-type="' + typeId + '" data-parent="' + $button.data('id') + '" data-section="' + sectionHandle + '" tabindex="0">' + entryTypes[entryTypeIds[j]] + '</a></li>');
}

menuHtml += menuOptions.join('') + '</ul></div>';

$button
.data('_childmemenu', $(menuHtml).appendTo($button))
.removeAttr('title')
.removeAttr('href');
menuHtml += menuOptions.join('') + '</ul></div>';

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) {
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 95a7150

Please sign in to comment.