confirm.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /**
  6. * @api
  7. */
  8. define([
  9. 'jquery',
  10. 'underscore',
  11. 'mage/translate',
  12. 'jquery/ui',
  13. 'Magento_Ui/js/modal/modal'
  14. ], function ($, _, $t) {
  15. 'use strict';
  16. $.widget('mage.confirm', $.mage.modal, {
  17. options: {
  18. modalClass: 'confirm',
  19. title: '',
  20. focus: '.action-accept',
  21. actions: {
  22. /**
  23. * Callback always - called on all actions.
  24. */
  25. always: function () {},
  26. /**
  27. * Callback confirm.
  28. */
  29. confirm: function () {},
  30. /**
  31. * Callback cancel.
  32. */
  33. cancel: function () {}
  34. },
  35. buttons: [{
  36. text: $t('Cancel'),
  37. class: 'action-secondary action-dismiss',
  38. /**
  39. * Click handler.
  40. */
  41. click: function (event) {
  42. this.closeModal(event);
  43. }
  44. }, {
  45. text: $t('OK'),
  46. class: 'action-primary action-accept',
  47. /**
  48. * Click handler.
  49. */
  50. click: function (event) {
  51. this.closeModal(event, true);
  52. }
  53. }]
  54. },
  55. /**
  56. * Create widget.
  57. */
  58. _create: function () {
  59. this._super();
  60. this.modal.find(this.options.modalCloseBtn).off().on('click', _.bind(this.closeModal, this));
  61. this.openModal();
  62. },
  63. /**
  64. * Remove modal window.
  65. */
  66. _remove: function () {
  67. this.modal.remove();
  68. },
  69. /**
  70. * Open modal window.
  71. */
  72. openModal: function () {
  73. return this._super();
  74. },
  75. /**
  76. * Close modal window.
  77. */
  78. closeModal: function (event, result) {
  79. result = result || false;
  80. if (result) {
  81. this.options.actions.confirm(event);
  82. } else {
  83. this.options.actions.cancel(event);
  84. }
  85. this.options.actions.always(event);
  86. this.element.bind('confirmclosed', _.bind(this._remove, this));
  87. return this._super();
  88. }
  89. });
  90. return function (config) {
  91. return $('<div></div>').html(config.content).confirm(config);
  92. };
  93. });