xref: /dokuwiki/lib/scripts/media.js (revision 7800517deb6ab16f8211f73adc961a5bcb1ea328)
1/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, strict: true, newcap: true, immed: true */
2/*global jQuery, window, DOKU_BASE*/
3"use strict";
4
5// * refactor once the jQuery port is over ;)
6
7/**
8 * JavaScript functionality for the media management popup
9 *
10 * @author Andreas Gohr <andi@splitbrain.org>
11 * @author Pierre Spring <pierre.spring@caillou.ch>
12 */
13(function ($) {
14    var toggle, list, prepare_content, insert, confirmattach, attachoptions, initpopup, updatehide, setalign, setsize, inSet, outSet, media_manager, hasFlash;
15
16    var media_manager = {
17        keepopen: false,
18        hide: false,
19        align: false,
20        popup: false,
21        display: false,
22        link: false,
23        size: false,
24        ext: false,
25    };
26
27
28
29
30    /**
31     * build the popup window
32     *
33     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
34     */
35    initpopup = function() {
36        var popup;
37
38        popup = document.createElement('div');
39        popup.setAttribute('id','media__popup');
40        popup.style.display = "none";
41
42        var root = document.getElementById('media__manager');
43        if (root === null) return;
44        root.appendChild(popup);
45
46        var headline    = document.createElement('h1');
47        headline.innerHTML = LANG.mediatitle;
48        var headlineimg = document.createElement('img');
49        headlineimg.src = DOKU_BASE + 'lib/images/close.png';
50        headlineimg.id  = 'media__closeimg';
51        $(headlineimg).click(function () {$(popup).hide()});
52        headline.insertBefore(headlineimg, headline.firstChild);
53        popup.appendChild(headline);
54        $(popup).draggable({handle: headline});
55
56        // link
57        var linkp = document.createElement('p');
58
59        linkp.id = "media__linkstyle";
60        if (media_manager.display == "2") {
61            linkp.style.display = "none";
62        }
63
64        var linkl = document.createElement('label');
65        linkl.innerHTML = LANG.mediatarget;
66        linkp.appendChild(linkl);
67
68        var linkbtns = ['lnk', 'direct', 'nolnk', 'displaylnk'];
69        for (var i = 0 ; i < linkbtns.length ; ++i) {
70            var linkbtn = document.createElement('button');
71            linkbtn.className = 'button';
72            linkbtn.id    = "media__linkbtn" + (i+1);
73            linkbtn.title = LANG['media' + linkbtns[i]];
74            linkbtn.style.borderStyle = 'outset';
75            $(linkbtn).click(function (event) { return setlink(event,this); });
76
77            var linkimg = document.createElement('img');
78            linkimg.src = DOKU_BASE + 'lib/images/media_link_' + linkbtns[i] + '.png';
79
80            linkbtn.appendChild(linkimg);
81            linkp.appendChild(linkbtn);
82        }
83
84        popup.appendChild(linkp);
85
86        // align
87
88        var alignp    = document.createElement('p');
89        var alignl    = document.createElement('label');
90
91        alignp.appendChild(alignl);
92        alignp.id = 'media__align';
93        if (media_manager.display == "2") {
94            alignp.style.display = "none";
95        }
96        alignl.innerHTML = LANG['mediaalign'];
97
98        var alignbtns = ['noalign', 'left', 'center', 'right'];
99        for (var n = 0 ; n < alignbtns.length ; ++n) {
100            var alignbtn = document.createElement('button');
101            var alignimg = document.createElement('img');
102            alignimg.src = DOKU_BASE + 'lib/images/media_align_' + alignbtns[n] + '.png';
103
104            alignbtn.id    = "media__alignbtn" + (n+1);
105            alignbtn.title = LANG['media' + alignbtns[n]];
106            alignbtn.className = 'button';
107            alignbtn.appendChild(alignimg);
108            alignbtn.style.borderStyle = 'outset';
109            $(alignbtn).click(function (event) { return setalign(event,this); });
110
111            alignp.appendChild(alignbtn);
112        }
113
114        popup.appendChild(alignp);
115
116        // size
117
118        var sizep    = document.createElement('p');
119        var sizel    = document.createElement('label');
120
121        sizep.id = 'media__size';
122        if (media_manager.display == "2") {
123            sizep.style.display = "none";
124        }
125        sizep.appendChild(sizel);
126        sizel.innerHTML = LANG['mediasize'];
127
128        var sizebtns = ['small', 'medium', 'large', 'original'];
129        for (var size = 0 ; size < sizebtns.length ; ++size) {
130            var sizebtn = document.createElement('button');
131            var sizeimg = document.createElement('img');
132
133            sizep.appendChild(sizebtn);
134            sizeimg.src = DOKU_BASE + 'lib/images/media_size_' + sizebtns[size] + '.png';
135
136            sizebtn.className = 'button';
137            sizebtn.appendChild(sizeimg);
138            sizebtn.id    = 'media__sizebtn' + (size + 1);
139            sizebtn.title = LANG['media' + sizebtns[size]];
140            sizebtn.style.borderStyle = 'outset';
141            $(sizebtn).click(function (event) { return setsize(event,this); });
142        }
143
144        popup.appendChild(sizep);
145
146        // send and close button
147
148        var btnp = document.createElement('p');
149        popup.appendChild(btnp);
150        btnp.setAttribute('class','btnlbl');
151
152        var btn  = document.createElement('input');
153        btn.type = 'button';
154        btn.id   = 'media__sendbtn';
155        btn.setAttribute('class','button');
156        btn.value = LANG['mediainsert'];
157        btnp.appendChild(btn);
158    };
159
160    // moved from helpers.js temporarily here
161    /**
162     * Very simplistic Flash plugin check, probably works for Flash 8 and higher only
163     *
164     */
165    hasFlash = function(version){
166        var ver = 0;
167        try{
168            if(navigator.plugins != null && navigator.plugins.length > 0){
169               ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0];
170            }else{
171               var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
172               ver = axo.GetVariable("$version").split(' ')[1].split(',')[0];
173            }
174        }catch(e){ }
175
176        if(ver >= version) return true;
177        return false;
178    };
179
180    /**
181     * Insert the clicked image into the opener's textarea
182     *
183     * @author Andreas Gohr <andi@splitbrain.org>
184     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
185     * @author Pierre Spring <pierre.spring@caillou.ch>
186     */
187    insert = function (id) {
188        var opts, optsstart, alignleft, alignright;
189
190        // set syntax options
191        $('#media__popup').hide();
192
193        opts = '';
194        optsstart = '';
195        alignleft = '';
196        alignright = '';
197
198        if (media_manager.ext == 'img' || media_manager.ext == 'swf') {
199
200            if (media_manager.link == '4') {
201                    opts = '?linkonly';
202            } else {
203
204                if (media_manager.link == "3" && media_manager.ext == 'img') {
205                    opts = '?nolink';
206                    optsstart = true;
207                } else if (media_manager.link == "2" && media_manager.ext == 'img') {
208                    opts = '?direct';
209                    optsstart = true;
210                }
211
212                var s = parseInt(media_manager.size, 10);
213
214                if (s && s >= 1) {
215                    opts += (optsstart)?'&':'?';
216                    if (s=="1") {
217                        opts += '100';
218                        if (media_manager.ext == 'swf') {
219                            opts += 'x62';
220                        }
221                    } else if (s=="2") {
222                        opts += '200';
223                        if (media_manager.ext == 'swf') {
224                            opts += 'x123';
225                        }
226                    } else if (s=="3"){
227                        opts += '300';
228                        if (media_manager.ext == 'swf') {
229                            opts += 'x185';
230                        }
231                    }
232                }
233                if (media_manager.align == '2') {
234                    alignleft = '';
235                    alignright = ' ';
236                }
237                if (media_manager.align == '3') {
238                    alignleft = ' ';
239                    alignright = ' ';
240                }
241                if (media_manager.align == '4') {
242                    alignleft = ' ';
243                    alignright = '';
244                }
245            }
246        }
247        var edid = String.prototype.match.call(document.location, /&edid=([^&]+)/);
248        edid = edid ? edid[1] : 'wiki__text';
249        opener.insertTags(edid,'{{'+alignleft+id+opts+alignright+'|','}}','');
250
251        if(!media_manager.keepopen) window.close();
252        opener.focus();
253        return false;
254    };
255
256    /**
257     * Prefills the wikiname.
258     *
259     * @author Andreas Gohr <andi@splitbrain.org>
260     */
261    suggest = function(){
262        var file, name, text;
263
264        file = $(this);
265        name = $('#upload__name');
266        if(!file.size() || !name.size()) return;
267
268        text = file.val();
269        text = text.substr(text.lastIndexOf('/')+1);
270        text = text.substr(text.lastIndexOf('\\')+1);
271        name.val(text);
272    };
273
274    /**
275     * Open or close a subtree using AJAX
276     *
277     * @author Andreas Gohr <andi@splitbrain.org>
278     * @author Pierre Spring <pierre.spring@caillou.ch>
279     */
280    toggle = function (event) {
281        var clicky, listitem, sublist, link, ul;
282
283        event.preventDefault(); // TODO: really here?
284
285        var clicky = $(this);
286        var listitem = clicky.parent();
287
288        // if already open, close by removing the sublist
289        sublist = listitem.find('ul').first();
290        if(sublist.size()){
291            sublist.remove(); // TODO: really? we could just hide it, right?
292            clicky.attr('src', DOKU_BASE + 'lib/images/plus.gif');
293            return;
294        }
295
296        // get the enclosed link (is always the first one)
297        link = listitem.find('a').first();
298
299        //prepare the new ul
300        ul = $('<ul/>');
301
302        //fixme add classname here
303
304        $.post(
305            DOKU_BASE + 'lib/exe/ajax.php',
306            link.attr('search').substr(1) + '&call=medians',
307            function (data) {
308                ul.html(data);
309                listitem.append(ul);
310            },
311            'html'
312        );
313
314        clicky.attr('src', DOKU_BASE + 'lib/images/minus.gif');
315    };
316
317    /**
318     * list the content of a namespace using AJAX
319     *
320     * @author Andreas Gohr <andi@splitbrain.org>
321     * @author Pierre Spring <pierre.spring@caillou.ch>
322     */
323    list = function (event) {
324        var link, content;
325        link = $(this);
326
327        event.preventDefault();
328
329        cleanMsgArea();
330        content = $('#media__content');
331        content.html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
332
333        // fetch the subtree
334        $.post(
335            DOKU_BASE + 'lib/exe/ajax.php',
336            link.attr('search').substr(1)+'&call=medialist',
337            function (data) {
338                content.html(data);
339                prepare_content(content);
340                updatehide();
341            },
342            'html'
343        );
344
345    };
346
347    prepare_content = function (content) {
348        // hide syntax example
349        content.find('div.example:visible').hide();
350        initFlashUpload();
351    };
352
353    /**
354         * shows the popup for a image link
355         */
356        select = function(event){
357            var link, id, dot, ext;
358
359            event.preventDefault();
360
361            link = $(this);
362            id = link.attr('name').substr(2);
363
364            if(!opener){
365                // if we don't run in popup display example
366                // the id's are a bit wired and $('#ex_wiki_dokuwiki-128.png') will not be found
367                // by Sizzle (the CSS Selector Engine used by jQuery),
368                // hence the document.getElementById() call
369                $(document.getElementById('ex_'+id.replace(/:/g,'_').replace(/^_/,''))).toggle();
370                return;
371            }
372
373            link = link[0];
374
375            media_manager.ext = false;
376            dot = id.lastIndexOf(".");
377
378            if (-1 === dot) {
379                insert(id);
380                return;
381            }
382
383            ext = id.substr(dot);
384
385            if (ext != '.jpg' && ext != '.jpeg' && ext != '.png' && ext != '.gif' && ext != '.swf') {
386                insert(id);
387                return;
388            }
389
390            // remove old callback from the insert button and set the new one.
391            $('#media__sendbtn').unbind().click(function () {insert(id)});
392
393            $('#media__popup').show()
394                .css('left', event.pageX + 'px')
395                .css('top', event.pageY + 'px');
396
397            $('#media__popup button.button').each(function (index, element) { outSet(element) } );
398
399
400            if (ext == '.swf') {
401                media_manager.ext = 'swf';
402
403                // disable display buttons for detail and linked image
404                $('#media__linkbtn1').hide();
405                $('#media__linkbtn2').hide();
406
407                // set the link button to default
408                if (media_manager.link != false) {
409                    if ( media_manager.link == '2' || media_manager.link == '1')  {
410                        inSet('media__linkbtn3');
411                        media_manager.link = '3';
412                        DokuCookie.setValue('link','3');
413                    } else {
414                        inSet('media__linkbtn'+media_manager.link);
415                    }
416                } else if (DokuCookie.getValue('link')) {
417                    if ( DokuCookie.getValue('link') == '2' ||  DokuCookie.getValue('link') == '1')  {
418                        // this options are not availible
419                        inSet('media__linkbtn3');
420                        media_manager.link = '3';
421                        DokuCookie.setValue('link','3');
422                    } else {
423                        inSet('media__linkbtn'+DokuCookie.getValue('link'));
424                        media_manager.link = DokuCookie.getValue('link');
425                    }
426                } else {
427                    // default case
428                    media_manager.link = '3';
429                    inSet('media__linkbtn3');
430                    DokuCookie.setValue('link','3');
431                }
432
433                // disable button for original size
434                $('#media__sizebtn4').hide();
435
436            } else {
437                media_manager.ext = 'img';
438
439                // ensure that the display buttons are there
440                $('#media__linkbtn1').show();
441                $('#media__linkbtn2').show();
442                $('#media__sizebtn4').show();
443
444                // set the link button to default
445                if (DokuCookie.getValue('link')) {
446                    media_manager.link = DokuCookie.getValue('link');
447                }
448                if (media_manager.link == false) {
449                    // default case
450                    media_manager.link = '1';
451                    DokuCookie.setValue('link','1');
452                }
453                inSet('media__linkbtn'+media_manager.link);
454            }
455
456            if (media_manager.link == '4') {
457                media_manager.align = false;
458                media_manager.size = false;
459                $('#media__align').hide();
460                $('#media__size').hide();
461            } else {
462                $('#media__align').show();
463                $('#media__size').show();
464
465                // set the align button to default
466                if (media_manager.align != false) {
467                    inSet('media__alignbtn'+media_manager.align);
468                } else if (DokuCookie.getValue('align')) {
469                    inSet('media__alignbtn'+DokuCookie.getValue('align'));
470                    media_manager.align = DokuCookie.getValue('align');
471                } else {
472                    // default case
473                    media_manager.align = '0';
474                    inSet('media__alignbtn0');
475                    DokuCookie.setValue('align','0');
476                }
477
478                // set the size button to default
479                if (DokuCookie.getValue('size')) {
480                    media_manager.size = DokuCookie.getValue('size');
481                }
482                if (media_manager.size == false || (media_manager.size === '4' && ext === '.swf')) {
483                    // default case
484                    media_manager.size = '2';
485                    DokuCookie.setValue('size','2');
486                }
487                inSet('media__sizebtn'+media_manager.size);
488
489                $('#media__sendbtn').focus();
490            }
491
492           return;
493        };
494
495    /**
496     * Deletion confirmation dialog to the delete buttons.
497     *
498     * @author Michael Klier <chi@chimeric.de>
499     * @author Pierre Spring <pierre.spring@caillou.ch>
500     */
501    confirmattach = function(e){
502        if(!confirm(LANG['del_confirm'] + "\n" + jQuery(this).attr('title'))) {
503            e.preventDefault();
504        }
505    };
506
507    /**
508     * Creates checkboxes for additional options
509     *
510     * @author Andreas Gohr <andi@splitbrain.org>
511     * @author Pierre Spring <pierre.spring@caillou.ch>
512     */
513    attachoptions = function(){
514        obj = $('#media__opts')[0];
515        if(!obj) return;
516
517        // keep open
518        if(opener){
519            var kobox  = document.createElement('input');
520            kobox.type = 'checkbox';
521            kobox.id   = 'media__keepopen';
522            if(DokuCookie.getValue('keepopen')){
523                kobox.checked  = true;
524                kobox.defaultChecked = true; //IE wants this
525                media_manager.keepopen = true;
526            }
527
528            $(kobox).click(
529                function () {
530                    toggleOption(this, 'keepopen');
531                }
532            );
533
534            var kolbl  = document.createElement('label');
535            kolbl.htmlFor   = 'media__keepopen';
536            kolbl.innerHTML = LANG['keepopen'];
537
538            var kobr = document.createElement('br');
539
540            obj.appendChild(kobox);
541            obj.appendChild(kolbl);
542            obj.appendChild(kobr);
543        }
544
545        // hide details
546        var hdbox  = document.createElement('input');
547        hdbox.type = 'checkbox';
548        hdbox.id   = 'media__hide';
549        if(DokuCookie.getValue('hide')){
550            hdbox.checked = true;
551            hdbox.defaultChecked = true; //IE wants this
552            media_manager.hide    = true;
553        }
554        $(hdbox).click(
555            function () {
556                toggleOption(this, 'hide');
557                updatehide();
558            }
559        );
560
561        var hdlbl  = document.createElement('label');
562        hdlbl.htmlFor   = 'media__hide';
563        hdlbl.innerHTML = LANG['hidedetails'];
564
565        var hdbr = document.createElement('br');
566
567        obj.appendChild(hdbox);
568        obj.appendChild(hdlbl);
569        obj.appendChild(hdbr);
570        updatehide();
571    };
572
573    /**
574     * Generalized toggler
575     *
576     * @author Pierre Spring <pierre.spring@caillou.ch>
577     */
578    toggleOption = function (checkbox, variable) {
579        if (checkbox.checked) {
580            DokuCookie.setValue(variable, 1);
581            media_manager[variable] = true;
582        } else {
583            DokuCookie.setValue(variable, '');
584            media_manager[variable] = false;
585        }
586    };
587
588    initFlashUpload = function () {
589        var oform, oflash, title;
590        if(!hasFlash(8)) return;
591
592        oform  = $('#dw__upload');
593        oflash = $('#dw__flashupload');
594
595        if(!oform.size() || !oflash.size()) return;
596
597        title = LANG['mu_btn'];
598
599        $('<img/>').attr('src', DOKU_BASE+'lib/images/multiupload.png')
600            .attr('title', title)
601            .attr('alt', title)
602            .css('cursor', 'pointer')
603            .click(
604                function () {
605                    oform.hide();
606                    oflash.show();
607                }
608            )
609            .appendTo(oform);
610    };
611
612    /**
613     * Sets the visibility of the image details accordingly to the
614     * chosen hide state
615     *
616     * @author Andreas Gohr <andi@splitbrain.org>
617     */
618    updatehide = function(){
619        var content = $('#media__content');
620        if(0 === content.size()) {
621            return;
622        }
623        content.find('div.detail').each(
624            function (index, element) {
625                if(media_manager.hide){
626                    element.style.display = 'none';
627                }else{
628                    element.style.display = '';
629                }
630            }
631
632        );
633    };
634
635    /**
636     * set the align
637     *
638     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
639     */
640    setalign = function(event,cb){
641
642        var id = cb.id.substring(cb.id.length -1);
643        if(id){
644            DokuCookie.setValue('align',id);
645            media_manager.align = id;
646            for (var i = 1; i<=4; i++) {
647                outSet("media__alignbtn" + i);
648            }
649            inSet("media__alignbtn"+id);
650        }else{
651            DokuCookie.setValue('align','');
652            media_manager.align = false;
653        }
654    };
655
656    /**
657     * set the link type
658     *
659     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
660     */
661    setlink = function(event,cb){
662        var id = cb.id.substring(cb.id.length -1);
663        if(id){
664            DokuCookie.setValue('link',id);
665            for (var i = 1; i<=4; i++) {
666                outSet("media__linkbtn"+i);
667            }
668            inSet("media__linkbtn"+id);
669
670            var size = $("#media__size");
671            var align = $("#media__align");
672            if (id != '4') {
673                size.show();
674                align.show();
675                if (media_manager.link == '4') {
676                    media_manager.align = '1';
677                    DokuCookie.setValue('align', '1');
678                    inSet('media__alignbtn1');
679
680                    media_manager.size = '2';
681                    DokuCookie.setValue('size', '2');
682                    inSet('media__sizebtn2');
683                }
684
685            } else {
686                size.hide();
687                align.hide();
688            }
689            media_manager.link = id;
690        }else{
691            DokuCookie.setValue('link','');
692            media_manager.link = false;
693        }
694    };
695
696    /**
697     * set the image size
698     *
699     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
700     */
701    setsize = function(event,cb){
702        var id = cb.id.substring(cb.id.length -1);
703        if (id) {
704            DokuCookie.setValue('size',id);
705            media_manager.size = id;
706            for (var i = 1 ; i <=4 ; ++i) {
707                outSet("media__sizebtn" + i);
708            }
709            inSet("media__sizebtn"+id);
710        } else {
711            DokuCookie.setValue('size','');
712            media_manager.width = false;
713        }
714    };
715
716    /**
717     * sets the border to inset
718     */
719    inSet = function(id) {
720        var ele = $('#' + id).css('border-style', 'inset');
721    };
722
723    /**
724     * sets the border to outset
725     */
726    outSet = function(element) {
727        if ('string' === typeof element) {
728            element = '#' + element;
729        }
730        $(element).css('border-style', 'outset');
731    };
732
733    $(function () {
734        var content = $('#media__content');
735        prepare_content(content);
736
737        attachoptions();
738        initpopup();
739
740        // add the action to autofill the "upload as" field
741        content.delegate('#upload__file', 'change', suggest)
742            // Attach the image selector action to all links
743            .delegate('a.select', 'click', select)
744            // Attach deletion confirmation dialog to the delete buttons
745            .delegate('#media__content a.btn_media_delete', 'click', confirmattach);
746
747
748        $('#media__tree').delegate('img', 'click', toggle)
749            .delegate('a', 'click', list);
750    });
751}(jQuery));
752