James Moger
2015-11-22 ed552ba47c02779c270ffd62841d6d1048dade70
commit | author | age
430496 1 /*
JM 2  * Copyright 2013 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.panels;
17
18 import java.io.Serializable;
19 import java.text.DateFormat;
20 import java.text.MessageFormat;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.wicket.behavior.HeaderContributor;
28 import org.apache.wicket.markup.html.basic.Label;
29
30 import com.gitblit.Keys;
31 import com.gitblit.models.RepositoryModel;
32 import com.gitblit.utils.StringUtils;
33 import com.gitblit.wicket.WicketUtils;
34 import com.gitblit.wicket.freemarker.FreemarkerPanel;
35 import com.gitblit.wicket.ng.NgController;
36
37 /**
38  * A client-side filterable rich repository list which uses Freemarker, Wicket,
699e71 39  * and AngularJS.
JM 40  *
430496 41  * @author James Moger
JM 42  *
43  */
44 public class FilterableRepositoryList extends BasePanel {
45
46     private static final long serialVersionUID = 1L;
47
48     private final List<RepositoryModel> repositories;
699e71 49
430496 50     private String title;
699e71 51
430496 52     private String iconClass;
699e71 53
430496 54     private boolean allowCreate;
699e71 55
430496 56     public FilterableRepositoryList(String id, List<RepositoryModel> repositories) {
JM 57         super(id);
58         this.repositories = repositories;
59     }
699e71 60
430496 61     public void setTitle(String title, String iconClass) {
JM 62         this.title = title;
63         this.iconClass = iconClass;
64     }
699e71 65
430496 66     public void setAllowCreate(boolean value) {
JM 67         this.allowCreate = value;
68     }
69
70     @Override
71     protected void onInitialize() {
72         super.onInitialize();
73
74         String id = getId();
75         String ngCtrl = id + "Ctrl";
76         String ngList = id + "List";
699e71 77
430496 78         Map<String, Object> values = new HashMap<String, Object>();
JM 79         values.put("ngCtrl",  ngCtrl);
80         values.put("ngList",  ngList);
699e71 81
430496 82         // use Freemarker to setup an AngularJS/Wicket html snippet
JM 83         FreemarkerPanel panel = new FreemarkerPanel("listComponent", "FilterableRepositoryList.fm", values);
84         panel.setParseGeneratedMarkup(true);
85         panel.setRenderBodyOnly(true);
86         add(panel);
699e71 87
JM 88         // add the Wicket controls that are referenced in the snippet
430496 89         String listTitle = StringUtils.isEmpty(title) ? getString("gb.repositories") : title;
JM 90         panel.add(new Label(ngList + "Title", MessageFormat.format("{0} ({1})", listTitle, repositories.size())));
91         if (StringUtils.isEmpty(iconClass)) {
92             panel.add(new Label(ngList + "Icon").setVisible(false));
93         } else {
94             Label icon = new Label(ngList + "Icon");
95             WicketUtils.setCssClass(icon, iconClass);
96             panel.add(icon);
97         }
699e71 98
430496 99         if (allowCreate) {
0047fb 100             panel.add(new LinkPanel(ngList + "Button", "btn btn-mini", getString("gb.newRepository"), app().getNewRepositoryPage()));
430496 101         } else {
JM 102             panel.add(new Label(ngList + "Button").setVisible(false));
103         }
699e71 104
99d0d4 105         String format = app().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy");
430496 106         final DateFormat df = new SimpleDateFormat(format);
JM 107         df.setTimeZone(getTimeZone());
108
109         // prepare the simplified repository models list
110         List<RepoListItem> list = new ArrayList<RepoListItem>();
111         for (RepositoryModel repo : repositories) {
699e71 112             String name = StringUtils.stripDotGit(repo.name);
430496 113             String path = "";
JM 114             if (name.indexOf('/') > -1) {
115                 path = name.substring(0, name.lastIndexOf('/') + 1);
116                 name = name.substring(name.lastIndexOf('/') + 1);
117             }
699e71 118
430496 119             RepoListItem item = new RepoListItem();
JM 120             item.n = name;
121             item.p = path;
122             item.r = repo.name;
123             item.i = repo.description;
99d0d4 124             item.s = app().repositories().getStarCount(repo);
430496 125             item.t = getTimeUtils().timeAgo(repo.lastChange);
JM 126             item.d = df.format(repo.lastChange);
127             item.c = StringUtils.getColor(StringUtils.stripDotGit(repo.name));
86bdc2 128             if (!repo.isBare) {
JM 129                 item.y = 3;
130             } else if (repo.isMirror) {
131                 item.y = 2;
132             } else if (repo.isFork()) {
133                 item.y = 1;
134             } else {
135                 item.y = 0;
136             }
430496 137             list.add(item);
JM 138         }
699e71 139
430496 140         // inject an AngularJS controller with static data
JM 141         NgController ctrl = new NgController(ngCtrl);
142         ctrl.addVariable(ngList, list);
143         add(new HeaderContributor(ctrl));
144     }
699e71 145
430496 146     protected class RepoListItem implements Serializable {
JM 147
148         private static final long serialVersionUID = 1L;
699e71 149
430496 150         String r; // repository
JM 151         String n; // name
152         String p; // project/path
153         String t; // time ago
154         String d; // last updated
155         String i; // information/description
156         long s;   // stars
157         String c; // html color
86bdc2 158         int y;    // type: 0 = normal, 1 = fork, 2 = mirror, 3 = clone
430496 159     }
JM 160 }