script.js 3.2 KB

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