]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - js/editPage.js
implemented page deleting
[Rakka.git] / js / editPage.js
index 4623849dec6e35fde133d2e10be86ec79d962854..959d0889c72d5310df4c3fd3b68404331d099f7c 100644 (file)
@@ -1,3 +1,6 @@
+Rakka.$previewHeader = null;
+Rakka.$previewArea   = null;
+
 Rakka.editPage = function (pageName) {
     var $area = Rakka.switchScreen();
 
@@ -45,8 +48,16 @@ Rakka.displayPageEditor = function (pageName, oldRevision, defaultType, source)
     var $area = Rakka.switchScreen();
     $area.empty();
 
-    $area.append($.H1({}, pageName == "" ? "Create page" : "Edit page"));
+    Rakka.$previewHeader = $( $.H1({}, "Preview") );
+    $area.append(Rakka.$previewHeader);
+    Rakka.$previewHeader.hide();
 
+    Rakka.$previewArea = $( $.DIV({className: "preview"}) );
+    $area.append(Rakka.$previewArea);
+    Rakka.$previewArea.hide();
+
+    $area.append($.H1({}, pageName == "" ? "Create page" : "Edit page"));
+    
     var fldPageName
         = $.INPUT({type : "text", value: pageName});
 
@@ -92,7 +103,14 @@ Rakka.displayPageEditor = function (pageName, oldRevision, defaultType, source)
         = $.INPUT({type: "button", value: "Preview page"});
     
     $(btnPreview).click(function () {
-        throw new Error("FIXME: not implemented yet");
+        if (btnTypeRakka.checked) {
+            Rakka.previewRakkaPage(
+                fldPageName.value, fldRakkaSource.value);
+        }
+        else if (btnTypeBinary.checked) {
+            Rakka.previewBinaryPage(
+                fldPageName.value, fldUploadFile.value);
+        }
     });
 
     var btnSubmit
@@ -135,7 +153,9 @@ Rakka.displayPageEditor = function (pageName, oldRevision, defaultType, source)
         = $.INPUT({type: "button", value: "Delete this page"});
     
     $(btnDelete).click(function () {
-        throw new Error("FIXME: not implemented yet");
+        if (window.confirm("Do you really want to delete this page?")) {
+            Rakka.deletePage(pageName);
+        }
     });
 
     var updateTRContent = function () {
@@ -220,6 +240,66 @@ Rakka.displayPageEditor = function (pageName, oldRevision, defaultType, source)
     $area.append(pageEditor);
 };
 
+Rakka.previewRakkaPage = function (pageName, source) {
+    Rakka.displayWaitingMessage("Loading... please wait.");
+    
+    var url = Rakka.baseURI + "render/" + encodeURI(pageName);
+    $.ajax({
+        type       : "POST",
+        url        : url,
+        contentType: "text/x-rakka",
+        data       : source,
+        processData: false,
+        success    : function (resultDoc) {
+            Rakka.hideWaitingMessage();
+            Rakka.showPreview(resultDoc);
+        },
+        error      : function (req) {
+            Rakka.hideWaitingMessage();
+            alert("Error: " + req.status + " " + req.statusText);
+        }
+    });
+};
+
+Rakka.previewBinaryPage = function (pageName, path) {
+    Rakka.displayWaitingMessage("Loading... please wait.");
+
+    /* Firefox でバイナリを送らうとすると 0x00 の位置で切れてしまふ。*/
+    var bin = Rakka.loadLocalBinaryFile(path);
+    var url = Rakka.baseURI + "render/" + encodeURI(pageName);
+    $.ajax({
+        type       : "POST",
+        url        : url,
+        contentType: "application/x-rakka-base64-stream",
+        data       : Rakka.encodeBase64(bin),
+        processData: false,
+        success    : function (resultDoc) {
+            Rakka.hideWaitingMessage();
+            Rakka.showPreview(resultDoc);
+        },
+        error      : function (req) {
+            Rakka.hideWaitingMessage();
+            alert("Error: " + req.status + " " + req.statusText);
+        }
+    });
+};
+
+Rakka.showPreview = function (doc) {
+    Rakka.$previewArea.empty();
+    
+    Rakka.$previewHeader.show();
+    Rakka.$previewArea.show();
+    
+    var root  = doc.documentElement;
+    var child = root.firstChild;
+    do {
+        if (child.nodeType == 1) {
+            // 要素だったので複製
+            Rakka.$previewArea.append(child.cloneNode(true));
+        }
+    } while (child = child.nextSibling);
+};
+
 Rakka.submitTextPage = function (pageName, oldRevision, givenPageName, mimeType, text) {
     var doc = document.implementation.createDocument(
         "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
@@ -269,6 +349,58 @@ Rakka.submitTextPage = function (pageName, oldRevision, givenPageName, mimeType,
     });
 };
 
+Rakka.submitBinaryPage = function (pageName, oldRevision, givenPageName, path) {
+    var doc = document.implementation.createDocument(
+        "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
+
+    var page = doc.documentElement;
+
+    if (oldRevision != null) {
+        // ページ書換時
+        var updateInfo = doc.createElement("updateInfo");
+        updateInfo.setAttribute("oldRevision", oldRevision);
+
+        if (pageName != givenPageName) {
+            var move = doc.createElement("move");
+            move.setAttribute("from", pageName);
+            updateInfo.appendChild(move);
+        }
+
+        page.appendChild(updateInfo);
+    }
+
+    page.setAttribute("type", "");
+
+    var bin = Rakka.loadLocalBinaryFile(path);
+    var b64 = Rakka.encodeBase64(bin);
+
+    var binaryData = doc.createElement("binaryData");
+    binaryData.appendChild(
+        doc.createTextNode(b64));
+
+    page.appendChild(binaryData);
+
+    Rakka.displayWaitingMessage("Submitting... please wait.");
+
+    var url = Rakka.baseURI + encodeURI(givenPageName);
+    $.ajax({
+        type       : "PUT",
+        url        : url,
+        contentType: "text/xml",
+        data       : doc,
+        processData: false,
+        success    : function () {
+            window.location.replace(url);
+        },
+        error      : function (req) {
+            Rakka.hideWaitingMessage();
+            
+            var $area = Rakka.switchScreen();
+            $area.text("Error: " + req.status + " " + req.statusText);
+        }
+    });
+};
+
 Rakka.submitRedirection = function (pageName, oldRevision, givenPageName, destination) {
     var doc = document.implementation.createDocument(
         "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
@@ -310,4 +442,21 @@ Rakka.submitRedirection = function (pageName, oldRevision, givenPageName, destin
             $area.text("Error: " + req.status + " " + req.statusText);
         }
     });
-};
\ No newline at end of file
+};
+
+Rakka.deletePage = function (pageName) {
+    var url = Rakka.baseURI + encodeURI(pageName);
+    $.ajax({
+        type       : "DELETE",
+        url        : url,
+        success    : function () {
+            window.location.replace(url);
+        },
+        error      : function (req) {
+            Rakka.hideWaitingMessage();
+            
+            var $area = Rakka.switchScreen();
+            $area.text("Error: " + req.status + " " + req.statusText);
+        }
+    });
+};