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