[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/editor/atto/plugins/managefiles/yui/build/moodle-atto_managefiles-usedfiles/ -> moodle-atto_managefiles-usedfiles-debug.js (source)

   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              Y.log("Unable to find parent window", 'warn', 'moodle-atto_managemedia-usedfiles');
 108              return;
 109          }
 110  
 111          usedFiles = this._getUsedFiles();
 112          unusedFiles = this.findUnusedFiles(allFiles, usedFiles);
 113          missingFiles = this.findMissingFiles(allFiles, usedFiles);
 114  
 115          // There are some unused files.
 116          if (unusedFiles.length > 0) {
 117              // Loop over all the files in the form.
 118              form.all('input[type=checkbox][name^="deletefile"]').each(function(node) {
 119                  // If the file is used, remove it.
 120                  if (Y.Array.indexOf(unusedFiles, node.getData('filename')) === -1) {
 121                      node.ancestor(SELECTORS.FILEANCESTOR).remove();
 122                  }
 123              });
 124              form.addClass(CSS.HASUNUSEDFILES);
 125          } else {
 126              // This is needed as the init may be called twice due to the double call to $PAGE->requires->yui_module().
 127              form.removeClass(CSS.HASUNUSEDFILES);
 128          }
 129  
 130          // There are some files missing.
 131          if (missingFiles.length > 0) {
 132              missingFilesTxt = '<ul>';
 133              for (i = 0; i < missingFiles.length; i++) {
 134                  missingFilesTxt += '<li>' + Y.Escape.html(missingFiles[i]) + '</li>';
 135              }
 136              missingFilesTxt += '</ul>';
 137              form.one(SELECTORS.MISSINGFILES).setHTML('').append(missingFilesTxt);
 138              form.addClass(CSS.HASMISSINGFILES);
 139          } else {
 140              form.removeClass(CSS.HASMISSINGFILES);
 141          }
 142      },
 143  
 144      /**
 145       * Return the list of files used in the area.
 146       *
 147       * @method _getUsedFiles
 148       * @return {Object} List of files used where the keys are the name of the files, the value is true.
 149       * @private
 150       */
 151      _getUsedFiles: function() {
 152          var content = Y.one(window.parent.document.getElementById(this._elementid + 'editable')),
 153              baseUrl = M.cfg.wwwroot + '/draftfile.php/' + this._usercontext + '/user/draft/' + this._itemid + '/',
 154              pattern = new RegExp(baseUrl.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&') + "(.+?)[\\?\"']", 'gm'),
 155              filename = '',
 156              match = '',
 157              usedFiles = {};
 158  
 159          while ((match = pattern.exec(content.get('innerHTML'))) !== null) {
 160              filename = decodeURI(match[1]);
 161              usedFiles[filename] = true;
 162          }
 163  
 164          return usedFiles;
 165      },
 166  
 167      /**
 168       * Return an array of unused files.
 169       *
 170       * @param {Object} allFiles Where the keys are the file names.
 171       * @param {Object} usedFiles Where the keys are the file names.
 172       * @return {Array} Of file names.
 173       */
 174      findUnusedFiles: function(allFiles, usedFiles) {
 175          var key,
 176              list = [];
 177          for (key in allFiles) {
 178              if (!usedFiles[key]) {
 179                  list.push(key);
 180              }
 181          }
 182          return list;
 183      },
 184  
 185      /**
 186       * Return an array of missing files.
 187       *
 188       * @param {Object} allFiles Where the keys are the file names.
 189       * @param {Object} usedFiles Where the keys are the file names.
 190       * @return {Array} Of file names.
 191       */
 192      findMissingFiles: function(allFiles, usedFiles) {
 193          var key,
 194              list = [];
 195          for (key in usedFiles) {
 196              if (!allFiles[key]) {
 197                  list.push(key);
 198              }
 199          }
 200          return list;
 201      }
 202  };
 203  
 204  
 205  }, '@VERSION@', {"requires": ["node", "escape"]});


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1