Add FileUtil.getIconNameByFilename()
authorMatthias Schmidt <gravatronics@live.com>
Sat, 20 May 2017 06:24:58 +0000 (08:24 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 20 May 2017 06:24:58 +0000 (08:24 +0200)
See #2276

wcfsetup/install/files/js/WoltLabSuite/Core/FileUtil.js

index a24aa91dd5c85888c04ae49a0c4ac9ef1ce09749..9750f9f9103ea63b8b4e46edb45d1d673afcbc87 100644 (file)
@@ -6,9 +6,65 @@
  * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @module     WoltLabSuite/Core/FileUtil
  */
-define(['StringUtil'], function(StringUtil) {
+define(['Dictionary', 'StringUtil'], function(Dictionary, StringUtil) {
        "use strict";
        
+       var _fileExtensionIconMapping = Dictionary.fromObject({
+               // archive
+               zip: 'archive',
+               rar: 'archive',
+               tar: 'archive',
+               gz: 'archive',
+               
+               // audio
+               mp3: 'audio',
+               ogg: 'audio',
+               wav: 'audio',
+               
+               // code
+               php: 'code',
+               html: 'code',
+               htm: 'code',
+               tpl: 'code',
+               js: 'code',
+               
+               // excel
+               xls: 'excel',
+               ods: 'excel',
+               xlsx: 'excel',
+               
+               // image
+               gif: 'image',
+               jpg: 'image',
+               jpeg: 'image',
+               png: 'image',
+               bmp: 'image',
+               
+               // video
+               avi: 'video',
+               wmv: 'video',
+               mov: 'video',
+               mp4: 'video',
+               mpg: 'video',
+               mpeg: 'video',
+               flv: 'video',
+               
+               // pdf
+               pdf: 'pdf',
+               
+               // powerpoint
+               ppt: 'powerpoint',
+               pptx: 'powerpoint',
+               
+               // text
+               txt: 'text',
+               
+               // word
+               doc: 'word',
+               docx: 'word',
+               odt: 'word'
+       });
+       
        return {
                /**
                 * Formats the given filesize.
@@ -41,6 +97,28 @@ define(['StringUtil'], function(StringUtil) {
                        }
                        
                        return StringUtil.formatNumeric(byte, -precision) + ' ' + symbol;
+               },
+               
+               /**
+                * Returns the icon name for given filename.
+                * 
+                * Note: For any file icon name like `fa-file-word`, only `word`
+                * will be returned by this method.
+                *
+                * @parsm       {string}        filename        name of file for which icon name will be returned
+                * @return      {string}        FontAwesome icon name
+                */
+               getIconNameByFilename: function(filename) {
+                       var lastDotPosition = filename.lastIndexOf('.');
+                       if (lastDotPosition !== false) {
+                               var extension = filename.substr(lastDotPosition + 1);
+                               
+                               if (_fileExtensionIconMapping.has(extension)) {
+                                       return _fileExtensionIconMapping.get(extension);
+                               }
+                       }
+                       
+                       return '';
                }
        };
 });