1/* DOKUWIKI:include lib/Supa.js */
2// vim :set ts=4 sw=4 expandtab
3/**
4 * Supa helper plugin
5 *
6 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author   Christoph Linder
8 */
9var supa_handler = {
10    preview_scaler: "",
11    preview_width: "100px",
12    preview_height: "100px",
13    sectok: "",
14    namespace: "",
15    default_filename: "",
16
17    applet_id: "Supa__Applet",
18    applet_name: "Supa__Applet",
19    file_extension: "png",
20
21    msg: function (text, style) {
22        var msgbox = $('media__left'),
23            div = document.createElement('div'),
24            tn = document.createTextNode(text),
25            c = "",
26      before = null;
27
28        if (!msgbox) {
29            alert(text);
30        }
31
32        div.appendChild(tn);
33        switch (style) {
34        case 0:
35            c = "success";
36            break;
37        case 1:
38            c = 'info';
39            break;
40        default:
41            c = "error";
42        }
43        div.setAttribute("class", c);
44
45        if (msgbox.childNodes) {
46            before = msgbox.childNodes[0];
47        }
48        msgbox.insertBefore(div, before);
49
50    },
51
52    pasteButtonHandler: function (e) {
53        cleanMsgArea();
54        var s = new Supa(),
55            supaApplet = document.getElementsByName(supa_handler.applet_name)[0],
56      err;
57
58        if (!s.ping(supaApplet)) {
59            alert(LANG['plugins']['supa']["err_not_loaded_yet"]);
60            return;
61        }
62        try {
63            err = supaApplet.pasteFromClipboard();
64            switch (err) {
65            case 0:
66                /* no error */
67                break;
68            case 1:
69                alert(LANG['plugins']['supa']["err_clipboard_unknown_error"]);
70                break;
71            case 2:
72                alert(LANG['plugins']['supa']["err_clipboard_empty"]);
73                break;
74            case 3:
75                alert(LANG['plugins']['supa']["err_clipboard_content_unsupported"]);
76                break;
77            default:
78                alert(LANG['plugins']['supa']["err_clipboard_error_code_unknown"] + err);
79            }
80        } catch (ex) {
81            if (typeof ex === "object") {
82                alert("Internal exception: " + ex.toString());
83            } else {
84                alert("Internal exception: " + ex);
85            }
86        }
87    },
88
89    uploadButtonHandler: function () {
90        cleanMsgArea();
91
92        var e_filename = $("supa__filename"),
93            filename = e_filename.value,
94      s,
95      supaApplet,
96      encodedData,
97      response,
98      loc;
99
100        if (!filename) {
101            alert(LANG['plugins']['supa']['err_need_filename']);
102            e_filename.focus();
103            return;
104        }
105    //FIXME: check the backslashing!
106        if (!e_filename.value.toLowerCase().match("\." + supa_handler.file_extension + "$")) {
107            e_filename.value += "." + supa_handler.file_extension;
108        }
109
110        //var encodedData = "Hello World";
111        s = new Supa();
112        supaApplet = document.getElementsByName(supa_handler.applet_name)[0];
113        if (!s.ping(supaApplet)) {
114            alert(LANG['plugins']['supa']["err_not_loaded_yet"]);
115            return;
116        }
117        encodedData = supaApplet.getEncodedString();
118        if (!encodedData) {
119            alert(LANG['plugins']['supa']['err_paste_image_first']);
120            return;
121        }
122
123        //FIXME: implement overwriting
124        try {
125            response = s.ajax_post(
126        DOKU_BASE + 'lib/exe/mediamanager.php',
127        encodedData,
128        'Filedata',
129        filename + ".supascreenshot",
130        [
131          {
132            name: 'sectok',
133            value: $("supa__sectok").value
134          },
135          {
136            name: 'ns',
137            value: $("supa__ns").value
138          },
139          {
140            name: 'id',
141            value: $("supa__filename").value + ".supascreenshot"
142          },
143          {
144            name: 'ow',
145            value: '1'
146          }
147        ]
148      );
149            //alert( "response: "+response );
150            if (response === "ok") {
151                // list() requires a location...
152                // as I don't know how to instantiate one, we're simulating...
153                loc = {
154                    search: "?ns=" + $("supa__ns").value
155                };
156
157                media_manager.list(null, loc);
158                //FIXME: both mediamanager.list and buildElements should paint the button!
159                // as we're overriding media_manager.list() this should already work but it dowsn't :(
160                // but neither does a direct call to buildElements :(
161                //supa_handler.buildElements();
162            } else {
163                supa_handler.msg(response);
164            }
165
166        } catch (ex) {
167            supa_handler.msg("Exception: " + ex);
168        }
169    },
170
171    createSupaApplet: function () {
172        var where, root, html;
173
174        function t(txt) {
175            return LANG['plugins']['supa'][txt];
176        }
177
178        function insertAfter(ref, newNode) {
179            ref.parentNode.insertBefore(newNode, ref.nextSibling);
180        }
181
182        where = $("dw__flashupload");
183        if (!where) {
184            return false;
185        }
186
187        root = document.createElement("div");
188        root.id = "supa__upload";
189        root.style.display = "none";
190
191        html = "";
192        html += "<div class='upload'>";
193        html += "  <button id='supa__pastebutton' type='button'>" + t("label_paste_image") + "</button><br/>";
194        html += "  <div style='border: 1px solid black;'>";
195        html += "    <applet id='" + supa_handler.applet_id + "' name='" + supa_handler.applet_name + "'";
196        html += "      archive='" + DOKU_BASE + "lib/plugins/supa/lib/Supa.jar'";
197        html += "      code='de.christophlinder.supa.SupaApplet.class'";
198        html += "      width='" + supa_handler.preview_width + "'";
199        html += "      height='" + supa_handler.preview_height + "'";
200        html += "      >";
201        html += "      <param name='imagecodec' value='png'/>";
202        html += "      <param name='previewscaler' value='" + supa_handler.preview_scaler + "'/>";
203        html += "      <param name='encoding' value='base64'/>";
204        html += "      " + t("txt_no_java");
205        html += "    </applet>";
206        html += "  </div>";
207        html += "  <br/>";
208
209        html += "  <input id='supa__sectok' type='hidden' value='" + supa_handler.sectok + "'/>";
210        html += "  <label for='supa__ns'>" + t("prompt_namespace") + "</label>";
211        html += "  <input id='supa__ns' value='" + supa_handler.namespace + "'/>";
212        html += "  <span title='" + t("txt_required_hint") + "'>" + t("txt_required") + "</span>";
213        html += "  <br/>";
214
215        html += "  <label for='supa__filename'>" + t("prompt_filename") + "</label>";
216        html += "  <input id='supa__filename' value='" + supa_handler.default_filename + "'/>";
217        html += "  <span title='" + t("txt_required_hint") + "'>" + t("txt_required") + "</span>";
218        html += "  <br/>";
219
220        html += "  <button id='supa__uploadbutton' type='button'>" + t("txt_upload_image") + "</button>";
221        html += "  <br/>";
222        html += "</div>";
223
224        root.innerHTML = html;
225
226        insertAfter(where, root);
227
228        return true;
229
230    },
231
232    createSupaButton: function () {
233        var uploadForm = $('dw__upload'),
234        supaDiv = $('supa__upload'),
235      spacer,
236      icon,
237      pastebutton,
238      uploadbutton;
239
240        if (!supaDiv || !uploadForm) {
241            return false;
242        }
243
244        spacer = document.createElement('span');
245        spacer.innerHTML = '&nbsp;&nbsp;&nbsp;';
246        uploadForm.appendChild(spacer);
247
248        icon = document.createElement('img');
249        icon.src = DOKU_BASE + 'lib/plugins/supa/supa.png';
250        icon.title = LANG['plugins']['supa']['upload_button_hint'];
251        icon.alt = LANG['plugins']['supa']['upload_button_alt'];
252        icon.style.cursor = 'pointer';
253        icon.onclick = function () {
254            uploadForm.style.display = 'none';
255            supaDiv.style.display = '';
256        };
257
258        uploadForm.appendChild(icon);
259
260        pastebutton = $('supa__pastebutton');
261        pastebutton.onclick = function (ev) {
262            supa_handler.pasteButtonHandler(ev);
263        };
264
265        uploadbutton = $('supa__uploadbutton');
266        uploadbutton.onclick = function (ev) {
267            supa_handler.uploadButtonHandler(ev);
268        };
269
270        return true;
271    },
272
273    buildElements: function () {
274        if (!supa_handler.createSupaApplet()) {
275            // we're not on the media manager or something strange happened
276            return;
277        }
278
279        if (!supa_handler.createSupaButton()) {
280            // we're not on the media manager or something strange happened
281            return;
282        }
283    },
284
285    init: function (sectok, preview_scaler, preview_width, preview_height, namespace, default_filename) {
286        supa_handler.preview_scaler = preview_scaler;
287        supa_handler.preview_width = preview_width;
288        supa_handler.preview_height = preview_height;
289        supa_handler.sectok = sectok;
290        supa_handler.namespace = namespace;
291        supa_handler.default_filename = default_filename;
292
293        supa_handler.buildElements();
294
295        // okay... this is eveil but I don't have any idea how to resolve this
296        // If the user clicks on a namespace link the page is refreshed via
297        // AJAX (.e.: media_manager.list()). But: javascript is not executed
298        // in this process so we need a way to re-add the button/applet
299        var origList = media_manager.list;
300        media_manager.list = function (event, link) {
301            origList(event, link);
302            supa_handler.buildElements();
303
304
305        };
306    },
307
308    initSupaUpload: function () {
309        // this is just a stub to not make dokuwiki crash in case the
310        // user forgot to remove the legacy source patch
311    }
312
313
314};
315
316