diff options
author | Adrian Lang <mail@adrianlang.de> | 2011-07-07 16:08:05 +0200 |
---|---|---|
committer | Adrian Lang <mail@adrianlang.de> | 2011-07-10 12:15:25 +0200 |
commit | d10c9a7424d1ef0aace2fd34e1008196d111a88c (patch) | |
tree | 93f439df14ef3b0712f0eb9b3d83e82b6a18d121 /lib/scripts/media.js | |
parent | 881f2ee268e95e0cdd02bf593d89ca9b42d03060 (diff) | |
download | dokuwiki-d10c9a7424d1ef0aace2fd34e1008196d111a88c.tar.gz dokuwiki-d10c9a7424d1ef0aace2fd34e1008196d111a88c.zip |
Rewrite mediamanager JavaScript
Make it faster, prettier, less wrong, add UI effects, use jQuery UI Dialog,
Abstract tree view stuff away into jQuery.dw_tree
Diffstat (limited to 'lib/scripts/media.js')
-rw-r--r-- | lib/scripts/media.js | 941 |
1 files changed, 357 insertions, 584 deletions
diff --git a/lib/scripts/media.js b/lib/scripts/media.js index ebbee5a78..f7e78c747 100644 --- a/lib/scripts/media.js +++ b/lib/scripts/media.js @@ -1,8 +1,5 @@ -/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: false, strict: true, newcap: true, immed: true */ -/*global jQuery, window, DOKU_BASE*/ -"use strict"; - -// * refactor once the jQuery port is over ;) +/*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 */ +/*global jQuery, DOKU_BASE, LANG, bind, DokuCookie, opener, confirm*/ /** * JavaScript functionality for the media management popup @@ -10,172 +7,127 @@ * @author Andreas Gohr <andi@splitbrain.org> * @author Pierre Spring <pierre.spring@caillou.ch> */ -(function ($) { - var toggle, list, prepare_content, insert, confirmattach, attachoptions, initpopup, updatehide, setalign, setsize, inSet, outSet, media_manager, hasFlash; - - var media_manager = { - keepopen: false, - hide: false, - align: false, - popup: false, - display: false, - link: false, - size: false, - ext: false, - }; - - - - - /** - * build the popup window - * - * @author Dominik Eckelmann <eckelmann@cosmocode.de> - */ - initpopup = function() { - var popup; - - popup = document.createElement('div'); - popup.setAttribute('id','media__popup'); - popup.style.display = "none"; - - var root = document.getElementById('media__manager'); - if (root === null) return; - root.appendChild(popup); - - var headline = document.createElement('h1'); - headline.innerHTML = LANG.mediatitle; - var headlineimg = document.createElement('img'); - headlineimg.src = DOKU_BASE + 'lib/images/close.png'; - headlineimg.id = 'media__closeimg'; - $(headlineimg).click(function () {$(popup).hide()}); - headline.insertBefore(headlineimg, headline.firstChild); - popup.appendChild(headline); - $(popup).draggable({handle: headline}); - - // link - var linkp = document.createElement('p'); - - linkp.id = "media__linkstyle"; - if (media_manager.display == "2") { - linkp.style.display = "none"; - } - - var linkl = document.createElement('label'); - linkl.innerHTML = LANG.mediatarget; - linkp.appendChild(linkl); - - var linkbtns = ['lnk', 'direct', 'nolnk', 'displaylnk']; - for (var i = 0 ; i < linkbtns.length ; ++i) { - var linkbtn = document.createElement('button'); - linkbtn.className = 'button'; - linkbtn.id = "media__linkbtn" + (i+1); - linkbtn.title = LANG['media' + linkbtns[i]]; - linkbtn.style.borderStyle = 'outset'; - $(linkbtn).click(function (event) { return setlink(event,this); }); - - var linkimg = document.createElement('img'); - linkimg.src = DOKU_BASE + 'lib/images/media_link_' + linkbtns[i] + '.png'; - linkbtn.appendChild(linkimg); - linkp.appendChild(linkbtn); - } - - popup.appendChild(linkp); - - // align +var dw_mediamanager = { + keepopen: false, + hide: false, + popup: false, + display: false, + ext: false, + $popup: null, - var alignp = document.createElement('p'); - var alignl = document.createElement('label'); + // Image insertion opts + align: false, + link: false, + size: false, + forbidden_opts: {}, - alignp.appendChild(alignl); - alignp.id = 'media__align'; - if (media_manager.display == "2") { - alignp.style.display = "none"; - } - alignl.innerHTML = LANG['mediaalign']; - - var alignbtns = ['noalign', 'left', 'center', 'right']; - for (var n = 0 ; n < alignbtns.length ; ++n) { - var alignbtn = document.createElement('button'); - var alignimg = document.createElement('img'); - alignimg.src = DOKU_BASE + 'lib/images/media_align_' + alignbtns[n] + '.png'; - - alignbtn.id = "media__alignbtn" + (n+1); - alignbtn.title = LANG['media' + alignbtns[n]]; - alignbtn.className = 'button'; - alignbtn.appendChild(alignimg); - alignbtn.style.borderStyle = 'outset'; - $(alignbtn).click(function (event) { return setalign(event,this); }); - - alignp.appendChild(alignbtn); - } + init: function () { + var $content, $tree; + $content = jQuery('#media__content'); + $tree = jQuery('#media__tree'); - popup.appendChild(alignp); + dw_mediamanager.prepare_content($content); - // size + dw_mediamanager.attachoptions(); + dw_mediamanager.initpopup(); - var sizep = document.createElement('p'); - var sizel = document.createElement('label'); - - sizep.id = 'media__size'; - if (media_manager.display == "2") { - sizep.style.display = "none"; - } - sizep.appendChild(sizel); - sizel.innerHTML = LANG['mediasize']; - - var sizebtns = ['small', 'medium', 'large', 'original']; - for (var size = 0 ; size < sizebtns.length ; ++size) { - var sizebtn = document.createElement('button'); - var sizeimg = document.createElement('img'); - - sizep.appendChild(sizebtn); - sizeimg.src = DOKU_BASE + 'lib/images/media_size_' + sizebtns[size] + '.png'; - - sizebtn.className = 'button'; - sizebtn.appendChild(sizeimg); - sizebtn.id = 'media__sizebtn' + (size + 1); - sizebtn.title = LANG['media' + sizebtns[size]]; - sizebtn.style.borderStyle = 'outset'; - $(sizebtn).click(function (event) { return setsize(event,this); }); - } - - popup.appendChild(sizep); - - // send and close button - - var btnp = document.createElement('p'); - popup.appendChild(btnp); - btnp.setAttribute('class','btnlbl'); - - var btn = document.createElement('input'); - btn.type = 'button'; - btn.id = 'media__sendbtn'; - btn.setAttribute('class','button'); - btn.value = LANG['mediainsert']; - btnp.appendChild(btn); - }; + // add the action to autofill the "upload as" field + $content.delegate('#upload__file', 'change', dw_mediamanager.suggest) + // Attach the image selector action to all links + .delegate('a.select', 'click', dw_mediamanager.select) + // Attach deletion confirmation dialog to the delete buttons + .delegate('#media__content a.btn_media_delete', 'click', + dw_mediamanager.confirmattach); + + $tree.dw_tree({toggle_selector: 'img', + load_data: function (show_sublist, $clicky) { + // get the enclosed link (is always the first one) + var $link = $clicky.siblings('a').first(); + + jQuery.post( + DOKU_BASE + 'lib/exe/ajax.php', + $link[0].search.substr(1) + '&call=medians', + show_sublist, + 'html' + ); + + $clicky.attr('src', DOKU_BASE + 'lib/images/minus.gif'); + }, + + close: function ($clicky) { + $clicky.attr('src', + DOKU_BASE + 'lib/images/plus.gif'); + }}); + $tree.delegate('a', 'click', dw_mediamanager.list); + }, - // moved from helpers.js temporarily here /** - * Very simplistic Flash plugin check, probably works for Flash 8 and higher only + * build the popup window * + * @author Dominik Eckelmann <eckelmann@cosmocode.de> */ - hasFlash = function(version){ - var ver = 0; - try{ - if(navigator.plugins != null && navigator.plugins.length > 0){ - ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0]; - }else{ - var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); - ver = axo.GetVariable("$version").split(' ')[1].split(',')[0]; + initpopup: function () { + var opts, $insp, $insbtn; + + dw_mediamanager.$popup = jQuery(document.createElement('div')) + .attr('id', 'media__popup_content') + .dialog({autoOpen: false, width: 280, modal: true, + draggable: true, title: LANG.mediatitle, + resizable: false}); + + opts = [{id: 'link', label: LANG.mediatarget, + btns: ['lnk', 'direct', 'nolnk', 'displaylnk']}, + {id: 'align', label: LANG.mediaalign, + btns: ['noalign', 'left', 'center', 'right']}, + {id: 'size', label: LANG.mediasize, + btns: ['small', 'medium', 'large', 'original']} + ]; + + jQuery.each(opts, function (_, opt) { + var $p, $l; + $p = jQuery(document.createElement('p')) + .attr('id', 'media__' + opt.id); + + if (dw_mediamanager.display === "2") { + $p.hide(); } - }catch(e){ } - if(ver >= version) return true; - return false; - }; + $l = jQuery(document.createElement('label')) + .text(opt.label); + $p.append($l); + + jQuery.each(opt.btns, function (i, text) { + var $btn, $img; + $btn = jQuery(document.createElement('button')) + .addClass('button') + .attr('id', "media__" + opt.id + "btn" + (i + 1)) + .attr('title', LANG['media' + text]) + .click(bind(dw_mediamanager.setOpt, opt.id)); + + $img = jQuery(document.createElement('img')) + .attr('src', DOKU_BASE + 'lib/images/media_' + + opt.id + '_' + text + '.png'); + + $btn.append($img); + $p.append($btn); + }); + + dw_mediamanager.$popup.append($p); + }); + + // insert button + $insp = jQuery(document.createElement('p')) + .addClass('btnlbl'); + dw_mediamanager.$popup.append($insp); + + $insbtn = jQuery(document.createElement('input')) + .attr('id', 'media__sendbtn') + .attr('type', 'button') + .addClass('button') + .val(LANG.mediainsert); + $insp.append($insbtn); + }, /** * Insert the clicked image into the opener's textarea @@ -184,135 +136,81 @@ * @author Dominik Eckelmann <eckelmann@cosmocode.de> * @author Pierre Spring <pierre.spring@caillou.ch> */ - insert = function (id) { - var opts, optsstart, alignleft, alignright; + insert: function () { + var opts, alignleft, alignright, edid, s; // set syntax options - $('#media__popup').hide(); + dw_mediamanager.$popup.dialog('close'); opts = ''; - optsstart = ''; alignleft = ''; alignright = ''; - if (media_manager.ext == 'img' || media_manager.ext == 'swf') { + if ({img: 1, swf: 1}[dw_mediamanager.ext] === 1) { - if (media_manager.link == '4') { + if (dw_mediamanager.link === '4') { opts = '?linkonly'; } else { - if (media_manager.link == "3" && media_manager.ext == 'img') { + if (dw_mediamanager.link === "3" && dw_mediamanager.ext === 'img') { opts = '?nolink'; - optsstart = true; - } else if (media_manager.link == "2" && media_manager.ext == 'img') { + } else if (dw_mediamanager.link === "2" && dw_mediamanager.ext === 'img') { opts = '?direct'; - optsstart = true; } - var s = parseInt(media_manager.size, 10); + s = parseInt(dw_mediamanager.size, 10); - if (s && s >= 1) { - opts += (optsstart)?'&':'?'; - if (s=="1") { - opts += '100'; - if (media_manager.ext == 'swf') { + if (s && s >= 1 && s < 4) { + opts += (opts.length)?'&':'?'; + opts += dw_mediamanager.size + '00'; + if (dw_mediamanager.ext === 'swf') { + switch (s) { + case 1: opts += 'x62'; - } - } else if (s=="2") { - opts += '200'; - if (media_manager.ext == 'swf') { + break; + case 2: opts += 'x123'; - } - } else if (s=="3"){ - opts += '300'; - if (media_manager.ext == 'swf') { + break; + case 3: opts += 'x185'; + break; } } } - if (media_manager.align == '2') { - alignleft = ''; - alignright = ' '; - } - if (media_manager.align == '3') { - alignleft = ' '; - alignright = ' '; - } - if (media_manager.align == '4') { - alignleft = ' '; - alignright = ''; - } + alignleft = dw_mediamanager.align === '2' ? '' : ' '; + alignright = dw_mediamanager.align === '4' ? '' : ' '; } } - var edid = String.prototype.match.call(document.location, /&edid=([^&]+)/); - edid = edid ? edid[1] : 'wiki__text'; - opener.insertTags(edid,'{{'+alignleft+id+opts+alignright+'|','}}',''); + edid = String.prototype.match.call(document.location, /&edid=([^&]+)/); + opener.insertTags(edid ? edid[1] : 'wiki__text', + '{{'+alignleft+id+opts+alignright+'|','}}',''); - if(!media_manager.keepopen) window.close(); + if(!dw_mediamanager.keepopen) { + window.close(); + } opener.focus(); return false; - }; + }, /** * Prefills the wikiname. * * @author Andreas Gohr <andi@splitbrain.org> */ - suggest = function(){ - var file, name, text; + suggest: function(){ + var $file, $name, text; - file = $(this); - name = $('#upload__name'); - if(!file.size() || !name.size()) return; - - text = file.val(); - text = text.substr(text.lastIndexOf('/')+1); - text = text.substr(text.lastIndexOf('\\')+1); - name.val(text); - }; - - /** - * Open or close a subtree using AJAX - * - * @author Andreas Gohr <andi@splitbrain.org> - * @author Pierre Spring <pierre.spring@caillou.ch> - */ - toggle = function (event) { - var clicky, listitem, sublist, link, ul; - - event.preventDefault(); // TODO: really here? - - var clicky = $(this); - var listitem = clicky.parent(); - - // if already open, close by removing the sublist - sublist = listitem.find('ul').first(); - if(sublist.size()){ - sublist.remove(); // TODO: really? we could just hide it, right? - clicky.attr('src', DOKU_BASE + 'lib/images/plus.gif'); + $file = jQuery(this); + $name = jQuery('#upload__name'); + if(!$file.length || !$name.length) { return; } - // get the enclosed link (is always the first one) - link = listitem.find('a').first(); - - //prepare the new ul - ul = $('<ul/>'); - - //fixme add classname here - - $.post( - DOKU_BASE + 'lib/exe/ajax.php', - link[0].search.substr(1) + '&call=medians', - function (data) { - ul.html(data); - listitem.append(ul); - }, - 'html' - ); - - clicky.attr('src', DOKU_BASE + 'lib/images/minus.gif'); - }; + text = $file.val(); + text = text.substr(text.lastIndexOf('/')+1); + text = text.substr(text.lastIndexOf('\\')+1); + $name.val(text); + }, /** * list the content of a namespace using AJAX @@ -320,178 +218,96 @@ * @author Andreas Gohr <andi@splitbrain.org> * @author Pierre Spring <pierre.spring@caillou.ch> */ - list = function (event) { - var link, content; - link = $(this); + list: function (event) { + var $link, $content; event.preventDefault(); jQuery('div.success, div.info, div.error, div.notify').remove(); - content = $('#media__content'); - content.html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />'); + $link = jQuery(this); + $content = jQuery('#media__content'); + $content.html('<img src="' + DOKU_BASE + 'lib/images/loading.gif" alt="..." class="load" />'); // fetch the subtree - $.post( + jQuery.post( DOKU_BASE + 'lib/exe/ajax.php', - link[0].search.substr(1)+'&call=medialist', + $link[0].search.substr(1)+'&call=medialist', function (data) { - content.html(data); - prepare_content(content); - updatehide(); + $content.html(data); + dw_mediamanager.prepare_content($content); + dw_mediamanager.updatehide(); }, 'html' ); + }, - }; - - prepare_content = function (content) { + prepare_content: function ($content) { // hide syntax example - content.find('div.example:visible').hide(); - initFlashUpload(); - }; + $content.find('div.example:visible').hide(); + dw_mediamanager.initFlashUpload(); + }, /** - * shows the popup for a image link - */ - select = function(event){ - var link, id, dot, ext; - - event.preventDefault(); - - link = $(this); - id = link.attr('name').substr(2); - - if(!opener){ - // if we don't run in popup display example - // the id's are a bit wired and $('#ex_wiki_dokuwiki-128.png') will not be found - // by Sizzle (the CSS Selector Engine used by jQuery), - // hence the document.getElementById() call - $(document.getElementById('ex_'+id.replace(/:/g,'_').replace(/^_/,''))).toggle(); - return; - } - - link = link[0]; - - media_manager.ext = false; - dot = id.lastIndexOf("."); - - if (-1 === dot) { - insert(id); - return; - } - - ext = id.substr(dot); - - if (ext != '.jpg' && ext != '.jpeg' && ext != '.png' && ext != '.gif' && ext != '.swf') { - insert(id); - return; - } - - // remove old callback from the insert button and set the new one. - $('#media__sendbtn').unbind().click(function () {insert(id)}); - - $('#media__popup').show() - .css('left', event.pageX + 'px') - .css('top', event.pageY + 'px'); + * shows the popup for a image link + */ + select: function(event){ + var $link, id, dot, ext; - $('#media__popup button.button').each(function (index, element) { outSet(element) } ); + event.preventDefault(); + $link = jQuery(this); + id = $link.attr('name').substr(2); - if (ext == '.swf') { - media_manager.ext = 'swf'; + if(!opener){ + // if we don't run in popup display example + // the id's are a bit wierd and jQuery('#ex_wiki_dokuwiki-128.png') + // will not be found by Sizzle (the CSS Selector Engine + // used by jQuery), hence the document.getElementById() call + jQuery(document.getElementById('ex_'+id.replace(/:/g,'_').replace(/^_/,''))).dw_toggle(); + return; + } - // disable display buttons for detail and linked image - $('#media__linkbtn1').hide(); - $('#media__linkbtn2').hide(); + dw_mediamanager.ext = false; + dot = id.lastIndexOf("."); - // set the link button to default - if (media_manager.link != false) { - if ( media_manager.link == '2' || media_manager.link == '1') { - inSet('media__linkbtn3'); - media_manager.link = '3'; - DokuCookie.setValue('link','3'); - } else { - inSet('media__linkbtn'+media_manager.link); - } - } else if (DokuCookie.getValue('link')) { - if ( DokuCookie.getValue('link') == '2' || DokuCookie.getValue('link') == '1') { - // this options are not availible - inSet('media__linkbtn3'); - media_manager.link = '3'; - DokuCookie.setValue('link','3'); - } else { - inSet('media__linkbtn'+DokuCookie.getValue('link')); - media_manager.link = DokuCookie.getValue('link'); - } - } else { - // default case - media_manager.link = '3'; - inSet('media__linkbtn3'); - DokuCookie.setValue('link','3'); - } + if (-1 === dot) { + dw_mediamanager.insert(id); + return; + } - // disable button for original size - $('#media__sizebtn4').hide(); + ext = id.substr(dot); - } else { - media_manager.ext = 'img'; + if ({'.jpg':1, '.jpeg':1, '.png':1, '.gif':1, '.swf':1}[ext] !== 1) { + dw_mediamanager.insert(id); + return; + } - // ensure that the display buttons are there - $('#media__linkbtn1').show(); - $('#media__linkbtn2').show(); - $('#media__sizebtn4').show(); + // remove old callback from the insert button and set the new one. + jQuery('#media__sendbtn').unbind().click(bind(dw_mediamanager.insert, id)); - // set the link button to default - if (DokuCookie.getValue('link')) { - media_manager.link = DokuCookie.getValue('link'); - } - if (media_manager.link == false) { - // default case - media_manager.link = '1'; - DokuCookie.setValue('link','1'); - } - inSet('media__linkbtn'+media_manager.link); - } + dw_mediamanager.unforbid('ext'); + if (ext === '.swf') { + dw_mediamanager.ext = 'swf'; + dw_mediamanager.forbid('ext', {link: ['1', '2'], + size: ['4']}); + } else { + dw_mediamanager.ext = 'img'; + } - if (media_manager.link == '4') { - media_manager.align = false; - media_manager.size = false; - $('#media__align').hide(); - $('#media__size').hide(); - } else { - $('#media__align').show(); - $('#media__size').show(); - - // set the align button to default - if (media_manager.align != false) { - inSet('media__alignbtn'+media_manager.align); - } else if (DokuCookie.getValue('align')) { - inSet('media__alignbtn'+DokuCookie.getValue('align')); - media_manager.align = DokuCookie.getValue('align'); - } else { - // default case - media_manager.align = '0'; - inSet('media__alignbtn0'); - DokuCookie.setValue('align','0'); - } + // Set to defaults + dw_mediamanager.setOpt('link'); + dw_mediamanager.setOpt('align'); + dw_mediamanager.setOpt('size'); - // set the size button to default - if (DokuCookie.getValue('size')) { - media_manager.size = DokuCookie.getValue('size'); - } - if (media_manager.size == false || (media_manager.size === '4' && ext === '.swf')) { - // default case - media_manager.size = '2'; - DokuCookie.setValue('size','2'); - } - inSet('media__sizebtn'+media_manager.size); + // toggle buttons for detail and linked image, original size + jQuery('#media__linkbtn1, #media__linkbtn2, #media__sizebtn4') + .toggle(dw_mediamanager.ext === 'img'); - $('#media__sendbtn').focus(); - } + dw_mediamanager.$popup.dialog('open'); - return; - }; + jQuery('#media__sendbtn').focus(); + }, /** * Deletion confirmation dialog to the delete buttons. @@ -499,11 +315,11 @@ * @author Michael Klier <chi@chimeric.de> * @author Pierre Spring <pierre.spring@caillou.ch> */ - confirmattach = function(e){ - if(!confirm(LANG['del_confirm'] + "\n" + jQuery(this).attr('title'))) { + confirmattach: function(e){ + if(!confirm(LANG.del_confirm + "\n" + jQuery(this).attr('title'))) { e.preventDefault(); } - }; + }, /** * Creates checkboxes for additional options @@ -511,104 +327,89 @@ * @author Andreas Gohr <andi@splitbrain.org> * @author Pierre Spring <pierre.spring@caillou.ch> */ - attachoptions = function(){ - obj = $('#media__opts')[0]; - if(!obj) return; - - // keep open - if(opener){ - var kobox = document.createElement('input'); - kobox.type = 'checkbox'; - kobox.id = 'media__keepopen'; - if(DokuCookie.getValue('keepopen')){ - kobox.checked = true; - kobox.defaultChecked = true; //IE wants this - media_manager.keepopen = true; - } + attachoptions: function(){ + var $obj, opts; - $(kobox).click( - function () { - toggleOption(this, 'keepopen'); - } - ); - - var kolbl = document.createElement('label'); - kolbl.htmlFor = 'media__keepopen'; - kolbl.innerHTML = LANG['keepopen']; - - var kobr = document.createElement('br'); - - obj.appendChild(kobox); - obj.appendChild(kolbl); - obj.appendChild(kobr); + $obj = jQuery('#media__opts'); + if($obj.length === 0) { + return; } - // hide details - var hdbox = document.createElement('input'); - hdbox.type = 'checkbox'; - hdbox.id = 'media__hide'; - if(DokuCookie.getValue('hide')){ - hdbox.checked = true; - hdbox.defaultChecked = true; //IE wants this - media_manager.hide = true; + opts = []; + // keep open + if(opener){ + opts.push(['keepopen', 'keepopen']); } - $(hdbox).click( - function () { - toggleOption(this, 'hide'); - updatehide(); - } - ); + opts.push(['hide', 'hidedetails']); + + jQuery.each(opts, + function(_, opt) { + var $box, $lbl; + $box = jQuery(document.createElement('input')) + .attr('type', 'checkbox') + .attr('id', 'media__' + opt[0]) + .click(bind(dw_mediamanager.toggleOption, + opt[0])); + + if(DokuCookie.getValue(opt[0])){ + $box.prop('checked', true); + dw_mediamanager[opt[0]] = true; + } - var hdlbl = document.createElement('label'); - hdlbl.htmlFor = 'media__hide'; - hdlbl.innerHTML = LANG['hidedetails']; + $lbl = jQuery(document.createElement('label')) + .attr('for', 'media__' + opt[0]) + .text(LANG[opt[1]]); - var hdbr = document.createElement('br'); + $obj.append($box, $lbl, document.createElement('br')); + }); - obj.appendChild(hdbox); - obj.appendChild(hdlbl); - obj.appendChild(hdbr); - updatehide(); - }; + dw_mediamanager.updatehide(); + }, /** * Generalized toggler * * @author Pierre Spring <pierre.spring@caillou.ch> */ - toggleOption = function (checkbox, variable) { - if (checkbox.checked) { + toggleOption: function (variable) { + if (jQuery(this).prop('checked')) { DokuCookie.setValue(variable, 1); - media_manager[variable] = true; + dw_mediamanager[variable] = true; } else { DokuCookie.setValue(variable, ''); - media_manager[variable] = false; + dw_mediamanager[variable] = false; } - }; - - initFlashUpload = function () { - var oform, oflash, title; - if(!hasFlash(8)) return; + if (variable === 'hide') { + dw_mediamanager.updatehide(); + } + }, - oform = $('#dw__upload'); - oflash = $('#dw__flashupload'); + initFlashUpload: function () { + var $oform, $oflash; + if(!hasFlash(8)) { + return; + } - if(!oform.size() || !oflash.size()) return; + $oform = jQuery('#dw__upload'); + $oflash = jQuery('#dw__flashupload'); - title = LANG['mu_btn']; + if(!$oform.length || !$oflash.length) { + return; + } - $('<img/>').attr('src', DOKU_BASE+'lib/images/multiupload.png') - .attr('title', title) - .attr('alt', title) + jQuery(document.createElement('img')) + .attr('src', DOKU_BASE+'lib/images/multiupload.png') + .attr('title', LANG.mu_btn) + .attr('alt', LANG.mu_btn) .css('cursor', 'pointer') .click( function () { - oform.hide(); - oflash.show(); + $oform.hide(); + $oflash.show(); } ) - .appendTo(oform); - }; + .appendTo($oform); + }, /** * Sets the visibility of the image details accordingly to the @@ -616,137 +417,109 @@ * * @author Andreas Gohr <andi@splitbrain.org> */ - updatehide = function(){ - var content = $('#media__content'); - if(0 === content.size()) { - return; - } - content.find('div.detail').each( - function (index, element) { - if(media_manager.hide){ - element.style.display = 'none'; - }else{ - element.style.display = ''; - } - } - - ); - }; + updatehide: function(){ + jQuery('#media__content div.detail').dw_toggle(!dw_mediamanager.hide); + }, /** - * set the align + * set media insertion option * * @author Dominik Eckelmann <eckelmann@cosmocode.de> */ - setalign = function(event,cb){ - - var id = cb.id.substring(cb.id.length -1); - if(id){ - DokuCookie.setValue('align',id); - media_manager.align = id; - for (var i = 1; i<=4; i++) { - outSet("media__alignbtn" + i); - } - inSet("media__alignbtn"+id); - }else{ - DokuCookie.setValue('align',''); - media_manager.align = false; + setOpt: function(opt, e){ + var val, i; + if (typeof e !== 'undefined') { + val = this.id.substring(this.id.length - 1); + } else { + val = dw_mediamanager.getOpt(opt); } - }; - /** - * set the link type - * - * @author Dominik Eckelmann <eckelmann@cosmocode.de> - */ - setlink = function(event,cb){ - var id = cb.id.substring(cb.id.length -1); - if(id){ - DokuCookie.setValue('link',id); - for (var i = 1; i<=4; i++) { - outSet("media__linkbtn"+i); - } - inSet("media__linkbtn"+id); - - var size = $("#media__size"); - var align = $("#media__align"); - if (id != '4') { - size.show(); - align.show(); - if (media_manager.link == '4') { - media_manager.align = '1'; - DokuCookie.setValue('align', '1'); - inSet('media__alignbtn1'); - - media_manager.size = '2'; - DokuCookie.setValue('size', '2'); - inSet('media__sizebtn2'); - } - - } else { - size.hide(); - align.hide(); - } - media_manager.link = id; - }else{ - DokuCookie.setValue('link',''); - media_manager.link = false; + if (val === false) { + DokuCookie.setValue(opt,''); + dw_mediamanager[opt] = false; + return; } - }; - /** - * set the image size - * - * @author Dominik Eckelmann <eckelmann@cosmocode.de> - */ - setsize = function(event,cb){ - var id = cb.id.substring(cb.id.length -1); - if (id) { - DokuCookie.setValue('size',id); - media_manager.size = id; - for (var i = 1 ; i <=4 ; ++i) { - outSet("media__sizebtn" + i); + if (opt === 'link') { + if (val !== '4' && dw_mediamanager.link === '4') { + dw_mediamanager.unforbid('linkonly'); + dw_mediamanager.setOpt('align'); + dw_mediamanager.setOpt('size'); + } else if (val === '4') { + dw_mediamanager.forbid('linkonly', {align: false, size: false}); } - inSet("media__sizebtn"+id); - } else { - DokuCookie.setValue('size',''); - media_manager.width = false; + + jQuery("#media__size, #media__align").dw_toggle(val !== '4'); } - }; - /** - * sets the border to inset - */ - inSet = function(id) { - var ele = $('#' + id).css('border-style', 'inset'); - }; + DokuCookie.setValue(opt, val); + dw_mediamanager[opt] = val; - /** - * sets the border to outset - */ - outSet = function(element) { - if ('string' === typeof element) { - element = '#' + element; + for (i = 1; i <= 4; i++) { + jQuery("#media__" + opt + "btn" + i).removeClass('selected'); + } + jQuery('#media__' + opt + 'btn' + val).addClass('selected'); + }, + + unforbid: function (group) { + delete dw_mediamanager.forbidden_opts[group]; + }, + + forbid: function (group, forbids) { + dw_mediamanager.forbidden_opts[group] = forbids; + }, + + allowedOpt: function (opt, val) { + var ret = true; + jQuery.each(dw_mediamanager.forbidden_opts, + function (_, forbids) { + ret = forbids[opt] !== false && + jQuery.inArray(val, forbids[opt]) === -1; + return ret; + }); + return ret; + }, + + getOpt: function (opt) { + var allowed = bind(dw_mediamanager.allowedOpt, opt); + + // Current value + if (dw_mediamanager[opt] !== false && allowed(dw_mediamanager[opt]) { + return dw_mediamanager[opt]; } - $(element).css('border-style', 'outset'); - }; - $(function () { - var content = $('#media__content'); - prepare_content(content); + // From cookie + if (DokuCookie.getValue(opt) && allowed(DokuCookie.getValue(opt))) { + return DokuCookie.getValue(opt); + } - attachoptions(); - initpopup(); + // size default + if (opt === 'size' && allowed('2')) { + return '2'; + } - // add the action to autofill the "upload as" field - content.delegate('#upload__file', 'change', suggest) - // Attach the image selector action to all links - .delegate('a.select', 'click', select) - // Attach deletion confirmation dialog to the delete buttons - .delegate('#media__content a.btn_media_delete', 'click', confirmattach); + // Whatever is allowed, and be it false + return jQuery.grep(['1', '2', '3', '4'], allowed)[0] || false; + } +}; + +// moved from helpers.js temporarily here +/** + * Very simplistic Flash plugin check, probably works for Flash 8 and higher only + * + */ +function hasFlash(version){ + var ver = 0, axo; + try{ + if(navigator.plugins !== null && navigator.plugins.length > 0){ + ver = navigator.plugins["Shockwave Flash"].description.split(' ')[2].split('.')[0]; + }else{ + axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash"); + ver = axo.GetVariable("$version").split(' ')[1].split(',')[0]; + } + }catch(e){ } + return ver >= version; +} - $('#media__tree').delegate('img', 'click', toggle) - .delegate('a', 'click', list); - }); -}(jQuery)); +jQuery(dw_mediamanager.init); |