Browse Source

UI: CURD labels

- fix update lable break connection with repository
Unknwon 9 years ago
parent
commit
dc4aab9925

+ 11 - 2
conf/locale/locale_en-US.ini

@@ -143,6 +143,11 @@ invalid_code = Sorry, your confirmation code has expired or not valid.
 reset_password_helper = Click here to reset your password
 password_too_short = Password length cannot be less then 6.
 
+[modal]
+yes = Yes
+no = No
+modify = Modify
+
 [form]
 UserName = Username
 RepoName = Repository name
@@ -372,14 +377,18 @@ issues.filter_type.assigned_to_you = Assigned to you
 issues.filter_type.created_by_you = Created by you
 issues.filter_type.mentioning_you = Mentioning you
 issues.opened_by = opened %s by <a href="/%[2]s">%[2]s</a>
+issues.previous = Previous Page
+issues.next = Next Page
 issues.label_title = Label name
 issues.label_color = Label color
 issues.label_count = %d labels
 issues.label_open_issues = %d open issues
 issues.label_edit = Edit
 issues.label_delete = Delete
-issues.previous = Previous Page
-issues.next = Next Page
+issues.label_modify = Label Modification
+issues.label_deletion = Label Deletion
+issues.label_deletion_desc = Delete label will remove it's information in all related issues. Do you want to continue?
+issues.label_deletion_success = Label has been deleted successfully!
 
 settings = Settings
 settings.options = Options

+ 0 - 9
config.codekit

@@ -86,15 +86,6 @@
 		"randomFootnoteNumbers": 0,
 		"useCompatibilityMode": 0
 		},
-	"\/public\/css\/bootstrap-colorpicker.min.css": {
-		"fileType": 16,
-		"ignore": 0,
-		"ignoreWasSetByUser": 0,
-		"inputAbbreviatedPath": "\/public\/css\/bootstrap-colorpicker.min.css",
-		"outputAbbreviatedPath": "No Output Path",
-		"outputPathIsOutsideProject": 0,
-		"outputPathIsSetByUser": 0
-		},
 	"\/public\/css\/bootstrap.min.css": {
 		"fileType": 16,
 		"ignore": 0,

+ 7 - 11
models/issue.go

@@ -242,10 +242,9 @@ const (
 )
 
 // GetIssuesByLabel returns a list of issues by given label and repository.
-func GetIssuesByLabel(repoId int64, label string) ([]*Issue, error) {
+func GetIssuesByLabel(repoID, labelID int64) ([]*Issue, error) {
 	issues := make([]*Issue, 0, 10)
-	err := x.Where("repo_id=?", repoId).And("label_ids like '%$" + label + "|%'").Find(&issues)
-	return issues, err
+	return issues, x.Where("repo_id=?", repoID).And("label_ids like '%$" + com.ToStr(labelID) + "|%'").Find(&issues)
 }
 
 // GetIssueCountByPoster returns number of issues of repository by poster.
@@ -577,9 +576,8 @@ func UpdateLabel(l *Label) error {
 }
 
 // DeleteLabel delete a label of given repository.
-func DeleteLabel(repoId int64, strId string) error {
-	id, _ := com.StrTo(strId).Int64()
-	l, err := GetLabelById(id)
+func DeleteLabel(repoID, labelID int64) error {
+	l, err := GetLabelById(labelID)
 	if err != nil {
 		if err == ErrLabelNotExist {
 			return nil
@@ -587,27 +585,25 @@ func DeleteLabel(repoId int64, strId string) error {
 		return err
 	}
 
-	issues, err := GetIssuesByLabel(repoId, strId)
+	issues, err := GetIssuesByLabel(repoID, labelID)
 	if err != nil {
 		return err
 	}
 
 	sess := x.NewSession()
-	defer sess.Close()
+	defer sessionRelease(sess)
 	if err = sess.Begin(); err != nil {
 		return err
 	}
 
 	for _, issue := range issues {
-		issue.LabelIds = strings.Replace(issue.LabelIds, "$"+strId+"|", "", -1)
+		issue.LabelIds = strings.Replace(issue.LabelIds, "$"+com.ToStr(labelID)+"|", "", -1)
 		if _, err = sess.Id(issue.ID).AllCols().Update(issue); err != nil {
-			sess.Rollback()
 			return err
 		}
 	}
 
 	if _, err = sess.Delete(l); err != nil {
-		sess.Rollback()
 		return err
 	}
 	return sess.Commit()

+ 1 - 0
modules/auth/repo_form.go

@@ -134,6 +134,7 @@ func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors
 //         \/    \/    \/     \/
 
 type CreateLabelForm struct {
+	ID    int64
 	Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
 	Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
 }

File diff suppressed because it is too large
+ 0 - 0
modules/bindata/bindata.go


File diff suppressed because it is too large
+ 0 - 8
public/css/bootstrap-colorpicker.min.css


File diff suppressed because it is too large
+ 0 - 0
public/css/gogs.min.css


File diff suppressed because it is too large
+ 0 - 0
public/js/bootstrap-colorpicker.min.js


+ 36 - 4
public/js/gogs.js

@@ -1,3 +1,5 @@
+var csrf;
+
 function initInstall() {
     if ($('.install').length == 0) {
         return;
@@ -32,25 +34,55 @@ function initInstall() {
     });
 };
 
-function initRepository(){
+function initRepository() {
     if ($('.repository').length == 0) {
         return;
     }
 
-    if ($('.labels').length == 0) {
+    // Labels
+    if ($('.repository.labels').length == 0) {
         return;
     }
-    $('.color-picker').each( function() {
+    $('.color-picker').each(function () {
         $(this).minicolors();
     });
-    $('.precolors .color').click(function(){
+    $('.precolors .color').click(function () {
         var color_hex = $(this).data('color-hex')
         $('.color-picker').val(color_hex);
         $('.minicolors-swatch-color').css("background-color", color_hex);
     });
+    $('.delete-label-button').click(function () {
+        var $this = $(this);
+        $('.delete-label.modal').modal({
+            closable: false,
+            onApprove: function () {
+                $.post($this.data('url'), {
+                    "_csrf": csrf,
+                    "id": $this.data("id")
+                }).done(function (data) {
+                    window.location.href = data.redirect;
+                });
+            }
+        }).modal('show');
+        return false;
+    });
+    $('.edit-label-button').click(function () {
+        $('#label-modal-id').val($(this).data('id'));
+        $('#label-modal-title').val($(this).data('title'));
+        $('#label-modal-color').val($(this).data('color'))
+        $('.minicolors-swatch-color').css("background-color", $(this).data('color'));
+        $('.edit-label.modal').modal({
+            onApprove: function () {
+                $('.edit-label.form').submit();
+            }
+        }).modal('show');
+        return false;
+    });
 };
 
 $(document).ready(function () {
+    csrf = $('meta[name=_csrf]').attr("content");
+
     // Semantic UI modules.
     $('.dropdown').dropdown();
     $('.jump.dropdown').dropdown({

+ 28 - 0
public/less/_repository.less

@@ -137,4 +137,32 @@
 			}
 		}
 	}
+}
+
+.edit-label.modal {
+	.color-picker {
+		margin-top: -8px!important;
+		height: 35px;
+		width: auto!important;
+		padding-left: 30px!important;
+	}
+	.minicolors-swatch.minicolors-sprite {
+		top: 1px;
+		left: 10px;
+		width: 15px;
+		height: 15px;
+	}
+	.precolors {
+		margin-bottom: -11px!important;
+		padding-left: 0!important;
+		padding-right: 0!important;
+		margin-right: 10px!important;
+		width: 120px!important;
+		.color {
+			float: left;
+			margin: 0!important;
+			width: 15px;
+			height: 15px;
+		}
+	}
 }

+ 20 - 24
routers/repo/issue.go

@@ -936,44 +936,40 @@ func NewLabel(ctx *middleware.Context, form auth.CreateLabelForm) {
 }
 
 func UpdateLabel(ctx *middleware.Context, form auth.CreateLabelForm) {
-	id := com.StrTo(ctx.Query("id")).MustInt64()
-	if id == 0 {
-		ctx.Error(404)
+	l, err := models.GetLabelById(form.ID)
+	if err != nil {
+		switch err {
+		case models.ErrLabelNotExist:
+			ctx.Error(404)
+		default:
+			ctx.Handle(500, "UpdateLabel", err)
+		}
 		return
 	}
 
-	l := &models.Label{
-		ID:    id,
-		Name:  form.Title,
-		Color: form.Color,
-	}
+	l.Name = form.Title
+	l.Color = form.Color
 	if err := models.UpdateLabel(l); err != nil {
-		ctx.Handle(500, "issue.UpdateLabel(UpdateLabel)", err)
+		ctx.Handle(500, "UpdateLabel", err)
 		return
 	}
-	ctx.Redirect(ctx.Repo.RepoLink + "/issues")
+	ctx.Redirect(ctx.Repo.RepoLink + "/labels")
 }
 
 func DeleteLabel(ctx *middleware.Context) {
-	removes := ctx.Query("remove")
-	if len(strings.TrimSpace(removes)) == 0 {
-		ctx.JSON(200, map[string]interface{}{
-			"ok": true,
-		})
-		return
-	}
-
-	strIds := strings.Split(removes, ",")
-	for _, strId := range strIds {
-		if err := models.DeleteLabel(ctx.Repo.Repository.Id, strId); err != nil {
-			ctx.Handle(500, "issue.DeleteLabel(DeleteLabel)", err)
-			return
+	id := ctx.QueryInt64("id")
+	if id > 0 {
+		if err := models.DeleteLabel(ctx.Repo.Repository.Id, id); err != nil {
+			ctx.Flash.Error("DeleteLabel: " + err.Error())
+		} else {
+			ctx.Flash.Success(ctx.Tr("repo.issues.label_deletion_success"))
 		}
 	}
 
 	ctx.JSON(200, map[string]interface{}{
-		"ok": true,
+		"redirect": ctx.Repo.RepoLink + "/labels",
 	})
+	return
 }
 
 func Milestones(ctx *middleware.Context) {

+ 5 - 0
templates/base/alert.tmpl

@@ -2,4 +2,9 @@
 <div class="ui negative message">
   <p>{{.Flash.ErrorMsg}}</p>
 </div>
+{{end}}
+{{if .Flash.SuccessMsg}}
+<div class="ui positive message">
+  <p>{{.Flash.SuccessMsg}}</p>
+</div>
 {{end}}

+ 16 - 0
templates/repo/issue/label_precolors.tmpl

@@ -0,0 +1,16 @@
+<a class="color" style="background-color:#e11d21" data-color-hex="#e11d21"></a>
+<a class="color" style="background-color:#eb6420" data-color-hex="#eb6420"></a>
+<a class="color" style="background-color:#fbca04" data-color-hex="#fbca04"></a>
+<a class="color" style="background-color:#009800" data-color-hex="#009800"></a>
+<a class="color" style="background-color:#006b75" data-color-hex="#006b75"></a>
+<a class="color" style="background-color:#207de5" data-color-hex="#207de5"></a>
+<a class="color" style="background-color:#0052cc" data-color-hex="#0052cc"></a>
+<a class="color" style="background-color:#53e917" data-color-hex="#53e917"></a>
+<a class="color" style="background-color:#f6c6c7" data-color-hex="#f6c6c7"></a>
+<a class="color" style="background-color:#fad8c7" data-color-hex="#fad8c7"></a>
+<a class="color" style="background-color:#fef2c0" data-color-hex="#fef2c0"></a>
+<a class="color" style="background-color:#bfe5bf" data-color-hex="#bfe5bf"></a>
+<a class="color" style="background-color:#bfdadc" data-color-hex="#bfdadc"></a>
+<a class="color" style="background-color:#c7def8" data-color-hex="#c7def8"></a>
+<a class="color" style="background-color:#bfd4f2" data-color-hex="#bfd4f2"></a>
+<a class="color" style="background-color:#d4c5f9" data-color-hex="#d4c5f9"></a>

+ 63 - 18
templates/repo/issue/labels.tmpl

@@ -19,22 +19,7 @@
 			    	</div>
 					</div>
 					<div class="item precolors">
-						<a class="color" style="background-color:#e11d21" data-color-hex="#e11d21"></a>
-						<a class="color" style="background-color:#eb6420" data-color-hex="#eb6420"></a>
-						<a class="color" style="background-color:#fbca04" data-color-hex="#fbca04"></a>
-						<a class="color" style="background-color:#009800" data-color-hex="#009800"></a>
-						<a class="color" style="background-color:#006b75" data-color-hex="#006b75"></a>
-						<a class="color" style="background-color:#207de5" data-color-hex="#207de5"></a>
-						<a class="color" style="background-color:#0052cc" data-color-hex="#0052cc"></a>
-						<a class="color" style="background-color:#53e917" data-color-hex="#53e917"></a>
-						<a class="color" style="background-color:#f6c6c7" data-color-hex="#f6c6c7"></a>
-						<a class="color" style="background-color:#fad8c7" data-color-hex="#fad8c7"></a>
-						<a class="color" style="background-color:#fef2c0" data-color-hex="#fef2c0"></a>
-						<a class="color" style="background-color:#bfe5bf" data-color-hex="#bfe5bf"></a>
-						<a class="color" style="background-color:#bfdadc" data-color-hex="#bfdadc"></a>
-						<a class="color" style="background-color:#c7def8" data-color-hex="#c7def8"></a>
-						<a class="color" style="background-color:#bfd4f2" data-color-hex="#bfd4f2"></a>
-						<a class="color" style="background-color:#d4c5f9" data-color-hex="#d4c5f9"></a>
+						{{template "repo/issue/label_precolors"}}
 					</div>
 					<button class="ui green button">{{.i18n.Tr "repo.issues.new_label"}}</button>
 				</div>
@@ -52,8 +37,8 @@
 			<li class="item">
 				<div class="ui label" style="background-color: {{.Color}}"><i class="octicon octicon-tag"></i> {{.Name}}</div>
 				{{if $.IsRepositoryAdmin}}
-				<a class="ui right" href="#"><i class="octicon octicon-x"></i> {{$.i18n.Tr "repo.issues.label_delete"}}</a>
-				<a class="ui right" href="#"><i class="octicon octicon-pencil"></i> {{$.i18n.Tr "repo.issues.label_edit"}}</a>
+				<a class="ui right delete-label-button" href="#" data-url="{{$.RepoLink}}/labels/delete" data-id="{{.ID}}"><i class="octicon octicon-x"></i> {{$.i18n.Tr "repo.issues.label_delete"}}</a>
+				<a class="ui right edit-label-button" href="#" data-id={{.ID}} data-title={{.Name}} data-color={{.Color}}><i class="octicon octicon-pencil"></i> {{$.i18n.Tr "repo.issues.label_edit"}}</a>
 				{{end}}
 				<a class="ui right open-issues" href="{{$.RepoLink}}/issues?labels={{.ID}}"><i class="octicon octicon-issue-opened"></i> {{$.i18n.Tr "repo.issues.label_open_issues" .NumOpenIssues}}</a>
 			</li>
@@ -61,4 +46,64 @@
 		</div>
 	</div>
 </div>
+
+{{if .IsRepositoryAdmin}}
+<div class="ui basic delete-label modal">
+  <div class="header">
+    {{.i18n.Tr "repo.issues.label_deletion"}}
+  </div>
+  <div class="content">
+    <div class="image">
+      <i class="trash icon"></i>
+    </div>
+    <div class="description">
+      <p>{{.i18n.Tr "repo.issues.label_deletion_desc"}}</p>
+    </div>
+  </div>
+  <div class="actions">
+    <div class="two fluid ui inverted buttons">
+      <div class="ui red basic inverted button">
+        <i class="remove icon"></i>
+        {{.i18n.Tr "modal.no"}}
+      </div>
+      <div class="ui green basic inverted positive button">
+        <i class="checkmark icon"></i>
+        {{.i18n.Tr "modal.yes"}}
+      </div>
+    </div>
+  </div>
+</div>
+
+<div class="ui small edit-label modal">
+  <div class="header">
+    {{.i18n.Tr "repo.issues.label_modify"}}
+  </div>
+  <div class="content">
+		<form class="ui edit-label form" action="{{$.RepoLink}}/labels/edit" method="post">
+			{{.CsrfTokenHtml}}
+			<input id="label-modal-id" name="id" type="hidden">
+			<div class="inline fields">
+				<div class="field">
+					<input id="label-modal-title" name="title" placeholder="{{.i18n.Tr "repo.issues.new_label_placeholder"}}" required>
+				</div>
+				<div class="field">
+		      <input id="label-modal-color" class="color-picker" name="color" value="#70c24a" required>
+				</div>
+				<div class="field precolors">
+					{{template "repo/issue/label_precolors"}}
+				</div>
+			</div>
+		</form>
+  </div>
+  <div class="actions">
+    <div class="ui negative button">
+      {{.i18n.Tr "modal.no"}}
+    </div>
+    <div class="ui positive right labeled icon button">
+      {{.i18n.Tr "modal.modify"}}
+      <i class="checkmark icon"></i>
+    </div>
+  </div>
+</div>
+{{end}}
 {{template "base/footer" .}}

Some files were not shown because too many files changed in this diff