1/**
2 * PreserveFilenames Plugin for DokuWiki / script.js
3 *
4 * @author Kazutaka Miyasaka <kazmiya@gmail.com>
5 */
6
7(function() {
8    // check DokuWiki version
9    if (
10        typeof DEPRECATED === 'function' ||
11        typeof addInitEvent === 'undefined'
12    ) {
13        // DokuWiki Angua or later
14        jQuery(_Angua);
15    } else if (
16        typeof JSINFO === 'object' &&
17        JSINFO.plugin_preservefilenames
18    ) {
19        // DokuWiki Anteater or Rincewind
20        addInitEvent(_Anteater);
21    }
22
23    /**
24     * for DokuWiki Angua or later
25     */
26    function _Angua() {
27        var update_content_orig,
28            insert_orig;
29
30        // replace built-in methods with plugin's
31        if (typeof dw_mediamanager === 'object') {
32            update_content_orig = dw_mediamanager.update_content;
33            dw_mediamanager.update_content = mod_update_content;
34
35            insert_orig = dw_mediamanager.insert;
36            dw_mediamanager.insert = mod_insert;
37        }
38
39        if (
40            typeof qq === 'object' &&
41            typeof qq.FileUploaderExtended === 'function'
42        ) {
43            qq.FileUploaderExtended.prototype._addToList = mod_addToList;
44        }
45
46        /**
47         * Changes ajax call parameter in media manager
48         */
49        function mod_update_content($content, params, update_list) {
50            params = params.replace(
51                /\bcall=(media(?:list))\&/,
52                'call=preservefilenames_$1&'
53            );
54
55            update_content_orig($content, params, update_list);
56        }
57
58        /**
59         * Changes file part of a media id into its original filename
60         */
61        function mod_insert(id) {
62            var filename,
63                matches;
64
65            // retrieve original filename by DOM manipulation
66            filename = jQuery('#media__content')
67                .find('a[id="h_' + id + '"]:first, a[name="h_' + id + '"]:first')
68                .text();
69
70            if (
71                filename !== '' &&
72                (matches = id.match(/^((?::[^:]+)*:)[^:]+$/))
73            ) {
74                filename = filename.replace(/\{/g, '(').replace(/\}/g, ')');
75                id = matches[1] + filename;
76            }
77
78            insert_orig(id);
79        }
80
81        /**
82         * Fills input box with raw file name (without cleanID process)
83         */
84        function mod_addToList(id, fileName) {
85            var item = qq.toElement(this._options.fileTemplate);
86            item.qqFileId = id;
87
88            var fileElement = this._find(item, 'file');
89            qq.setText(fileElement, fileName);
90            this._find(item, 'size').style.display = 'none';
91
92            var nameElement = this._find(item, 'nameInput');
93            nameElement.value = fileName;
94            nameElement.id = 'mediamanager__upload_item' + id;
95
96            this._listElement.appendChild(item);
97        }
98    }
99
100    /**
101     * for DokuWiki Anteater and Rincewind
102     */
103    function _Anteater() {
104        if (
105            !JSINFO ||
106            !JSINFO.plugin_preservefilenames ||
107            !JSINFO.plugin_preservefilenames.in_mediamanager
108        ) {
109            return;
110        }
111
112        JSINFO.plugin_preservefilenames.getFakeID = function(id) {
113            var matched = id.match(/^(.*?:)[^:]+$/);
114            if (!matched) return id;
115            var namespace = matched[1];
116
117            var content_div = $('media__content');
118            if (!content_div) return id;
119
120            var medialinks = getElementsByClass('mediafile', content_div, 'a');
121
122            for (var i = 0, i_len = medialinks.length; i < i_len; i++) {
123                if (!medialinks[i].name || medialinks[i].name != 'h_' + id) continue;
124                var link = medialinks[i];
125                break;
126            }
127            if (!link || !link.firstChild || !link.firstChild.nodeValue) return id;
128
129            // replace characters which cannot be included in a link title
130            var filename_orig = link.firstChild.nodeValue;
131            filename_orig = filename_orig.replace(/\{/g, '(');
132            filename_orig = filename_orig.replace(/\}/g, ')');
133
134            // return namespace + filename_orig as a "fake id"
135            return namespace + filename_orig;
136        };
137
138        // override media listing via ajax
139        eval('media_manager.list = ' + media_manager.list.toString().replace(
140            /&call=medialist/,
141            '&call=medialist_preservefilenames'
142        ));
143
144        // override snippet insertion
145        if (!!media_manager.select) { // DokuWiki 2009-12-25
146            eval('media_manager.select = ' + media_manager.select.toString().replace(
147                /\+\s*id\s*\+/,
148                '+ JSINFO.plugin_preservefilenames.getFakeID(id) +'
149            ));
150        }
151
152        if (!!media_manager.insert) { // DokuWiki dev@2010/03
153            eval('media_manager.insert = ' + media_manager.insert.toString().replace(
154                /\+\s*id\s*\+/,
155                '+ JSINFO.plugin_preservefilenames.getFakeID(id) +'
156            ));
157        }
158    }
159})();
160