You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DocsPage.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.util.List;
  18. import org.apache.wicket.Component;
  19. import org.apache.wicket.PageParameters;
  20. import org.apache.wicket.behavior.SimpleAttributeModifier;
  21. import org.apache.wicket.markup.html.basic.Label;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.apache.wicket.markup.html.link.ExternalLink;
  24. import org.apache.wicket.markup.html.panel.Fragment;
  25. import org.apache.wicket.markup.repeater.Item;
  26. import org.apache.wicket.markup.repeater.data.DataView;
  27. import org.apache.wicket.markup.repeater.data.ListDataProvider;
  28. import org.eclipse.jgit.lib.Repository;
  29. import org.eclipse.jgit.revwalk.RevCommit;
  30. import com.gitblit.models.PathModel;
  31. import com.gitblit.servlet.RawServlet;
  32. import com.gitblit.utils.ByteFormat;
  33. import com.gitblit.utils.JGitUtils;
  34. import com.gitblit.utils.StringUtils;
  35. import com.gitblit.wicket.CacheControl;
  36. import com.gitblit.wicket.CacheControl.LastModified;
  37. import com.gitblit.wicket.MarkupProcessor;
  38. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  39. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  40. import com.gitblit.wicket.WicketUtils;
  41. import com.gitblit.wicket.panels.LinkPanel;
  42. @CacheControl(LastModified.REPOSITORY)
  43. public class DocsPage extends RepositoryPage {
  44. public DocsPage(PageParameters params) {
  45. super(params);
  46. String objectId = WicketUtils.getObject(params);
  47. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  48. Repository r = getRepository();
  49. RevCommit head = JGitUtils.getCommit(r, objectId);
  50. final String commitId = getBestCommitId(head);
  51. List<String> extensions = processor.getAllExtensions();
  52. List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
  53. List<MarkupDocument> roots = processor.getRootDocs(r, repositoryName, commitId);
  54. Fragment fragment = null;
  55. if (roots.isEmpty()) {
  56. // no identified root documents
  57. fragment = new Fragment("docs", "noIndexFragment", this);
  58. setResponsePage(NoDocsPage.class, params);
  59. } else {
  60. // root documents, use tabbed ui of index/root and document list
  61. fragment = new Fragment("docs", "tabsFragment", this);
  62. ListDataProvider<MarkupDocument> docDp = new ListDataProvider<MarkupDocument>(roots);
  63. // tab titles
  64. DataView<MarkupDocument> tabTitles = new DataView<MarkupDocument>("tabTitle", docDp) {
  65. private static final long serialVersionUID = 1L;
  66. int counter;
  67. @Override
  68. public void populateItem(final Item<MarkupDocument> item) {
  69. MarkupDocument doc = item.getModelObject();
  70. String file = StringUtils.getLastPathElement(doc.documentPath);
  71. file = StringUtils.stripFileExtension(file);
  72. String name = file.replace('_', ' ').replace('-', ' ');
  73. ExternalLink link = new ExternalLink("link", "#" + file);
  74. link.add(new Label("label", name.toUpperCase()).setRenderBodyOnly(true));
  75. item.add(link);
  76. if (counter == 0) {
  77. counter++;
  78. item.add(new SimpleAttributeModifier("class", "active"));
  79. }
  80. }
  81. };
  82. fragment.add(tabTitles);
  83. // tab content
  84. DataView<MarkupDocument> tabsView = new DataView<MarkupDocument>("tabContent", docDp) {
  85. private static final long serialVersionUID = 1L;
  86. int counter;
  87. @Override
  88. public void populateItem(final Item<MarkupDocument> item) {
  89. MarkupDocument doc = item.getModelObject();
  90. // document page links
  91. item.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
  92. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  93. item.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  94. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  95. item.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  96. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  97. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, doc.documentPath);
  98. item.add(new ExternalLink("rawLink", rawUrl));
  99. // document content
  100. String file = StringUtils.getLastPathElement(doc.documentPath);
  101. file = StringUtils.stripFileExtension(file);
  102. Component content = new Label("content", doc.html)
  103. .setEscapeModelStrings(false);
  104. if (!MarkupSyntax.PLAIN.equals(doc.syntax)) {
  105. content.add(new SimpleAttributeModifier("class", "markdown"));
  106. }
  107. item.add(content);
  108. item.add(new SimpleAttributeModifier("id", file));
  109. if (counter == 0) {
  110. counter++;
  111. item.add(new SimpleAttributeModifier("class", "tab-pane active"));
  112. }
  113. }
  114. };
  115. fragment.add(tabsView);
  116. }
  117. // document list
  118. final ByteFormat byteFormat = new ByteFormat();
  119. Fragment docs = new Fragment("documents", "documentsFragment", this);
  120. ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
  121. DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
  122. private static final long serialVersionUID = 1L;
  123. int counter;
  124. @Override
  125. public void populateItem(final Item<PathModel> item) {
  126. PathModel entry = item.getModelObject();
  127. item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
  128. item.add(new Label("docSize", byteFormat.format(entry.size)));
  129. item.add(new LinkPanel("docName", "list", StringUtils.stripFileExtension(entry.name),
  130. DocPage.class, WicketUtils.newPathParameter(repositoryName, commitId, entry.path)));
  131. // links
  132. item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
  133. .newPathParameter(repositoryName, commitId, entry.path)));
  134. item.add(new BookmarkablePageLink<Void>("edit", EditFilePage.class, WicketUtils
  135. .newPathParameter(repositoryName, commitId, entry.path)));
  136. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, entry.path);
  137. item.add(new ExternalLink("raw", rawUrl));
  138. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  139. .newPathParameter(repositoryName, commitId, entry.path)));
  140. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  141. .newPathParameter(repositoryName, commitId, entry.path)));
  142. WicketUtils.setAlternatingBackground(item, counter);
  143. counter++;
  144. }
  145. };
  146. docs.add(pathsView);
  147. fragment.add(docs);
  148. add(fragment);
  149. }
  150. @Override
  151. protected String getPageName() {
  152. return getString("gb.docs");
  153. }
  154. @Override
  155. protected boolean isCommitPage() {
  156. return true;
  157. }
  158. }