<%--
/**
 * SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
 * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
 */
--%>

<c:if test="<%= (layout != null) && LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.UPDATE) %>">
	<aui:script>
		Liferay.provide(
			Liferay.Portlet,
			'refreshLayout',
			function(portletBound) {
				if (!portletBound.isStatic) {
					Liferay.Layout.refresh(portletBound);
				}
			},
			['liferay-layout']
		);
	</aui:script>
</c:if>

<aui:script position="inline">
	<c:choose>
		<c:when test="<%= themeDisplay.isStatePopUp() %>">
			Liferay.Util.getTop().Liferay.fire(
				'popupReady',
				{
					doc: document,
					win: window,
					windowName: window.name
				}
			);
		</c:when>
		<c:otherwise>

			<%
			String scroll = ParamUtil.getString(request, "scroll");
			%>

			<c:if test="<%= Validator.isNotNull(scroll) %>">
				Liferay.on(
					'allPortletsReady',
					function(event) {
						document.getElementById('<%= HtmlUtil.escape(scroll) %>').scrollIntoView();
					}
				);
			</c:if>
		</c:otherwise>
	</c:choose>
</aui:script>

<aui:script>
	if (window.svg4everybody && Liferay.Data.ICONS_INLINE_SVG) {
		svg4everybody(
			{
				polyfill: true,
				validate: function (src, svg, use) {
					return !src || !src.startsWith('#');
				}
			}
		);
	}
</aui:script>

<aui:script sandbox="<%= true %>">
	Liferay.Util.delegate(
		document,
		'focusin',
		'.portlet',
		function(event) {
			event.delegateTarget.closest('.portlet').classList.add('open');
		}
	);

	Liferay.Util.delegate(
		document,
		'focusout',
		'.portlet',
		function(event) {
			event.delegateTarget.closest('.portlet').classList.remove('open');
		}
	);
</aui:script>

<%
Group group = null;

LayoutRevision layoutRevision = null;

if (layout != null) {
	group = layout.getGroup();

	layoutRevision = LayoutStagingUtil.getLayoutRevision(layout);
}
%>

<c:if test="<%= !themeDisplay.isStatePopUp() && !group.isControlPanel() && (layout != null) && (!group.hasStagingGroup() || group.isStagingGroup() || !PropsValues.STAGING_LIVE_GROUP_LOCKING_ENABLED || layoutTypePortlet.isCustomizable()) && (GroupPermissionUtil.contains(permissionChecker, scopeGroupId, ActionKeys.ADD_LAYOUT) || LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.UPDATE) || (layoutTypePortlet.isCustomizable() && LayoutPermissionUtil.contains(permissionChecker, layout, ActionKeys.CUSTOMIZE))) %>">
	<c:if test="<%= layout.isTypePortlet() %>">
		<aui:script position="inline">
			Liferay.Data.layoutConfig = {
				container: '#main-content',
				dropNodes: '.portlet-column',

				<c:choose>
					<c:when test="<%= BrowserSnifferUtil.isMobile(request) %>">
						handles: Liferay.Data.PORTLET_TOUCH_DRAG_HANDLE_SELECTOR || ['.portlet-title-default'],
					</c:when>
					<c:otherwise>
						handles: Liferay.Data.PORTLET_DRAG_HANDLE_SELECTOR || ['.portlet-title-default', '.portlet-topper'],
					</c:otherwise>
				</c:choose>

				disabledDropContainerClass: 'portlet-dropzone-disabled',
				dragNodes: '.portlet-boundary:not(.portlet-static)',
				dropContainer: '.portlet-dropzone',
				emptyColumnClass: 'empty',
				invalid: '.portlet-static',
				nestedPortletId: '_<%= PortletKeys.NESTED_PORTLETS %>_INSTANCE',
				portletBoundary: '.portlet-boundary'
			};

			Liferay.fire('dataLayoutConfigReady');
		</aui:script>

		<aui:script use="liferay-layout">
			<%-- aui:script won't accept a null body --%>
		</aui:script>
	</c:if>

	<aui:script use="event-move,liferay-navigation">
		var navBlock = A.one(Liferay.Data.NAV_SELECTOR);

		if (navBlock) {
			if (A.UA.touchMobile) {
				var panel = document.querySelector(Liferay.Data.NAV_SELECTOR_MOBILE);

				Liferay.on('liferay.collapse.shown',
					function(event) {
						var panelId = event.panel.geAttribute('id');

						if (panelId === panel.geAttribute('id')) {
							Liferay.fire('initNavigation');
						}
					}
				);
			}
			else {
				navBlock.once(
					['gesturemovestart', 'mousemove'],
					function () {
						Liferay.fire('initNavigation');
					}
				);
			}
		}

		A.on(
			'io:complete',
			function () {
				Liferay.fire('io:complete');
			}
		);
	</aui:script>
</c:if>

<aui:script use="liferay-menu">
	new Liferay.Menu();

	var liferayNotices = Liferay.Data.notices;

	for (var i = 0; i < liferayNotices.length; i++) {
		Liferay.Util.openToast(liferayNotices[i]);
	}

</aui:script>

<aui:script type="text/javascript">
	// <![CDATA[

		<%
		String currentURL = PortalUtil.getCurrentURL(request);
		%>

		Liferay.currentURL = '<%= HtmlUtil.escapeJS(currentURL) %>';
		Liferay.currentURLEncoded = '<%= HtmlUtil.escapeJS(URLCodec.encodeURL(currentURL)) %>';

	// ]]>
</aui:script>

<c:if test="<%= (layout != null) && !layout.isTypeControlPanel() %>">

	<%-- User Inputted Layout and LayoutSet JavaScript --%>

	<%
	UnicodeProperties masterLayoutTypeSettingsUnicodeProperties = new UnicodeProperties();

	if (layout.getMasterLayoutPlid() > 0) {
		Layout masterLayout = LayoutLocalServiceUtil.fetchLayout(layout.getMasterLayoutPlid());

		if (masterLayout != null) {
			masterLayoutTypeSettingsUnicodeProperties = masterLayout.getTypeSettingsProperties();
		}
	}

	LayoutSet layoutSet = themeDisplay.getLayoutSet();

	UnicodeProperties layoutSetSettingsUnicodeProperties = layoutSet.getSettingsProperties();

	UnicodeProperties layoutTypeSettingsUnicodeProperties = layout.getTypeSettingsProperties();

	String snippet = GetterUtil.getString(layoutSetSettingsUnicodeProperties.getProperty("javascript"));
	%>

	<c:if test="<%= Validator.isNotNull(snippet) %>">
		<%@ include file="/html/common/themes/bottom_js_script.jspf" %>
	</c:if>

	<%
	snippet = GetterUtil.getString(masterLayoutTypeSettingsUnicodeProperties.getProperty("javascript"));
	%>

	<c:if test="<%= Validator.isNotNull(snippet) %>">
		<%@ include file="/html/common/themes/bottom_js_script.jspf" %>
	</c:if>

	<%
	snippet = GetterUtil.getString(layoutTypeSettingsUnicodeProperties.getProperty("javascript"));
	%>

	<c:if test="<%= Validator.isNotNull(snippet) %>">
		<%@ include file="/html/common/themes/bottom_js_script.jspf" %>
	</c:if>
</c:if>