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