Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend refactor: move Vue related code from index.js to components dir, and remove unused codes. #17301

Merged
merged 14 commits into from
Oct 15, 2021
Merged
2 changes: 0 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ reportUnusedDisableDirectives: true

ignorePatterns:
- /web_src/js/vendor
- /templates/repo/activity.tmpl
- /templates/repo/view_file.tmpl

parserOptions:
sourceType: module
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ _test

# IntelliJ
.idea
# Goland's output filename can not be set manually
/go_build_*

# MS VSCode
.vscode
Expand Down
53 changes: 53 additions & 0 deletions docs/content/doc/developers/guidelines-frontend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
date: "2021-10-13T16:00:00+02:00"
title: "Guidelines for Frontend Development"
slug: "guidelines-frontend"
weight: 20
toc: false
draft: false
menu:
sidebar:
parent: "developers"
name: "Guidelines for Frontend"
weight: 20
identifier: "guidelines-frontend"
---

# Guidelines for Frontend Development

**Table of Contents**

{{< toc >}}

## Background

Gitea uses [Less CSS](https://lesscss.org), [Fomantic-UI](https://fomantic-ui.com/introduction/getting-started.html) (based on [jQuery](https://api.jquery.com)) and [Vue2](https://vuejs.org/v2/guide/) for its frontend.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

The HTML pages are rendered by [Go Text Template](https://pkg.go.dev/text/template)
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

## General Guidelines

We recommend [Google HTML/CSS Style Guide](https://google.github.io/styleguide/htmlcssguide.html) and [Google JavaScript Style Guide](https://google.github.io/styleguide/jsguide.html)
6543 marked this conversation as resolved.
Show resolved Hide resolved

Guidelines specialized for Gitea:
6543 marked this conversation as resolved.
Show resolved Hide resolved

1. Every feature (Fomantic-UI/jQuery module) should be put in separated files/directories.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
2. HTML id/css-class-name should use kebab-case.
3. HTML id/css-class-name used by JavaScript top-level selector should be unique in whole project,
and should contain 2-3 feature related keywords. Recommend to use `js-` prefix for CSS names for JavaScript usage only.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
4. jQuery events across different features should use their own namespaces.
5. CSS styles provided by frameworks should not be overwritten by framework's selectors globally.
Always use new defined CSS names to overwrite framework styles. Recommend to use `us-` prefix for user defined styles.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
6. Backend can pass data to frontend (JavaScript) by `ctx.PageData["myModuleData"] = map{}`
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
7. Simple pages and SEO-related pages use Go Text Template render to generate static Fomantic-UI HTML output. Complex pages can use Vue2 (or Vue3 in future).
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

## Legacy Problems and Solutions

### Too many codes in `web_src/index.js`
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

In history, many JavaScript codes are written into `web_src/index.js` directly, which becomes too big to maintain.
We should split this file into small modules, the separated files can be put in `web_src/js/features` for the first step.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

### Vue2/Vue3 and JSX

Gitea is using Vue2 now, we have plan to upgrade to Vue3. We decide not to introduce JSX now to make sure the HTML and JavaScript codes are not mixed together.
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 10 additions & 1 deletion docs/content/doc/developers/hacking-on-gitea.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ See `make help` for all available `make` targets. Also see [`.drone.yml`](https:
To run and continuously rebuild when source files change:

```bash
# for both frontend and backend
make watch

# or: watch frontend files (html/js/css) only
make watch-frontend

# or: watch backend files (go) only
make watch-backend
```

On macOS, watching all backend source files may hit the default open files limit which can be increased via `ulimit -n 12288` for the current shell or in your shell startup file for all future shells.
Expand Down Expand Up @@ -167,7 +174,9 @@ make revive vet misspell-check

### Working on JS and CSS

Either use the `watch-frontend` target mentioned above or just build once:
Frontend development should follow [Guidelines for Frontend Development](./guidelines-frontend.md)

To build with frontend resources, either use the `watch-frontend` target mentioned above or just build once:
6543 marked this conversation as resolved.
Show resolved Hide resolved

```bash
make build && ./gitea
Expand Down
1 change: 1 addition & 0 deletions modules/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ func Contexter() func(next http.Handler) http.Handler {
"CurrentURL": setting.AppSubURL + req.URL.RequestURI(),
"PageStartTime": startTime,
"Link": link,
"IsProd": setting.IsProd(),
},
}
// PageData is passed by reference, and it will be rendered to `window.config.PageData` in `head.tmpl` for JavaScript modules
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func Activity(ctx *context.Context) {
return
}

if ctx.Data["ActivityTopAuthors"], err = models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10); err != nil {
if ctx.PageData["repoActivityTopAuthors"], err = models.GetActivityStatsTopAuthors(ctx.Repo.Repository, timeFrom, 10); err != nil {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
ctx.ServerError("GetActivityStatsTopAuthors", err)
return
}
Expand Down
5 changes: 4 additions & 1 deletion routers/web/user/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func Dashboard(ctx *context.Context) {
ctx.Data["Title"] = ctxUser.DisplayName() + " - " + ctx.Tr("dashboard")
ctx.Data["PageIsDashboard"] = true
ctx.Data["PageIsNews"] = true
ctx.Data["SearchLimit"] = setting.UI.User.RepoPagingNum

ctx.PageData["dashboardRepoList"] = map[string]interface{}{
"searchLimit": setting.UI.User.RepoPagingNum,
}
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved

if setting.Service.EnableUserHeatmap {
data, err := models.GetUserHeatmapDataByUserTeam(ctxUser, ctx.Org.Team, ctx.User)
Expand Down
11 changes: 1 addition & 10 deletions templates/base/head.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<!DOCTYPE html>
<html lang="{{.Lang}}" class="theme-{{.SignedUser.Theme}}">
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
<head data-suburl="{{AppSubUrl}}">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{if .Title}}{{.Title | RenderEmojiPlain}} - {{end}} {{if .Repository.Name}}{{.Repository.Name}} - {{end}}{{AppName}} </title>
Expand All @@ -12,15 +11,6 @@
<meta name="keywords" content="{{MetaKeywords}}">
<meta name="referrer" content="no-referrer" />
<meta name="_csrf" content="{{.CsrfToken}}" />
{{if .IsSigned}}
<meta name="_uid" content="{{.SignedUser.ID}}" />
{{end}}
{{if .ContextUser}}
<meta name="_context_uid" content="{{.ContextUser.ID}}" />
{{end}}
{{if .SearchLimit}}
<meta name="_search_limit" content="{{.SearchLimit}}" />
{{end}}
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
{{if .GoGetImport}}
<meta name="go-import" content="{{.GoGetImport}} git {{.CloneLink.HTTPS}}">
<meta name="go-source" content="{{.GoGetImport}} _ {{.GoDocDirectory}} {{.GoDocFile}}">
Expand All @@ -31,6 +21,7 @@
AppVer: '{{AppVer}}',
AppSubUrl: '{{AppSubUrl}}',
AssetUrlPrefix: '{{AssetUrlPrefix}}',
IsProd: {{.IsProd}},
CustomEmojis: {{CustomEmojis}},
UseServiceWorker: {{UseServiceWorker}},
csrf: '{{.CsrfToken}}',
Expand Down
19 changes: 8 additions & 11 deletions templates/repo/activity.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@
{{.i18n.Tr "repo.activity.git_stats_and_deletions" }}
<strong class="text red">{{.i18n.Tr (TrN .i18n.Lang .Activity.Code.Deletions "repo.activity.git_stats_deletion_1" "repo.activity.git_stats_deletion_n") .Activity.Code.Deletions }}</strong>.
</div>
<div class="ui attached segment" id="app">
<script type="text/javascript">
var ActivityTopAuthors = {{Json .ActivityTopAuthors | SafeJS}};
</script>
<activity-top-authors :data="activityTopAuthors" />
<div class="ui attached segment">
<div id="repo-activity-top-authors-chart"></div>
</div>
</div>
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
{{end}}
Expand All @@ -126,7 +123,7 @@
<div class="list">
{{range .Activity.PublishedReleases}}
<p class="desc">
<div class="ui green label">{{$.i18n.Tr "repo.activity.published_release_label"}}</div>
<span class="ui green label">{{$.i18n.Tr "repo.activity.published_release_label"}}</span>
{{.TagName}}
{{if not .IsTag}}
<a class="title" href="{{$.RepoLink}}/src/{{.TagName | EscapePound}}">{{.Title | RenderEmoji}}</a>
Expand All @@ -145,7 +142,7 @@
<div class="list">
{{range .Activity.MergedPRs}}
<p class="desc">
<div class="ui purple label">{{$.i18n.Tr "repo.activity.merged_prs_label"}}</div>
<span class="ui purple label">{{$.i18n.Tr "repo.activity.merged_prs_label"}}</span>
#{{.Index}} <a class="title" href="{{$.RepoLink}}/pulls/{{.Index}}">{{.Issue.Title | RenderEmoji}}</a>
{{TimeSinceUnix .MergedUnix $.Lang}}
</p>
Expand All @@ -161,7 +158,7 @@
<div class="list">
{{range .Activity.OpenedPRs}}
<p class="desc">
<div class="ui green label">{{$.i18n.Tr "repo.activity.opened_prs_label"}}</div>
<span class="ui green label">{{$.i18n.Tr "repo.activity.opened_prs_label"}}</span>
#{{.Index}} <a class="title" href="{{$.RepoLink}}/pulls/{{.Index}}">{{.Issue.Title | RenderEmoji}}</a>
{{TimeSinceUnix .Issue.CreatedUnix $.Lang}}
</p>
Expand All @@ -177,7 +174,7 @@
<div class="list">
{{range .Activity.ClosedIssues}}
<p class="desc">
<div class="ui red label">{{$.i18n.Tr "repo.activity.closed_issue_label"}}</div>
<span class="ui red label">{{$.i18n.Tr "repo.activity.closed_issue_label"}}</span>
#{{.Index}} <a class="title" href="{{$.RepoLink}}/issues/{{.Index}}">{{.Title | RenderEmoji}}</a>
{{TimeSinceUnix .ClosedUnix $.Lang}}
</p>
Expand All @@ -193,7 +190,7 @@
<div class="list">
{{range .Activity.OpenedIssues}}
<p class="desc">
<div class="ui green label">{{$.i18n.Tr "repo.activity.new_issue_label"}}</div>
<span class="ui green label">{{$.i18n.Tr "repo.activity.new_issue_label"}}</span>
#{{.Index}} <a class="title" href="{{$.RepoLink}}/issues/{{.Index}}">{{.Title | RenderEmoji}}</a>
{{TimeSinceUnix .CreatedUnix $.Lang}}
</p>
Expand All @@ -212,7 +209,7 @@
<div class="list">
{{range .Activity.UnresolvedIssues}}
<p class="desc">
<div class="ui green label">{{$.i18n.Tr "repo.activity.unresolved_conv_label"}}</div>
<span class="ui green label">{{$.i18n.Tr "repo.activity.unresolved_conv_label"}}</span>
#{{.Index}}
{{if .IsPull}}
<a class="title" href="{{$.RepoLink}}/pulls/{{.Index}}">{{.Title | RenderEmoji}}</a>
Expand Down
10 changes: 0 additions & 10 deletions templates/repo/view_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,3 @@
</div>
</div>
</div>

<script>
function submitDeleteForm() {
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
var message = prompt("{{.i18n.Tr "repo.delete_confirm_message"}}\n\n{{.i18n.Tr "repo.delete_commit_summary"}}", "Delete '{{.TreeName}}'");
if (message != null) {
$("#delete-message").val(message);
$("#delete-file-form").submit()
}
}
</script>
13 changes: 6 additions & 7 deletions templates/user/dashboard/repolist.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<div id="app" class="six wide column">
<div id="dashboard-repo-list" class="six wide column">
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
<repo-search
:search-limit="searchLimit"
:suburl="suburl"
:uid="uid"
:sub-url="subUrl"
{{if .Team}}
:team-id="{{.Team.ID}}"
{{end}}
Expand Down Expand Up @@ -31,7 +30,7 @@
{{.i18n.Tr "home.my_repos"}}
<span class="ui grey label ml-3">${reposTotalCount}</span>
</div>
<a class="poping up" :href="suburl + '/repo/create'" data-content="{{.i18n.Tr "new_repo"}}" data-variation="tiny inverted" data-position="left center">
<a class="poping up" :href="subUrl + '/repo/create'" data-content="{{.i18n.Tr "new_repo"}}" data-variation="tiny inverted" data-position="left center">
{{svg "octicon-plus"}}
<span class="sr-only">{{.i18n.Tr "new_repo"}}</span>
</a>
Expand Down Expand Up @@ -122,7 +121,7 @@
<div v-if="repos.length" class="ui attached table segment rounded-bottom">
<ul class="repo-owner-name-list">
<li v-for="repo in repos" :class="{'private': repo.private || repo.internal}">
<a class="repo-list-link df ac sb" :href="suburl + '/' + repo.full_name">
<a class="repo-list-link df ac sb" :href="subUrl + '/' + repo.full_name">
<div class="text truncate item-name f1">
<component v-bind:is="repoIcon(repo)" size="16"></component>
<strong>${repo.full_name}</strong>
Expand Down Expand Up @@ -168,15 +167,15 @@
{{.i18n.Tr "home.my_orgs"}}
<span class="ui grey label ml-3">${organizationsTotalCount}</span>
</div>
<a v-if="canCreateOrganization" class="poping up" :href="suburl + '/org/create'" data-content="{{.i18n.Tr "new_org"}}" data-variation="tiny inverted" data-position="left center">
<a v-if="canCreateOrganization" class="poping up" :href="subUrl + '/org/create'" data-content="{{.i18n.Tr "new_org"}}" data-variation="tiny inverted" data-position="left center">
{{svg "octicon-plus"}}
<span class="sr-only">{{.i18n.Tr "new_org"}}</span>
</a>
</h4>
<div v-if="organizations.length" class="ui attached table segment rounded-bottom">
<ul class="repo-owner-name-list">
<li v-for="org in organizations">
<a class="repo-list-link df ac sb" :href="suburl + '/' + org.name">
<a class="repo-list-link df ac sb" :href="subUrl + '/' + org.name">
<div class="text truncate item-name f1">
{{svg "octicon-organization" 16 "mr-2"}}
<strong>${org.name}</strong>
Expand Down
1 change: 0 additions & 1 deletion web_src/js/components/ActivityHeatmap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ export default {
},
};
</script>
<style scoped/>
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
Loading