2 * JavaScript functionalitiy for the media management popup
4 * @author Andreas Gohr <andi@splitbrain.org>
11 * Attach event handlers to all "folders" below the given element
13 * @author Andreas Gohr <andi@splitbrain.org>
15 treeattach: function(obj){
18 var items = obj.getElementsByTagName('li');
19 for(var i=0; i<items.length; i++){
22 // attach action to make the +/- clickable
23 var clicky = elem.getElementsByTagName('img')[0];
24 clicky.style.cursor = 'pointer';
25 addEvent(clicky,'click',function(event){ return media_manager.toggle(event,this); });
27 // attach action load folder list via AJAX
28 var link = elem.getElementsByTagName('a')[0];
29 link.style.cursor = 'pointer';
30 addEvent(link,'click',function(event){ return media_manager.list(event,this); });
35 * Attach the image selector action to all links below the given element
36 * also add the action to autofill the "upload as" field
38 * @author Andreas Gohr <andi@splitbrain.org>
40 selectorattach: function(obj){
43 var items = getElementsByClass('select',obj,'a');
44 for(var i=0; i<items.length; i++){
46 elem.style.cursor = 'pointer';
47 addEvent(elem,'click',function(event){ return media_manager.select(event,this); });
50 // hide syntax example
51 items = getElementsByClass('example',obj,'div');
52 for(var i=0; i<items.length; i++){
54 elem.style.display = 'none';
57 var file = $('upload__file');
59 addEvent(file,'change',media_manager.suggest);
63 * Attach deletion confirmation dialog to the delete buttons.
65 * Michael Klier <chi@chimeric.de>
67 confirmattach: function(obj){
70 items = getElementsByClass('btn_media_delete',obj,'a');
71 for(var i=0; i<items.length; i++){
73 addEvent(elem,'click',function(e){
74 if(e.target.tagName == 'IMG'){
75 var name = e.target.parentNode.title;
77 var name = e.target.title;
79 if(!confirm(LANG['del_confirm'] + "\n" + name)) {
90 * Creates checkboxes for additional options
92 * @author Andreas Gohr <andi@splitbrain.org>
94 attachoptions: function(obj){
99 var kobox = document.createElement('input');
100 kobox.type = 'checkbox';
101 kobox.id = 'media__keepopen';
102 if(DokuCookie.getValue('keepopen')){
103 kobox.checked = true;
104 kobox.defaultChecked = true; //IE wants this
105 media_manager.keepopen = true;
107 addEvent(kobox,'click',function(event){ return media_manager.togglekeepopen(event,this); });
109 var kolbl = document.createElement('label');
110 kolbl.htmlFor = 'media__keepopen';
111 kolbl.innerHTML = LANG['keepopen'];
113 var kobr = document.createElement('br');
115 obj.appendChild(kobox);
116 obj.appendChild(kolbl);
117 obj.appendChild(kobr);
121 var hdbox = document.createElement('input');
122 hdbox.type = 'checkbox';
123 hdbox.id = 'media__hide';
124 if(DokuCookie.getValue('hide')){
125 hdbox.checked = true;
126 hdbox.defaultChecked = true; //IE wants this
127 media_manager.hide = true;
129 addEvent(hdbox,'click',function(event){ return media_manager.togglehide(event,this); });
131 var hdlbl = document.createElement('label');
132 hdlbl.htmlFor = 'media__hide';
133 hdlbl.innerHTML = LANG['hidedetails'];
135 var hdbr = document.createElement('br');
137 obj.appendChild(hdbox);
138 obj.appendChild(hdlbl);
139 obj.appendChild(hdbr);
140 media_manager.updatehide();
144 * Opens the searchfield
146 * @author Tobias Sarnowski <sarnowski@cosmocode.de>
148 showsearchfield: function(event,link){
149 // prepare an AJAX call to fetch the search
150 var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
151 ajax.AjaxFailedAlert = '';
152 ajax.encodeURIString = false;
153 if(ajax.failed) return true;
157 var content = $('media__content');
158 content.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" alt="..." class="load" />';
160 ajax.elementObj = content;
161 ajax.afterCompletion = function(){
162 media_manager.selectorattach(content);
163 media_manager.confirmattach(content);
164 media_manager.updatehide();
166 ajax.runAJAX(link.search.substr(1)+'&call=mediasearchlist');
171 * Toggles the keep open state
173 * @author Andreas Gohr <andi@splitbrain.org>
175 togglekeepopen: function(event,cb){
177 DokuCookie.setValue('keepopen',1);
178 media_manager.keepopen = true;
180 DokuCookie.setValue('keepopen','');
181 media_manager.keepopen = false;
186 * Toggles the hide details state
188 * @author Andreas Gohr <andi@splitbrain.org>
190 togglehide: function(event,cb){
192 DokuCookie.setValue('hide',1);
193 media_manager.hide = true;
195 DokuCookie.setValue('hide','');
196 media_manager.hide = false;
198 media_manager.updatehide();
202 * Sets the visibility of the image details accordingly to the
205 * @author Andreas Gohr <andi@splitbrain.org>
207 updatehide: function(){
208 var obj = $('media__content');
210 var details = getElementsByClass('detail',obj,'div');
211 for(var i=0; i<details.length; i++){
212 if(media_manager.hide){
213 details[i].style.display = 'none';
215 details[i].style.display = '';
221 * Insert the clicked image into the opener's textarea
223 * @author Andreas Gohr <andi@splitbrain.org>
225 select: function(event,link){
226 var id = link.name.substr(2);
229 // if we don't run in popup display example
230 var ex = $('ex_'+id.replace(/:/g,'_'));
231 if(ex.style.display == ''){
232 ex.style.display = 'none';
234 ex.style.display = '';
238 opener.insertTags('wiki__text','{{'+id+'|','}}','');
240 if(!media_manager.keepopen) window.close();
246 * list the content of a namespace using AJAX
248 * @author Andreas Gohr <andi@splitbrain.org>
250 list: function(event,link){
251 // prepare an AJAX call to fetch the subtree
252 var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
253 ajax.AjaxFailedAlert = '';
254 ajax.encodeURIString = false;
255 if(ajax.failed) return true;
259 var content = $('media__content');
260 content.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" alt="..." class="load" />';
262 ajax.elementObj = content;
263 ajax.afterCompletion = function(){
264 media_manager.selectorattach(content);
265 media_manager.confirmattach(content);
266 media_manager.updatehide();
267 media_manager.initFlashUpload();
269 ajax.runAJAX(link.search.substr(1)+'&call=medialist');
275 * Open or close a subtree using AJAX
277 * @author Andreas Gohr <andi@splitbrain.org>
279 toggle: function(event,clicky){
280 var listitem = clicky.parentNode;
282 // if already open, close by removing the sublist
283 var sublists = listitem.getElementsByTagName('ul');
285 listitem.removeChild(sublists[0]);
286 clicky.src = DOKU_BASE+'lib/images/plus.gif';
290 // get the enclosed link (is always the first one)
291 var link = listitem.getElementsByTagName('a')[0];
293 // prepare an AJAX call to fetch the subtree
294 var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
295 ajax.AjaxFailedAlert = '';
296 ajax.encodeURIString = false;
297 if(ajax.failed) return true;
300 var ul = document.createElement('ul');
301 //fixme add classname here
302 listitem.appendChild(ul);
303 ajax.elementObj = ul;
304 ajax.afterCompletion = function(){ media_manager.treeattach(ul); };
305 ajax.runAJAX(link.search.substr(1)+'&call=medians');
306 clicky.src = DOKU_BASE+'lib/images/minus.gif';
311 * Prefills the wikiname.
313 * @author Andreas Gohr <andi@splitbrain.org>
316 var file = $('upload__file');
317 var name = $('upload__name');
318 if(!file || !name) return;
320 var text = file.value;
321 text = text.substr(text.lastIndexOf('/')+1);
322 text = text.substr(text.lastIndexOf('\\')+1);
327 initFlashUpload: function(){
328 if(!hasFlash(8)) return;
329 var oform = $('dw__upload');
330 var oflash = $('dw__flashupload');
331 if(!oform || !oflash) return;
333 var clicky = document.createElement('img');
334 clicky.src = DOKU_BASE+'lib/images/multiupload.png';
335 clicky.title = LANG['mu_btn'];
336 clicky.alt = LANG['mu_btn'];
337 clicky.style.cursor = 'pointer';
338 clicky.onclick = function(){
339 oform.style.display = 'none';
340 oflash.style.display = '';
342 oform.appendChild(clicky);
346 addInitEvent(function(){
347 media_manager.treeattach($('media__tree'));
348 media_manager.selectorattach($('media__content'));
349 media_manager.confirmattach($('media__content'));
350 media_manager.attachoptions($('media__opts'));
351 media_manager.initFlashUpload();