import ngModule from '../module';
import Dialog from '../components/dialog';
import {kebabToCamel} from '../lib/string';

/**
 * Registers a click handler on the element that opens the dialog id
 * specified as value.
 *
 * @return {Object} The directive
 */
export default function directive() {
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.on('click', function(event) {
                if (event.defaultPrevented) return;
                let dialogKey = kebabToCamel($attrs.vnDialog);
                let dialog = $scope[dialogKey];
                if (dialog instanceof Dialog)
                    dialog.show();
            });
        }
    };
}
ngModule.directive('vnDialog', directive);