2 * TinyMCE plugin ManageFiles - provides UI to edit files embedded in the text editor.
4 * @author Marina Glancy
5 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9 tinymce.create('tinymce.plugins.MoodleManageFiles', {
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.
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.
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()))
26 ed.execCommand('mceRepaint');
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(),
34 // Reduce viewport size to avoid scrollbars
38 w.oldPos = w.element.getXY();
39 w.oldSize = w.element.getSize();
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');
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]) + "&";
53 var onClose = function() {
54 ed.windowManager.onClose.remove(onClose);
55 ed.execCommand('mceForceRepaint');
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);
66 w = ed.windowManager.open({
72 plugin_url : url // Plugin absolute URL
75 ed.execCommand('mceMaximizeWindow', w);
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;
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;
102 if (managefiles.itemid) {
103 ed.addButton('managefiles', {
104 title : 'managefiles.desc',
105 cmd : 'mceManageFiles',
106 image : url + '/img/managefiles.png'
110 createControl : function(n, cm) {
115 * Returns information about the plugin as a name/value array.
116 * The current keys are longname, author, authorurl, infourl and version.
118 * @return {Object} Name/value array containing information about the plugin.
120 getInfo : function() {
122 longname : 'Moodle Manage embedded files plugin',
123 author : 'Marina Glancy',
124 infourl : 'http://moodle.org',
131 tinymce.PluginManager.add('managefiles', tinymce.plugins.MoodleManageFiles);