rulesDatafields.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. define(['prototype', 'domReady!'], function () {
  2. 'use strict';
  3. /**
  4. * Do update for condition
  5. * @param {Object} item
  6. * @param {String} url
  7. */
  8. function doUpdateForCondition(item, url) {
  9. var attribute = item.up(1).down(),
  10. attributeValue = attribute.down().value,
  11. value = item.up().next(),
  12. valueName = value.down().readAttribute('name'),
  13. condValue = item.value;
  14. new Ajax.Request(url, {
  15. method: 'post',
  16. parameters: {
  17. attributeValue: attributeValue,
  18. value: valueName,
  19. condValue: condValue
  20. },
  21. /**
  22. * @param {Object} transport
  23. */
  24. onSuccess: function (transport) {
  25. var json = transport.responseJSON;
  26. value.update(json.cvalue);
  27. }
  28. });
  29. }
  30. /**
  31. * @param {Object} item
  32. * @param {String} url
  33. * @param {String} valueAjaxUrl
  34. */
  35. function doUpdate(item, url, valueAjaxUrl) {
  36. var cond = item.up(1).down().next(),
  37. condName = cond.down().readAttribute('name'),
  38. value = item.up(1).down().next(1),
  39. valueName = value.down().readAttribute('name'),
  40. attribute = item.value;
  41. new Ajax.Request(url, {
  42. method: 'post',
  43. parameters: {
  44. attribute: attribute,
  45. condition: condName,
  46. value: valueName
  47. },
  48. /**
  49. * @param {Object} transport
  50. */
  51. onSuccess: function (transport) {
  52. var json = transport.responseJSON;
  53. cond.update(json.condition);
  54. value.update(json.cvalue);
  55. $$('.admin__control-table tr td select').each(function (itemToObserve) {
  56. Event.observe(itemToObserve, 'change', function () {
  57. if (itemToObserve.readAttribute('title') === 'conditions') {
  58. doUpdateForCondition(itemToObserve, valueAjaxUrl);
  59. }
  60. });
  61. });
  62. }
  63. });
  64. }
  65. /**
  66. * Do update with values
  67. * @param {Object} item
  68. * @param {String} url
  69. * @param {String} valueAjaxUrl
  70. */
  71. function doUpdateWithValues(item, url, valueAjaxUrl) {
  72. var arrayKey = item.up(1).readAttribute('id'),
  73. cond = item.up(1).down().next(),
  74. condName = cond.down().readAttribute('name'),
  75. value = item.up(1).down().next(1),
  76. valueName = value.down().readAttribute('name'),
  77. attribute = item.value,
  78. ruleId = $('rule_id').value;
  79. new Ajax.Request(url, {
  80. method: 'post',
  81. parameters: {
  82. attribute: attribute,
  83. condition: condName,
  84. value: valueName,
  85. arraykey: arrayKey,
  86. ruleid: ruleId
  87. },
  88. /**
  89. * @param {Object} transport
  90. */
  91. onSuccess: function (transport) {
  92. var json = transport.responseJSON;
  93. cond.update(json.condition);
  94. value.update(json.cvalue);
  95. $$('.admin__control-table tr td select').each(function (itemToObserve) {
  96. Event.observe(itemToObserve, 'change', function () {
  97. if (itemToObserve.readAttribute('title') === 'conditions') {
  98. doUpdateForCondition(itemToObserve, valueAjaxUrl);
  99. }
  100. });
  101. });
  102. }
  103. });
  104. }
  105. /**
  106. * Observe on click add new condition
  107. * @param {String} ajaxUrl
  108. * @param {String} valueAjaxUrl
  109. */
  110. function observeOnClickAddNewCondition(ajaxUrl, valueAjaxUrl) {
  111. $$('.admin__control-table tr td:first-child select').each(function (item) {
  112. Event.observe(item, 'change', function () {
  113. doUpdate(item, ajaxUrl, valueAjaxUrl);
  114. });
  115. });
  116. $$('.admin__control-table tr td select').each(function (item) {
  117. Event.observe(item, 'change', function () {
  118. if (item.readAttribute('title') === 'conditions') {
  119. doUpdateForCondition(item, valueAjaxUrl);
  120. }
  121. });
  122. });
  123. }
  124. /**
  125. * init
  126. * @param {String} ajaxUrl
  127. * @param {String} selectAjaxUrl
  128. * @param {String} valueAjaxUrl
  129. */
  130. function init(ajaxUrl, selectAjaxUrl, valueAjaxUrl) {
  131. $$('.admin__control-table tr td:first-child select').each(function (item) {
  132. doUpdateWithValues(item, selectAjaxUrl, valueAjaxUrl);
  133. });
  134. $$('.admin__control-table tr td:first-child select').each(function (item) {
  135. Event.observe(item, 'change', function () {
  136. doUpdate(item, ajaxUrl, valueAjaxUrl);
  137. });
  138. });
  139. $$('.admin__control-table button.action-add').each(function (item) {
  140. Event.observe(item, 'click', function () {
  141. observeOnClickAddNewCondition(ajaxUrl, valueAjaxUrl);
  142. });
  143. });
  144. }
  145. /**
  146. * export/return
  147. * @param {Object} rules
  148. */
  149. return function (rules) {
  150. init(
  151. rules.ajaxUrl,
  152. rules.selectAjaxUrl,
  153. rules.valueAjaxUrl
  154. );
  155. };
  156. });