<%--
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */
--%>

<%
DLFilePicker dlFilePicker = dlEditFileEntryDisplayContext.getDLFilePicker("onFilePick");
%>

<c:choose>
	<c:when test="<%= dlFilePicker == null %>">
		<aui:input autoFocus="<%= windowState.equals(WindowState.MAXIMIZED) || windowState.equals(LiferayWindowState.POP_UP) %>" name="file" onChange='<%= liferayPortletResponse.getNamespace() + "updateTitle();" %>' type="file">
			<aui:validator name="acceptFiles">
				'<%= StringUtil.merge(dlConfiguration.fileExtensions()) %>'
			</aui:validator>

			<aui:validator errorMessage='<%= LanguageUtil.format(locale, "please-enter-a-file-with-a-valid-file-size-no-larger-than-x", LanguageUtil.formatStorageSize(dlEditFileEntryDisplayContext.getMaximumUploadSize(), locale)) %>' name="maxFileSize"><%= dlEditFileEntryDisplayContext.getMaximumUploadSize() %></aui:validator>
		</aui:input>
	</c:when>
	<c:otherwise>
		<aui:field-wrapper label="file">
			<aui:button cssClass="customized-file-button disabled" disabled="<%= true %>" name="file" value="select-file" />

			<img id="<portlet:namespace />pickButtonIcon" />

			<span id="<portlet:namespace />pickButtonTitle"></span>
		</aui:field-wrapper>

		<aui:script sandbox="<%= true %>">
			var onFilePick = function (fields) {
				var documentIconURL = fields['<%= dlFilePicker.getIconFieldName() %>'];

				var pickButtonIcon = document.getElementById(
					'<portlet:namespace />pickButtonIcon'
				);

				if (pickButtonIcon) {
					pickButtonIcon.setAttribute('src', documentIconURL);
				}

				var documentTitle = fields['<%= dlFilePicker.getTitleFieldName() %>'];

				var pickButtonTitleElement = document.getElementById(
					'<portlet:namespace />pickButtonTitle'
				);

				if (pickButtonTitleElement) {
					pickButtonTitleElement.innerHTML = documentTitle;
				}

				var titleElement = document.getElementById('<portlet:namespace />title');

				if (titleElement) {
					titleElement.value = documentTitle;
				}

				var documentDescription =
					fields['<%= dlFilePicker.getDescriptionFieldName() %>'];

				var documentDescriptionElement = document.getElementById(
					'<portlet:namespace />documentDescription'
				);

				if (documentDescriptionElement) {
					documentDescriptionElement.value = documentDescription;
				}

				var ddmFieldWrappers = document.querySelectorAll(
					'#<portlet:namespace />fm .lfr-ddm-container .field-wrapper'
				);

				Array.prototype.forEach.call(ddmFieldWrappers, function (ddmFieldWrapper) {
					var fieldName = ddmFieldWrapper.dataset.fieldname;

					var input = ddmFieldWrapper.querySelector('input');

					if (input) {
						input.value = fields[fieldName];
					}
				});
			};

			<%= dlFilePicker.getJavaScript() %>;

			var customizedFileButton = document.getElementById('<portlet:namespace />file');

			if (customizedFileButton) {
				var filePicker = new <%= dlFilePicker.getJavaScriptModuleName() %>(
					function () {
						Liferay.Util.toggleDisabled(customizedFileButton, false);
					}
				);

				customizedFileButton.addEventListener(
					'click',
					filePicker['<%= dlFilePicker.getOnClickCallback() %>'].bind(filePicker)
				);
			}
		</aui:script>
	</c:otherwise>
</c:choose>