1jQuery(function () {
2
3    /**
4     * Function to reload the preview styles in the main window
5     *
6     * @param {Window} target the main window
7     */
8    function applyPreview(target) {
9        // remove style
10        var $style = target.jQuery('link[rel=stylesheet][href*="lib/exe/css.php"]');
11        $style.attr('href', '');
12
13        // append the loader screen
14        var $loader = target.jQuery('#plugin__styling_loader');
15        if (!$loader.length) {
16            $loader = target.jQuery('<div id="plugin__styling_loader">' + LANG.plugins.styling.loader + '</div>');
17            $loader.css({
18                'position':         'absolute',
19                'width':            '100%',
20                'height':           '100%',
21                'top':              0,
22                'left':             0,
23                'z-index':          5000,
24                'background-color': '#fff',
25                'opacity':          '0.7',
26                'color':            '#000',
27                'font-size':        '2.5em',
28                'text-align':       'center',
29                'line-height':      1.5,
30                'padding-top':      '2em'
31            });
32            target.jQuery('body').append($loader);
33        }
34
35        // load preview in main window (timeout works around chrome updating CSS weirdness)
36        setTimeout(function () {
37            var now = new Date().getTime();
38            $style.attr('href', DOKU_BASE + 'lib/exe/css.php?preview=1&tseed=' + now);
39        }, 500);
40    }
41
42    var doreload = 1;
43    var $styling_plugin = jQuery('#plugin__styling');
44
45    // if we are not on the plugin page (either main or popup)
46    if (!$styling_plugin.length) {
47        // handle the preview cookie
48        if(DokuCookie.getValue('styling_plugin') == 1) {
49            applyPreview(window);
50        }
51        return; // nothing more to do here
52    }
53
54    /* ---- from here on we're in the popup or admin page ---- */
55
56    // add button on main page
57    if (!$styling_plugin.hasClass('ispopup')) {
58        var $form = $styling_plugin.find('form.styling').first();
59        var $btn = jQuery('<button>' + LANG.plugins.styling.popup + '</button>');
60        $form.prepend($btn);
61
62        $btn.on('click', function (e) {
63            var windowFeatures = "menubar=no,location=no,resizable=yes,scrollbars=yes,status=false,width=500,height=500";
64            window.open(DOKU_BASE + 'lib/plugins/styling/popup.php', 'styling_popup', windowFeatures);
65            e.preventDefault();
66            e.stopPropagation();
67        }).wrap('<p></p>');
68        return; // we exit here if this is not the popup
69    }
70
71    /* ---- from here on we're in the popup only ---- */
72
73    // reload the main page on close
74    window.onunload = function(e) {
75        if(doreload) {
76            DokuCookie.setValue('styling_plugin', 0);
77            if(window.opener) window.opener.document.location.reload();
78        }
79        return null;
80    };
81
82    // don't reload on our own buttons
83    jQuery(':button').click(function(e){
84        doreload = false;
85    });
86
87    // on first load apply preview
88    if(window.opener) applyPreview(window.opener);
89
90    // enable the preview cookie
91    DokuCookie.setValue('styling_plugin', 1);
92});
93