[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 YUI.add('moodle-atto_managefiles-usedfiles', function (Y, NAME) { 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * @package atto_managefiles 20 * @copyright 2014 Frédéric Massart 21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 22 */ 23 24 /** 25 * @module moodle-atto_managefiles-usedfiles 26 */ 27 28 29 /** 30 * Atto text editor managefiles usedfiles plugin. 31 * 32 * @namespace M.atto_managefiles 33 * @class usedfiles 34 */ 35 36 /** 37 * CSS constants. 38 * 39 * @type {Object} 40 */ 41 var CSS = { 42 HASMISSINGFILES: 'has-missing-files', 43 HASUNUSEDFILES: 'has-unused-files' 44 }; 45 46 /** 47 * Selectors constants. 48 * 49 * @type {Object} 50 */ 51 var SELECTORS = { 52 FILEANCESTOR: '.fitem_fcheckbox', 53 FORM: '#atto_managefiles_manageform', 54 MISSINGFILES: '.missing-files' 55 }; 56 57 M.atto_managefiles = M.atto_managefiles || {}; 58 M.atto_managefiles.usedfiles = M.atto_managefiles.usedfiles || { 59 60 /** 61 * The user context. 62 * 63 * @property _usercontext 64 * @type Number 65 * @private 66 */ 67 _usercontext: null, 68 69 /** 70 * Area Item ID. 71 * 72 * @property _itemid 73 * @type String 74 * @private 75 */ 76 _itemid: null, 77 78 /** 79 * The editor elementid 80 * 81 * @property _elementid 82 * @type String 83 * @private 84 */ 85 _elementid: null, 86 87 /** 88 * Init function. 89 * 90 * @param {Object} allFiles The keys are the file names, the values are the hashes. 91 * @return {Void} 92 */ 93 init: function(config) { 94 this._usercontext = config.usercontext; 95 this._itemid = config.itemid; 96 this._elementid = config.elementid; 97 98 var allFiles = config.files; 99 var form = Y.one(SELECTORS.FORM), 100 usedFiles, 101 unusedFiles, 102 missingFiles, 103 missingFilesTxt, 104 i; 105 106 if (!form || !window.parent) { 107 return; 108 } 109 110 usedFiles = this._getUsedFiles(); 111 unusedFiles = this.findUnusedFiles(allFiles, usedFiles); 112 missingFiles = this.findMissingFiles(allFiles, usedFiles); 113 114 // There are some unused files. 115 if (unusedFiles.length > 0) { 116 // Loop over all the files in the form. 117 form.all('input[type=checkbox][name^="deletefile"]').each(function(node) { 118 // If the file is used, remove it. 119 if (Y.Array.indexOf(unusedFiles, node.getData('filename')) === -1) { 120 node.ancestor(SELECTORS.FILEANCESTOR).remove(); 121 } 122 }); 123 form.addClass(CSS.HASUNUSEDFILES); 124 } else { 125 // This is needed as the init may be called twice due to the double call to $PAGE->requires->yui_module(). 126 form.removeClass(CSS.HASUNUSEDFILES); 127 } 128 129 // There are some files missing. 130 if (missingFiles.length > 0) { 131 missingFilesTxt = '<ul>'; 132 for (i = 0; i < missingFiles.length; i++) { 133 missingFilesTxt += '<li>' + Y.Escape.html(missingFiles[i]) + '</li>'; 134 } 135 missingFilesTxt += '</ul>'; 136 form.one(SELECTORS.MISSINGFILES).setHTML('').append(missingFilesTxt); 137 form.addClass(CSS.HASMISSINGFILES); 138 } else { 139 form.removeClass(CSS.HASMISSINGFILES); 140 } 141 }, 142 143 /** 144 * Return the list of files used in the area. 145 * 146 * @method _getUsedFiles 147 * @return {Object} List of files used where the keys are the name of the files, the value is true. 148 * @private 149 */ 150 _getUsedFiles: function() { 151 var content = Y.one(window.parent.document.getElementById(this._elementid + 'editable')), 152 baseUrl = M.cfg.wwwroot + '/draftfile.php/' + this._usercontext + '/user/draft/' + this._itemid + '/', 153 pattern = new RegExp(baseUrl.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&') + "(.+?)[\\?\"']", 'gm'), 154 filename = '', 155 match = '', 156 usedFiles = {}; 157 158 while ((match = pattern.exec(content.get('innerHTML'))) !== null) { 159 filename = decodeURI(match[1]); 160 usedFiles[filename] = true; 161 } 162 163 return usedFiles; 164 }, 165 166 /** 167 * Return an array of unused files. 168 * 169 * @param {Object} allFiles Where the keys are the file names. 170 * @param {Object} usedFiles Where the keys are the file names. 171 * @return {Array} Of file names. 172 */ 173 findUnusedFiles: function(allFiles, usedFiles) { 174 var key, 175 list = []; 176 for (key in allFiles) { 177 if (!usedFiles[key]) { 178 list.push(key); 179 } 180 } 181 return list; 182 }, 183 184 /** 185 * Return an array of missing files. 186 * 187 * @param {Object} allFiles Where the keys are the file names. 188 * @param {Object} usedFiles Where the keys are the file names. 189 * @return {Array} Of file names. 190 */ 191 findMissingFiles: function(allFiles, usedFiles) { 192 var key, 193 list = []; 194 for (key in usedFiles) { 195 if (!allFiles[key]) { 196 list.push(key); 197 } 198 } 199 return list; 200 } 201 }; 202 203 204 }, '@VERSION@', {"requires": ["node", "escape"]});
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |