]> gitweb @ CieloNegro.org - Rakka.git/blob - js/editPage.js
49b021b619c2231335bc3f7575104f58e129ff89
[Rakka.git] / js / editPage.js
1 Rakka.editPage = function (baseURI, pageName) {
2     var $body = $("div.body");
3
4     $body.text("Loading... please wait.");
5     
6     // XML 版のページを取得する。
7     $.ajax({
8         url    : baseURI + pageName + ".xml",
9         success: function (pageXml) {
10                      var $page         = $(pageXml).find("page");
11                      var oldRevision   = $page.attr("revision");
12                      var defaultType   = $page.attr("isBinary") == "yes"          ? "binary"
13                                        : $page.attr("type")     == "text/x-rakka" ? "rakka"
14                                        : $page.attr("type")     == "text/css"     ? "css"
15                                        :                                            "unknown"
16                                        ;
17                      var source        = $page.find("textData").text();
18                      Rakka.displayPageEditor($body, pageName, oldRevision, defaultType, source);
19                  },
20         error  : function (req) {
21                      if (req.status == 404) {
22                          Rakka.displayPageEditor($body, pageName, null, "rakka", null);
23                      }
24                      else {
25                          $body.text("Error: " + req.status + " " + req.statusText);
26                      }
27                  }
28         });
29 };
30
31 Rakka.displayPageEditor = function ($place, pageName, oldRevision, defaultType, source) {
32     $place.empty();
33
34     $place.append($.H1({}, "Edit page"));
35
36     var fldPageName
37       = $.INPUT({type : "text", value: pageName});
38
39     var btnTypeRakka
40       = $.INPUT({type   : "radio",
41                  name   : "type",
42                  checked: (defaultType == "rakka"  ? "checked" : "")});
43
44     var btnTypeCSS
45       = $.INPUT({type   : "radio",
46                  name   : "type",
47                  checked: (defaultType == "css"    ? "checked" : "")});
48
49     var btnTypeBinary
50       = $.INPUT({type   : "radio",
51                  name   : "type",
52                  checked: (defaultType == "binary" ? "checked" : "")});
53     
54
55     var fldRakkaSource
56       = $.TEXTAREA({}, (defaultType == "rakka" && source != null ? source : ""));
57
58     var fldCSSSource
59       = $.TEXTAREA({}, (defaultType == "css"   && source != null ? source : ""));
60
61     var fldUploadFile
62       = $.INPUT({type: "file"});
63
64     var trContent
65       = $.TR({}, 
66           $.TH({}),
67           $.TD({})
68         );
69
70     var btnPreview
71       = $.INPUT({type: "button", value: "Preview page"});
72
73     var btnSubmit
74       = $.INPUT({type: "button", value: "Submit page"});
75
76     var btnDelete
77       = $.INPUT({type: "button", value: "Delete this page"});
78     
79     var updateTRContent = function () {
80         if (btnTypeRakka.checked) {
81             $(trContent).find("th").text("Wiki source");
82             $(trContent).find("td").empty().append(fldRakkaSource);
83             $(trContent).show();
84         }
85         else if (btnTypeCSS.checked) {
86             $(trContent).find("th").text("CSS source");
87             $(trContent).find("td").empty().append(fldCSSSource);
88             $(trContent).show();
89         }
90         else if (btnTypeBinary.checked) {
91             $(trContent).find("th").text("File");
92             $(trContent).find("td").empty().append(fldUploadFile);
93             $(trContent).show();
94         }
95         else {
96             $(trContent).hide();
97         }
98     };
99     $(btnTypeRakka ).change(updateTRContent);
100     $(btnTypeCSS   ).change(updateTRContent);
101     $(btnTypeBinary).change(updateTRContent);
102     updateTRContent();
103
104     var pageEditor
105       = $.TABLE({className: "pageEditor"},
106           $.TBODY({},
107             $.TR({},
108               $.TH({}, "Page name"),
109               $.TD({}, fldPageName)
110             ),
111             $.TR({},
112               $.TH({}, "Page type"),
113               $.TD({},
114                 $.UL({},
115                   $.LI({},
116                     $.LABEL({},
117                       btnTypeRakka,
118                       "Wiki page"
119                     )
120                   ),
121                   $.LI({},
122                     $.LABEL({},
123                       btnTypeCSS,
124                       "Style sheet"
125                     )
126                   ),
127                   $.LI({},
128                     $.LABEL({},
129                       btnTypeBinary,
130                       "Binary file"
131                     )
132                   )
133                 )
134               )
135             ),
136             trContent,
137             $.TR({},
138               $.TH({}),
139               $.TD({}, btnPreview, btnSubmit, btnDelete)
140             )
141           )
142         );
143
144     if (oldRevision == null || oldRevision == 0) {
145         // 削除不可
146         $(btnDelete).hide();
147     }
148
149     $place.append(pageEditor);
150 };