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

33 lines
899 B
JavaScript
Raw Normal View History

2019-06-20 11:50:58 +00:00
import ngModule from '../module';
/**
* Disables a clicked element 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.vnHttpClick);
const element = $element[0];
$element.on('click', () => {
const controller = element.$ctrl;
2020-02-20 09:57:06 +00:00
controller.$oldDisabled = controller.disabled;
controller.disabled = true;
2019-06-20 11:50:58 +00:00
cb($scope).finally(() => {
if (!controller.$oldDisabled)
controller.disabled = false;
2019-06-20 11:50:58 +00:00
});
});
}
};
}
directive.$inject = ['$parse'];
ngModule.directive('vnHttpClick', directive);