X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=js%2FeditPage.js;h=406b7e3561603a5b083cf35e6870ad8f48b7bed2;hb=cddd833c65cdd8092588bd3bdfe2b9153848302f;hp=f0d3d507021ce012ea140ecd1ec256834726b516;hpb=0447be1b59496ca4266226ed52d264009cf41899;p=Rakka.git diff --git a/js/editPage.js b/js/editPage.js index f0d3d50..406b7e3 100644 --- a/js/editPage.js +++ b/js/editPage.js @@ -3,42 +3,53 @@ var $previewHeader = null; var $previewArea = null; - var isDirty = null; - Rakka.editPage = function (pageName) { var $area = Rakka.switchScreen(); Rakka.displayWaitingMessage("Loading... please wait."); - + // XML 版のページを取得する。 $.ajax({ 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 isLocked = $page.attr("isLocked") == "yes"; - var source - = $page.attr("redirect") != null ? $page.attr("redirect") - : $page.find("textData").text() - ; - var summary = $page.find("summary").text(); - - displayPageEditor(pageName, oldRevision, defaultType, isLocked, source, summary); + + 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, 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", false, null, ""); + displayPageEditor(pageName, null, "rakka", null, false, {}, null, ""); } else { $area.text("Error: " + req.status + " " + req.statusText); @@ -48,10 +59,10 @@ }; Rakka.newPage = function () { - displayPageEditor("", null, "rakka", false, null, ""); + displayPageEditor("", null, "rakka", null, false, {}, null, ""); }; - var displayPageEditor = function (pageName, oldRevision, defaultType, isLocked, source, summary) { + var displayPageEditor = function (pageName, oldRevision, defaultType, lang, isLocked, otherLangs, source, summary) { var $area = Rakka.switchScreen(); $previewHeader = $( $.H1({}, "Preview") ); @@ -64,18 +75,19 @@ $area.append($.H1({}, pageName == "" ? "Create page" : "Edit page")); + var isDirty = null; var makeDirty = function () { isDirty = true; }; - + var fldPageName = $.INPUT({type : "text", value: pageName}); $(fldPageName).change(makeDirty); var chkIsLocked - = $.INPUT({type : "checkbox", - checked: (isLocked ? "checked" : "")}); + = $.INPUT({type : "checkbox", + checked : (isLocked ? "checked" : "")}); $(chkIsLocked).change(makeDirty); @@ -86,7 +98,7 @@ $.LABEL({}, chkIsLocked, "Disallow anonymous users to edit or delete this page"))); - + var btnTypeRakka = $.INPUT({type : "radio", name : "type", @@ -115,6 +127,72 @@ $(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); @@ -148,28 +226,30 @@ $(fldRedirect).change(makeDirty); var trContent - = $.TR({}, + = $.TR({}, $.TH({}), $.TD({}) ); var btnPreview = $.INPUT({type: "button", value: "Preview page"}); - + $(btnPreview).click(function () { if (btnTypeRakka.checked) { previewRakkaPage( fldPageName.value, fldRakkaSource.value); } else if (btnTypeBinary.checked) { - previewBinaryPage( - fldPageName.value, fldUploadFile.value); + if (fldUploadFile.value != "") { + previewBinaryPage( + fldPageName.value, fldUploadFile.value); + } } }); var btnSubmit = $.INPUT({type: "button", value: "Submit page"}); - + $(btnSubmit).click(function () { if (btnTypeRakka.checked) { submitTextPage( @@ -178,6 +258,8 @@ fldPageName.value, chkIsLocked.checked, "text/x-rakka", + $(selPageLang).val(), + otherLangs, fldSummary.value, fldRakkaSource.value); } @@ -188,17 +270,23 @@ fldPageName.value, chkIsLocked.checked, "text/css", + $(selPageLang).val(), + otherLangs, fldSummary.value, fldCSSSource.value); } else if (btnTypeBinary.checked) { - submitBinaryPage( - pageName, - oldRevision, - fldPageName.value, - chkIsLocked.checked, - fldSummary.value, - fldUploadFile.value); + if (fldUploadFile.value != "") { + submitBinaryPage( + pageName, + oldRevision, + fldPageName.value, + chkIsLocked.checked, + $(selPageLang).val(), + otherLangs, + fldSummary.value, + fldUploadFile.value); + } } else if (btnTypeRedirect.checked) { submitRedirection( @@ -212,7 +300,7 @@ var btnDelete = $.INPUT({type: "button", value: "Delete this page"}); - + $(btnDelete).click(function () { if (window.confirm("Do you really want to delete this page?")) { deletePage(pageName); @@ -235,24 +323,32 @@ 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); @@ -304,6 +400,8 @@ ) ) ), + trPageLang, + trOtherLangs, trSummary, trContent, $.TR({}, @@ -313,6 +411,48 @@ ) ); + var validate = function () { + var isValid = (function () { + if (fldPageName.value.match(Rakka.rePageName) == null) { + return false; + } + + if (btnTypeRedirect.checked) { + if (fldRedirect.value.match(Rakka.rePageName) == null) { + return false; + } + } + else { + for (var tag in otherLangs) { + if (otherLangs[tag].match(Rakka.rePageName) == null) { + return false; + } + } + + if (btnTypeBinary.checked) { + if (fldUploadFile.value == "") { + return false; + } + } + } + + return true; + })(); + + $(btnSubmit).attr({disabled: (isValid ? "" : "disabled")}); + }; + $(fldPageName) + .add(btnTypeRakka) + .add(btnTypeCSS) + .add(btnTypeBinary) + .add(btnTypeRedirect) + .add($(trOtherLangs).find("input")) + .add(fldUploadFile) + .add(fldRedirect) + .change(validate) + .keyup(validate); + validate(); + if (oldRevision == null || oldRevision == 0) { // 削除不可 $(btnDelete).hide(); @@ -320,12 +460,14 @@ $area.append(pageEditor); - isDirty = false; + if (!Rakka.isLoggedIn() || Rakka.isGlobalLocked) { + $(trIsLocked).hide(); + } }; var previewRakkaPage = function (pageName, source) { Rakka.displayWaitingMessage("Loading... please wait."); - + var url = Rakka.baseURI + "render/" + encodeURI(pageName); $.ajax({ type : "POST", @@ -369,10 +511,10 @@ var showPreview = function (doc) { $previewArea.empty(); - + $previewHeader.show(); $previewArea.show(); - + var root = doc.documentElement; var child = root.firstChild; do { @@ -385,19 +527,19 @@ Rakka.scrollToTopLeft(); }; - var submitTextPage = function (pageName, oldRevision, givenPageName, isLocked, mimeType, summary, text) { - var doc = document.implementation.createDocument( - "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null); - - var page = doc.documentElement; + var submitTextPage + = function (pageName, oldRevision, givenPageName, isLocked, mimeType, lang, otherLangs, summary, text) { + var NS = "http://cielonegro.org/schema/Rakka/Page/1.0"; + var doc = document.implementation.createDocument(NS, "page", null); + var page = doc.documentElement; if (oldRevision != null) { // ページ書換時 - var updateInfo = doc.createElement("updateInfo"); + var updateInfo = doc.createElementNS(NS, "updateInfo"); updateInfo.setAttribute("oldRevision", oldRevision); if (pageName != givenPageName) { - var move = doc.createElement("move"); + var move = doc.createElementNS(NS, "move"); move.setAttribute("from", pageName); updateInfo.appendChild(move); } @@ -408,14 +550,27 @@ page.setAttribute("isLocked", isLocked ? "yes" : "no"); page.setAttribute("type", mimeType); - if (summary != null) { - var s = doc.createElement("summary"); + if (lang != null && lang != "") { + page.setAttribute("lang", lang); + } + + if (summary != null && summary != "") { + var s = doc.createElementNS(NS, "summary"); s.appendChild( doc.createTextNode(summary)); page.appendChild(s); } - var textData = doc.createElement("textData"); + var oLang = doc.createElementNS(NS, "otherLang"); + for (var tag in otherLangs) { + var link = doc.createElementNS(NS, "link"); + link.setAttribute("lang", tag); + link.setAttribute("page", otherLangs[tag]); + oLang.appendChild(link); + } + page.appendChild(oLang); + + var textData = doc.createElementNS(NS, "textData"); textData.appendChild( doc.createTextNode(text)); @@ -438,26 +593,25 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } }); }; - var submitBinaryPage = function (pageName, oldRevision, givenPageName, isLocked, summary, path) { - var doc = document.implementation.createDocument( - "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null); - + var submitBinaryPage = function (pageName, oldRevision, givenPageName, isLocked, lang, otherLangs, summary, path) { + var NS = "http://cielonegro.org/schema/Rakka/Page/1.0"; + var doc = document.implementation.createDocument(NS, "page", null); var page = doc.documentElement; if (oldRevision != null) { // ページ書換時 - var updateInfo = doc.createElement("updateInfo"); + var updateInfo = doc.createElementNS(NS, "updateInfo"); updateInfo.setAttribute("oldRevision", oldRevision); if (pageName != givenPageName) { - var move = doc.createElement("move"); + var move = doc.createElementNS(NS, "move"); move.setAttribute("from", pageName); updateInfo.appendChild(move); } @@ -468,17 +622,30 @@ 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"); + var s = doc.createElementNS(NS, "summary"); s.appendChild( doc.createTextNode(summary)); page.appendChild(s); } + var oLang = doc.createElementNS(NS, "otherLang"); + for (var tag in otherLangs) { + var link = doc.createElementNS(NS, "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); - var binaryData = doc.createElement("binaryData"); + var binaryData = doc.createElementNS(NS, "binaryData"); binaryData.appendChild( doc.createTextNode(b64)); @@ -501,7 +668,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } @@ -509,18 +676,17 @@ }; var submitRedirection = function (pageName, oldRevision, givenPageName, isLocked, destination) { - var doc = document.implementation.createDocument( - "http://cielonegro.org/schema/Rakka/Page/1.0", "page", null); - + var NS = "http://cielonegro.org/schema/Rakka/Page/1.0"; + var doc = document.implementation.createDocument(NS, "page", null); var page = doc.documentElement; if (oldRevision != null) { // ページ書換時 - var updateInfo = doc.createElement("updateInfo"); + var updateInfo = doc.createElementNS(NS, "updateInfo"); updateInfo.setAttribute("oldRevision", oldRevision); if (pageName != givenPageName) { - var move = doc.createElement("move"); + var move = doc.createElementNS(NS, "move"); move.setAttribute("from", pageName); updateInfo.appendChild(move); } @@ -548,7 +714,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } @@ -568,7 +734,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); }