Skip to content

Instantly share code, notes, and snippets.

@Akeri
Created March 28, 2020 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akeri/04b11a911fbabac166cd7cce929bde03 to your computer and use it in GitHub Desktop.
Save Akeri/04b11a911fbabac166cd7cce929bde03 to your computer and use it in GitHub Desktop.
AngularJS Filter to transform MIME type to FontAwesome 4 icon class
angular.module('ngapp')
/**
* Transform MIME type to FontAwesome 4 icon class
*/
.filter('mime2fa', function () {
const iconClasses = {
// Media
'image': 'fa-file-image-o',
'audio': 'fa-file-audio-o',
'video': 'fa-file-video-o',
// Documents
'application/pdf': 'fa-file-pdf-o',
'application/msword': 'fa-file-word-o',
'application/vnd.ms-word': 'fa-file-word-o',
'application/vnd.oasis.opendocument.text': 'fa-file-word-o',
'application/vnd.openxmlformats-officedocument.wordprocessingml': 'fa-file-word-o',
'application/vnd.ms-excel': 'fa-file-excel-o',
'application/vnd.openxmlformats-officedocument.spreadsheetml': 'fa-file-excel-o',
'application/vnd.oasis.opendocument.spreadsheet': 'fa-file-excel-o',
'application/vnd.ms-powerpoint': 'fa-file-powerpoint-o',
'application/vnd.openxmlformats-officedocument.presentationml': 'fa-file-powerpoint-o',
'application/vnd.oasis.opendocument.presentation': 'fa-file-powerpoint-o',
'text/plain': 'fa-file-text-o',
'text/html': 'fa-file-code-o',
'application/json': 'fa-file-code-o',
// Archives
'application/gzip': 'fa-file-archive-o',
'application/zip': 'fa-file-archive-o'
};
return function (mimeType) {
let fa = 'file-o';
for (let key in iconClasses) {
if (iconClasses.hasOwnProperty(key) && mimeType.search(key) === 0) {
fa = iconClasses[key];
}
}
return `fa ${fa}`;
};
});
@Akeri
Copy link
Author

Akeri commented Mar 28, 2020

Usage example

<i ng-class="document.mimeType | mime2fa"></i>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment