[bugzilla-gnome-org-extensions] Clean up loading the global attachment object



commit 242c9d7bf5a806ff9bb05998c1dc55e9d2aba5b0
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Sat Sep 12 18:22:18 2009 -0400

    Clean up loading the global attachment object
    
    Factor out searching for the attachment into Bug.getAttachment()
    Provide good error messages when the attachment is missing from
      the bug or isn't a patch.

 js/bug.js      |   10 ++++++++++
 js/splinter.js |   47 ++++++++++++++++++++++++-----------------------
 2 files changed, 34 insertions(+), 23 deletions(-)
---
diff --git a/js/bug.js b/js/bug.js
index 67b75f6..bb288f5 100644
--- a/js/bug.js
+++ b/js/bug.js
@@ -98,6 +98,16 @@ Bug.prototype = {
         this.comments = [];
     },
 
+    getAttachment : function(attachmentId) {
+        for (i = 0; i < this.attachments.length; i++) {
+            var attachment = theBug.attachments[i];
+            if (attachment.id == attachmentId)
+                return attachment;
+            }
+
+        return null;
+    },
+
     getReporter : function() {
         return _formatWho(this.reporterName, this.reporterEmail);
     }
diff --git a/js/splinter.js b/js/splinter.js
index bfd003a..5313faf 100644
--- a/js/splinter.js
+++ b/js/splinter.js
@@ -342,6 +342,8 @@ function addPatchFile(file) {
 var REVIEW_RE = /^\s*review\s+of\s+attachment\s+(\d+)\s*:\s*/i;
 
 function start(xml) {
+    theReview = new Review.Review(thePatch);
+
     $("#loading").hide();
     $("#headers").show();
     $("#controls").show();
@@ -365,22 +367,13 @@ function start(xml) {
     else
         $("#patchIntro").hide();
 
-    for (i = 0; i < theBug.attachments.length; i++) {
-        var attachment = theBug.attachments[i];
-        if (attachment.id == attachmentId) {
-            theAttachment = attachment;
-
-            $("#attachmentId").text(attachment.id);
-            $("#attachmentDesc").text(attachment.description);
-            $("#attachmentDate").text(Utils.formatDate(attachment.date));
-            if (attachment.status != null)
-                $("#attachmentStatus").val(attachment.status);
-            else
-                $("#attachmentStatusSpan").hide();
-
-            break;
-        }
-    }
+    $("#attachmentId").text(theAttachment.id);
+    $("#attachmentDesc").text(theAttachment.description);
+    $("#attachmentDate").text(Utils.formatDate(theAttachment.date));
+    if (theAttachment.status != null)
+        $("#attachmentStatus").val(theAttachment.status);
+    else
+        $("#attachmentStatusSpan").hide();
 
     for (i = 0; i < theBug.comments.length; i++) {
         var comment = theBug.comments[i];
@@ -407,23 +400,31 @@ function start(xml) {
     for (i = 0; i < thePatch.files.length; i++)
         addPatchFile(thePatch.files[i]);
 
-    $("#saveButton").click(saveReview);
+    $("#saveButton").click(publishReview);
 }
 
 function gotBug(xml) {
     theBug = Bug.Bug.fromDOM(xml);
 
-    if (theBug !== undefined && thePatch !== undefined)
-        start();
-    else if (attachmentId === undefined)
+    if (attachmentId != null) {
+        theAttachment = theBug.getAttachment(attachmentId);
+        if (theAttachment == null)
+            displayError("Attachment " + attachmentId + " is not an attachment to bug " + theBug.id);
+        else if (!theAttachment.isPatch) {
+            displayError("Attachment " + attachmentId + " is not a patch");
+            theAttachment = null;
+        }
+    }
+
+    if (theAttachment == null)
         showChooseAttachment();
+    else if (thePatch != null)
+        start();
 }
 
 function gotAttachment(text) {
     thePatch = new Patch.Patch(text);
-    theReview = new Review.Review(thePatch);
-
-    if (theBug !== undefined && thePatch !== undefined)
+    if (theAttachment != null)
         start();
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]