From 379e1b431afd70ed824a0da110a4579f78f1e0b2 Mon Sep 17 00:00:00 2001
From: Kevin Ditscheid <kevin.ditscheid@sgalinski.de>
Date: Tue, 18 Dec 2018 18:00:46 +0100
Subject: [PATCH] [BUGFIX] Fix sg_news Backend Module JavaScript

---
 .../ViewHelpers/Backend/ControlViewHelper.php |  17 +--
 Resources/Private/Layouts/Backend.html        |   9 +-
 Resources/Public/JavaScript/Backend.js        | 113 ++++++------------
 ext_tables.php                                |   1 +
 4 files changed, 50 insertions(+), 90 deletions(-)

diff --git a/Classes/ViewHelpers/Backend/ControlViewHelper.php b/Classes/ViewHelpers/Backend/ControlViewHelper.php
index c6a3fad..63cbf47 100644
--- a/Classes/ViewHelpers/Backend/ControlViewHelper.php
+++ b/Classes/ViewHelpers/Backend/ControlViewHelper.php
@@ -32,6 +32,8 @@ use TYPO3\CMS\Backend\Clipboard\Clipboard;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
+use TYPO3\CMS\Core\Localization\LanguageService;
+use TYPO3\CMS\Core\Page\PageRenderer;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
 use TYPO3\CMS\Recordlist\RecordList\DatabaseRecordList;
@@ -72,6 +74,12 @@ class ControlViewHelper extends AbstractViewHelper {
 		$backendUser = $GLOBALS['BE_USER'];
 		$pageInfo = BackendUtility::readPageAccess($row['pid'], $backendUser->getPagePermsClause(1));
 		$databaseRecordList->calcPerms = $GLOBALS['BE_USER']->calcPerms($pageInfo);
+		$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
+		$pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/AjaxDataHandler');
+		$pageRenderer->addInlineLanguageLabelFile('EXT:backend/Resources/Private/Language/locallang_alt_doc.xlf');
+		$languageService = GeneralUtility::makeInstance(LanguageService::class);
+		$languageService->includeLLFile('EXT:backend/Resources/Private/Language/locallang_alt_doc.xlf');
+
 		$databaseRecordList->currentTable = $sortingData;
 		if ($table === 'pages') {
 			$databaseRecordList->searchLevels = 1;
@@ -87,16 +95,9 @@ class ControlViewHelper extends AbstractViewHelper {
 		}
 
 		if ($table === 'pages' && LicensingService::checkKey()) {
-			$rootline = BackendUtility::BEgetRootLine($row['uid'], '', TRUE);
-			ksort($rootline);
-			$path = '/root';
-			foreach ($rootline as $page) {
-				$path .= '/p' . dechex($page['uid']);
-			}
-
 			$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
 			$buttonLabel = LocalizationUtility::translate('backend.button.editPageContent', 'SgNews');
-			$onclick = 'return sgNewsGoToPageModule(' . $row['uid'] . ', \'' . $path . '\');';
+			$onclick = 'return TYPO3.SgNewsModule.sgNewsGoToPageModule(' . $row['uid'] . ');';
 			$icon = $iconFactory->getIcon('actions-document-open-white', Icon::SIZE_SMALL)->render();
 			$wrap = ' <div class="btn-group" role="group">%s</div>';
 			$link = '<a href="#" onclick="' . $onclick . '" class="btn btn-primary" title="' . $buttonLabel . '">%s</a>';
diff --git a/Resources/Private/Layouts/Backend.html b/Resources/Private/Layouts/Backend.html
index a97ecad..f7ba56b 100644
--- a/Resources/Private/Layouts/Backend.html
+++ b/Resources/Private/Layouts/Backend.html
@@ -3,12 +3,9 @@
 
 <f:be.container
 	includeRequireJsModules="{
-		0: 'TYPO3/CMS/Backend/AjaxDataHandler',
-		1: 'TYPO3/CMS/Backend/ContextMenu',
-		2: 'TYPO3/CMS/Backend/Tooltip',
-		3: 'TYPO3/CMS/Recordlist/Tooltip',
-		4: 'TYPO3/CMS/SgNews/Backend'}"
-	includeJsFiles="{0: '{f:uri.resource(path: \'Scripts/Backend.js\')}'}">
+		0: 'TYPO3/CMS/Backend/ContextMenu',
+		1: 'TYPO3/CMS/Backend/Tooltip',
+		2: 'TYPO3/CMS/SgNews/Backend'}">
 	<div class="module" data-module-id="" data-module-name="">
 		<div class="module-docheader t3js-module-docheader">
 			<div class="module-docheader-bar module-docheader-bar-navigation t3js-module-docheader-bar t3js-module-docheader-bar-navigation">
diff --git a/Resources/Public/JavaScript/Backend.js b/Resources/Public/JavaScript/Backend.js
index 512a775..06ea6e8 100644
--- a/Resources/Public/JavaScript/Backend.js
+++ b/Resources/Public/JavaScript/Backend.js
@@ -22,7 +22,11 @@
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
-define(['jquery'], function($) {
+define([
+	'jquery',
+	'TYPO3/CMS/Backend/ModuleMenu',
+	'TYPO3/CMS/Backend/Viewport'
+], function($, ModuleMenu, Viewport) {
 	'use strict';
 	var SgNewsModule = {
 		init: function() {
@@ -35,83 +39,15 @@ define(['jquery'], function($) {
 				this.form.submit();
 			});
 		},
-		// functions for backend docheader functionality
-		jumpExt: function(URL, anchor) {
-			var anc = anchor ? anchor : "";
-			window.location.href = URL + (T3_THIS_LOCATION ? "&returnUrl=" + T3_THIS_LOCATION : "") + anc;
-			return false;
-		},
-		jumpSelf: function(URL) {
-			window.location.href = URL + (T3_RETURN_URL ? "&returnUrl=" + T3_RETURN_URL : "");
-			return false;
-		},
-		jumpToUrl: function(URL) {
-			window.location.href = URL;
-			return false;
-		},
-		setHighlight: function(id) {
-			top.fsMod.recentIds["web"] = id;
-			top.fsMod.navFrameHighlightedID["web"] = "pages" + id + "_" + top.fsMod.currentBank;    // For highlighting
-			if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
-				top.content.nav_frame.refresh_nav();
-			}
-		},
-		/**
-		 * Switches to the spefied page in the BE
-		 *
-		 * @param {number} uid
-		 * @param {string} path
-		 */
-		sgNewsGoToPage: function(uid, path, selectOnly) {
-			parent.fsMod.recentIds['web'] = uid;
-			if (typeof selectOnly === 'undefined') {
-				selectOnly = false;
-			}
-			selectOnly = Boolean(selectOnly);
-			if (top.nav) {
-				if (selectOnly) {
-					top.nav.invokePageId(uid, SgNewsModule.gotToPageCallbackNoFollow);
-				} else {
-					top.nav.invokePageId(uid, SgNewsModule.gotToPageCallback);
-				}
-			} else {
-				var tree = top.Ext.getCmp('typo3-pagetree');
-				if (tree) {
-					tree.activeTree.selectPath(path);
+		sgNewsGoToPageModule: function(uid) {
+			ModuleMenu.App.showModule('web_layout', 'id=' + uid);
+			var pageTreeNodes = Viewport.NavigationContainer.PageTree.instance.nodes;
+			for (var nodeIndex in pageTreeNodes) {
+				if (pageTreeNodes[nodeIndex].identifier === uid) {
+					Viewport.NavigationContainer.PageTree.selectNode(pageTreeNodes[nodeIndex]);
+					break;
 				}
-				if (selectOnly) {
-					return;
-				}
-				var separator = '?';
-				if (top.currentSubScript.indexOf('?') !== -1) {
-					separator = '&';
-				}
-				top.TYPO3.Backend.ContentContainer.setUrl(
-					top.currentSubScript + separator + 'id=' + uid
-				);
-			}
-		},
-		/**
-		 * Callback for page selection in the pagetree without follow
-		 */
-		gotToPageCallbackNoFollow: function(path) {
-			var callback = top.Ext.createDelegate(top.nav.mainTree.selectPath, top.nav.mainTree);
-			callback.apply(this, arguments);
-		},
-		/**
-		 * Callback for page selection in the pagetree
-		 */
-		gotToPageCallback: function(path) {
-			var callback = top.Ext.createDelegate(top.nav.mainTree.selectPath, top.nav.mainTree);
-			callback.apply(this, arguments);
-			var node = top.nav.getSelected();
-			if (node) {
-				top.TYPO3.Components.PageTree.Actions.singleClick(node, top.TYPO3.Components.PageTree.Tree);
 			}
-		},
-		sgNewsGoToPageModule: function(uid, path) {
-			SgNewsModule.sgNewsGoToPage(uid, path, true);
-			parent.TYPO3.ModuleMenu.App.showModule('web_layout');
 			return false;
 		}
 	};
@@ -121,3 +57,28 @@ define(['jquery'], function($) {
 	SgNewsModule.init();
 	return SgNewsModule;
 });
+
+// functions for backend docheader functionality
+function jumpExt(URL, anchor) {
+	var anc = anchor ? anchor : "";
+	window.location.href = URL + (T3_THIS_LOCATION ? "&returnUrl=" + T3_THIS_LOCATION : "") + anc;
+	return false;
+}
+
+function jumpSelf(URL) {
+	window.location.href = URL + (T3_RETURN_URL ? "&returnUrl=" + T3_RETURN_URL : "");
+	return false;
+}
+
+function jumpToUrl(URL) {
+	window.location.href = URL;
+	return false;
+}
+
+function setHighlight(id) {
+	top.fsMod.recentIds["web"] = id;
+	top.fsMod.navFrameHighlightedID["web"] = "pages" + id + "_" + top.fsMod.currentBank;    // For highlighting
+	if (top.content && top.content.nav_frame && top.content.nav_frame.refresh_nav) {
+		top.content.nav_frame.refresh_nav();
+	}
+}
diff --git a/ext_tables.php b/ext_tables.php
index 8b9a4f6..07f142c 100644
--- a/ext_tables.php
+++ b/ext_tables.php
@@ -41,6 +41,7 @@ if (TYPO3_MODE === 'BE') {
 			'access' => 'user,group',
 			'icon' => 'EXT:sg_news/Resources/Public/Icons/module-sgnews.svg',
 			'labels' => 'LLL:EXT:sg_news/Resources/Private/Language/locallang.xlf',
+			'navigationComponentId' => 'TYPO3/CMS/Backend/PageTree/PageTreeElement'
 		]
 	);
 }
-- 
GitLab