Nav apraksta

admin-builder-providers.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /* global wpforms_builder_providers, wpforms_builder, wpf */
  2. ( function( $ ) {
  3. var s;
  4. var WPFormsProviders = {
  5. settings: {
  6. form : $( '#wpforms-builder-form' ),
  7. spinner: '<i class="wpforms-loading-spinner wpforms-loading-inline"></i>',
  8. spinnerWhite: '<i class="wpforms-loading-spinner wpforms-loading-inline wpforms-loading-white"></i>',
  9. },
  10. /**
  11. * Start the engine.
  12. *
  13. * @since 1.0.0
  14. */
  15. init: function() {
  16. s = this.settings;
  17. // Document ready.
  18. $( WPFormsProviders.ready );
  19. WPFormsProviders.bindUIActions();
  20. },
  21. /**
  22. * Document ready.
  23. *
  24. * @since 1.1.1
  25. */
  26. ready: function() {
  27. // Setup/cache some vars not available before.
  28. s.formID = $( '#wpforms-builder-form' ).data( 'id' );
  29. },
  30. /**
  31. * Element bindings.
  32. *
  33. * @since 1.0.0
  34. */
  35. bindUIActions: function() {
  36. // Delete connection.
  37. $( document ).on( 'click', '.wpforms-provider-connection-delete', function( e ) {
  38. WPFormsProviders.connectionDelete( this, e );
  39. } );
  40. // Add new connection.
  41. $( document ).on( 'click', '.wpforms-provider-connections-add', function( e ) {
  42. WPFormsProviders.connectionAdd( this, e );
  43. } );
  44. // Add new provider account.
  45. $( document ).on( 'click', '.wpforms-provider-account-add button', function( e ) {
  46. WPFormsProviders.accountAdd( this, e );
  47. } );
  48. // Select provider account.
  49. $( document ).on( 'change', '.wpforms-provider-accounts select', function( e ) {
  50. WPFormsProviders.accountSelect( this, e );
  51. } );
  52. // Select account list.
  53. $( document ).on( 'change', '.wpforms-provider-lists select', function( e ) {
  54. WPFormsProviders.accountListSelect( this, e );
  55. } );
  56. $( document ).on( 'wpformsPanelSwitch', function( e, targetPanel ) {
  57. WPFormsProviders.providerPanelConfirm( targetPanel );
  58. } );
  59. // Alert users if they save a form and do not configure required
  60. // fields.
  61. $( document ).on( 'wpformsSaved', function() {
  62. var providerAlerts = [];
  63. var $connectionBlocks = $( '#wpforms-panel-providers' ).find( '.wpforms-connection-block' );
  64. if ( ! $connectionBlocks.length ) {
  65. return;
  66. }
  67. $connectionBlocks.each( function() {
  68. var requiredEmpty = false,
  69. providerName;
  70. $( this ).find( 'table span.required' ).each(function() {
  71. var $element = $( this ).parent().parent().find( 'select' );
  72. if ( $element.val() === '' ) {
  73. requiredEmpty = true;
  74. }
  75. } );
  76. if ( requiredEmpty ) {
  77. var $titleArea = $( this ).closest( '.wpforms-panel-content-section' ).find( '.wpforms-panel-content-section-title' ).clone();
  78. $titleArea.find( 'button' ).remove();
  79. providerName = $titleArea.text().trim();
  80. var msg = wpforms_builder.provider_required_flds;
  81. if ( -1 < providerAlerts.indexOf( providerName ) ) {
  82. return;
  83. }
  84. $.alert( {
  85. title: wpforms_builder.heads_up,
  86. content: msg.replace( '{provider}', providerName ),
  87. icon: 'fa fa-exclamation-circle',
  88. type: 'orange',
  89. buttons: {
  90. confirm: {
  91. text: wpforms_builder.ok,
  92. btnClass: 'btn-confirm',
  93. keys: [ 'enter' ],
  94. },
  95. },
  96. } );
  97. providerAlerts.push( providerName );
  98. }
  99. } );
  100. } );
  101. },
  102. /**
  103. * Delete provider connection
  104. *
  105. * @since 1.0.0
  106. */
  107. connectionDelete: function( el, e ) {
  108. e.preventDefault();
  109. var $this = $( el );
  110. $.confirm( {
  111. title: false,
  112. content: wpforms_builder_providers.confirm_connection,
  113. icon: 'fa fa-exclamation-circle',
  114. type: 'orange',
  115. buttons: {
  116. confirm: {
  117. text: wpforms_builder.ok,
  118. btnClass: 'btn-confirm',
  119. keys: [ 'enter' ],
  120. action: function() {
  121. var $section = $this.closest( '.wpforms-panel-content-section' );
  122. $this.closest( '.wpforms-provider-connection' ).remove();
  123. if ( ! $section.find( '.wpforms-provider-connection' ).length ) {
  124. $section.find( '.wpforms-builder-provider-connections-default' ).removeClass( 'wpforms-hidden' );
  125. }
  126. },
  127. },
  128. cancel: {
  129. text: wpforms_builder.cancel,
  130. },
  131. },
  132. } );
  133. },
  134. /**
  135. * Add new provider connection.
  136. *
  137. * @since 1.0.0
  138. */
  139. connectionAdd: function( el, e ) {
  140. e.preventDefault();
  141. var $this = $( el ),
  142. $connections = $this.parent().parent(),
  143. $container = $this.parent(),
  144. provider = $this.data( 'provider' ),
  145. type = $this.data( 'type' ),
  146. namePrompt = wpforms_builder_providers.prompt_connection,
  147. nameField = '<input autofocus="" type="text" id="provider-connection-name" placeholder="' + wpforms_builder_providers.prompt_placeholder + '">',
  148. nameError = '<p class="error">' + wpforms_builder_providers.error_name + '</p>',
  149. modalContent = namePrompt + nameField + nameError;
  150. modalContent = modalContent.replace( /%type%/g, type );
  151. $.confirm( {
  152. title: false,
  153. content: modalContent,
  154. icon: 'fa fa-info-circle',
  155. type: 'blue',
  156. buttons: {
  157. confirm: {
  158. text: wpforms_builder.ok,
  159. btnClass: 'btn-confirm',
  160. keys: [ 'enter' ],
  161. action: function() {
  162. var name = this.$content.find( 'input#provider-connection-name' ).val().trim();
  163. var error = this.$content.find( '.error' );
  164. if ( name === '' ) {
  165. error.show();
  166. return false;
  167. } else {
  168. // Disable button.
  169. WPFormsProviders.inputToggle( $this, 'disable' );
  170. // Fire AJAX.
  171. var data = {
  172. action : 'wpforms_provider_ajax_' + provider,
  173. provider: provider,
  174. task : 'new_connection',
  175. name : name,
  176. id : s.form.data( 'id' ),
  177. nonce : wpforms_builder.nonce,
  178. };
  179. WPFormsProviders.fireAJAX( $this, data, function( res ) {
  180. if ( res.success ) {
  181. $connections.find( '.wpforms-builder-provider-connections-default' ).addClass( 'wpforms-hidden' );
  182. $connections.find( '.wpforms-provider-connections' ).prepend( res.data.html );
  183. // Process and load the accounts if they exist.
  184. var $connection = $connections.find( '.wpforms-provider-connection:first' );
  185. if ( $connection.find( '.wpforms-provider-accounts option:selected' ) ) {
  186. $connection.find( '.wpforms-provider-accounts option:first' ).prop( 'selected', true );
  187. $connection.find( '.wpforms-provider-accounts select' ).trigger( 'change' );
  188. }
  189. } else {
  190. WPFormsProviders.errorDisplay( res.data.error, $container );
  191. }
  192. } );
  193. }
  194. },
  195. },
  196. cancel: {
  197. text: wpforms_builder.cancel,
  198. },
  199. },
  200. } );
  201. },
  202. /**
  203. * Add and authorize provider account.
  204. *
  205. * @since 1.0.0
  206. */
  207. accountAdd: function( el, e ) {
  208. e.preventDefault();
  209. var $this = $( el ),
  210. provider = $this.data( 'provider' ),
  211. $connection = $this.closest( '.wpforms-provider-connection' ),
  212. $container = $this.parent(),
  213. $fields = $container.find( ':input' ),
  214. errors = WPFormsProviders.requiredCheck( $fields, $container );
  215. // Disable button.
  216. WPFormsProviders.inputToggle( $this, 'disable' );
  217. // Bail if we have any errors.
  218. if ( errors ) {
  219. $this.prop( 'disabled', false ).find( 'i' ).remove();
  220. return false;
  221. }
  222. // Fire AJAX.
  223. var data = {
  224. action : 'wpforms_provider_ajax_' + provider,
  225. provider : provider,
  226. connection_id: $connection.data( 'connection_id' ),
  227. task : 'new_account',
  228. data : WPFormsProviders.fakeSerialize( $fields ),
  229. };
  230. WPFormsProviders.fireAJAX( $this, data, function( res ) {
  231. if ( res.success ) {
  232. $container.nextAll( '.wpforms-connection-block' ).remove();
  233. $container.nextAll( '.wpforms-conditional-block' ).remove();
  234. $container.after( res.data.html );
  235. $container.slideUp();
  236. $connection.find( '.wpforms-provider-accounts select' ).trigger( 'change' );
  237. } else {
  238. WPFormsProviders.errorDisplay( res.data.error, $container );
  239. }
  240. } );
  241. },
  242. /**
  243. * Selecting a provider account
  244. *
  245. * @since 1.0.0
  246. */
  247. accountSelect: function( el, e ) {
  248. e.preventDefault();
  249. var $this = $( el ),
  250. $connection = $this.closest( '.wpforms-provider-connection' ),
  251. $container = $this.parent(),
  252. provider = $connection.data( 'provider' );
  253. // Disable select, show loading.
  254. WPFormsProviders.inputToggle( $this, 'disable' );
  255. // Remove any blocks that might exist as we prep for new account.
  256. $container.nextAll( '.wpforms-connection-block' ).remove();
  257. $container.nextAll( '.wpforms-conditional-block' ).remove();
  258. if ( ! $this.val() ) {
  259. // User selected to option to add new account.
  260. $connection.find( '.wpforms-provider-account-add input' ).val( '' );
  261. $connection.find( '.wpforms-provider-account-add' ).slideDown();
  262. WPFormsProviders.inputToggle( $this, 'enable' );
  263. } else {
  264. $connection.find( '.wpforms-provider-account-add' ).slideUp();
  265. // Fire AJAX.
  266. var data = {
  267. action : 'wpforms_provider_ajax_' + provider,
  268. provider : provider,
  269. connection_id: $connection.data( 'connection_id' ),
  270. task : 'select_account',
  271. account_id : $this.find( ':selected' ).val(),
  272. };
  273. WPFormsProviders.fireAJAX( $this, data, function( res ) {
  274. if ( res.success ) {
  275. $container.after( res.data.html );
  276. // Process first list found.
  277. $connection.find( '.wpforms-provider-lists option:first' ).prop( 'selected', true );
  278. $connection.find( '.wpforms-provider-lists select' ).trigger( 'change' );
  279. } else {
  280. WPFormsProviders.errorDisplay( res.data.error, $container );
  281. }
  282. } );
  283. }
  284. },
  285. /**
  286. * Selecting a provider account list.
  287. *
  288. * @since 1.0.0
  289. */
  290. accountListSelect: function( el, e ) {
  291. e.preventDefault();
  292. var $this = $( el ),
  293. $connection = $this.closest( '.wpforms-provider-connection' ),
  294. $container = $this.parent(),
  295. provider = $connection.data( 'provider' );
  296. // Disable select, show loading.
  297. WPFormsProviders.inputToggle( $this, 'disable' );
  298. // Remove any blocks that might exist as we prep for new account.
  299. $container.nextAll( '.wpforms-connection-block' ).remove();
  300. $container.nextAll( '.wpforms-conditional-block' ).remove();
  301. var data = {
  302. action : 'wpforms_provider_ajax_' + provider,
  303. provider : provider,
  304. connection_id: $connection.data( 'connection_id' ),
  305. task : 'select_list',
  306. account_id : $connection.find( '.wpforms-provider-accounts option:selected' ).val(),
  307. list_id : $this.find( ':selected' ).val(),
  308. form_id : s.formID,
  309. };
  310. WPFormsProviders.fireAJAX( $this, data, function( res ) {
  311. if ( res.success ) {
  312. $container.after( res.data.html );
  313. // Re-init tooltips for new fields.
  314. wpf.initTooltips();
  315. } else {
  316. WPFormsProviders.errorDisplay( res.data.error, $container );
  317. }
  318. } );
  319. },
  320. /**
  321. * Confirm form save before loading Provider panel.
  322. * If confirmed, save and reload panel.
  323. *
  324. * @since 1.0.0
  325. */
  326. providerPanelConfirm: function( targetPanel ) {
  327. wpforms_panel_switch = true;
  328. if ( targetPanel === 'providers' ) {
  329. if ( wpf.savedState != wpf.getFormState( '#wpforms-builder-form' ) ) {
  330. wpforms_panel_switch = false;
  331. $.confirm( {
  332. title: false,
  333. content: wpforms_builder_providers.confirm_save,
  334. icon: 'fa fa-info-circle',
  335. type: 'blue',
  336. buttons: {
  337. confirm: {
  338. text: wpforms_builder.ok,
  339. btnClass: 'btn-confirm',
  340. keys: [ 'enter' ],
  341. action: function() {
  342. $( '#wpforms-save' ).trigger( 'click' );
  343. $( document ).on( 'wpformsSaved', function() {
  344. window.location.href = wpforms_builder_providers.url;
  345. } );
  346. },
  347. },
  348. cancel: {
  349. text: wpforms_builder.cancel,
  350. },
  351. },
  352. } );
  353. }
  354. }
  355. },
  356. //--------------------------------------------------------------------//
  357. // Helper functions.
  358. //--------------------------------------------------------------------//
  359. /**
  360. * Fire AJAX call.
  361. *
  362. * @since 1.0.0
  363. */
  364. fireAJAX: function( el, d, success ) {
  365. var $this = $( el );
  366. var data = {
  367. id : $( '#wpforms-builder-form' ).data( 'id' ),
  368. nonce : wpforms_builder.nonce,
  369. };
  370. $.extend( data, d );
  371. $.post( wpforms_builder.ajax_url, data, function( res ) {
  372. success( res );
  373. WPFormsProviders.inputToggle( $this, 'enable' );
  374. } ).fail( function( xhr, textStatus, e ) {
  375. console.log( xhr.responseText );
  376. } );
  377. },
  378. /**
  379. * Toggle input with loading indicator.
  380. *
  381. * @since 1.0.0
  382. */
  383. inputToggle: function( el, status ) {
  384. var $this = $( el );
  385. if ( status === 'enable' ) {
  386. if ( $this.is( 'select' ) ) {
  387. $this.prop( 'disabled', false ).next( 'i' ).remove();
  388. } else {
  389. $this.prop( 'disabled', false ).find( 'i' ).remove();
  390. }
  391. } else if ( status === 'disable' ) {
  392. if ( $this.is( 'select' ) ) {
  393. $this.prop( 'disabled', true ).after( s.spinner );
  394. } else {
  395. $this.prop( 'disabled', true ).prepend( s.spinnerWhite );
  396. }
  397. }
  398. },
  399. /**
  400. * Display error.
  401. *
  402. * @since 1.0.0
  403. */
  404. errorDisplay: function( msg, location ) {
  405. location.find( '.wpforms-error-msg' ).remove();
  406. location.prepend( '<p class="wpforms-alert-danger wpforms-alert wpforms-error-msg">' + msg + '</p>' );
  407. },
  408. /**
  409. * Check for required fields.
  410. *
  411. * @since 1.0.0
  412. */
  413. requiredCheck: function( fields, location ) {
  414. var error = false;
  415. // Remove any previous errors.
  416. location.find( '.wpforms-alert-required' ).remove();
  417. // Loop through input fields and check for values.
  418. fields.each( function( index, el ) {
  419. if ( $( el ).hasClass( 'wpforms-required' ) && $( el ).val().length === 0 ) {
  420. $( el ).addClass( 'wpforms-error' );
  421. error = true;
  422. } else {
  423. $( el ).removeClass( 'wpforms-error' );
  424. }
  425. } );
  426. if ( error ) {
  427. location.prepend( '<p class="wpforms-alert-danger wpforms-alert wpforms-alert-required">' + wpforms_builder_providers.required_field + '</p>' );
  428. }
  429. return error;
  430. },
  431. /**
  432. * Pseudo serializing. Fake it until you make it.
  433. *
  434. * @since 1.0.0
  435. */
  436. fakeSerialize: function( els ) {
  437. var fields = els.clone();
  438. fields.each( function( index, el ) {
  439. if ( $( el ).data( 'name' ) ) {
  440. $( el ).attr( 'name', $( el ).data( 'name' ) );
  441. }
  442. } );
  443. return fields.serialize();
  444. },
  445. };
  446. WPFormsProviders.init();
  447. } )( jQuery );