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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.apache.wicket.model.StringResourceModel;
  29. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  30. import org.eclipse.jgit.lib.Repository;
  31. import org.eclipse.jgit.revwalk.RevCommit;
  32. import com.gitblit.models.PathModel;
  33. import com.gitblit.models.UserModel;
  34. import com.gitblit.servlet.RawServlet;
  35. import com.gitblit.utils.ByteFormat;
  36. import com.gitblit.utils.JGitUtils;
  37. import com.gitblit.utils.StringUtils;
  38. import com.gitblit.wicket.CacheControl;
  39. import com.gitblit.wicket.GitBlitWebSession;
  40. import com.gitblit.wicket.CacheControl.LastModified;
  41. import com.gitblit.wicket.MarkupProcessor;
  42. import com.gitblit.wicket.MarkupProcessor.MarkupDocument;
  43. import com.gitblit.wicket.MarkupProcessor.MarkupSyntax;
  44. import com.gitblit.wicket.WicketUtils;
  45. import com.gitblit.wicket.panels.LinkPanel;
  46. @CacheControl(LastModified.REPOSITORY)
  47. public class DocsPage extends RepositoryPage {
  48. public DocsPage(PageParameters params) {
  49. super(params);
  50. String objectId = WicketUtils.getObject(params);
  51. MarkupProcessor processor = new MarkupProcessor(app().settings(), app().xssFilter());
  52. Repository r = getRepository();
  53. UserModel currentUser = (GitBlitWebSession.get().getUser() != null) ? GitBlitWebSession.get().getUser() : UserModel.ANONYMOUS;
  54. final boolean userCanEdit = currentUser.canEdit(getRepositoryModel());
  55. RevCommit head = JGitUtils.getCommit(r, objectId);
  56. if (head == null) {
  57. setResponsePage(NoDocsPage.class, params);
  58. return;
  59. }
  60. final String commitId = getBestCommitId(head);
  61. List<String> extensions = processor.getAllExtensions();
  62. List<PathModel> paths = JGitUtils.getDocuments(r, extensions);
  63. List<MarkupDocument> roots = processor.getRootDocs(r, repositoryName, commitId);
  64. Fragment fragment = null;
  65. if (roots.isEmpty()) {
  66. // no identified root documents
  67. fragment = new Fragment("docs", "noIndexFragment", this);
  68. setResponsePage(NoDocsPage.class, params);
  69. } else {
  70. // root documents, use tabbed ui of index/root and document list
  71. fragment = new Fragment("docs", "tabsFragment", this);
  72. ListDataProvider<MarkupDocument> docDp = new ListDataProvider<MarkupDocument>(roots);
  73. // tab titles
  74. DataView<MarkupDocument> tabTitles = new DataView<MarkupDocument>("tabTitle", docDp) {
  75. private static final long serialVersionUID = 1L;
  76. int counter;
  77. @Override
  78. public void populateItem(final Item<MarkupDocument> item) {
  79. MarkupDocument doc = item.getModelObject();
  80. String file = StringUtils.getLastPathElement(doc.documentPath);
  81. file = StringUtils.stripFileExtension(file);
  82. String name = file.replace('_', ' ').replace('-', ' ');
  83. ExternalLink link = new ExternalLink("link", "#" + file);
  84. link.add(new Label("label", name.toUpperCase()).setRenderBodyOnly(true));
  85. item.add(link);
  86. if (counter == 0) {
  87. counter++;
  88. item.add(new SimpleAttributeModifier("class", "active"));
  89. }
  90. }
  91. };
  92. fragment.add(tabTitles);
  93. // tab content
  94. DataView<MarkupDocument> tabsView = new DataView<MarkupDocument>("tabContent", docDp) {
  95. private static final long serialVersionUID = 1L;
  96. int counter;
  97. @Override
  98. public void populateItem(final Item<MarkupDocument> item) {
  99. MarkupDocument doc = item.getModelObject();
  100. item.add(new BookmarkablePageLink<Void>("editLink", EditFilePage.class,
  101. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath))
  102. .setEnabled(userCanEdit));
  103. // document page links
  104. item.add(new BookmarkablePageLink<Void>("blameLink", BlamePage.class,
  105. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  106. item.add(new BookmarkablePageLink<Void>("historyLink", HistoryPage.class,
  107. WicketUtils.newPathParameter(repositoryName, commitId, doc.documentPath)));
  108. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, doc.documentPath);
  109. item.add(new ExternalLink("rawLink", rawUrl));
  110. // document content
  111. String file = StringUtils.getLastPathElement(doc.documentPath);
  112. file = StringUtils.stripFileExtension(file);
  113. Component content = new Label("content", doc.html)
  114. .setEscapeModelStrings(false);
  115. if (!MarkupSyntax.PLAIN.equals(doc.syntax)) {
  116. content.add(new SimpleAttributeModifier("class", "markdown"));
  117. }
  118. item.add(content);
  119. item.add(new SimpleAttributeModifier("id", file));
  120. if (counter == 0) {
  121. counter++;
  122. item.add(new SimpleAttributeModifier("class", "tab-pane active"));
  123. }
  124. }
  125. };
  126. fragment.add(tabsView);
  127. }
  128. // document list
  129. final ByteFormat byteFormat = new ByteFormat();
  130. Fragment docs = new Fragment("documents", "documentsFragment", this);
  131. ListDataProvider<PathModel> pathsDp = new ListDataProvider<PathModel>(paths);
  132. DataView<PathModel> pathsView = new DataView<PathModel>("document", pathsDp) {
  133. private static final long serialVersionUID = 1L;
  134. int counter;
  135. @Override
  136. public void populateItem(final Item<PathModel> item) {
  137. PathModel entry = item.getModelObject();
  138. item.add(WicketUtils.newImage("docIcon", "file_world_16x16.png"));
  139. item.add(new Label("docSize", byteFormat.format(entry.size)));
  140. item.add(new LinkPanel("docName", "list", StringUtils.stripFileExtension(entry.name),
  141. DocPage.class, WicketUtils.newPathParameter(repositoryName, commitId, entry.path)));
  142. // links
  143. item.add(new BookmarkablePageLink<Void>("view", DocPage.class, WicketUtils
  144. .newPathParameter(repositoryName, commitId, entry.path)));
  145. item.add(new BookmarkablePageLink<Void>("edit", EditFilePage.class, WicketUtils
  146. .newPathParameter(repositoryName, commitId, entry.path))
  147. .setEnabled(userCanEdit));
  148. String rawUrl = RawServlet.asLink(getContextUrl(), repositoryName, commitId, entry.path);
  149. item.add(new ExternalLink("raw", rawUrl));
  150. item.add(new BookmarkablePageLink<Void>("blame", BlamePage.class, WicketUtils
  151. .newPathParameter(repositoryName, commitId, entry.path)));
  152. item.add(new BookmarkablePageLink<Void>("history", HistoryPage.class, WicketUtils
  153. .newPathParameter(repositoryName, commitId, entry.path)));
  154. WicketUtils.setAlternatingBackground(item, counter);
  155. counter++;
  156. }
  157. };
  158. docs.add(pathsView);
  159. fragment.add(docs);
  160. add(fragment);
  161. }
  162. @Override
  163. protected String getPageName() {
  164. return getString("gb.docs");
  165. }
  166. @Override
  167. protected boolean isCommitPage() {
  168. return true;
  169. }
  170. }