import ngModule from '../module';
import noImage from './no-image-dark.png';

/**
 * Sets a default image when there is an error loading
 * the original image.
 *
 * @return {Object} The directive
 */
export default function onErrorSrc() {
    return {
        restrict: 'A',
        link: (scope, element, attrs) => {
            let imgError = noImage;
            element.bind('error', function() {
                if (attrs.src != imgError)
                    attrs.$set('src', imgError);
            });
        }
    };
}
ngModule.directive('onErrorSrc', onErrorSrc);