<%--
/**
 * 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.
 *
 *
 *
 */
--%>

<%@ include file="/init.jsp" %>

<clay:management-toolbar
	displayContext="<%= new AssetBrowserManagementToolbarDisplayContext(liferayPortletRequest, liferayPortletResponse, request, assetBrowserDisplayContext) %>"
/>

<aui:form action="<%= assetBrowserDisplayContext.getPortletURL() %>" cssClass="container-fluid-1280" method="post" name="selectAssetFm">
	<aui:input name="typeSelection" type="hidden" value="<%= assetBrowserDisplayContext.getTypeSelection() %>" />

	<liferay-ui:search-container
		id="selectAssetEntries"
		iteratorURL="<%= assetBrowserDisplayContext.getPortletURL() %>"
		searchContainer="<%= assetBrowserDisplayContext.getAssetBrowserSearch() %>"
		var="assetEntriesSearchContainer"
	>
		<liferay-ui:search-container-row
			className="com.liferay.asset.kernel.model.AssetEntry"
			escapedModel="<%= true %>"
			modelVar="assetEntry"
		>

			<%
			AssetRenderer assetRenderer = assetEntry.getAssetRenderer();
			%>

			<c:choose>
				<c:when test="<%= (assetRenderer != null) && (assetEntry.getEntryId() != assetBrowserDisplayContext.getRefererAssetEntryId()) %>">

					<%
					AssetRendererFactory assetRendererFactory = assetRenderer.getAssetRendererFactory();

					Group group = GroupLocalServiceUtil.getGroup(assetEntry.getGroupId());

					String cssClass = StringPool.BLANK;

					Map<String, Object> data = new HashMap<String, Object>();

					if (assetEntry.getEntryId() != assetBrowserDisplayContext.getRefererAssetEntryId()) {
						data.put("assetclassname", assetEntry.getClassName());
						data.put("assetclassnameid", assetEntry.getClassNameId());
						data.put("assetclasspk", assetEntry.getClassPK());
						data.put("assettitle", assetRenderer.getTitle(locale));
						data.put("assettitlemap", JSONFactoryUtil.looseSerialize(LocalizationUtil.getLocalizationMap(assetEntry.getTitle())));
						data.put("assettype", assetRendererFactory.getTypeName(locale, assetBrowserDisplayContext.getSubtypeSelectionId()));
						data.put("entityid", assetEntry.getEntryId());
						data.put("groupdescriptivename", group.getDescriptiveName(locale));

						if (assetBrowserDisplayContext.isMultipleSelection()) {
							row.setData(data);
						}

						cssClass = "selector-button";
					}
					%>

					<c:choose>
						<c:when test='<%= Objects.equals(assetBrowserDisplayContext.getDisplayStyle(), "descriptive") %>'>
							<liferay-ui:search-container-column-text>
								<liferay-ui:user-portrait
									userId="<%= assetEntry.getUserId() %>"
								/>
							</liferay-ui:search-container-column-text>

							<liferay-ui:search-container-column-text
								colspan="<%= 2 %>"
							>

								<%
								Date modifiedDate = assetEntry.getModifiedDate();
								%>

								<h6 class="text-default">
									<span><liferay-ui:message arguments="<%= LanguageUtil.getTimeDescription(request, System.currentTimeMillis() - modifiedDate.getTime(), true) %>" key="modified-x-ago" /></span>
								</h6>

								<h5>
									<c:choose>
										<c:when test="<%= (assetEntry.getEntryId() != assetBrowserDisplayContext.getRefererAssetEntryId()) && !assetBrowserDisplayContext.isMultipleSelection() %>">
											<aui:a cssClass="<%= cssClass %>" data="<%= data %>" href="javascript:;">
												<%= HtmlUtil.escape(assetRenderer.getTitle(locale)) %>
											</aui:a>
										</c:when>
										<c:otherwise>
											<%= HtmlUtil.escape(assetRenderer.getTitle(locale)) %>
										</c:otherwise>
									</c:choose>
								</h5>

								<c:if test="<%= Validator.isNull(assetBrowserDisplayContext.getTypeSelection()) %>">
									<h6 class="text-muted">
										<%= HtmlUtil.escape(assetRendererFactory.getTypeName(locale, assetBrowserDisplayContext.getSubtypeSelectionId())) %>
									</h6>
								</c:if>

								<h6 class="text-default">
									<%= HtmlUtil.escape(group.getDescriptiveName(locale)) %>
								</h6>
							</liferay-ui:search-container-column-text>
						</c:when>
						<c:when test='<%= Objects.equals(assetBrowserDisplayContext.getDisplayStyle(), "icon") %>'>

							<%
							row.setCssClass("entry-card lfr-asset-item");
							%>

							<liferay-ui:search-container-column-text>
								<clay:vertical-card
									verticalCard="<%= new AssetEntryVerticalCard(assetEntry, renderRequest, assetBrowserDisplayContext) %>"
								/>
							</liferay-ui:search-container-column-text>
						</c:when>
						<c:when test='<%= Objects.equals(assetBrowserDisplayContext.getDisplayStyle(), "list") %>'>
							<liferay-ui:search-container-column-text
								name="title"
								truncate="<%= true %>"
							>
								<c:choose>
									<c:when test="<%= (assetEntry.getEntryId() != assetBrowserDisplayContext.getRefererAssetEntryId()) && !assetBrowserDisplayContext.isMultipleSelection() %>">
										<aui:a cssClass="<%= cssClass %>" data="<%= data %>" href="javascript:;">
											<%= HtmlUtil.escape(assetRenderer.getTitle(locale)) %>
										</aui:a>
									</c:when>
									<c:otherwise>
										<%= HtmlUtil.escape(assetRenderer.getTitle(locale)) %>
									</c:otherwise>
								</c:choose>
							</liferay-ui:search-container-column-text>

							<c:if test="<%= Validator.isNull(assetBrowserDisplayContext.getTypeSelection()) %>">
								<liferay-ui:search-container-column-text
									name="type"
									truncate="<%= true %>"
									value="<%= HtmlUtil.escape(assetRendererFactory.getTypeName(locale, assetBrowserDisplayContext.getSubtypeSelectionId())) %>"
								/>
							</c:if>

							<liferay-ui:search-container-column-text
								name="description"
								truncate="<%= true %>"
								value="<%= HtmlUtil.escape(assetRenderer.getSummary(renderRequest, renderResponse)) %>"
							/>

							<liferay-ui:search-container-column-text
								name="author"
								value="<%= PortalUtil.getUserName(assetEntry) %>"
							/>

							<liferay-ui:search-container-column-date
								name="modified-date"
								value="<%= assetEntry.getModifiedDate() %>"
							/>

							<liferay-ui:search-container-column-text
								name="site"
								value="<%= HtmlUtil.escape(group.getDescriptiveName(locale)) %>"
							/>
						</c:when>
					</c:choose>
				</c:when>
				<c:otherwise>

					<%
					if (assetRenderer == null) {
						_log.error("Unable to get asset renderer for asset entry with primary key " + assetEntry.getEntryId());
					}

					row.setSkip(true);
					%>

				</c:otherwise>
			</c:choose>
		</liferay-ui:search-container-row>

		<liferay-ui:search-iterator
			displayStyle="<%= assetBrowserDisplayContext.getDisplayStyle() %>"
			markupView="lexicon"
			paginate="<%= true %>"
		/>
	</liferay-ui:search-container>
</aui:form>

<c:choose>
	<c:when test="<%= assetBrowserDisplayContext.isMultipleSelection() %>">
		<aui:script use="liferay-search-container">
			var searchContainer = Liferay.SearchContainer.get(
				'<portlet:namespace />selectAssetEntries'
			);

			searchContainer.on('rowToggled', function(event) {
				var selectedItems = event.elements.allSelectedElements;
				var arr = [];

				selectedItems.each(function() {
					var domElement = this.ancestor('tr');

					if (domElement == null) {
						domElement = this.ancestor('li');
					}

					if (domElement == null) {
						domElement = this.ancestor('dd');
					}

					if (domElement != null) {
						var data = domElement.getDOM().dataset;
						arr.push(data);
					}
				});

				Liferay.Util.getOpener().Liferay.fire(
					'<%= HtmlUtil.escapeJS(assetBrowserDisplayContext.getEventName()) %>',
					{
						data: arr
					}
				);
			});
		</aui:script>
	</c:when>
	<c:otherwise>
		<aui:script>
			Liferay.Util.selectEntityHandler(
				'#<portlet:namespace />selectAssetFm',
				'<%= HtmlUtil.escapeJS(assetBrowserDisplayContext.getEventName()) %>'
			);
		</aui:script>
	</c:otherwise>
</c:choose>

<%!
private static Log _log = LogFactoryUtil.getLog("com_liferay_asset_browser_web.view_jsp");
%>