salix/front/core/directives/http-submit.js

38 lines
1.1 KiB
JavaScript

import ngModule from '../module';
/**
* Disables a submitted form while request is being processed
* Enables again when promise ends
*
* @param {Object} $parse
* @return {Object} The directive
*/
export function directive($parse) {
return {
restrict: 'A',
link: function($scope, $element, $attrs) {
const cb = $parse($attrs.vnHttpSubmit);
const element = $element[0];
$element.on('submit', () => {
const selector = 'vn-textfield, vn-autocomplete, vn-submit';
const elements = element.querySelectorAll(selector);
const fields = angular.element(elements);
angular.forEach(fields, field => {
field.$ctrl.disabled = true;
});
cb($scope).finally(() => {
angular.forEach(fields, field => {
field.$ctrl.disabled = false;
});
});
});
}
};
}
directive.$inject = ['$parse'];
ngModule.directive('vnHttpSubmit', directive);