PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce-create-customer-on-order/assets/js/create-user-on-order.js

https://bitbucket.org/daniellbraun/wildernessatthesmokies
JavaScript | 529 lines | 288 code | 109 blank | 132 comment | 46 complexity | f633dfe2c475dc7defbe62a74ff2811c MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0, GPL-3.0, MIT
  1. jQuery( function($){
  2. $( document ).ready( function() {
  3. /*
  4. * SETTINGS PAGE.
  5. */
  6. // Bail if not Create Customer settings page.
  7. if ( $('body.settings_page_create_customer_settings').length ) {
  8. /*
  9. * Toggle 'User Role Hierarchy' setting row.
  10. */
  11. // Surpress flash & scroll-to on first page load.
  12. $surpress_notification = true;
  13. // Function to decide whether to show 'User Role Hierarchy'.
  14. function check_hierarchy_row(){
  15. // Get 'User Role Hierarchy' row.
  16. // var $hierarchy_row = $('#cxccoo_user_role_heirarchy').closest('tr');
  17. var $hierarchy_row = $('#cxccoo_user_role_heirarchy').closest('.form-table');
  18. var $hierarchy_heading = $hierarchy_row.prev();
  19. // Check.
  20. if (
  21. // 'administrator' == $("#cxccoo_user_can_create_customers").val() &&
  22. (
  23. 'administrator,shop_manager' == $('#cxccoo_user_can_create_customers').val() ||
  24. 'shop_manager' == $('#cxccoo_user_can_create_customers').val() ||
  25. 'administrator' == $('#cxccoo_user_can_create_customers').val()
  26. )
  27. &&
  28. (
  29. 'administrator' == $("#cxccoo_user_role_default").val() ||
  30. 'shop_manager' == $("#cxccoo_user_role_default").val() ||
  31. 'customer' == $("#cxccoo_user_role_default").val() ||
  32. 'editor' == $("#cxccoo_user_role_default").val() ||
  33. 'subscriber' == $("#cxccoo_user_role_default").val()
  34. )
  35. &&
  36. ! $('#cxccoo_user_role_selection').is(':checked')
  37. ) {
  38. /**
  39. * Hide.
  40. */
  41. $hierarchy_heading.hide();
  42. $hierarchy_row.hide();
  43. $hierarchy_row.removeClass('cxccoo-flash-setting');
  44. }
  45. else{
  46. /**
  47. * Show.
  48. */
  49. // Don't re-show/re-flash if already showing.
  50. if ( $hierarchy_row.is(':visible') ) {
  51. // console.log('ALREADY SHOWING!');
  52. return;
  53. }
  54. $hierarchy_heading.show();
  55. $hierarchy_row.show();
  56. if ( ! $surpress_notification ) {
  57. $('html, body').animate(
  58. {
  59. scrollTop: $hierarchy_row.offset().top,
  60. },
  61. {
  62. 'duration': 300,
  63. 'complete': function(){
  64. $hierarchy_row.addClass('cxccoo-flash-setting');
  65. },
  66. }
  67. );
  68. }
  69. }
  70. }
  71. // Trigger change on 'User Role Selection' setting.
  72. $('#cxccoo_user_role_selection').change( check_hierarchy_row );
  73. // Trigger change on 'Minimum User Role' setting.
  74. $('#cxccoo_user_can_create_customers').change( check_hierarchy_row );
  75. // Trigger change on 'Default User Role' setting.
  76. $('#cxccoo_user_role_default').change( check_hierarchy_row );
  77. // Set intial state of 'User Role Hierarchy' row.
  78. check_hierarchy_row();
  79. // Re-enable flash & scroll-to after page load.
  80. $surpress_notification = false;
  81. }
  82. /*
  83. * Address's on Order Page.
  84. */
  85. // Move the Save to Address checkboxes to their correct places - no filter to get them exactly where we want them.
  86. if ( $('.order_data_column .load_customer_billing').parents('.order_data_column').find('h4').length ) {
  87. // Backwards Compat - Old WC.
  88. var heading = $('.order_data_column .load_customer_billing').parents('.order_data_column').find('h4');
  89. heading.prepend( $('.cxccoo-save-billing-address') );
  90. var heading = $('.order_data_column .billing-same-as-shipping').parents('.order_data_column').find('h4');
  91. heading.prepend( $('.cxccoo-save-shipping-address') );
  92. }
  93. else {
  94. // Backwards Compat - New WC.
  95. var heading = $('.order_data_column .load_customer_billing').parents('.order_data_column').find('h3');
  96. heading.append( $('.cxccoo-save-billing-address') );
  97. var heading = $('.order_data_column .billing-same-as-shipping').parents('.order_data_column').find('h3');
  98. heading.append( $('.cxccoo-save-shipping-address') );
  99. $('.cxccoo-save-billing-address').addClass('cxccoo-order-save-actions-spacing');
  100. $('.cxccoo-save-shipping-address').addClass('cxccoo-order-save-actions-spacing');
  101. }
  102. // Only show 'Save To User' checkboxes when 'Edit Address' is clicked
  103. $( document ).on( 'click', 'a.edit_address', function() {
  104. var column_holder = $( this ).parents( '.order_data_column' );
  105. column_holder.find( '.cxccoo-order-save-actions' ).show();
  106. //column_holder.find( 'input[type="checkbox"]' ).attr('checked', true);
  107. });
  108. /*
  109. * `Create Customer` Form.
  110. */
  111. var $i = 0;
  112. $( woocommerce_create_customer_order_params.select_search_inputs ).each( function(){
  113. // Debugging: note the index for internal use.
  114. $i++;
  115. // Cache elements.
  116. var $wc_search_input = $(this);
  117. var $wc_search_input_container = $wc_search_input.parent();
  118. var $create_customer_container = $( $('#cxccoo-create-user-form-template').html() );
  119. var $create_customer_modal = $create_customer_container.find('.cxccoo-create-form');
  120. // Debugging: not the number of the modal, so we can check it's the correct modal for the button pushed.
  121. // $create_customer_container.find('[class$="create-user-main-button"]').append( $( '<i style="font-weight: normal;">&nbsp; ID: <strong>' + $i + '</strong></i>' ) );
  122. // $create_customer_container.find('[class$="modal-title"]').append( $( '<i style="font-weight: normal;">&nbsp; ID: <strong>' + $i + '</strong></i>' ) );
  123. // Add `Create New Customer` interface to after any of the `Search Customer` interfaces (with some specificity/restrictions so we only add them where we know it will work).
  124. $wc_search_input_container.append( $create_customer_container );
  125. // Handle `Create New Customer` button.
  126. $create_customer_container.find('.cxccoo-button.cxccoo-create-user-main-button').click(function() {
  127. open_modal(
  128. $create_customer_modal,
  129. {
  130. position : 'center',
  131. close_button : true,
  132. close_click_outside : false,
  133. }
  134. );
  135. return false;
  136. });
  137. // Handle pushing `enter/return` on `Create Customer` form.
  138. $create_customer_modal.find('input').keypress(function(e){
  139. if ( e.keyCode == 13 ) {
  140. $('.cxccoo-create-user-form-submit').click();
  141. return false;
  142. }
  143. });
  144. // Handle Cancel button on `Create Customer` form.
  145. $create_customer_modal.find('.cxccoo-button.cxccoo-create-user-form-cancel').click(function() {
  146. close_modal();
  147. return false;
  148. });
  149. // Debug: Auto Open Form.
  150. //$('.cxccoo-button.cxccoo-create-user-main-button').click();
  151. // Main `Create Customer` action.
  152. $create_customer_modal.find('.cxccoo-button.cxccoo-create-user-form-submit').click(function() {
  153. // Get values. (Old method)
  154. // var email_address = $.trim( $create_customer_modal.find( '#cxccoo_email_address' ).val() );
  155. // var first_name = $.trim( $create_customer_modal.find( '#cxccoo_first_name' ).val() );
  156. // var last_name = $.trim( $create_customer_modal.find( '#cxccoo_last_name' ).val() );
  157. // var username = $.trim( $create_customer_modal.find( '#cxccoo_username' ).val() );
  158. // var user_role = $.trim( $create_customer_modal.find( '#cxccoo_user_role' ).val() );
  159. // var disable_email = ( $create_customer_modal.find( '#cxccoo_disable_email' ).is(':checked') ) ? 'true' : 'false';
  160. $create_customer_modal.block({
  161. message: null,
  162. overlayCSS: {
  163. background: '#fff url( ' + woocommerce_create_customer_order_params.plugin_url + '/assets/images/select2-spinner.gif ) no-repeat center',
  164. opacity: 0.6
  165. }
  166. });
  167. // Get form data, and add data to it.
  168. var form_data = '';
  169. form_data = $create_customer_modal.find('#cxccoo-create-customer-form').serialize();
  170. form_data = 'action=woocommerce_order_create_user&' + form_data;
  171. form_data = "security=" + woocommerce_create_customer_order_params.create_customer_nonce + '&' + form_data;
  172. // Ajax submit form.
  173. $.ajax({
  174. type : 'post',
  175. dataType : 'json',
  176. url : woocommerce_create_customer_order_params.ajax_url,
  177. // Old method.
  178. /*data : {
  179. action : 'woocommerce_order_create_user',
  180. email_address : email_address,
  181. first_name : first_name,
  182. last_name : last_name,
  183. username : username,
  184. user_role : user_role,
  185. disable_email : disable_email,
  186. security : woocommerce_create_customer_order_params.create_customer_nonce,
  187. form_data : $('#cxccoo-create-customer-form').serialize(),
  188. },*/
  189. data: form_data,
  190. success: function( data ) {
  191. // First remove all form errors, to avoid duplicates.
  192. $create_customer_modal.find('.cxccoo-create-customer-form-error').remove();
  193. // Validation.
  194. if ( 'email_empty' == data.error_message ) {
  195. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_email_empty +'.</p></div>' );
  196. $el.insertBefore( $create_customer_modal.find( '#cxccoo_email_address' ) );
  197. $create_customer_modal.unblock();
  198. }
  199. else if ( 'email_invalid' == data.error_message ) {
  200. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_email_invalid +'.</p></div>' );
  201. $el.insertBefore( $create_customer_modal.find( '#cxccoo_email_address' ) );
  202. $create_customer_modal.unblock();
  203. }
  204. else if ( 'email_exists' == data.error_message ) {
  205. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_email_exists +'.</p></div>' );
  206. $el.insertBefore( $create_customer_modal.find( '#cxccoo_email_address' ) );
  207. $create_customer_modal.unblock();
  208. }
  209. else if ( 'username_invalid' == data.error_message ) {
  210. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_username_invalid +'.</p></div>' );
  211. $el.insertBefore( $create_customer_modal.find( '#cxccoo_username' ) );
  212. $create_customer_modal.unblock();
  213. }
  214. else if ( 'username_exists' == data.error_message ) {
  215. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_email_exists_username +'.</p></div>' );
  216. $el.insertBefore( $create_customer_modal.find( '#cxccoo_email_address' ) );
  217. $create_customer_modal.unblock();
  218. }
  219. else if ( 'role_unable' == data.error_message ) {
  220. $el = $( '<div class="inline error cxccoo-create-customer-form-error"><p><strong>'+ woocommerce_create_customer_order_params.msg_error +'</strong>: '+ woocommerce_create_customer_order_params.msg_role +'.</p></div>' );
  221. $el.insertBefore( $create_customer_modal.find( '#cxccoo_user_role' ) );
  222. $create_customer_modal.unblock();
  223. }
  224. else {
  225. // Success...
  226. var user_id = data.user_id;
  227. var username = data.username;
  228. // Update the WC Select2 input.
  229. $wc_search_input_container.find('.wc-customer-search.enhanced')
  230. .select2({
  231. data: [{ id: user_id, text: username }]
  232. });
  233. // Update the WC input.
  234. $wc_search_input_container.find( 'input.wc-customer-search, select.wc-customer-search' )
  235. .val( user_id )
  236. .trigger("change");
  237. // Reset our interface.
  238. $create_customer_modal.unblock();
  239. $create_customer_modal.find('input').val('');
  240. // Auto check the 'Save to Customer' checkboxes so details are saved.
  241. $create_customer_modal.find( '#cxccoo-save-billing-address-input' ).attr('checked','checked');
  242. $create_customer_modal.find( '#cxccoo-save-shipping-address-input' ).attr('checked','checked');
  243. // TODO: Show a success notification, then remove a few seconds later.
  244. // $el = $( '<div id="message" class="updated fade"><p><strong>'+ woocommerce_create_customer_order_params.msg_successful +'</strong>: '+ woocommerce_create_customer_order_params.msg_success +'.</p></div>' );
  245. // $el.insertAfter( $( '.cxccoo-button.cxccoo-create-user-main-button' ).parents("p:eq(0)"));
  246. // setTimeout(function(){
  247. // $create_customer_modal.find(".updated.fade").fadeOut().remove();
  248. // }, 8000);
  249. // Close our interface.
  250. $create_customer_modal.find( '.cxccoo-button.cxccoo-create-user-form-cancel' ).trigger("click");
  251. }
  252. },
  253. error: function(xhr, status, error) {
  254. }
  255. });
  256. return false;
  257. });
  258. });
  259. /**
  260. * RE-USABLE COMPONENTS.
  261. */
  262. // Helper function to check if we are in responsive/mobile.
  263. function is_mobile() {
  264. return ( $( window ).width() < 610 );
  265. }
  266. /**
  267. * Modal Popups.
  268. */
  269. function init_modal( $close_button ) {
  270. // Add the required elements if they not in the page yet.
  271. if ( ! $('.cxccoo-component-modal-popup').length ) {
  272. // Add the required elements to the dom.
  273. $('body').append( '<div class="cxccoo-component-modal-temp component-modal-hard-hide"></div>' );
  274. $('body').append( '<div class="cxccoo-component-modal-cover component-modal-hard-hide"></div>' );
  275. $popup_html = '';
  276. $popup_html += '<div class="cxccoo-component-modal-wrap cxccoo-component-modal-popup component-modal-hard-hide">';
  277. $popup_html += ' <div class="cxccoo-component-modal-container">';
  278. $popup_html += ' <div class="cxccoo-component-modal-content">';
  279. $popup_html += ' <span class="cxccoo-component-modal-cross cxccoo-top-bar-cross cxccoo-icon-cancel"></span>';
  280. $popup_html += ' </div>';
  281. $popup_html += ' </div>';
  282. $popup_html += '</div>';
  283. $('body').append( $popup_html );
  284. // Handle `close_button`.
  285. $( document ).on( 'click', '.cxccoo-component-modal-cross', function() {
  286. close_modal();
  287. return false;
  288. });
  289. // Handle `close_click_outside`.
  290. $('html').click(function(event) {
  291. if (
  292. 0 === $('[class*="component-modal-popup"]').filter('[class*="component-modal-hard-hide"]').length &&
  293. 0 !== $('[class*="-close-click-outside"]').length &&
  294. 0 === $(event.target).parents('[class*="component-modal-content"]').length
  295. ) {
  296. close_modal();
  297. return false;
  298. }
  299. });
  300. }
  301. }
  302. function open_modal( $content, $settings ) {
  303. // Set defaults
  304. $defaults = {
  305. position : 'center',
  306. cover : true,
  307. close_button : true,
  308. close_click_outside : true,
  309. };
  310. $settings = $.extend( true, $defaults, $settings );
  311. // Init modal - incase this is first run.
  312. init_modal( $settings.close_button );
  313. // Move any elements that may already be in the modal out, to the temp holder, as well as the close cross.
  314. $('.cxccoo-component-modal-temp').append( $('.cxccoo-component-modal-content').children().not('.cxccoo-component-modal-cross') );
  315. // Get content to load in modal.
  316. if ( 'string' == typeof $content ) {
  317. $content = $( $content );
  318. }
  319. // If content to load doesn't exist then rather close the whole modal and bail.
  320. if ( ! $content.length ) {
  321. close_modal();
  322. console.log( 'Content to load into modal does not exists.' );
  323. return;
  324. }
  325. // Enable whether to close when clicked outside the modal.
  326. if ( $settings.close_click_outside )
  327. $('.cxccoo-component-modal-popup').addClass('cxccoo-close-click-outside');
  328. else
  329. $('.cxccoo-component-modal-popup').removeClass('cxccoo-close-click-outside');
  330. // Show/Hide the close button.
  331. if ( $settings.close_button )
  332. $('.cxccoo-component-modal-content').find('.cxccoo-component-modal-cross').show();
  333. else
  334. $('.cxccoo-component-modal-content').find('.cxccoo-component-modal-cross').hide();
  335. // Add the intended content into the modal.
  336. $('.cxccoo-component-modal-content').prepend( $content );
  337. // Make sure this modal has the highest z-index, and hide any others that are open (incase we open 2 of our modals at once).
  338. var $open_modals = $('[class*="component-modal-cover"], [class*="component-modal-wrap"]').not('[class*="component-modal-hard-hide"], [class*="cxccoo-"]'); // Only check our 'open' modals.
  339. if ( $open_modals.length ) {
  340. $z_index = 0;
  341. $open_modals.each(function(){
  342. if ( $z_index < $(this).css('z-index') ) {
  343. // Loop the open modals and see which has the highest z-index.
  344. $z_index = $(this).css('z-index');
  345. }
  346. });
  347. // Set the current modal (modal & cover) to higher z-index than the existing one.
  348. $('.cxccoo-component-modal-cover, .cxccoo-component-modal-wrap').css( 'z-index', $z_index + 1 );
  349. // Hide the other open modal - only temporarily, by not adding the `hard-hide`.
  350. $open_modals.removeClass('component-modal-play-in').addClass('component-modal-play-out');
  351. }
  352. // Remove the class that's hiding the modal.
  353. $content.removeClass( 'component-modal-hard-hide' );
  354. // Apply positioning.
  355. // $('.cxccoo-component-modal-popup')
  356. // .removeClass( 'cxccoo-modal-position-center cxccoo-modal-position-top-right cxccoo-modal-position-top-center' )
  357. // .addClass( 'cxccoo-modal-position-' + $settings.position );
  358. // Move to top of page if Mobile.
  359. // if ( is_mobile() ) {
  360. // $('.cxccoo-component-modal-popup').css({ top: $(document).scrollTop() + 80 });
  361. // console.log( $(document).scrollTop() );
  362. // }
  363. // Control the overflow of long page content.
  364. $('html').css({ 'margin-right': '17px', 'overflow': 'hidden' });
  365. // Set a tiny defer timeout so that CSS fade-ins happen correctly.
  366. setTimeout(function() {
  367. // Move elements into the viewport by removing hard-hide, then fade in the elements.
  368. $('.cxccoo-component-modal-popup').removeClass( 'component-modal-hard-hide' );
  369. $('.cxccoo-component-modal-popup').addClass( 'component-modal-play-in' );
  370. }, 1 );
  371. // Optionally show the back cover. (not when in mobile)
  372. if ( $settings.cover ) {
  373. $('.cxccoo-component-modal-cover').removeClass( 'component-modal-hard-hide' );
  374. $('.cxccoo-component-modal-cover').addClass( 'component-modal-play-in' );
  375. }
  376. else {
  377. // If not showing then make sure to fade it out.
  378. $('.cxccoo-component-modal-cover').removeClass( 'component-modal-play-in' );
  379. setTimeout(function() {
  380. $('.cxccoo-component-modal-cover').addClass( 'component-modal-hard-hide' );
  381. }, 200 );
  382. }
  383. }
  384. function close_modal() {
  385. // Close the select 2 lip when clicking outside the modal to close.
  386. $('#cxccoo-select2-user-search').select2('close');
  387. // Fade out the elements.
  388. $('.cxccoo-component-modal-cover, .cxccoo-component-modal-popup').removeClass( 'component-modal-play-in' );
  389. // Hide the other open modals that were soft hidden by the openning of this one.
  390. var $open_modals = $('[class*="component-modal-cover"], [class*="component-modal-wrap"]').not('[class*="component-modal-hard-hide"], [class*="cxccoo-"]'); // Only check our 'open' modals.
  391. $open_modals.removeClass( 'component-modal-play-out' ).addClass( 'component-modal-play-in' );
  392. // Move elements out the viewport by adding hard-hide.
  393. setTimeout(function() {
  394. $('.cxccoo-component-modal-cover, .cxccoo-component-modal-popup').addClass( 'component-modal-hard-hide' );
  395. // Remove specific positioning.
  396. $('.cxccoo-component-modal-popup')
  397. .removeClass( 'cxccoo-modal-position-center cxccoo-modal-position-top-right cxccoo-modal-position-top-center' )
  398. .css({ top : '' });
  399. // Control the overflow of long page content - return it to normal.
  400. if ( ! $('[class*="component-modal-popup"]').filter('[class*="component-modal-play-in"]').length ) {
  401. $('html').css({ 'margin-right': '', 'overflow': '' });
  402. }
  403. }, 200 );
  404. }
  405. function resize_modal( $to_height ) {
  406. // Init modal - incase this is first run.
  407. init_modal();
  408. // Cache elements.
  409. $modal_popup = $('.cxccoo-component-modal-popup');
  410. // Get the intended heights.
  411. var $to_height = ( $to_height ) ? $to_height : $modal_popup.outerHeight();
  412. var $margin_top = ( $to_height / 2 );
  413. // Temporarily enable margin-top transition, do the height-ing/margin-ing, then remove the transtion.
  414. $modal_popup.css({ height: $to_height, marginTop: -$margin_top, transitionDelay: '0s', transition: 'margin .3s' });
  415. setTimeout( function(){
  416. $modal_popup.css({ height: '', transitionDelay: '', transition: '' });
  417. }, 1000 );
  418. }
  419. });
  420. });