Browse Source

Merge pull request 'feature/128_toggle_trust_prop_checkbox' (#150) from feature/128_toggle_trust_prop_checkbox into dev_mirocod

Reviewed-on: #150
dev_mirocod
parent
commit
166c8818b9
  1. 25
      routers/web/repo/competence.go
  2. 31
      routers/web/repo/resource.go
  3. 2
      routers/web/web.go
  4. 54
      services/trust_props/trust_props.go
  5. 4
      templates/repo/competences/list.tmpl
  6. 4
      templates/repo/resources/list.tmpl
  7. 3
      web_src/js/index.js
  8. 5
      web_src/js/markup/content.js
  9. 18
      web_src/js/markup/trust_props.js

25
routers/web/repo/competence.go

@ -3,6 +3,7 @@ package repo
import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/services/trust_props"
"net/http"
)
@ -15,7 +16,7 @@ func Competences(ctx *context.Context) {
repo := ctx.Data["Repository"]
var err error
ctx.Data["RenderedCompetences"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Competences")
ctx.Data["RenderedCompetences"], ctx.Data["TransformedTrustProps"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Competences")
if err != nil {
ctx.ServerError("Render", err)
@ -24,3 +25,25 @@ func Competences(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplCompetences)
}
func ToggleCompetences(ctx *context.Context) {
if !ctx.IsSigned {
ctx.Error(http.StatusForbidden)
return
}
toggledContent, err := trust_props.SaveRepoAndRenderContent(ctx, "competences")
if err != nil {
ctx.ServerError("ToggleCompetences", err)
return
}
responseBody := map[string]interface{}{
"content": toggledContent,
}
ctx.JSON(
http.StatusOK,
responseBody,
)
}

31
routers/web/repo/resource.go

@ -4,6 +4,7 @@ import (
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/routers/web/user"
"code.gitea.io/gitea/services/trust_props"
"fmt"
"net/http"
"regexp"
@ -19,7 +20,7 @@ func Resources(ctx *context.Context) {
repo := ctx.Data["Repository"]
var err error
ctx.Data["RenderedResources"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Resources")
ctx.Data["RenderedResources"], ctx.Data["TransformedTrustProps"], err = GetRenderedTrustPropsWithSearchLinks(ctx, repo, "Resources")
if err != nil {
ctx.ServerError("Render", err)
@ -29,7 +30,29 @@ func Resources(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplResources)
}
func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, error) {
func ToggleResources(ctx *context.Context) {
if !ctx.IsSigned {
ctx.Error(http.StatusForbidden)
return
}
toggledContent, err := trust_props.SaveRepoAndRenderContent(ctx, "resources")
if err != nil {
ctx.ServerError("ToggleCompetences", err)
return
}
responseBody := map[string]interface{}{
"content": toggledContent,
}
ctx.JSON(
http.StatusOK,
responseBody,
)
}
func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}, fieldName string) (string, string, error) {
trustProps := user.GetTextField(repo, fieldName)
var regExp *regexp.Regexp
@ -37,7 +60,7 @@ func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}
regExp, err = regexp.Compile(`- \[ \] (.+)`)
if err != nil {
return "", err
return "", "", err
}
trustPropNamesMatches := regExp.FindAllStringSubmatch(trustProps, -1)
@ -66,5 +89,5 @@ func GetRenderedTrustPropsWithSearchLinks(ctx *context.Context, repo interface{}
var renderedTrustPropsWithSafeURLs string
renderedTrustPropsWithSafeURLs, err = user.GetRenderedTextFieldByValue(ctx, repo, transformedTrustProps)
renderedTrustPropsWithTargetBlank := strings.ReplaceAll(renderedTrustPropsWithSafeURLs, "<a", "<a target='blank'")
return renderedTrustPropsWithTargetBlank, err
return renderedTrustPropsWithTargetBlank, transformedTrustProps, err
}

2
routers/web/web.go

@ -891,7 +891,9 @@ func RegisterRoutes(m *web.Route) {
m.Get("/{type:issues|pulls}", repo.Issues)
m.Get("/issues_tree", repo.IssuesTree)
m.Get("/resources", repo.Resources)
m.Post("/resources", repo.ToggleResources)
m.Get("/competences", repo.Competences)
m.Post("/competences", repo.ToggleCompetences)
m.Get("/{type:issues|pulls}/{index}", repo.ViewIssue)
m.Group("/{type:issues|pulls}/{index}/content-history", func() {
m.Get("/overview", repo.GetContentHistoryOverview)

54
services/trust_props/trust_props.go

@ -0,0 +1,54 @@
package trust_props
import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"reflect"
"regexp"
)
func SaveRepoAndRenderContent(ctx *context.Context, colName string) (string, error) {
repo := ctx.Repo.Repository
toggledContent := ctx.FormString("content")
content, err := markdown.RenderString(&markup.RenderContext{
URLPrefix: ctx.FormString("context"), // FIXME: <- IS THIS SAFE ?
Metas: ctx.Repo.Repository.ComposeMetas(),
GitRepo: ctx.Repo.GitRepo,
Ctx: ctx,
}, toggledContent)
if err != nil {
return "", err
}
trimmedToggledContent, err := trimSearchLinks(toggledContent)
if err != nil {
return "", err
}
fieldNames := map[string]string{
"resources": "Resources",
"competences": "Competences",
}
fieldName := fieldNames[colName]
reflectedRepo := reflect.ValueOf(repo)
reflectedRepo.Elem().FieldByName(fieldName).SetString(trimmedToggledContent)
err = repo_model.UpdateRepositoryCols(repo, colName)
if err != nil {
return "", err
}
return content, nil
}
func trimSearchLinks(content string) (string, error) {
regExp, err := regexp.Compile(`\s+\[найти\]\(.+\s*\)`)
if err != nil {
return "", err
}
trimmedSearchLink := regExp.ReplaceAllString(content, "")
return trimmedSearchLink, nil
}

4
templates/repo/competences/list.tmpl

@ -3,9 +3,11 @@
{{template "repo/header" .}}
<div class="ui container">
<h1>{{.i18n.Tr "repo.competences"}}</h1>
<div>
<div class="render-content markup trust-props" data-can-edit="true" data-trust-prop-type="competences">
{{$.RenderedCompetences | Str2html}}
</div>
<div class="raw-content hide">{{$.TransformedTrustProps}}</div>
<div class="edit-content-zone hide" data-update-url="{{$.RepoLink}}/competences" data-context="{{.RepoLink}}"></div>
</div>
</div>
{{template "base/footer" .}}

4
templates/repo/resources/list.tmpl

@ -3,9 +3,11 @@
{{template "repo/header" .}}
<div class="ui container">
<h1>{{.i18n.Tr "repo.resources"}}</h1>
<div>
<div class="render-content markup trust-props" data-can-edit="true" data-trust-prop-type="resources">
{{$.RenderedResources | Str2html}}
</div>
<div class="raw-content hide">{{$.TransformedTrustProps}}</div>
<div class="edit-content-zone hide" data-update-url="{{$.RepoLink}}/resources" data-context="{{.RepoLink}}"></div>
</div>
</div>
{{template "base/footer" .}}

3
web_src/js/index.js

@ -19,7 +19,7 @@ import {initMarkupAnchors} from './markup/anchors.js';
import {initNotificationCount, initNotificationsTable} from './features/notification.js';
import {initRepoIssueContentHistory} from './features/repo-issue-content.js';
import {initStopwatch} from './features/stopwatch.js';
import {initCommentContent, initMarkupContent} from './markup/content.js';
import {initCommentContent, initMarkupContent, initTrustPropsContent} from './markup/content.js';
import {initUserAuthLinkAccountView, initUserAuthOauth2} from './features/user-auth.js';
import {
@ -105,6 +105,7 @@ $(document).ready(() => {
initFootLanguageMenu();
initCommentContent();
initTrustPropsContent()
initContextPopups();
initHeatmap();
initImageDiff();

5
web_src/js/markup/content.js

@ -1,6 +1,7 @@
import {renderMermaid} from './mermaid.js';
import {renderCodeCopy} from './codecopy.js';
import {initMarkupTasklist} from './tasklist.js';
import {initMarkupTrustProps} from './trust_props.js'
// code that runs for all markup content
export function initMarkupContent() {
@ -12,3 +13,7 @@ export function initMarkupContent() {
export function initCommentContent() {
initMarkupTasklist();
}
export function initTrustPropsContent() {
initMarkupTrustProps();
}

18
web_src/js/markup/trust_props.js

@ -0,0 +1,18 @@
export function initMarkupTrustProps() {
const renderSearchLink = (trustPropName, trustPropType) => {
const safeName = trustPropName.replace(' ', '+');
return`&nbsp;<a target="blank" href="/explore/${trustPropType}?tab=&amp;q=${safeName}" rel="nofollow">найти</a>`;
}
$('.trust-props input[type="checkbox"]').click((e) => {
if (e.target.checked) {
$(e.target).parent().find('a').remove();
} else {
const $parent = $(e.target).parent();
const $listContainer = $parent.parents("div.render-content");
const trustPropType = $listContainer.attr("data-trust-prop-type");
const renderedSearchLink = renderSearchLink($parent.text(), trustPropType);
$parent.append(renderedSearchLink);
}
});
}
Loading…
Cancel
Save