<commit_before>package org.sagebionetworks.web.client.widget.entity.renderer;

import java.util.List;

import org.sagebionetworks.repo.model.v2.wiki.V2WikiHeader;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.renderer.WikiSubpagesWidget.UpdateOrderHintCallback;
import org.sagebionetworks.web.shared.WikiPageKey;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class WikiSubpagesViewImpl extends FlowPanel implements WikiSubpagesView {

	private Presenter presenter;
	private WikiSubpagesOrderEditorModalWidget orderEditorModal;
	private static final String SHOW_SUBPAGES_STYLE="col-xs-12 col-md-3 well";
	private static final String SHOW_SUBPAGES_MD_STYLE="col-xs-12 col-md-9";
	private static final String HIDE_SUBPAGES_STYLE="col-xs-12";
	private static final String HIDE_SUBPAGES_MD_STYLE="col-xs-12";
	
	private Button showHideButton;
	private Button editOrderButton;
	private FlowPanel navTreeContainer;
	private FlowPanel wikiSubpagesContainer;
	private FlowPanel wikiPageContainer;
	boolean isShowingSubpages;
	
	private WikiSubpageNavigationTree navTree;
	
	@Inject
	public WikiSubpagesViewImpl(WikiSubpagesOrderEditorModalWidget orderEditorModal,
								WikiSubpageNavigationTree navTree) {
		this.orderEditorModal = orderEditorModal;
		this.navTree = navTree;
	}
	
	@Override
	public void clear() {
		super.clear();
		if (wikiSubpagesContainer != null) {
			wikiSubpagesContainer.setStyleName("");
		}
	}
	
	@Override
	public void configure(final List<V2WikiHeader> wikiHeaders,
						FlowPanel wikiSubpagesContainer, FlowPanel wikiPageContainer,
						final String ownerObjectName, Place ownerObjectLink,
						WikiPageKey curWikiKey, boolean isEmbeddedInOwnerPage,
						final UpdateOrderHintCallback updateOrderHintCallback,
						CallbackP<WikiPageKey> wikiPageCallback) {
		clear();
		
		this.wikiSubpagesContainer = wikiSubpagesContainer;
		this.wikiPageContainer = wikiPageContainer;
		navTreeContainer = new FlowPanel();
		navTreeContainer.addStyleName("margin-bottom-10");

		//this widget shows nothing if it doesn't have any pages!
		if (wikiHeaders.size() <=1 )
			return;
		
		//only show the tree if the root has children
		navTree.configure(wikiHeaders, ownerObjectName, ownerObjectLink, curWikiKey, isEmbeddedInOwnerPage, wikiPageCallback);
		
		showHideButton = DisplayUtils.createButton("");
		editOrderButton = DisplayUtils.createButton("Edit Order");
		editOrderButton.addStyleName("btn btn-default btn-xs left");

		editOrderButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				orderEditorModal.configure(wikiHeaders, ownerObjectName);
				orderEditorModal.show(updateOrderHintCallback);
			}
		});
		
		showHideButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isShowingSubpages)
					hideSubpages();
				else
					showSubpages();
			}
		});
		
		navTreeContainer.add(navTree.asWidget());
		
		add(navTreeContainer);
		add(editOrderButton);
		add(showHideButton);
		showSubpages();
		clearWidths();
	}
	
	/**
	 * Work around the Chrome bug.  See DisplayUtils.clearElementWidth() for more info.
	 */
	private void clearWidths() {
		DisplayUtils.clearElementWidth(getElement());
		if (wikiSubpagesContainer != null) 
			DisplayUtils.clearElementWidth(wikiSubpagesContainer.getElement());
		if (navTreeContainer != null)
			DisplayUtils.clearElementWidth(navTreeContainer.getElement());
		if (wikiPageContainer != null)
			DisplayUtils.clearElementWidth(wikiPageContainer.getElement());
	}
	
	@Override
	public void hideSubpages() {
		isShowingSubpages = false;
		// This call to layout is necessary to force the scroll bar to appear on page-load
		if (wikiSubpagesContainer != null){
			wikiSubpagesContainer.setStyleName(HIDE_SUBPAGES_STYLE);
		}
		
		if (editOrderButton != null) {
			editOrderButton.setVisible(false);
		}
				
		if (showHideButton != null) {
			showHideButton.setText("Show Pages " + DisplayConstants.RIGHT_ARROWS);
			showHideButton.removeStyleName("right");
			showHideButton.addStyleName("btn btn-default btn-xs margin-right-40 left");
		}
		
		if (navTreeContainer != null)
			DisplayUtils.hide(navTreeContainer);
		
		if (wikiPageContainer != null) {
			wikiPageContainer.removeStyleName(SHOW_SUBPAGES_MD_STYLE);
			wikiPageContainer.addStyleName(HIDE_SUBPAGES_MD_STYLE);
			wikiPageContainer.setVisible(true);
		}
	}
	
	@Override
	public void showSubpages() {
		isShowingSubpages = true;
		if (wikiSubpagesContainer != null) {
			wikiSubpagesContainer.removeStyleName(HIDE_SUBPAGES_STYLE);
			wikiSubpagesContainer.addStyleName(SHOW_SUBPAGES_STYLE);
		}
		
		if (editOrderButton != null) {
			editOrderButton.setVisible(true);
		}
			
		if (wikiPageContainer != null) {
			wikiPageContainer.removeStyleName(HIDE_SUBPAGES_MD_STYLE);
			wikiPageContainer.addStyleName(SHOW_SUBPAGES_MD_STYLE);
		}
				
		if (showHideButton != null) {
			showHideButton.setText(DisplayConstants.LEFT_ARROWS);
			showHideButton.removeStyleName("left");
			showHideButton.addStyleName("btn btn-default btn-xs right");		
		}
		
		if (navTreeContainer != null)
			DisplayUtils.show(navTreeContainer);
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}	

	@Override <commit_msg>clean up styles applied to wiki subpages, and wiki container<commit_after>package org.sagebionetworks.web.client.widget.entity.renderer;

import java.util.List;

import org.sagebionetworks.repo.model.v2.wiki.V2WikiHeader;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.renderer.WikiSubpagesWidget.UpdateOrderHintCallback;
import org.sagebionetworks.web.shared.WikiPageKey;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class WikiSubpagesViewImpl extends FlowPanel implements WikiSubpagesView {

	private Presenter presenter;
	private WikiSubpagesOrderEditorModalWidget orderEditorModal;
	private static final String WELL="well";
	private static final String COL_MD_3="col-md-3";
	private static final String COL_MD_9="col-md-9";
	private static final String COL_XS_12="col-xs-12";
	
	private Button showHideButton;
	private Button editOrderButton;
	private FlowPanel navTreeContainer;
	private FlowPanel wikiSubpagesContainer;
	private FlowPanel wikiPageContainer;
	boolean isShowingSubpages;
	
	private WikiSubpageNavigationTree navTree;
	
	@Inject
	public WikiSubpagesViewImpl(WikiSubpagesOrderEditorModalWidget orderEditorModal,
								WikiSubpageNavigationTree navTree) {
		this.orderEditorModal = orderEditorModal;
		this.navTree = navTree;
	}
	
	@Override
	public void clear() {
		super.clear();
		if (wikiSubpagesContainer != null) {
			wikiSubpagesContainer.setStyleName("");
		}
		if (wikiPageContainer != null) {
			wikiPageContainer.setStyleName("");
		}
			
	}
	
	@Override
	public void configure(final List<V2WikiHeader> wikiHeaders,
						FlowPanel wikiSubpagesContainer, FlowPanel wikiPageContainer,
						final String ownerObjectName, Place ownerObjectLink,
						WikiPageKey curWikiKey, boolean isEmbeddedInOwnerPage,
						final UpdateOrderHintCallback updateOrderHintCallback,
						CallbackP<WikiPageKey> wikiPageCallback) {
		clear();
		
		this.wikiSubpagesContainer = wikiSubpagesContainer;
		this.wikiPageContainer = wikiPageContainer;
		navTreeContainer = new FlowPanel();
		navTreeContainer.addStyleName("margin-bottom-10");

		//this widget shows nothing if it doesn't have any pages!
		if (wikiHeaders.size() <=1 )
			return;
		
		//only show the tree if the root has children
		navTree.configure(wikiHeaders, ownerObjectName, ownerObjectLink, curWikiKey, isEmbeddedInOwnerPage, wikiPageCallback);
		
		showHideButton = DisplayUtils.createButton("");
		editOrderButton = DisplayUtils.createButton("Edit Order");
		editOrderButton.addStyleName("btn btn-default btn-xs left");

		editOrderButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				orderEditorModal.configure(wikiHeaders, ownerObjectName);
				orderEditorModal.show(updateOrderHintCallback);
			}
		});
		
		showHideButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (isShowingSubpages)
					hideSubpages();
				else
					showSubpages();
			}
		});
		
		navTreeContainer.add(navTree.asWidget());
		
		add(navTreeContainer);
		add(editOrderButton);
		add(showHideButton);
		showSubpages();
		clearWidths();
	}
	
	/**
	 * Work around the Chrome bug.  See DisplayUtils.clearElementWidth() for more info.
	 */
	private void clearWidths() {
		DisplayUtils.clearElementWidth(getElement());
		if (wikiSubpagesContainer != null) 
			DisplayUtils.clearElementWidth(wikiSubpagesContainer.getElement());
		if (navTreeContainer != null)
			DisplayUtils.clearElementWidth(navTreeContainer.getElement());
		if (wikiPageContainer != null)
			DisplayUtils.clearElementWidth(wikiPageContainer.getElement());
	}
	
	@Override
	public void hideSubpages() {
		isShowingSubpages = false;
		// This call to layout is necessary to force the scroll bar to appear on page-load
		if (wikiSubpagesContainer != null){
			wikiSubpagesContainer.setStyleName(COL_XS_12);
		}
		
		if (editOrderButton != null) {
			editOrderButton.setVisible(false);
		}
				
		if (showHideButton != null) {
			showHideButton.setText("Show Pages " + DisplayConstants.RIGHT_ARROWS);
			showHideButton.removeStyleName("right");
			showHideButton.addStyleName("btn btn-default btn-xs margin-right-40 left");
		}
		
		if (navTreeContainer != null)
			DisplayUtils.hide(navTreeContainer);
		
		if (wikiPageContainer != null) {
			wikiPageContainer.setStyleName("");
			wikiPageContainer.addStyleName(COL_XS_12);
			wikiPageContainer.setVisible(true);
		}
	}
	
	@Override
	public void showSubpages() {
		isShowingSubpages = true;
		if (wikiSubpagesContainer != null) {
			wikiSubpagesContainer.setStyleName("");
			wikiSubpagesContainer.addStyleName(COL_XS_12);
			wikiSubpagesContainer.addStyleName(WELL);
			wikiSubpagesContainer.addStyleName(COL_MD_3);
		}
		
		if (editOrderButton != null) {
			editOrderButton.setVisible(true);
		}
			
		if (wikiPageContainer != null) {
			wikiPageContainer.setStyleName("");
			wikiPageContainer.addStyleName(COL_XS_12);
			wikiPageContainer.addStyleName(COL_MD_9);
		}
				
		if (showHideButton != null) {
			showHideButton.setText(DisplayConstants.LEFT_ARROWS);
			showHideButton.removeStyleName("left");
			showHideButton.addStyleName("btn btn-default btn-xs right");		
		}
		
		if (navTreeContainer != null)
			DisplayUtils.show(navTreeContainer);
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}	

	@Override 
{'fraction_non_alphanumeric': 0.09649199545096666, 'fraction_numerical': 0.003849182048814627, 'mean_word_length': 5.090570058604156, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5940878', 'n_tokens_mistral': 4001, 'n_tokens_neox': 3458, 'n_words': 655}