X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=js%2FeditPage.js;h=e4483a1e3520104e5372401ae8f6f12a13dfd489;hb=a039dfc12ba7b8b703e9f0ee7deaaffbfbdd0b8f;hp=e390a1949ec67a7cf8c7e2b8f8b698887409f82e;hpb=f1016753ef45a4c25745ccb6e81e5acbc085cc42;p=Rakka.git diff --git a/js/editPage.js b/js/editPage.js index e390a19..e4483a1 100644 --- a/js/editPage.js +++ b/js/editPage.js @@ -3,13 +3,11 @@ 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", @@ -20,11 +18,12 @@ 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" + = $page.attr("isBinary") == "yes" ? "binary" + : $page.attr("type") == "text/x-rakka" ? "rakka" + : $page.attr("type") == "text/css" ? "css" + : $page.attr("type") == "text/javascript" ? "js" + : $page.attr("redirect") != null ? "redirect" + : "unknown" ; var lang = $page.attr("lang"); var isLocked = $page.attr("isLocked") == "yes"; @@ -40,7 +39,7 @@ : $page.find("textData").text() ; var summary = $page.find("summary").text(); - + displayPageEditor(pageName, oldRevision, defaultType, lang, isLocked, otherLangs, source, summary); } else { @@ -49,7 +48,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + if (req.status == 404) { displayPageEditor(pageName, null, "rakka", null, false, {}, null, ""); } @@ -77,10 +76,11 @@ $area.append($.H1({}, pageName == "" ? "Create page" : "Edit page")); + var isDirty = null; var makeDirty = function () { isDirty = true; }; - + var fldPageName = $.INPUT({type : "text", value: pageName}); @@ -99,7 +99,7 @@ $.LABEL({}, chkIsLocked, "Disallow anonymous users to edit or delete this page"))); - + var btnTypeRakka = $.INPUT({type : "radio", name : "type", @@ -114,6 +114,12 @@ $(btnTypeCSS).change(makeDirty); + var btnTypeJS + = $.INPUT({type : "radio", + name : "type", + checked: (defaultType == "js" ? "checked" : "")}); + $(btnTypeJS).change(makeDirty); + var btnTypeBinary = $.INPUT({type : "radio", name : "type", @@ -133,7 +139,7 @@ $.OPTION({value: ""}, "(unspecified)"), (function () { var options = []; - + $.each(Rakka.getSystemConfig().languages, function (tag, name) { options.push( $.OPTION({value: tag}, name)); @@ -179,7 +185,7 @@ var lang = $(selLang).val(); var pageName = $(this).val(); - + if (pageName == "") { delete otherLangs[lang]; } @@ -216,10 +222,24 @@ $(fldCSSSource).change(makeDirty); - var fldUploadFile - = $.INPUT({type: "file"}); + var fldJSSource + = $.TEXTAREA({className: "source"}, + (defaultType == "js" && source != null ? source : "")); + + $(fldJSSource).change(makeDirty); - $(fldUploadFile).change(makeDirty); + var fldUploadFile + = $.INPUT({type: "text", disabled: true}); + var btnSelectFile + = $.INPUT({type: "button", value: "Select File"}); + + $(btnSelectFile).click(function () { + var path = Rakka.selectFile("Select a binary file to upload", "open"); + if (path != null) { + fldUploadFile.value = path; + makeDirty(); + } + }); var fldRedirect = $.INPUT({type: "text", value: (defaultType == "redirect" ? source : "")}); @@ -227,28 +247,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( @@ -274,16 +296,30 @@ fldSummary.value, fldCSSSource.value); } - else if (btnTypeBinary.checked) { - submitBinaryPage( + else if (btnTypeJS.checked) { + submitTextPage( pageName, oldRevision, fldPageName.value, chkIsLocked.checked, + "text/javascript", $(selPageLang).val(), otherLangs, fldSummary.value, - fldUploadFile.value); + fldJSSource.value); + } + else if (btnTypeBinary.checked) { + if (fldUploadFile.value != "") { + submitBinaryPage( + pageName, + oldRevision, + fldPageName.value, + chkIsLocked.checked, + $(selPageLang).val(), + otherLangs, + fldSummary.value, + fldUploadFile.value); + } } else if (btnTypeRedirect.checked) { submitRedirection( @@ -297,7 +333,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); @@ -335,12 +371,20 @@ $(trContent).find("td").empty().append(fldCSSSource); $(btnPreview).hide(); } + else if (btnTypeJS.checked) { + $(trPageLang).show(); + $(trOtherLangs).show(); + $(trSummary).show(); + $(trContent).find("th").text("JavaScript source"); + $(trContent).find("td").empty().append(fldJSSource); + $(btnPreview).hide(); + } else if (btnTypeBinary.checked) { $(trPageLang).show(); $(trOtherLangs).show(); $(trSummary).show(); $(trContent).find("th").text("File"); - $(trContent).find("td").empty().append(fldUploadFile); + $(trContent).find("td").empty().append(fldUploadFile).append(btnSelectFile); $(btnPreview).show(); } else if (btnTypeRedirect.checked) { @@ -354,6 +398,7 @@ }; $(btnTypeRakka ).change(updateTRContent); $(btnTypeCSS ).change(updateTRContent); + $(btnTypeJS ).change(updateTRContent); $(btnTypeBinary ).change(updateTRContent); $(btnTypeRedirect).change(updateTRContent); updateTRContent(); @@ -382,6 +427,12 @@ "Style sheet" ) ), + $.LI({}, + $.LABEL({}, + btnTypeJS, + "JavaScript" + ) + ), $.LI({}, $.LABEL({}, btnTypeBinary, @@ -408,6 +459,49 @@ ) ); + 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(btnTypeJS) + .add(btnTypeBinary) + .add(btnTypeRedirect) + .add($(trOtherLangs).find("input")) + .add(fldUploadFile) + .add(fldRedirect) + .change(validate) + .keyup(validate); + validate(); + if (oldRevision == null || oldRevision == 0) { // 削除不可 $(btnDelete).hide(); @@ -418,13 +512,11 @@ if (!Rakka.isLoggedIn() || Rakka.isGlobalLocked) { $(trIsLocked).hide(); } - - isDirty = false; }; var previewRakkaPage = function (pageName, source) { Rakka.displayWaitingMessage("Loading... please wait."); - + var url = Rakka.baseURI + "render/" + encodeURI(pageName); $.ajax({ type : "POST", @@ -468,10 +560,10 @@ var showPreview = function (doc) { $previewArea.empty(); - + $previewHeader.show(); $previewArea.show(); - + var root = doc.documentElement; var child = root.firstChild; do { @@ -484,19 +576,19 @@ Rakka.scrollToTopLeft(); }; - 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); - - 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); } @@ -512,22 +604,22 @@ } if (summary != null && summary != "") { - var s = doc.createElement("summary"); + var s = doc.createElementNS(NS, "summary"); s.appendChild( doc.createTextNode(summary)); page.appendChild(s); } - var oLang = doc.createElement("otherLang"); + var oLang = doc.createElementNS(NS, "otherLang"); for (var tag in otherLangs) { - var link = doc.createElement("link"); + var link = doc.createElementNS(NS, "link"); link.setAttribute("lang", tag); link.setAttribute("page", otherLangs[tag]); oLang.appendChild(link); } page.appendChild(oLang); - var textData = doc.createElement("textData"); + var textData = doc.createElementNS(NS, "textData"); textData.appendChild( doc.createTextNode(text)); @@ -550,7 +642,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } @@ -558,18 +650,17 @@ }; 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); - + 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); } @@ -585,15 +676,15 @@ } 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.createElement("otherLang"); + var oLang = doc.createElementNS(NS, "otherLang"); for (var tag in otherLangs) { - var link = doc.createElement("link"); + var link = doc.createElementNS(NS, "link"); link.setAttribute("lang", tag); link.setAttribute("page", otherLangs[tag]); oLang.appendChild(link); @@ -603,7 +694,7 @@ 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)); @@ -626,7 +717,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } @@ -634,18 +725,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); } @@ -673,7 +763,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); } @@ -693,7 +783,7 @@ }, error : function (req) { Rakka.hideWaitingMessage(); - + var $area = Rakka.switchScreen(); $area.text("Error: " + req.status + " " + req.statusText); }