uc_order.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @file
  3. * Handles asynchronous requests for order editing forms.
  4. */
  5. var customer_select = '';
  6. /**
  7. * Adds double click behavior to the order and customer admin tables.
  8. */
  9. Drupal.behaviors.ucOrderClick = {
  10. attach: function(context, settings) {
  11. jQuery('.view-uc-orders tbody tr, .view-uc-customers tbody tr', context).dblclick(
  12. function() {
  13. window.location = jQuery(this).find('.views-field-order-id a').attr('href');
  14. }
  15. );
  16. }
  17. }
  18. /**
  19. * Adds the submit behavior to the order form
  20. */
  21. Drupal.behaviors.ucOrderSubmit = {
  22. attach: function(context, settings) {
  23. jQuery('#uc-order-edit-form:not(.ucOrderSubmit-processed)', context).addClass('ucOrderSubmit-processed').submit(
  24. function() {
  25. jQuery('#products-selector').empty().removeClass();
  26. jQuery('#delivery_address_select').empty().removeClass();
  27. jQuery('#billing_address_select').empty().removeClass();
  28. jQuery('#customer-select').empty().removeClass();
  29. }
  30. );
  31. }
  32. }
  33. /**
  34. * Copies the shipping data on the order edit screen to the corresponding
  35. * billing fields if they exist.
  36. */
  37. function uc_order_copy_shipping_to_billing() {
  38. if (jQuery('#edit-delivery-zone').html() != jQuery('#edit-billing-zone').html()) {
  39. jQuery('#edit-billing-zone').empty().append(jQuery('#edit-delivery-zone').children().clone());
  40. }
  41. jQuery('#uc-order-edit-form input, select, textarea').each(
  42. function() {
  43. if (this.id.substring(0, 13) == 'edit-delivery') {
  44. jQuery('#edit-billing' + this.id.substring(13)).val(jQuery(this).val());
  45. }
  46. }
  47. );
  48. }
  49. /**
  50. * Copies the billing data on the order edit screen to the corresponding
  51. * shipping fields if they exist.
  52. */
  53. function uc_order_copy_billing_to_shipping() {
  54. if (jQuery('#edit-billing-zone').html() != jQuery('#edit-delivery-zone').html()) {
  55. jQuery('#edit-delivery-zone').empty().append(jQuery('#edit-billing-zone').children().clone());
  56. }
  57. jQuery('#uc-order-edit-form input, select, textarea').each(
  58. function() {
  59. if (this.id.substring(0, 12) == 'edit-billing') {
  60. jQuery('#edit-delivery' + this.id.substring(12)).val(jQuery(this).val());
  61. }
  62. }
  63. );
  64. }
  65. /**
  66. * Loads the address book div on the order edit screen.
  67. */
  68. function load_address_select(uid, div, address_type) {
  69. var options = {
  70. 'uid' : uid,
  71. 'type' : address_type,
  72. 'func' : "apply_address('" + address_type + "', this.value);"
  73. };
  74. jQuery.post(Drupal.settings.ucURL.adminOrders + 'address_book', options,
  75. function (contents) {
  76. jQuery(div).empty().addClass('address-select-box').append(contents);
  77. }
  78. );
  79. }
  80. /**
  81. * Applys the selected address to the appropriate fields in the order edit form.
  82. */
  83. function apply_address(type, address_str) {
  84. eval('var address = ' + address_str + ';');
  85. jQuery('#edit-' + type + '-first-name').val(address['first_name']);
  86. jQuery('#edit-' + type + '-last-name').val(address['last_name']);
  87. jQuery('#edit-' + type + '-phone').val(address['phone']);
  88. jQuery('#edit-' + type + '-company').val(address['company']);
  89. jQuery('#edit-' + type + '-street1').val(address['street1']);
  90. jQuery('#edit-' + type + '-street2').val(address['street2']);
  91. jQuery('#edit-' + type + '-city').val(address['city']);
  92. jQuery('#edit-' + type + '-postal-code').val(address['postal_code']);
  93. if (jQuery('#edit-' + type + '-country').val() != address['country']) {
  94. jQuery('#edit-' + type + '-country').val(address['country']);
  95. }
  96. jQuery('#edit-' + type + '-zone').val(address['zone']);
  97. }
  98. /**
  99. * Closes the address book div.
  100. */
  101. function close_address_select(div) {
  102. jQuery(div).empty().removeClass('address-select-box');
  103. return false;
  104. }
  105. /**
  106. * Loads the customer select div on the order edit screen.
  107. */
  108. function load_customer_search() {
  109. if (customer_select == 'search' && jQuery('#customer-select #edit-back').val() == null) {
  110. return close_customer_select();
  111. }
  112. jQuery.post(Drupal.settings.ucURL.adminOrders + 'customer', {},
  113. function (contents) {
  114. jQuery('#customer-select').empty().addClass('customer-select-box').append(contents);
  115. jQuery('#customer-select #edit-first-name').val(jQuery('#edit-billing-first-name').val());
  116. jQuery('#customer-select #edit-last-name').val(jQuery('#edit-billing-last-name').val());
  117. customer_select = 'search';
  118. }
  119. );
  120. return false;
  121. }
  122. /**
  123. * Displays the results of the customer search.
  124. */
  125. function load_customer_search_results() {
  126. jQuery.post(Drupal.settings.ucURL.adminOrders + 'customer/search',
  127. {
  128. first_name: jQuery('#customer-select #edit-first-name').val(),
  129. last_name: jQuery('#customer-select #edit-last-name').val(),
  130. email: jQuery('#customer-select #edit-email').val()
  131. },
  132. function (contents) {
  133. jQuery('#customer-select').empty().append(contents);
  134. }
  135. );
  136. return false;
  137. }
  138. /**
  139. * Sets customer values from search selection.
  140. */
  141. function select_customer_search() {
  142. var data = jQuery('#edit-cust-select').val();
  143. var i = data.indexOf(':');
  144. return select_existing_customer(data.substr(0, i), data.substr(i + 1));
  145. }
  146. /**
  147. * Displays the new customer form.
  148. */
  149. function load_new_customer_form() {
  150. if (customer_select == 'new') {
  151. return close_customer_select();
  152. }
  153. jQuery.post(Drupal.settings.ucURL.adminOrders + 'customer/new', {},
  154. function (contents) {
  155. jQuery('#customer-select').empty().addClass('customer-select-box').append(contents);
  156. customer_select = 'new';
  157. }
  158. );
  159. return false;
  160. }
  161. /**
  162. * Validates the customer's email address.
  163. */
  164. function check_new_customer_address() {
  165. var options = {
  166. 'email' : jQuery('#customer-select #edit-email').val(),
  167. 'sendmail' : jQuery('#customer-select #edit-sendmail').attr('checked')
  168. };
  169. jQuery.post(Drupal.settings.ucURL.adminOrders + 'customer/new/check', options,
  170. function (contents) {
  171. jQuery('#customer-select').empty().append(contents);
  172. }
  173. );
  174. return false;
  175. }
  176. /**
  177. * Loads existing customer as new order's customer.
  178. */
  179. function select_existing_customer(uid, email) {
  180. jQuery('input[name=uid], #edit-uid-text').val(uid);
  181. jQuery('input[name=primary_email], #edit-primary-email-text').val(email);
  182. try {
  183. jQuery('#edit-submit-changes').click();
  184. }
  185. catch (err) {
  186. }
  187. return close_customer_select();
  188. }
  189. /**
  190. * Hides the customer selection form.
  191. */
  192. function close_customer_select() {
  193. jQuery('#customer-select').empty().removeClass('customer-select-box');
  194. customer_select = '';
  195. return false;
  196. }