]> gitweb @ CieloNegro.org - Rakka.git/blobdiff - js/editPage.js
implemented language link editor (partly)
[Rakka.git] / js / editPage.js
index fb887352f85a5b80bac5092b146ea6ed68ad03bd..e390a1949ec67a7cf8c7e2b8f8b698887409f82e 100644 (file)
             url    : Rakka.baseURI + pageName + ".xml",
             success: function (pageXml) {
                 Rakka.hideWaitingMessage();
-            
-                var $page       = $(pageXml).find("page");
-                var oldRevision = $page.attr("revision");
-                var defaultType
-                    = $page.attr("isBinary") == "yes"          ? "binary"
-                    : $page.attr("type")     == "text/x-rakka" ? "rakka"
-                    : $page.attr("type")     == "text/css"     ? "css"
-                    : $page.attr("redirect") != null           ? "redirect"
-                    :                                            "unknown"
-                    ;
-                var source
-                    = $page.attr("redirect") != null ? $page.attr("redirect")
-                    : $page.find("textData").text()
-                    ;
-                var summary     = $page.find("summary").text();
+
+                if (pageXml.documentElement.tagName == "page") {
+                    var $page       = $(pageXml).find("page");
+                    var oldRevision = $page.attr("revision");
+                    var defaultType
+                        = $page.attr("isBinary") == "yes"          ? "binary"
+                        : $page.attr("type")     == "text/x-rakka" ? "rakka"
+                        : $page.attr("type")     == "text/css"     ? "css"
+                        : $page.attr("redirect") != null           ? "redirect"
+                        :                                            "unknown"
+                        ;
+                    var lang        = $page.attr("lang");
+                    var isLocked    = $page.attr("isLocked") == "yes";
+                    var otherLangs  = (function () {
+                        var obj = {};
+                        $page.find("otherLang > link").each(function () {
+                            obj[this.getAttribute("lang")] = this.getAttribute("page");
+                        });
+                        return obj;
+                    })();
+                    var source
+                        = $page.attr("redirect") != null ? $page.attr("redirect")
+                        : $page.find("textData").text()
+                        ;
+                    var summary     = $page.find("summary").text();
                 
-                displayPageEditor(pageName, oldRevision, defaultType, source, summary);
+                    displayPageEditor(pageName, oldRevision, defaultType, lang, isLocked, otherLangs, source, summary);
+                }
+                else {
+                    displayPageEditor(pageName, null, "rakka", null, false, {}, null, "");
+                }
             },
             error  : function (req) {
                 Rakka.hideWaitingMessage();
                 
                 if (req.status == 404) {
-                    displayPageEditor(pageName, null, "rakka", null, "");
+                    displayPageEditor(pageName, null, "rakka", null, false, {}, null, "");
                 }
                 else {
                     $area.text("Error: " + req.status + " " + req.statusText);
     };
 
     Rakka.newPage = function () {
-        displayPageEditor("", null, "rakka", null, "");
+        displayPageEditor("", null, "rakka", null, false, {}, null, "");
     };
 
-    var displayPageEditor = function (pageName, oldRevision, defaultType, source, summary) {
+    var displayPageEditor = function (pageName, oldRevision, defaultType, lang, isLocked, otherLangs, source, summary) {
         var $area = Rakka.switchScreen();
 
         $previewHeader = $( $.H1({}, "Preview") );
 
         $(fldPageName).change(makeDirty);
 
+        var chkIsLocked
+            = $.INPUT({type    : "checkbox",
+                       checked : (isLocked ? "checked" : "")});
+
+        $(chkIsLocked).change(makeDirty);
+
+        var trIsLocked
+            = $.TR({},
+                   $.TH({}, "Page lock"),
+                   $.TD({},
+                        $.LABEL({},
+                                chkIsLocked,
+                                "Disallow anonymous users to edit or delete this page")));
+        
         var btnTypeRakka
             = $.INPUT({type   : "radio",
                        name   : "type",
 
         $(btnTypeRedirect).change(makeDirty);
 
+        var selPageLang
+            = $.SELECT({},
+                       $.OPTION({value: ""}, "(unspecified)"),
+                       (function () {
+                           var options = [];
+                           
+                           $.each(Rakka.getSystemConfig().languages, function (tag, name) {
+                               options.push(
+                                   $.OPTION({value: tag}, name));
+                           });
+
+                           return options;
+                       })());
+
+        $(selPageLang).change(makeDirty);
+
+        if (lang == null || lang == "") {
+            $(selPageLang).val($("html").attr("xml:lang"));
+        }
+        else {
+            $(selPageLang).val(lang);
+        }
+
+        var trPageLang
+            = $.TR({},
+                   $.TH({}, "Page language"),
+                   $.TD({}, selPageLang));
+
+        var trOtherLangs = (function () {
+            var options = [];
+
+            $.each(Rakka.getSystemConfig().languages, function (tag, name) {
+                options.push(
+                    $.OPTION({value: tag}, name));
+            });
+
+            var selLang = $.SELECT({}, options);
+            var fldLink = $.INPUT({type: "text", className: "smallField"});
+
+            $(selLang).change(function () {
+                var pageName = otherLangs[$(selLang).val()];
+                $(fldLink).val(
+                    pageName == null ? "" : pageName
+                );
+            }).trigger("change");
+
+            var onLinkChanged = function () {
+                isDirty = true;
+
+                var lang     = $(selLang).val();
+                var pageName = $(this).val();
+                
+                if (pageName == "") {
+                    delete otherLangs[lang];
+                }
+                else {
+                    otherLangs[lang] = pageName;
+                }
+            };
+            $(fldLink).change(onLinkChanged).keyup(onLinkChanged);
+
+            return $.TR({},
+                        $.TH({}, "Language links"),
+                        $.TD({}, selLang, fldLink));
+        })();
+
         var fldSummary
             = $.TEXTAREA({className: "summary"}, summary);
 
                     pageName,
                     oldRevision,
                     fldPageName.value,
+                    chkIsLocked.checked,
                     "text/x-rakka",
+                    $(selPageLang).val(),
+                    otherLangs,
                     fldSummary.value,
                     fldRakkaSource.value);
             }
                     pageName,
                     oldRevision,
                     fldPageName.value,
+                    chkIsLocked.checked,
                     "text/css",
+                    $(selPageLang).val(),
+                    otherLangs,
                     fldSummary.value,
                     fldCSSSource.value);
             }
                     pageName,
                     oldRevision,
                     fldPageName.value,
+                    chkIsLocked.checked,
+                    $(selPageLang).val(),
+                    otherLangs,
                     fldSummary.value,
                     fldUploadFile.value);
             }
                     pageName,
                     oldRevision,
                     fldPageName.value,
+                    chkIsLocked.checked,
                     fldRedirect.value);
             }
         });
 
         var updateTRContent = function () {
             if (btnTypeRakka.checked) {
+                $(trPageLang).show();
+                $(trOtherLangs).show();
                 $(trSummary).show();
                 $(trContent).find("th").text("Wiki source");
                 $(trContent).find("td").empty().append(fldRakkaSource);
                 $(btnPreview).show();
             }
             else if (btnTypeCSS.checked) {
+                $(trPageLang).show();
+                $(trOtherLangs).show();
                 $(trSummary).show();
                 $(trContent).find("th").text("CSS source");
                 $(trContent).find("td").empty().append(fldCSSSource);
                 $(btnPreview).hide();
             }
             else if (btnTypeBinary.checked) {
+                $(trPageLang).show();
+                $(trOtherLangs).show();
                 $(trSummary).show();
                 $(trContent).find("th").text("File");
                 $(trContent).find("td").empty().append(fldUploadFile);
                 $(btnPreview).show();
             }
             else if (btnTypeRedirect.checked) {
+                $(trPageLang).hide();
+                $(trOtherLangs).hide();
                 $(trSummary).hide();
                 $(trContent).find("th").text("Destination Page");
                 $(trContent).find("td").empty().append(fldRedirect);
                                    $.TH({}, "Page name"),
                                    $.TD({}, fldPageName)
                                   ),
+                              trIsLocked,
                               $.TR({},
                                    $.TH({}, "Page type"),
                                    $.TD({},
                                             )
                                        )
                                   ),
+                              trPageLang,
+                              trOtherLangs,
                               trSummary,
                               trContent,
                               $.TR({},
 
         $area.append(pageEditor);
 
+        if (!Rakka.isLoggedIn() || Rakka.isGlobalLocked) {
+            $(trIsLocked).hide();
+        }
+
         isDirty = false;
     };
 
         Rakka.scrollToTopLeft();
     };
 
-    var submitTextPage = function (pageName, oldRevision, givenPageName, mimeType, summary, text) {
+    var submitTextPage = function (pageName, oldRevision, givenPageName, isLocked, mimeType, lang, otherLangs, summary, text) {
         var doc = document.implementation.createDocument(
             "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
 
             page.appendChild(updateInfo);
         }
 
+        page.setAttribute("isLocked", isLocked ? "yes" : "no");
         page.setAttribute("type", mimeType);
 
-        if (summary != null) {
+        if (lang != null && lang != "") {
+            page.setAttribute("lang", lang);
+        }
+
+        if (summary != null && summary != "") {
             var s = doc.createElement("summary");
             s.appendChild(
                 doc.createTextNode(summary));
             page.appendChild(s);
         }
 
+        var oLang = doc.createElement("otherLang");
+        for (var tag in otherLangs) {
+            var link = doc.createElement("link");
+            link.setAttribute("lang", tag);
+            link.setAttribute("page", otherLangs[tag]);
+            oLang.appendChild(link);
+        }
+        page.appendChild(oLang);
+
         var textData = doc.createElement("textData");
         textData.appendChild(
             doc.createTextNode(text));
             contentType: "text/xml",
             data       : doc,
             processData: false,
+            beforeSend : function (req) {
+                Rakka.setAuthorization(req);
+            },
             success    : function () {
                 window.location.replace(url);
             },
         });
     };
 
-    var submitBinaryPage = function (pageName, oldRevision, givenPageName, summary, path) {
+    var submitBinaryPage = function (pageName, oldRevision, givenPageName, isLocked, lang, otherLangs, summary, path) {
         var doc = document.implementation.createDocument(
             "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
 
             page.appendChild(updateInfo);
         }
 
+        page.setAttribute("isLocked", isLocked ? "yes" : "no");
         page.setAttribute("type", "");
 
+        if (lang != null && lang != "") {
+            page.setAttribute("lang", lang);
+        }
+
         if (summary != null) {
             var s = doc.createElement("summary");
             s.appendChild(
             page.appendChild(s);
         }
 
+        var oLang = doc.createElement("otherLang");
+        for (var tag in otherLangs) {
+            var link = doc.createElement("link");
+            link.setAttribute("lang", tag);
+            link.setAttribute("page", otherLangs[tag]);
+            oLang.appendChild(link);
+        }
+        page.appendChild(oLang);
+
         var bin = Rakka.loadLocalBinaryFile(path);
         var b64 = Rakka.encodeBase64(bin);
 
             contentType: "text/xml",
             data       : doc,
             processData: false,
+            beforeSend : function (req) {
+                Rakka.setAuthorization(req);
+            },
             success    : function () {
                 window.location.replace(url);
             },
         });
     };
 
-    var submitRedirection = function (pageName, oldRevision, givenPageName, destination) {
+    var submitRedirection = function (pageName, oldRevision, givenPageName, isLocked, destination) {
         var doc = document.implementation.createDocument(
             "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null);
 
             page.appendChild(updateInfo);
         }
 
+        page.setAttribute("isLocked", isLocked ? "yes" : "no");
         page.setAttribute("redirect", destination);
 
         Rakka.displayWaitingMessage("Submitting... please wait.");
             contentType: "text/xml",
             data       : doc,
             processData: false,
+            beforeSend : function (req) {
+                Rakka.setAuthorization(req);
+            },
             success    : function () {
                 window.location.replace(url);
             },
         $.ajax({
             type       : "DELETE",
             url        : url,
+            beforeSend : function (req) {
+                Rakka.setAuthorization(req);
+            },
             success    : function () {
                 window.location.replace(url);
             },