captcha.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /*global alert*/
  6. define([
  7. 'jquery',
  8. 'ko',
  9. 'Magento_Captcha/js/action/refresh'
  10. ], function ($, ko, refreshAction) {
  11. 'use strict';
  12. return function (captchaData) {
  13. return {
  14. formId: captchaData.formId,
  15. imageSource: ko.observable(captchaData.imageSrc),
  16. visibility: ko.observable(false),
  17. captchaValue: ko.observable(null),
  18. isRequired: ko.observable(captchaData.isRequired),
  19. isCaseSensitive: captchaData.isCaseSensitive,
  20. imageHeight: captchaData.imageHeight,
  21. refreshUrl: captchaData.refreshUrl,
  22. isLoading: ko.observable(false),
  23. timestamp: null,
  24. /**
  25. * @return {String}
  26. */
  27. getFormId: function () {
  28. return this.formId;
  29. },
  30. /**
  31. * @param {String} formId
  32. */
  33. setFormId: function (formId) {
  34. this.formId = formId;
  35. },
  36. /**
  37. * @return {Boolean}
  38. */
  39. getIsVisible: function () {
  40. return this.visibility();
  41. },
  42. /**
  43. * @param {Boolean} flag
  44. */
  45. setIsVisible: function (flag) {
  46. this.visibility(flag);
  47. },
  48. /**
  49. * @return {Boolean}
  50. */
  51. getIsRequired: function () {
  52. return this.isRequired();
  53. },
  54. /**
  55. * @param {Boolean} flag
  56. */
  57. setIsRequired: function (flag) {
  58. this.isRequired(flag);
  59. },
  60. /**
  61. * @return {Boolean}
  62. */
  63. getIsCaseSensitive: function () {
  64. return this.isCaseSensitive;
  65. },
  66. /**
  67. * @param {Boolean} flag
  68. */
  69. setIsCaseSensitive: function (flag) {
  70. this.isCaseSensitive = flag;
  71. },
  72. /**
  73. * @return {String|Number}
  74. */
  75. getImageHeight: function () {
  76. return this.imageHeight;
  77. },
  78. /**
  79. * @param {String|Number}height
  80. */
  81. setImageHeight: function (height) {
  82. this.imageHeight = height;
  83. },
  84. /**
  85. * @return {String}
  86. */
  87. getImageSource: function () {
  88. return this.imageSource;
  89. },
  90. /**
  91. * @param {String} imageSource
  92. */
  93. setImageSource: function (imageSource) {
  94. this.imageSource(imageSource);
  95. },
  96. /**
  97. * @return {String}
  98. */
  99. getRefreshUrl: function () {
  100. return this.refreshUrl;
  101. },
  102. /**
  103. * @param {String} url
  104. */
  105. setRefreshUrl: function (url) {
  106. this.refreshUrl = url;
  107. },
  108. /**
  109. * @return {*}
  110. */
  111. getCaptchaValue: function () {
  112. return this.captchaValue;
  113. },
  114. /**
  115. * @param {*} value
  116. */
  117. setCaptchaValue: function (value) {
  118. this.captchaValue(value);
  119. },
  120. /**
  121. * Refresh captcha.
  122. */
  123. refresh: function () {
  124. var refresh,
  125. self = this;
  126. this.isLoading(true);
  127. refresh = refreshAction(this.getRefreshUrl(), this.getFormId(), this.getImageSource());
  128. $.when(refresh).done(function () {
  129. self.isLoading(false);
  130. });
  131. }
  132. };
  133. };
  134. });