xref: /dokuwiki/lib/scripts/media.js (revision 9bd9c4373f31bb381fd80564decd8671d942fdc8)
1/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, strict: true, newcap: true, immed: true, sloppy: true, browser: true */
2/*global jQuery, DOKU_BASE, LANG, bind, DokuCookie, opener, confirm*/
3
4/**
5 * JavaScript functionality for the media management popup
6 *
7 * @author Andreas Gohr <andi@splitbrain.org>
8 * @author Pierre Spring <pierre.spring@caillou.ch>
9 */
10
11var dw_mediamanager = {
12    keepopen: false,
13    hide: false,
14    popup: false,
15    display: false,
16    ext: false,
17    $popup: null,
18
19    // Image insertion opts
20    align: false,
21    link: false,
22    size: false,
23    forbidden_opts: {},
24
25    // File list view type
26    view: false,
27
28    layout_width: 0,
29
30    init: function () {
31        var $content, $tree;
32        $content = jQuery('#media__content');
33        $tree    = jQuery('#media__tree');
34
35        dw_mediamanager.prepare_content($content);
36
37        dw_mediamanager.attachoptions();
38        dw_mediamanager.initpopup();
39
40        // add the action to autofill the "upload as" field
41        $content.delegate('#upload__file', 'change', dw_mediamanager.suggest)
42                // Attach the image selector action to all links
43                .delegate('a.select', 'click', dw_mediamanager.select)
44                // Attach deletion confirmation dialog to the delete buttons
45                .delegate('#media__content a.btn_media_delete', 'click',
46                          dw_mediamanager.confirmattach);
47
48        $tree.dw_tree({toggle_selector: 'img',
49                       load_data: function (show_sublist, $clicky) {
50                           // get the enclosed link (is always the first one)
51                           var $link = $clicky.parent().find('div.li a.idx_dir');
52
53                           jQuery.post(
54                               DOKU_BASE + 'lib/exe/ajax.php',
55                               $link[0].search.substr(1) + '&call=medians',
56                               show_sublist,
57                               'html'
58                           );
59                       },
60
61                       toggle_display: function ($clicky, opening) {
62                           $clicky.attr('src',
63                                        DOKU_BASE + 'lib/images/' +
64                                        (opening ? 'minus' : 'plus') + '.gif');
65                       }});
66        $tree.delegate('a', 'click', dw_mediamanager.list);
67
68        dw_mediamanager.set_filelist_view(dw_mediamanager.view, false);
69        jQuery('#mediamanager__form_sort').find('input[type=submit]').hide();
70        dw_mediamanager.image_diff();
71
72        // changing opened tab in the file list panel
73        jQuery('#mediamanager__layout_list').delegate('#mediamanager__tabs_files a', 'click', dw_mediamanager.list)
74            // changing type of the file list view
75            .delegate('#mediamanager__tabs_list a', 'click', dw_mediamanager.list_view)
76            // loading file details
77            .delegate('#mediamanager__file_list a', 'click', dw_mediamanager.details)
78            // search form
79            .delegate('#dw__mediasearch', 'submit', dw_mediamanager.list)
80            // "upload as" field autofill
81            .delegate('#upload__file', 'change', dw_mediamanager.suggest)
82            // sort type selection
83            .delegate('#mediamanager__form_sort select', 'change', dw_mediamanager.list);
84
85        // changing opened tab in the file details panel
86        jQuery('#mediamanager__layout_detail').delegate('#mediamanager__tabs_details a', 'click', dw_mediamanager.details)
87            // "update new version" button
88            .delegate('#mediamanager__btn_update', 'submit', dw_mediamanager.list)
89            // revisions form
90            .delegate('#page__revisions', 'submit', dw_mediamanager.details)
91            .delegate('#page__revisions a', 'click', dw_mediamanager.details)
92            // meta edit form
93            .delegate('#mediamanager__save_meta', 'submit', dw_mediamanager.details)
94            // delete button
95            .delegate('#mediamanager__btn_delete', 'submit', dw_mediamanager.details)
96            // "restore this version" button
97            .delegate('#mediamanager__btn_restore', 'submit', dw_mediamanager.details);
98
99    },
100
101    /**
102     * build the popup window
103     *
104     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
105     */
106    initpopup: function () {
107        var opts, $insp, $insbtn;
108
109        dw_mediamanager.$popup = jQuery(document.createElement('div'))
110                 .attr('id', 'media__popup_content')
111                 .dialog({autoOpen: false, width: 280, modal: true,
112                          draggable: true, title: LANG.mediatitle,
113                          resizable: false});
114
115        opts = [{id: 'link', label: LANG.mediatarget,
116                 btns: ['lnk', 'direct', 'nolnk', 'displaylnk']},
117                {id: 'align', label: LANG.mediaalign,
118                 btns: ['noalign', 'left', 'center', 'right']},
119                {id: 'size', label: LANG.mediasize,
120                 btns: ['small', 'medium', 'large', 'original']}
121               ];
122
123        jQuery.each(opts, function (_, opt) {
124            var $p, $l;
125            $p = jQuery(document.createElement('p'))
126                 .attr('id', 'media__' + opt.id);
127
128            if (dw_mediamanager.display === "2") {
129                $p.hide();
130            }
131
132            $l = jQuery(document.createElement('label'))
133                 .text(opt.label);
134            $p.append($l);
135
136            jQuery.each(opt.btns, function (i, text) {
137                var $btn, $img;
138                $btn = jQuery(document.createElement('button'))
139                       .addClass('button')
140                       .attr('id', "media__" + opt.id + "btn" + (i + 1))
141                       .attr('title', LANG['media' + text])
142                       .click(bind(dw_mediamanager.setOpt, opt.id));
143
144                $img = jQuery(document.createElement('img'))
145                       .attr('src', DOKU_BASE + 'lib/images/media_' +
146                                    opt.id + '_' + text + '.png');
147
148                $btn.append($img);
149                $p.append($btn);
150            });
151
152            dw_mediamanager.$popup.append($p);
153        });
154
155        // insert button
156        $insp = jQuery(document.createElement('p'))
157                .addClass('btnlbl');
158        dw_mediamanager.$popup.append($insp);
159
160        $insbtn = jQuery(document.createElement('input'))
161                  .attr('id', 'media__sendbtn')
162                  .attr('type', 'button')
163                  .addClass('button')
164                  .val(LANG.mediainsert);
165        $insp.append($insbtn);
166    },
167
168    /**
169     * Insert the clicked image into the opener's textarea
170     *
171     * @author Andreas Gohr <andi@splitbrain.org>
172     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
173     * @author Pierre Spring <pierre.spring@caillou.ch>
174     */
175    insert: function () {
176        var opts, alignleft, alignright, edid, s;
177
178        // set syntax options
179        dw_mediamanager.$popup.dialog('close');
180
181        opts = '';
182        alignleft = '';
183        alignright = '';
184
185        if ({img: 1, swf: 1}[dw_mediamanager.ext] === 1) {
186
187            if (dw_mediamanager.link === '4') {
188                    opts = '?linkonly';
189            } else {
190
191                if (dw_mediamanager.link === "3" && dw_mediamanager.ext === 'img') {
192                    opts = '?nolink';
193                } else if (dw_mediamanager.link === "2" && dw_mediamanager.ext === 'img') {
194                    opts = '?direct';
195                }
196
197                s = parseInt(dw_mediamanager.size, 10);
198
199                if (s && s >= 1 && s < 4) {
200                    opts += (opts.length)?'&':'?';
201                    opts += dw_mediamanager.size + '00';
202                    if (dw_mediamanager.ext === 'swf') {
203                        switch (s) {
204                        case 1:
205                            opts += 'x62';
206                            break;
207                        case 2:
208                            opts += 'x123';
209                            break;
210                        case 3:
211                            opts += 'x185';
212                            break;
213                        }
214                    }
215                }
216                alignleft = dw_mediamanager.align === '2' ? '' : ' ';
217                alignright = dw_mediamanager.align === '4' ? '' : ' ';
218            }
219        }
220        edid = String.prototype.match.call(document.location, /&edid=([^&]+)/);
221        opener.insertTags(edid ? edid[1] : 'wiki__text',
222                          '{{'+alignleft+id+opts+alignright+'|','}}','');
223
224        if(!dw_mediamanager.keepopen) {
225            window.close();
226        }
227        opener.focus();
228        return false;
229    },
230
231    /**
232     * Prefills the wikiname.
233     *
234     * @author Andreas Gohr <andi@splitbrain.org>
235     */
236    suggest: function(){
237        var $file, $name, text;
238
239        $file = jQuery(this);
240        $name = jQuery('#upload__name');
241
242        if ($name.val() != '') return;
243
244        if(!$file.length || !$name.length) {
245            return;
246        }
247
248        text = $file.val();
249        text = text.substr(text.lastIndexOf('/')+1);
250        text = text.substr(text.lastIndexOf('\\')+1);
251        $name.val(text);
252    },
253
254    /**
255     * list the content of a namespace using AJAX
256     *
257     * @author Andreas Gohr <andi@splitbrain.org>
258     * @author Pierre Spring <pierre.spring@caillou.ch>
259     */
260    list: function (event) {
261        var $link, $content, params;
262        $link = jQuery(this);
263
264        event.preventDefault();
265
266        jQuery('div.success, div.info, div.error, div.notify').remove();
267
268        if (document.getElementById('media__content')) {
269            //popup
270            $content = jQuery('#media__content');
271            $content.html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
272
273        } else {
274            //fullscreen media manager
275            $content = jQuery('#mediamanager__layout_list');
276
277            if ($link.hasClass('idx_dir')) {
278                //changing namespace
279                jQuery('#mediamanager__layout_detail').empty();
280                jQuery('#media__tree .selected').each(function(){
281                    jQuery(this).removeClass('selected');
282                });
283                $link.addClass('selected');
284            }
285
286            jQuery('.scroll-container', $content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
287        }
288
289        params = '';
290
291        if ($link[0].search) {
292            params = $link[0].search.substr(1)+'&call=medialist';
293        } else if ($link[0].action) {
294            params = dw_mediamanager.form_params($link)+'&call=medialist';
295        } else if ($link.parents('form')) {
296            params = dw_mediamanager.form_params($link.parents('form'))+'&call=medialist';
297
298            if ($link.parents('form')[0].id == 'mediamanager__form_sort') {
299                DokuCookie.setValue('sort', $link[0].value);
300            }
301        }
302
303        // fetch the subtree
304        dw_mediamanager.update_content($content, params);
305
306    },
307
308     /**
309     * Returns form parameters
310     *
311     * @author Kate Arzamastseva <pshns@ukr.net>
312     */
313    form_params: function ($form) {
314        if (!$form.length) return;
315        var elements = $form.serialize();
316        var action = '';
317        var i = $form[0].action.indexOf('?');
318        if (i >= 0) action = $form[0].action.substr(i+1);
319        return elements+'&'+action;
320    },
321
322     /**
323     * Changes view of media files list
324     *
325     * @author Kate Arzamastseva <pshns@ukr.net>
326     */
327    list_view: function (event) {
328        var $link, $content;
329        $link = jQuery(this);
330
331        event.preventDefault();
332
333        $content = jQuery('#mediamanager__file_list');
334
335        if ($link[0].id == 'mediamanager__link_thumbs') {
336            dw_mediamanager.set_filelist_view('thumbs', true);
337
338        } else if ($link[0].id == 'mediamanager__link_list') {
339            dw_mediamanager.set_filelist_view('list', true);
340        }
341    },
342
343    set_filelist_view: function (type, cookies) {
344        var $content = jQuery('#mediamanager__file_list');
345        if (!type) type = DokuCookie.getValue('view');
346
347        if (type == 'thumbs') {
348            $content.removeClass('mediamanager-list');
349            $content.addClass('mediamanager-thumbs');
350            if (cookies) DokuCookie.setValue('view', 'thumbs');
351            dw_mediamanager.view = 'thumbs';
352
353        } else if (type == 'list') {
354            $content.removeClass('mediamanager-thumbs');
355            $content.addClass('mediamanager-list');
356            if (cookies) DokuCookie.setValue('view', 'list');
357            dw_mediamanager.view = 'list';
358        }
359    },
360
361     /**
362     * Lists the content of the right column (image details) using AJAX
363     *
364     * @author Kate Arzamastseva <pshns@ukr.net>
365     */
366    details: function (event) {
367        var $link, $content, params, update_list;
368        $link = jQuery(this);
369
370        event.preventDefault();
371
372        jQuery('div.success, div.info, div.error, div.notify').remove();
373
374        if ($link[0].id == 'mediamanager__btn_delete' && !confirm(LANG['del_confirm'])) return false;
375        if ($link[0].id == 'mediamanager__btn_restore' && !confirm(LANG['restore_confirm'])) return false;
376
377        $content = jQuery('#mediamanager__layout_detail');
378        if (jQuery('.scroll-container', $content).length) {
379            jQuery('.scroll-container', $content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
380        } else {
381            jQuery($content).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
382        }
383
384        params = '';
385
386        if ($link[0].search) {
387            params = $link[0].search.substr(1)+'&call=mediadetails';
388        } else if ($link[0].action) {
389            params = dw_mediamanager.form_params($link)+'&call=mediadetails';
390        } else if ($link.parents('form')) {
391            params = dw_mediamanager.form_params($link.parents('form'))+'&call=mediadetails';
392        }
393
394        dw_mediamanager.update_content($content, params);
395
396        update_list = ($link[0].id == 'mediamanager__btn_delete' || $link[0].id == 'mediamanager__btn_restore');
397        if (update_list) {
398            var $link1, $content1, params1;
399            $link1 = jQuery('a.files');
400            params1 = $link1[0].search.substr(1)+'&call=medialist';
401            $content1 = jQuery('#mediamanager__layout_list');
402            jQuery('.scroll-container', $content1).html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />');
403
404            dw_mediamanager.update_content($content1, params1);
405        }
406    },
407
408    update_content: function ($content, params) {
409        jQuery.post(
410            DOKU_BASE + 'lib/exe/ajax.php',
411            params,
412            function (data) {
413                jQuery('.ui-resizable').each(function(){
414                    jQuery(this).resizable('destroy');
415                });
416
417                $content.html(data);
418
419                dw_mediamanager.prepare_content($content);
420                dw_mediamanager.updatehide();
421
422                dw_mediamanager.update_resizable();
423                addInitEvent(revisionsForm);
424                jQuery('#mediamanager__form_sort').find('input[type=submit]').hide();
425                dw_mediamanager.set_filelist_view(dw_mediamanager.view, false);
426                dw_mediamanager.image_diff();
427            },
428            'html'
429        );
430    },
431
432    window_resize: function () {
433        if (jQuery('#mediamanager__layout').width() == dw_mediamanager.layout_width) {
434            return;
435        }
436
437        dw_mediamanager.layout_width = jQuery('#mediamanager__layout').width();
438
439        $r = jQuery("#mediamanager__layout .layout-resizable, #mediamanager__layout .layout");
440
441        var w = 0, wSum = 0, mCount = 0, mArray = [];
442        $r.each(function() {
443            w = jQuery(this).width();
444            if (w == parseFloat(jQuery(this).css("min-width"))) {
445                wSum += w;
446            } else {
447                mArray[mCount] = jQuery(this);
448                mCount++;
449            }
450        });
451
452        if (mCount > 0) {
453            var width = (0.95 * jQuery('#mediamanager__layout').width() - wSum - 30);
454            wSum = 0;
455            for(var i = 0; i < mArray.length; i++) {
456                wSum += mArray[i].width();
457            }
458            for(var i = 0; i < mArray.length; i++) {
459                w = mArray[i].width();
460                w = 100*w / wSum;
461                mArray[i].width(width*w/100);
462            }
463        }
464
465        $r.each(function() {
466            w = jQuery(this).width();
467            w = (100 * w / jQuery('#mediamanager__layout').width());
468            w += "%";
469            jQuery(this).width(w);
470        });
471    },
472
473    /**
474     * Updates mediamanager layout
475     *
476     * @author Kate Arzamastseva <pshns@ukr.net>
477     */
478    update_resizable: function () {
479        $resizable = jQuery("#mediamanager__layout .layout-resizable");
480
481        $resizable.resizable({ handles: 'e' ,
482            resize: function(event, ui){
483                var w = 0;
484                $resizable.each(function() {
485                    w += jQuery(this).width();
486                });
487                wSum = w + parseFloat(jQuery('#mediamanager__layout_detail').css("min-width"));
488
489                // max width of resizable column
490                var maxWidth = 0.95 * jQuery('#mediamanager__layout').width() - wSum + jQuery(this).width() - 30;
491                $resizable.resizable( "option", "maxWidth", maxWidth );
492
493                // percentage width of the first two columns
494                var wLeft = ( 100*(w+30) / jQuery('#mediamanager__layout').width() );
495
496                // width of the third column
497                var wRight = 95-wLeft;
498                wRight += "%";
499                jQuery('#mediamanager__layout_detail').width(wRight);
500
501                $resizable.each(function() {
502                    w = jQuery(this).width();
503                    w = (100 * w / jQuery('#mediamanager__layout').width());
504                    w += "%";
505                    jQuery(this).width(w);
506                });
507            }
508        });
509
510        var windowHeight = jQuery(window).height();
511        var height = windowHeight - 300;
512        jQuery('#mediamanager__layout .scroll-container').each(function (i) {
513            jQuery(this).height(height);
514        });
515        $resizable.each(function() {
516            jQuery(this).height(height+100);
517        });
518    },
519
520     /**
521     * Prints 'select' for image difference representation type
522     *
523     * @author Kate Arzamastseva <pshns@ukr.net>
524     */
525    image_diff: function () {
526        if (jQuery('#mediamanager__difftype').length) return;
527
528        $form = jQuery('#mediamanager__form_diffview');
529        if (!$form.length) return;
530
531        $label = jQuery(document.createElement('label'));
532        $label.append('<span>'+LANG.media_diff+'</span>');
533        $select = jQuery(document.createElement('select'))
534         .attr('id', 'mediamanager__difftype')
535         .attr('name', 'difftype')
536         .change(dw_mediamanager.change_diff_type);
537        $select.append(new Option(LANG.media_diff_both, "both"));
538        $select.append(new Option(LANG.media_diff_opacity, "opacity"));
539        $select.append(new Option(LANG.media_diff_portions, "portions"));
540        $label.append($select);
541        $form.append($label);
542    },
543
544    /**
545     * Handles selection of image difference representation type
546     *
547     * @author Kate Arzamastseva <pshns@ukr.net>
548     */
549    change_diff_type: function () {
550        $select = jQuery('#mediamanager__difftype');
551        $content = jQuery('#mediamanager__diff');
552
553        params = dw_mediamanager.form_params($select.parents('form'))+'&call=mediadiff';
554        jQuery.post(
555            DOKU_BASE + 'lib/exe/ajax.php',
556            params,
557            function (data) {
558                $content.html(data);
559                dw_mediamanager.opacity_slider();
560                dw_mediamanager.portions_slider();
561            },
562            'html'
563        );
564    },
565
566    /**
567     * Sets options for opacity diff slider
568     *
569     * @author Kate Arzamastseva <pshns@ukr.net>
570     */
571    opacity_slider: function () {
572        var $slider = jQuery( "#mediamanager__opacity_slider" );
573        $slider.slider();
574        $slider.slider("option", "min", 0);
575        $slider.slider("option", "max", 0.999);
576        $slider.slider("option", "step", 0.001);
577        $slider.slider("option", "value", 0.5);
578        $slider.bind("slide", function(event, ui) {
579            jQuery('#mediamanager__diff_opacity_image2').css({ opacity: $slider.slider("option", "value")});
580        });
581    },
582
583     /**
584     * Sets options for red line diff slider
585     *
586     * @author Kate Arzamastseva <pshns@ukr.net>
587     */
588    portions_slider: function () {
589        var $slider = jQuery( "#mediamanager__portions_slider" );
590        $slider.slider();
591        $slider.slider("option", "min", 0);
592        $slider.slider("option", "max", 100);
593        $slider.slider("option", "step", 1);
594        $slider.slider("option", "value", 50);
595        $slider.bind("slide", function(event, ui) {
596            jQuery('#mediamanager__diff_portions_image2').css({ width: $slider.slider("option", "value")+'%'});
597        });
598    },
599
600    prepare_content: function ($content) {
601        // hide syntax example
602        $content.find('div.example:visible').hide();
603        dw_mediamanager.initFlashUpload();
604    },
605
606    /**
607     * shows the popup for a image link
608     */
609    select: function(event){
610        var $link, id, dot, ext;
611
612        event.preventDefault();
613
614        $link = jQuery(this);
615        id = $link.attr('name').substr(2);
616
617        if(!opener){
618            // if we don't run in popup display example
619            // the id's are a bit wierd and jQuery('#ex_wiki_dokuwiki-128.png')
620            // will not be found by Sizzle (the CSS Selector Engine
621            // used by jQuery), hence the document.getElementById() call
622            jQuery(document.getElementById('ex_'+id.replace(/:/g,'_').replace(/^_/,''))).dw_toggle();
623            return;
624        }
625
626        dw_mediamanager.ext = false;
627        dot = id.lastIndexOf(".");
628
629        if (-1 === dot) {
630            dw_mediamanager.insert(id);
631            return;
632        }
633
634        ext = id.substr(dot);
635
636        if ({'.jpg':1, '.jpeg':1, '.png':1, '.gif':1, '.swf':1}[ext] !== 1) {
637            dw_mediamanager.insert(id);
638            return;
639        }
640
641        // remove old callback from the insert button and set the new one.
642        jQuery('#media__sendbtn').unbind().click(bind(dw_mediamanager.insert, id));
643
644        dw_mediamanager.unforbid('ext');
645        if (ext === '.swf') {
646            dw_mediamanager.ext = 'swf';
647            dw_mediamanager.forbid('ext', {link: ['1', '2'],
648                                           size: ['4']});
649        } else {
650            dw_mediamanager.ext = 'img';
651        }
652
653        // Set to defaults
654        dw_mediamanager.setOpt('link');
655        dw_mediamanager.setOpt('align');
656        dw_mediamanager.setOpt('size');
657
658        // toggle buttons for detail and linked image, original size
659        jQuery('#media__linkbtn1, #media__linkbtn2, #media__sizebtn4')
660            .toggle(dw_mediamanager.ext === 'img');
661
662        dw_mediamanager.$popup.dialog('open');
663
664        jQuery('#media__sendbtn').focus();
665    },
666
667    /**
668     * Deletion confirmation dialog to the delete buttons.
669     *
670     * @author Michael Klier <chi@chimeric.de>
671     * @author Pierre Spring <pierre.spring@caillou.ch>
672     */
673    confirmattach: function(e){
674        if(!confirm(LANG.del_confirm + "\n" + jQuery(this).attr('title'))) {
675            e.preventDefault();
676        }
677    },
678
679    /**
680     * Creates checkboxes for additional options
681     *
682     * @author Andreas Gohr <andi@splitbrain.org>
683     * @author Pierre Spring <pierre.spring@caillou.ch>
684     */
685    attachoptions: function(){
686        var $obj, opts;
687
688        $obj = jQuery('#media__opts');
689        if($obj.length === 0) {
690            return;
691        }
692
693        opts = [];
694        // keep open
695        if(opener){
696            opts.push(['keepopen', 'keepopen']);
697        }
698        opts.push(['hide', 'hidedetails']);
699
700        jQuery.each(opts,
701                    function(_, opt) {
702                        var $box, $lbl;
703                        $box = jQuery(document.createElement('input'))
704                                 .attr('type', 'checkbox')
705                                 .attr('id', 'media__' + opt[0])
706                                 .click(bind(dw_mediamanager.toggleOption,
707                                             opt[0]));
708
709                        if(DokuCookie.getValue(opt[0])){
710                            $box.prop('checked', true);
711                            dw_mediamanager[opt[0]] = true;
712                        }
713
714                        $lbl = jQuery(document.createElement('label'))
715                                 .attr('for', 'media__' + opt[0])
716                                 .text(LANG[opt[1]]);
717
718                        $obj.append($box, $lbl, document.createElement('br'));
719                    });
720
721        dw_mediamanager.updatehide();
722    },
723
724    /**
725     * Generalized toggler
726     *
727     * @author Pierre Spring <pierre.spring@caillou.ch>
728     */
729    toggleOption: function (variable) {
730        if (jQuery(this).prop('checked')) {
731            DokuCookie.setValue(variable, 1);
732            dw_mediamanager[variable] = true;
733        } else {
734            DokuCookie.setValue(variable, '');
735            dw_mediamanager[variable] = false;
736        }
737        if (variable === 'hide') {
738            dw_mediamanager.updatehide();
739        }
740    },
741
742    initFlashUpload: function () {
743        var $oform, $oflash;
744        if(!hasFlash(8)) {
745            return;
746        }
747
748        $oform  = jQuery('#dw__upload');
749        $oflash = jQuery('#dw__flashupload');
750
751        if(!$oform.length || !$oflash.length) {
752            return;
753        }
754
755        jQuery(document.createElement('img'))
756            .attr('src', DOKU_BASE+'lib/images/multiupload.png')
757            .attr('title', LANG.mu_btn)
758            .attr('alt', LANG.mu_btn)
759            .css('cursor', 'pointer')
760            .click(
761                function () {
762                    $oform.hide();
763                    $oflash.show();
764                }
765            )
766            .appendTo($oform);
767    },
768
769    /**
770     * Sets the visibility of the image details accordingly to the
771     * chosen hide state
772     *
773     * @author Andreas Gohr <andi@splitbrain.org>
774     */
775    updatehide: function(){
776        jQuery('#media__content div.detail').dw_toggle(!dw_mediamanager.hide);
777    },
778
779    /**
780     * set media insertion option
781     *
782     * @author Dominik Eckelmann <eckelmann@cosmocode.de>
783     */
784    setOpt: function(opt, e){
785        var val, i;
786        if (typeof e !== 'undefined') {
787            val = this.id.substring(this.id.length - 1);
788        } else {
789            val = dw_mediamanager.getOpt(opt);
790        }
791
792        if (val === false) {
793            DokuCookie.setValue(opt,'');
794            dw_mediamanager[opt] = false;
795            return;
796        }
797
798        if (opt === 'link') {
799            if (val !== '4' && dw_mediamanager.link === '4') {
800                dw_mediamanager.unforbid('linkonly');
801                dw_mediamanager.setOpt('align');
802                dw_mediamanager.setOpt('size');
803            } else if (val === '4') {
804                dw_mediamanager.forbid('linkonly', {align: false, size: false});
805            }
806
807            jQuery("#media__size, #media__align").dw_toggle(val !== '4');
808        }
809
810        DokuCookie.setValue(opt, val);
811        dw_mediamanager[opt] = val;
812
813        for (i = 1; i <= 4; i++) {
814            jQuery("#media__" + opt + "btn" + i).removeClass('selected');
815        }
816        jQuery('#media__' + opt + 'btn' + val).addClass('selected');
817    },
818
819    unforbid: function (group) {
820        delete dw_mediamanager.forbidden_opts[group];
821    },
822
823    forbid: function (group, forbids) {
824        dw_mediamanager.forbidden_opts[group] = forbids;
825    },
826
827    allowedOpt: function (opt, val) {
828        var ret = true;
829        jQuery.each(dw_mediamanager.forbidden_opts,
830                    function (_, forbids) {
831                        ret = forbids[opt] !== false &&
832                              jQuery.inArray(val, forbids[opt]) === -1;
833                        return ret;
834                    });
835        return ret;
836    },
837
838    getOpt: function (opt) {
839        var allowed = bind(dw_mediamanager.allowedOpt, opt);
840
841        // Current value
842        if (dw_mediamanager[opt] !== false && allowed(dw_mediamanager[opt])) {
843            return dw_mediamanager[opt];
844        }
845
846        // From cookie
847        if (DokuCookie.getValue(opt) && allowed(DokuCookie.getValue(opt))) {
848            return DokuCookie.getValue(opt);
849        }
850
851        // size default
852        if (opt === 'size' && allowed('2')) {
853            return '2';
854        }
855
856        // Whatever is allowed, and be it false
857        return jQuery.grep(['1', '2', '3', '4'], allowed)[0] || false;
858    }
859};
860
861// moved from helpers.js temporarily here
862/**
863 * Very simplistic Flash plugin check, probably works for Flash 8 and higher only
864 *
865 */
866function hasFlash(version){
867    var ver = 0, axo;
868    try{
869        if(navigator.plugins !== null && navigator.plugins.length > 0){
870           ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0];
871        }else{
872           axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
873           ver = axo.GetVariable("$version").split(' ')[1].split(',')[0];
874        }
875    }catch(e){ }
876
877    return ver >= version;
878}
879
880jQuery(document).ready(function() {
881    dw_mediamanager.update_resizable();
882    dw_mediamanager.layout_width = jQuery("#mediamanager__layout").width();
883    jQuery(window).resize(dw_mediamanager.window_resize);
884});
885
886jQuery(dw_mediamanager.init);
887