xref: /dokuwiki/lib/scripts/media.js (revision 3df72098bbc205fa4bd4735d52d2626baad93548)
1/**
2 * JavaScript functionalitiy for the media management popup
3 *
4 * @author Andreas Gohr <andi@splitbrain.org>
5 */
6media = {
7    keepopen: false,
8
9    /**
10     * Attach event handlers to all "folders" below the given element
11     *
12     * @author Andreas Gohr <andi@splitbrain.org>
13     */
14    treeattach: function(obj){
15        if(!obj) return;
16
17        var items = obj.getElementsByTagName('li');
18        for(var i=0; i<items.length; i++){
19            var elem = items[i];
20
21            // attach action to make the +/- clickable
22            var clicky = elem.getElementsByTagName('img')[0];
23            clicky.style.cursor = 'pointer';
24            addEvent(clicky,'click',function(event){ return media.toggle(event,this); });
25
26            // attach action load folder list via AJAX
27            var link = elem.getElementsByTagName('a')[0];
28            link.style.cursor = 'pointer';
29            addEvent(link,'click',function(event){ return media.list(event,this); });
30        }
31    },
32
33    /**
34     * Attach the image selector action to all links below the given element
35     * also add the action to autofill the "upload as" field
36     *
37     * @author Andreas Gohr <andi@splitbrain.org>
38     */
39    selectorattach: function(obj){
40        if(!obj) return;
41
42        var items = getElementsByClass('select',obj,'a');
43        for(var i=0; i<items.length; i++){
44            var elem = items[i];
45            elem.style.cursor = 'pointer';
46            addEvent(elem,'click',function(event){ return media.select(event,this); });
47        }
48
49        var file = $('upload__file');
50        if(!file) return;
51        addEvent(file,'change',media.suggest);
52    },
53
54    /**
55     * Creates a checkbox for keeping the popup on selection
56     *
57     * @author Andreas Gohr <andi@splitbrain.org>
58     */
59    attachkeepopen: function(obj){
60        if(!obj) return;
61
62        var cbox  = document.createElement('input');
63        cbox.type = 'checkbox';
64        cbox.id   = 'media__keepopen';
65        if(DokuCookie.getValue('keepopen')){
66            cbox.checked = true;
67        }
68        addEvent(cbox,'change',function(event){ return media.togglekeepopen(event,this); });
69
70        var clbl  = document.createElement('label');
71        clbl.htmlFor   = 'media__keepopen';
72        clbl.innerHTML = LANG['keepopen'];
73
74        obj.appendChild(cbox);
75        obj.appendChild(clbl);
76    },
77
78    /**
79     * Toggles the keep open state
80     *
81     * @author Andreas Gohr <andi@splitbrain.org>
82     */
83    togglekeepopen: function(event,cb){
84        if(cb.checked){
85            DokuCookie.setValue('keepopen',1);
86            media.keepopen = true;
87        }else{
88            DokuCookie.setValue('keepopen','');
89            media.keepopen = false;
90        }
91    },
92
93    /**
94     * Insert the clicked image into the opener's textarea
95     *
96     * @author Andreas Gohr <andi@splitbrain.org>
97     */
98    select: function(event,link){
99        var id = link.name.substr(2);
100
101        if(!opener){
102            alert(LANG['idtouse']+"\n:"+id);
103            return false;
104        }
105        opener.insertTags('wiki__text','{{'+id+'|','}}','');
106
107        if(!media.keepopen) window.close();
108        return false;
109    },
110
111    /**
112     * list the content of a namespace using AJAX
113     *
114     * @author Andreas Gohr <andi@splitbrain.org>
115     */
116    list: function(event,link){
117        // prepare an AJAX call to fetch the subtree
118        var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
119        ajax.AjaxFailedAlert = '';
120        ajax.encodeURIString = false;
121        if(ajax.failed) return true;
122
123        cleanMsgArea();
124
125        var content = $('media__content');
126        content.innerHTML = '<img src="'+DOKU_BASE+'lib/images/loading.gif" alt="..." class="load" />';
127
128        ajax.elementObj = content;
129        ajax.afterCompletion = function(){ media.selectorattach(content); };
130        ajax.runAJAX(link.search.substr(1)+'&call=medialist');
131        return false;
132    },
133
134
135    /**
136     * Open or close a subtree using AJAX
137     *
138     * @author Andreas Gohr <andi@splitbrain.org>
139     */
140    toggle: function(event,clicky){
141        var listitem = clicky.parentNode;
142
143        // if already open, close by removing the sublist
144        var sublists = listitem.getElementsByTagName('ul');
145        if(sublists.length){
146            listitem.removeChild(sublists[0]);
147            clicky.src = DOKU_BASE+'lib/images/plus.gif';
148            return false;
149        }
150
151        // get the enclosed link (is always the first one)
152        var link = listitem.getElementsByTagName('a')[0];
153
154        // prepare an AJAX call to fetch the subtree
155        var ajax = new sack(DOKU_BASE + 'lib/exe/ajax.php');
156        ajax.AjaxFailedAlert = '';
157        ajax.encodeURIString = false;
158        if(ajax.failed) return true;
159
160        //prepare the new ul
161        var ul = document.createElement('ul');
162        //fixme add classname here
163        listitem.appendChild(ul);
164        ajax.elementObj = ul;
165        ajax.afterCompletion = function(){ media.treeattach(ul); };
166        ajax.runAJAX(link.search.substr(1)+'&call=medians');
167        clicky.src = DOKU_BASE+'lib/images/minus.gif';
168        return false;
169    },
170
171    /**
172     * Prefills the wikiname.
173     *
174     * @author Andreas Gohr <andi@splitbrain.org>
175     */
176    suggest: function(){
177        var file = $('upload__file');
178        var name = $('upload__name');
179        if(!file || !name) return;
180
181        var text = file.value;
182        text = text.substr(text.lastIndexOf('/')+1);
183        text = text.substr(text.lastIndexOf('\\')+1);
184        name.value = text;
185    }
186
187}
188
189addInitEvent(function(){media.treeattach($('media__tree'));});
190addInitEvent(function(){media.selectorattach($('media__content'));});
191addInitEvent(function(){media.attachkeepopen($('media__opts'));});
192