media.js 30 KB

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