aboalkhair

Confirm Modal /AngularJs Directive

Oct 7th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. JS
  2.  
  3. (function () {
  4. 'use strict';
  5.  
  6. angular
  7. .module('app')
  8. .directive('ngConfirmClick', ['$modal', "$translate",
  9. function ($modal, $translate) {
  10.  
  11. var ModalInstanceCtrl = function ($scope, $modalInstance) {
  12. $scope.ok = function () {
  13. $modalInstance.close();
  14. };
  15.  
  16. $scope.cancel = function () {
  17. $modalInstance.dismiss('cancel');
  18. };
  19. };
  20.  
  21. return {
  22. restrict: 'A',
  23. scope: {
  24. ngConfirmClick: "&",
  25. item: "="
  26. },
  27. link: function (scope, element, attrs) {
  28. element.bind('click', function () {
  29. var message = $translate.instant(attrs.ngConfirmMessage) || "Are you sure ?";
  30. var Type = attrs.ngConfirmType || "warning";
  31. var title = $translate.instant(attrs.ngConfirmTitle) || "";
  32. var ShowConfirm = attrs.ngConfirm || true;
  33. var ConfirmText = $translate.instant(attrs.ngConfirmText) || "CONFIRM";
  34. var ConfirmCanceltext = $translate.instant(attrs.ngConfirmCanceltext) || "CANCEL";
  35.  
  36. var windowClass = "";
  37. /*
  38. //This works
  39. if (message && confirm(message)) {
  40. scope.$apply(attrs.ngConfirmClick);
  41. }
  42. //*/
  43.  
  44. //*This doesn't works
  45. var icon = 'fa fa-warning';
  46. switch (Type) {
  47. case "warning":
  48. icon = 'fa fa-warning';
  49. windowClass = 'modal-message modal-warning';
  50. break;
  51. case "danger":
  52. icon = 'glyphicon glyphicon-fire';
  53. windowClass = 'modal-message modal-danger';
  54. break;
  55. case "info":
  56. icon = 'fa fa-envelope';
  57. windowClass = "modal-message modal-info";
  58. break;
  59. case "success":
  60. icon = 'glyphicon glyphicon-check';
  61. windowClass = "modal-message modal-success";
  62. break;
  63.  
  64. }
  65.  
  66. var modalHeader = "<div class='modal-header'><i class='" + icon + "'></i></div>" + (title != "" ? "<div class='modal-title'>" + title + "</div>" : "")
  67. var modalHtml = '<div class="modal-body">' + message + '</div>';
  68. var modalfooter = '<div class="modal-footer">' + (ShowConfirm ? '<button class="btn btn-primary" ng-click="ok()">' + $translate.instant(ConfirmText) + '</button>' : "") + "<button class='btn btn-warning' ng-click='cancel()'>" + $translate.instant(ConfirmCanceltext) + "</button></div>";
  69.  
  70. var modalInstance = $modal.open({
  71. windowClass: windowClass,
  72. template: modalHeader + modalHtml + modalfooter,
  73. controller: ModalInstanceCtrl
  74. });
  75.  
  76. modalInstance.result.then(function () {
  77. scope.ngConfirmClick({ item: scope.item }); //raise an error : $digest already in progress
  78. }, function () {
  79. //Modal dismissed
  80. });
  81. //*/
  82.  
  83. });
  84.  
  85. }
  86. }
  87. }
  88. ]);
  89. })();
  90.  
  91.  
  92.  
  93.  
  94. HTML
  95.  
  96. <button class="btn btn-danger"
  97. ng-if="currentAbsence != null"
  98. ng-confirm-message="CONFIRM_DELETE_MESSAGE" ng-confirm-title="TITLE" ng-confirm-click="delete()">
  99. <i class="fa fa-trash"></i>
  100. {{'DELETE' | translate}}
  101. </button>
Advertisement
Add Comment
Please, Sign In to add comment