rules.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. define([
  6. 'uiClass',
  7. 'Magento_Ui/js/modal/alert'
  8. ], function (Class, alert) {
  9. 'use strict';
  10. /**
  11. * Check is solution enabled
  12. *
  13. * @param {*} solution
  14. * @param {String} enabler
  15. * @returns {Boolean}
  16. */
  17. var isSolutionEnabled = function (solution, enabler) {
  18. return solution.find(enabler).val() === '1';
  19. },
  20. /**
  21. * Check is solution has related solutions enabled
  22. *
  23. * @param {Object} data
  24. * @returns {Boolean}
  25. */
  26. hasRelationsEnabled = function (data) {
  27. var name;
  28. for (name in data.argument) {
  29. if (
  30. data.solutionsElements[name] &&
  31. isSolutionEnabled(data.solutionsElements[name], data.enableButton)
  32. ) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. },
  38. /**
  39. * Set solution select-enabler to certain option
  40. *
  41. * @param {*} solution
  42. * @param {String} enabler
  43. * @param {Boolean} enabled
  44. */
  45. setSolutionSelectEnabled = function (solution, enabler, enabled) {
  46. enabled = !(enabled || typeof enabled === 'undefined') ? '0' : '1';
  47. solution.find(enabler + ' option[value=' + enabled + ']')
  48. .prop('selected', true);
  49. },
  50. /**
  51. * Set solution property 'disabled' value
  52. *
  53. * @param {*} solution
  54. * @param {String} enabler
  55. * @param {Boolean} enabled
  56. */
  57. setSolutionPropEnabled = function (solution, enabler, enabled) {
  58. enabled = !(enabled || typeof enabled === 'undefined');
  59. solution.find(enabler).prop('disabled', enabled);
  60. },
  61. /**
  62. * Set/unset solution select-enabler label
  63. *
  64. * @param {*} solution
  65. * @param {String} enabler
  66. * @param {Boolean} enabled
  67. */
  68. setSolutionMarkEnabled = function (solution, enabler, enabled) {
  69. var solutionEnabler = solution.find('label[for="' + solution.find(enabler).attr('id') + '"]');
  70. enabled || typeof enabled === 'undefined' ?
  71. solutionEnabler.addClass('enabled') :
  72. solutionEnabler.removeClass('enabled');
  73. },
  74. /**
  75. * Set/unset solution section label
  76. *
  77. * @param {*} solution
  78. * @param {Boolean} enabled
  79. */
  80. setSolutionSectionMarkEnabled = function (solution, enabled) {
  81. var solutionSection = solution.find('.section-config');
  82. enabled || typeof enabled === 'undefined' ?
  83. solutionSection.addClass('enabled') :
  84. solutionSection.removeClass('enabled');
  85. },
  86. /**
  87. * Set/unset solution section inner labels
  88. *
  89. * @param {*} solution
  90. * @param {Boolean} enabled
  91. */
  92. setSolutionLabelsMarkEnabled = function (solution, enabled) {
  93. var solutionLabels = solution.find('label.enabled');
  94. enabled || typeof enabled === 'undefined' ?
  95. solutionLabels.addClass('enabled') :
  96. solutionLabels.removeClass('enabled');
  97. },
  98. /**
  99. * Set/unset solution usedefault checkbox
  100. *
  101. * @param {*} solution
  102. * @param {String} enabler
  103. * @param {Boolean} checked
  104. */
  105. setSolutionUsedefaultEnabled = function (solution, enabler, checked) {
  106. checked = !(checked || typeof checked === 'undefined');
  107. solution.find('input[id="' + solution.find(enabler).attr('id') + '_inherit"]')
  108. .prop('checked', checked);
  109. },
  110. /**
  111. * Set solution as disabled
  112. *
  113. * @param {*} solution
  114. * @param {String} enabler
  115. */
  116. disableSolution = function (solution, enabler) {
  117. setSolutionUsedefaultEnabled(solution, enabler);
  118. setSolutionMarkEnabled(solution, enabler, false);
  119. setSolutionSelectEnabled(solution, enabler, false);
  120. setSolutionPropEnabled(solution, enabler, false);
  121. },
  122. /**
  123. * Set solution as enabled
  124. *
  125. * @param {*} solution
  126. * @param {String} enabler
  127. */
  128. enableSolution = function (solution, enabler) {
  129. setSolutionUsedefaultEnabled(solution, enabler);
  130. setSolutionPropEnabled(solution, enabler);
  131. setSolutionSelectEnabled(solution, enabler);
  132. setSolutionMarkEnabled(solution, enabler);
  133. },
  134. /**
  135. * Lock/unlock solution configuration button
  136. *
  137. * @param {*} solution
  138. * @param {String} buttonConfiguration
  139. * @param {Boolean} unlock
  140. */
  141. setSolutionConfigurationUnlock = function (solution, buttonConfiguration, unlock) {
  142. var solutionConfiguration = solution.find(buttonConfiguration);
  143. unlock || typeof unlock === 'undefined' ?
  144. solutionConfiguration.removeClass('disabled').removeAttr('disabled') :
  145. solutionConfiguration.addClass('disabled').attr('disabled', 'disabled');
  146. },
  147. /**
  148. * Forward solution select-enabler changes
  149. *
  150. * @param {*} solution
  151. * @param {String} enabler
  152. */
  153. forwardSolutionChange = function (solution, enabler) {
  154. solution.find(enabler).change();
  155. },
  156. /**
  157. * Show/hide dependent fields
  158. *
  159. * @param {*} solution
  160. * @param {String} identifier
  161. * @param {Boolean} show
  162. */
  163. showDependsField = function (solution, identifier, show) {
  164. show = show || typeof show === 'undefined';
  165. solution.find(identifier).toggle(show);
  166. solution.find(identifier).closest('tr').toggle(show);
  167. solution.find(identifier).attr('disabled', !show);
  168. };
  169. return Class.extend({
  170. defaults: {
  171. /**
  172. * Payment conflicts checker
  173. */
  174. executed: false
  175. },
  176. /**
  177. * @param {*} $target
  178. * @param {*} $owner
  179. * @param {Object} data
  180. */
  181. simpleDisable: function ($target, $owner, data) {
  182. setSolutionSelectEnabled($target, data.enableButton, false);
  183. setSolutionLabelsMarkEnabled($target, false);
  184. setSolutionSectionMarkEnabled($target, false);
  185. },
  186. /**
  187. * @param {*} $target
  188. */
  189. simpleMarkEnable: function ($target) {
  190. setSolutionSectionMarkEnabled($target);
  191. },
  192. /**
  193. * @param {*} $target
  194. * @param {*} $owner
  195. * @param {Object} data
  196. */
  197. disable: function ($target, $owner, data) {
  198. this.simpleDisable($target, $owner, data);
  199. forwardSolutionChange($target, data.enableButton);
  200. },
  201. /**
  202. * @param {*} $target
  203. * @param {*} $owner
  204. * @param {Object} data
  205. */
  206. paypalExpressDisable: function ($target, $owner, data) {
  207. setSolutionSelectEnabled($target, data.enableButton, false);
  208. setSolutionLabelsMarkEnabled($target, false);
  209. forwardSolutionChange($target, data.enableButton);
  210. },
  211. /**
  212. * @param {*} $target
  213. * @param {*} $owner
  214. * @param {Object} data
  215. */
  216. paypalExpressLockConfiguration: function ($target, $owner, data) {
  217. setSolutionConfigurationUnlock($target, data.buttonConfiguration, false);
  218. },
  219. /**
  220. * @param {*} $target
  221. * @param {*} $owner
  222. * @param {Object} data
  223. */
  224. paypalExpressLockConfigurationConditional: function ($target, $owner, data) {
  225. if (
  226. !isSolutionEnabled($target, data.enableInContextPayPal) &&
  227. hasRelationsEnabled(data)
  228. ) {
  229. this.paypalExpressLockConfiguration($target, $owner, data);
  230. }
  231. },
  232. /**
  233. * @param {*} $target
  234. * @param {*} $owner
  235. * @param {Object} data
  236. */
  237. paypalExpressMarkDisable: function ($target, $owner, data) {
  238. if (!hasRelationsEnabled(data)) {
  239. this.simpleDisable($target, $owner, data);
  240. }
  241. },
  242. /**
  243. * @param {*} $target
  244. * @param {*} $owner
  245. * @param {Object} data
  246. */
  247. paypalExpressUnlockConfiguration: function ($target, $owner, data) {
  248. if (!hasRelationsEnabled(data)) {
  249. setSolutionConfigurationUnlock($target, data.buttonConfiguration);
  250. }
  251. },
  252. /**
  253. * @param {*} $target
  254. * @param {*} $owner
  255. * @param {Object} data
  256. */
  257. paypalBmlDisable: function ($target, $owner, data) {
  258. disableSolution($target, data.enableBmlPayPal);
  259. },
  260. /**
  261. * @param {*} $target
  262. * @param {*} $owner
  263. * @param {Object} data
  264. */
  265. paypalBmlDisableConditional: function ($target, $owner, data) {
  266. if (!isSolutionEnabled($target, data.enableButton)) {
  267. this.paypalBmlDisable($target, $owner, data);
  268. }
  269. },
  270. /**
  271. * @param {*} $target
  272. * @param {*} $owner
  273. * @param {Object} data
  274. */
  275. paypalBmlEnable: function ($target, $owner, data) {
  276. enableSolution($target, data.enableBmlPayPal);
  277. },
  278. /**
  279. * @param {*} $target
  280. * @param {*} $owner
  281. * @param {Object} data
  282. */
  283. payflowExpressDisable: function ($target, $owner, data) {
  284. disableSolution($target, data.enableExpress);
  285. },
  286. /**
  287. * @param {*} $target
  288. * @param {*} $owner
  289. * @param {Object} data
  290. */
  291. payflowExpressDisableConditional: function ($target, $owner, data) {
  292. if (
  293. !isSolutionEnabled($target, data.enableButton) ||
  294. hasRelationsEnabled(data)
  295. ) {
  296. this.payflowExpressDisable($target, $owner, data);
  297. forwardSolutionChange($target, data.enableExpress);
  298. }
  299. },
  300. /**
  301. * @param {*} $target
  302. * @param {*} $owner
  303. * @param {Object} data
  304. */
  305. payflowExpressEnable: function ($target, $owner, data) {
  306. enableSolution($target, data.enableExpress);
  307. forwardSolutionChange($target, data.enableExpress);
  308. },
  309. /**
  310. * @param {*} $target
  311. * @param {*} $owner
  312. * @param {Object} data
  313. */
  314. payflowExpressEnableConditional: function ($target, $owner, data) {
  315. if (hasRelationsEnabled(data)) {
  316. setSolutionPropEnabled($target, data.enableExpress, false);
  317. setSolutionSelectEnabled($target, data.enableExpress);
  318. setSolutionMarkEnabled($target, data.enableExpress);
  319. } else {
  320. disableSolution($target, data.enableExpress);
  321. }
  322. },
  323. /**
  324. * @param {*} $target
  325. * @param {*} $owner
  326. * @param {Object} data
  327. */
  328. payflowExpressLockConditional: function ($target, $owner, data) {
  329. if (!isSolutionEnabled($target, data.enableButton)) {
  330. setSolutionPropEnabled($target, data.enableExpress, false);
  331. }
  332. },
  333. /**
  334. * @param {*} $target
  335. * @param {*} $owner
  336. * @param {Object} data
  337. */
  338. payflowExpressUsedefaultDisable: function ($target, $owner, data) {
  339. setSolutionUsedefaultEnabled($target, data.enableExpress, false);
  340. this.payflowExpressEnable($target, $owner, data);
  341. forwardSolutionChange($target, data.enableExpress);
  342. },
  343. /**
  344. * @param {*} $target
  345. * @param {*} $owner
  346. * @param {Object} data
  347. */
  348. payflowExpressUsedefaultEnable: function ($target, $owner, data) {
  349. setSolutionUsedefaultEnabled($target, data.enableExpress);
  350. this.payflowExpressDisable($target, $owner, data);
  351. forwardSolutionChange($target, data.enableExpress);
  352. },
  353. /**
  354. * @param {*} $target
  355. * @param {*} $owner
  356. * @param {Object} data
  357. */
  358. payflowBmlDisable: function ($target, $owner, data) {
  359. disableSolution($target, data.enableBml);
  360. },
  361. /**
  362. * @param {*} $target
  363. * @param {*} $owner
  364. * @param {Object} data
  365. */
  366. payflowBmlDisableConditional: function ($target, $owner, data) {
  367. if (
  368. !isSolutionEnabled($target, data.enableButton) ||
  369. hasRelationsEnabled(data)
  370. ) {
  371. this.payflowBmlDisable($target, $owner, data);
  372. }
  373. },
  374. /**
  375. * @param {*} $target
  376. * @param {*} $owner
  377. * @param {Object} data
  378. */
  379. payflowBmlDisableConditionalExpress: function ($target, $owner, data) {
  380. if (!isSolutionEnabled($target, data.enableExpress)) {
  381. this.payflowBmlDisable($target, $owner, data);
  382. }
  383. },
  384. /**
  385. * @param {*} $target
  386. * @param {*} $owner
  387. * @param {Object} data
  388. */
  389. payflowBmlEnable: function ($target, $owner, data) {
  390. enableSolution($target, data.enableBml);
  391. },
  392. /**
  393. * @param {*} $target
  394. * @param {*} $owner
  395. * @param {Object} data
  396. */
  397. payflowBmlEnableConditional: function ($target, $owner, data) {
  398. if (hasRelationsEnabled(data)) {
  399. setSolutionPropEnabled($target, data.enableBml, false);
  400. setSolutionSelectEnabled($target, data.enableBml);
  401. setSolutionMarkEnabled($target, data.enableBml);
  402. } else {
  403. disableSolution($target, data.enableBml);
  404. }
  405. },
  406. /**
  407. * @param {*} $target
  408. * @param {*} $owner
  409. * @param {Object} data
  410. */
  411. payflowBmlLockConditional: function ($target, $owner, data) {
  412. if (!isSolutionEnabled($target, data.enableButton)) {
  413. setSolutionPropEnabled($target, data.enableBml, false);
  414. }
  415. },
  416. /**
  417. * @param {*} $target
  418. * @param {*} $owner
  419. * @param {Object} data
  420. */
  421. inContextEnable: function ($target, $owner, data) {
  422. enableSolution($target, data.enableInContextPayPal);
  423. },
  424. /**
  425. * @param {*} $target
  426. * @param {*} $owner
  427. * @param {Object} data
  428. */
  429. inContextDisable: function ($target, $owner, data) {
  430. disableSolution($target, data.enableInContextPayPal);
  431. },
  432. /**
  433. * @param {*} $target
  434. * @param {*} $owner
  435. * @param {Object} data
  436. */
  437. inContextShowMerchantId: function ($target, $owner, data) {
  438. showDependsField($target, data.dependsMerchantId);
  439. },
  440. /**
  441. * @param {*} $target
  442. * @param {*} $owner
  443. * @param {Object} data
  444. */
  445. inContextHideMerchantId: function ($target, $owner, data) {
  446. showDependsField($target, data.dependsMerchantId, false);
  447. },
  448. /**
  449. * @param {*} $target
  450. * @param {*} $owner
  451. * @param {Object} data
  452. */
  453. payflowShowSortOrder: function ($target, $owner, data) {
  454. showDependsField($target, data.dependsBmlSortOrder);
  455. },
  456. /**
  457. * @param {*} $target
  458. * @param {*} $owner
  459. * @param {Object} data
  460. */
  461. payflowHideSortOrder: function ($target, $owner, data) {
  462. showDependsField($target, data.dependsBmlSortOrder, false);
  463. },
  464. /**
  465. * @param {*} $target
  466. * @param {*} $owner
  467. * @param {Object} data
  468. */
  469. paypalShowSortOrder: function ($target, $owner, data) {
  470. showDependsField($target, data.dependsBmlApiSortOrder);
  471. },
  472. /**
  473. * @param {*} $target
  474. * @param {*} $owner
  475. * @param {Object} data
  476. */
  477. paypalHideSortOrder: function ($target, $owner, data) {
  478. showDependsField($target, data.dependsBmlApiSortOrder, false);
  479. },
  480. /**
  481. * @param {*} $target
  482. * @param {*} $owner
  483. * @param {Object} data
  484. */
  485. inContextActivate: function ($target, $owner, data) {
  486. setSolutionMarkEnabled($target, data.enableInContextPayPal);
  487. },
  488. /**
  489. * @param {*} $target
  490. * @param {*} $owner
  491. * @param {Object} data
  492. */
  493. inContextDeactivate: function ($target, $owner, data) {
  494. setSolutionMarkEnabled($target, data.enableInContextPayPal, false);
  495. },
  496. /**
  497. * @param {*} $target
  498. * @param {*} $owner
  499. * @param {Object} data
  500. */
  501. inContextDisableConditional: function ($target, $owner, data) {
  502. if (!isSolutionEnabled($target, data.enableButton)) {
  503. this.inContextDisable($target, $owner, data);
  504. }
  505. },
  506. /**
  507. * @param {*} $target
  508. * @param {*} $owner
  509. * @param {Object} data
  510. */
  511. conflict: function ($target, $owner, data) {
  512. var newLine = String.fromCharCode(10, 13);
  513. if (
  514. isSolutionEnabled($owner, data.enableButton) &&
  515. hasRelationsEnabled(data) &&
  516. !this.executed
  517. ) {
  518. this.executed = true;
  519. alert({
  520. content: 'The following error(s) occurred:' +
  521. newLine +
  522. 'Some PayPal solutions conflict.' +
  523. newLine +
  524. 'Please re-enable the previously enabled payment solutions.'
  525. });
  526. }
  527. },
  528. /**
  529. * @param {*} $target
  530. * @param {*} $owner
  531. * @param {Object} data
  532. */
  533. removeCreditOption: function ($target, $owner, data) {
  534. if ($target.find(data.dependsButtonLabel + ' option[value="credit"]').length > 0) {
  535. $target.find(data.dependsButtonLabel + ' option[value="credit"]').remove();
  536. }
  537. },
  538. /**
  539. * @param {*} $target
  540. * @param {*} $owner
  541. * @param {Object} data
  542. */
  543. addCreditOption: function ($target, $owner, data) {
  544. if ($target.find(data.dependsButtonLabel + ' option[value="credit"]').length === 0) {
  545. $target.find(data.dependsButtonLabel).append('<option value="credit">Credit</option>');
  546. }
  547. },
  548. /**
  549. * @param {*} $target
  550. * @param {*} $owner
  551. * @param {Object} data
  552. */
  553. removeCreditOptionConditional: function ($target, $owner, data) {
  554. if ($target.find(data.dependsDisableFundingOptions + ' option[value="CREDIT"]').length === 0 ||
  555. $target.find(data.dependsDisableFundingOptions + ' option[value="CREDIT"]:selected').length > 0
  556. ) {
  557. this.removeCreditOption($target, $owner, data);
  558. }
  559. }
  560. });
  561. });