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

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-06-20 11:50:58 +00:00
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 => {
const controller = field.$ctrl;
controller.$oldDisabled = controller.disabled;
controller.disabled = true;
2019-06-20 11:50:58 +00:00
});
cb($scope).finally(() => {
angular.forEach(fields, field => {
const controller = field.$ctrl;
if (!controller.$oldDisabled)
controller.disabled = false;
2019-06-20 11:50:58 +00:00
});
});
});
}
};
}
directive.$inject = ['$parse'];
ngModule.directive('vnHttpSubmit', directive);