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