Commit | Line | Data |
---|---|---|
9e88661e MG |
1 | /** |
2 | * TinyMCE plugin ManageFiles - provides UI to edit files embedded in the text editor. | |
3 | * | |
4 | * @author Marina Glancy | |
5 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6 | */ | |
7 | ||
8 | (function() { | |
9 | tinymce.create('tinymce.plugins.MoodleManageFiles', { | |
10 | /** | |
11 | * Initializes the plugin, this will be executed after the plugin has been created. | |
12 | * This call is done before the editor instance has finished it's initialization so use the onInit event | |
13 | * of the editor instance to intercept that event. | |
14 | * | |
15 | * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in. | |
16 | * @param {string} url Absolute URL to where the plugin is located. | |
17 | */ | |
18 | init : function(ed, url) { | |
19 | ed.addCommand('mceForceRepaint', function() { | |
20 | var root = ed.dom.getRoot(); | |
21 | items = root.getElementsByTagName("img"); | |
22 | for (var i = 0; i < items.length; i++) { | |
23 | src = items[i].getAttribute('src').replace(/\?\d+$/, ''); | |
24 | items[i].setAttribute('src', src+'?'+(new Date().getTime())) | |
25 | } | |
683bd5b1 DW |
26 | ed.execCommand('mceRepaint'); |
27 | ed.focus(); | |
9e88661e MG |
28 | }); |
29 | ||
30 | ed.addCommand('mceMaximizeWindow', function(w) { | |
31 | // This function duplicates the TinyMCE windowManager code when 'maximize' button is pressed. | |
32 | var vp = ed.dom.getViewPort(), | |
33 | id = w.id; | |
34 | // Reduce viewport size to avoid scrollbars | |
35 | vp.w -= 2; | |
36 | vp.h -= 2; | |
37 | ||
38 | w.oldPos = w.element.getXY(); | |
39 | w.oldSize = w.element.getSize(); | |
40 | ||
41 | w.element.moveTo(vp.x, vp.y); | |
42 | w.element.resizeTo(vp.w, vp.h); | |
43 | ed.dom.setStyles(id + '_ifr', {width : vp.w - w.deltaWidth, height : vp.h - w.deltaHeight}); | |
44 | ed.dom.addClass(id + '_wrapper', 'mceMaximized'); | |
45 | }); | |
46 | ||
47 | ed.addCommand('mceManageFiles', function() { | |
48 | var managefiles = ed.getParam('managefiles', {}), key, cnt = 0, | |
49 | fileurl = ed.getParam("moodle_plugin_base") + 'managefiles/manage.php?'; | |
50 | for (key in managefiles) { | |
51 | fileurl += (cnt++ ? '&' : '') + encodeURIComponent(key) + "=" + encodeURIComponent(managefiles[key]) + "&"; | |
52 | } | |
53 | var onClose = function() { | |
54 | ed.windowManager.onClose.remove(onClose); | |
55 | ed.execCommand('mceForceRepaint'); | |
56 | }; | |
57 | ed.windowManager.onClose.add(onClose); | |
58 | var vp = ed.dom.getViewPort(), | |
59 | width = 865 + parseInt(ed.getLang('advimage.delta_width', 0)), | |
60 | height = 600 + parseInt(ed.getLang('advimage.delta_height', 0)), | |
61 | maximizedmode = (width >= vp.w - 2 || height >= vp.h - 2); | |
62 | if (maximizedmode) { | |
63 | width = vp.w; | |
64 | height = vp.h; | |
65 | } | |
66 | w = ed.windowManager.open({ | |
67 | file : fileurl , | |
68 | width : width, | |
69 | height : height, | |
70 | inline : 1 | |
71 | }, { | |
72 | plugin_url : url // Plugin absolute URL | |
73 | }); | |
74 | if (maximizedmode) { | |
75 | ed.execCommand('mceMaximizeWindow', w); | |
76 | } | |
77 | }); | |
78 | ||
79 | ed.addCommand('mceManageFilesUsedFiles', function() { | |
80 | var managefiles = ed.getParam('managefiles', {}), | |
81 | text = ed.dom.getRoot().innerHTML, | |
82 | base = ed.getParam('document_base_url') + '/draftfile.php/' + managefiles['usercontext'] + '/user/draft/' + managefiles['itemid'] + '/', | |
83 | patt = new RegExp(base.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "(.+?)[\\?\"']", 'gm'), | |
84 | arr = [], match, filename; | |
85 | while ((match = patt.exec(text)) !== null) { | |
86 | filename = unescape(match[1]); | |
87 | if (arr.indexOf(filename) === -1) { | |
88 | arr[arr.length] = filename; | |
89 | } | |
90 | } | |
91 | return arr; | |
92 | }); | |
93 | ||
94 | var managefiles = ed.getParam('managefiles', {}); | |
95 | // Get draft area id from filepicker options. | |
96 | if (!managefiles.itemid && M.editor_tinymce.filepicker_options && M.editor_tinymce.filepicker_options[ed.id]) { | |
97 | managefiles.itemid = M.editor_tinymce.filepicker_options[ed.id].image.itemid; | |
98 | ed.settings['managefiles'].itemid = managefiles.itemid; | |
99 | } | |
100 | ||
101 | // Register buttons | |
102 | if (managefiles.itemid) { | |
103 | ed.addButton('managefiles', { | |
104 | title : 'managefiles.desc', | |
105 | cmd : 'mceManageFiles', | |
106 | image : url + '/img/managefiles.png' | |
107 | }); | |
108 | } | |
109 | }, | |
110 | createControl : function(n, cm) { | |
111 | return null; | |
112 | }, | |
113 | ||
114 | /** | |
115 | * Returns information about the plugin as a name/value array. | |
116 | * The current keys are longname, author, authorurl, infourl and version. | |
117 | * | |
118 | * @return {Object} Name/value array containing information about the plugin. | |
119 | */ | |
120 | getInfo : function() { | |
121 | return { | |
122 | longname : 'Moodle Manage embedded files plugin', | |
123 | author : 'Marina Glancy', | |
124 | infourl : 'http://moodle.org', | |
125 | version : "1.0" | |
126 | }; | |
127 | } | |
128 | }); | |
129 | ||
130 | // Register plugin. | |
131 | tinymce.PluginManager.add('managefiles', tinymce.plugins.MoodleManageFiles); | |
132 | })(); |