Bez popisu

admin-popup-editor.js 45KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  1. /*******************************************************************************
  2. * Copyright (c) 2020, Code Atlantic LLC
  3. ******************************************************************************/
  4. ( function( $ ) {
  5. 'use strict';
  6. window.PUM_Admin = window.PUM_Admin || {};
  7. window.pum_popup_settings_editor = window.pum_popup_settings_editor || {
  8. form_args: {},
  9. current_values: {},
  10. };
  11. $( document )
  12. .on( 'keydown', '#popup-title', function( event ) {
  13. var keyCode = event.keyCode || event.which;
  14. if ( 9 === keyCode ) {
  15. event.preventDefault();
  16. $( '#title' ).focus();
  17. }
  18. } )
  19. .on( 'keydown', '#title, #popup-title', function( event ) {
  20. var keyCode = event.keyCode || event.which,
  21. target;
  22. if ( ! event.shiftKey && 9 === keyCode ) {
  23. event.preventDefault();
  24. target =
  25. $( this ).attr( 'id' ) === 'title'
  26. ? '#popup-title'
  27. : '#insert-media-button';
  28. $( target ).focus();
  29. }
  30. } )
  31. .on( 'keydown', '#popup-title, #insert-media-button', function(
  32. event
  33. ) {
  34. var keyCode = event.keyCode || event.which,
  35. target;
  36. if ( event.shiftKey && 9 === keyCode ) {
  37. event.preventDefault();
  38. target =
  39. $( this ).attr( 'id' ) === 'popup-title'
  40. ? '#title'
  41. : '#popup-title';
  42. $( target ).focus();
  43. }
  44. } );
  45. // Initiate when ready.
  46. $( function() {
  47. $( this ).trigger( 'pum_init' );
  48. $( '#title' ).prop( 'required', true );
  49. var $container = $( '#pum-popup-settings-container' ),
  50. args = pum_popup_settings_editor.form_args || {},
  51. values = pum_popup_settings_editor.current_values || {};
  52. if ( $container.length ) {
  53. $container.find( '.pum-no-js' ).hide();
  54. PUM_Admin.forms.render( args, values, $container );
  55. }
  56. $( 'a.page-title-action' )
  57. .clone()
  58. .attr( 'target', '_blank' )
  59. .attr(
  60. 'href',
  61. pum_admin_vars.homeurl +
  62. '?popup_preview=true&popup=' +
  63. $( '#post_ID' ).val()
  64. )
  65. .text( pum_admin_vars.I10n.preview_popup )
  66. .insertAfter( 'a.page-title-action' );
  67. // TODO Can't figure out why this is needed, but it looks stupid otherwise when the first condition field defaults to something other than the placeholder.
  68. $( '#pum-first-condition, #pum-first-trigger, #pum-first-cookie' )
  69. .val( null )
  70. .trigger( 'change' );
  71. // Add event handler to detect when opening sound is change and play the sound to allow admin to preview it.
  72. document
  73. .querySelector( '#pum-popup-settings-container' )
  74. .addEventListener( 'change', function( e ) {
  75. if ( 'open_sound' === e.target.id ) {
  76. // Only play if the sound selected is not None or Custom.
  77. var notThese = [ 'none', 'custom' ];
  78. if ( notThese.indexOf( e.target.value ) === -1 ) {
  79. var audio = new Audio(
  80. pum_admin_vars.pm_dir_url +
  81. '/assets/sounds/' +
  82. e.target.value
  83. );
  84. audio.addEventListener(
  85. 'canplaythrough',
  86. function() {
  87. this.play().catch( function( reason ) {
  88. console.warn(
  89. 'Sound was not able to play when selected. Reason: ' +
  90. reason
  91. );
  92. } );
  93. }
  94. );
  95. audio.addEventListener( 'error', function() {
  96. console.warn(
  97. 'Error occurred when trying to load popup opening sound.'
  98. );
  99. } );
  100. }
  101. }
  102. } );
  103. // Dynamically switches example click trigger from popup-{popup-id} to using real ID.
  104. $( document ).on( 'pum_init', function() {
  105. $(
  106. '#pum-default-click-trigger-class:not(.pum-click-trigger-initialized)'
  107. ).each( function() {
  108. $( this )
  109. .addClass( 'pum-click-trigger-initialized' )
  110. .text( $( '#popup-id' ).data( 'popup-id' ) );
  111. } );
  112. } );
  113. document
  114. .querySelector( '#pum-popup-settings-container' )
  115. .addEventListener( 'click', function( e ) {
  116. if (
  117. Array.from( e.target.classList ).includes(
  118. 'popup-type'
  119. ) ||
  120. Array.from( e.target.parentElement.classList ).includes(
  121. 'popup-type'
  122. )
  123. ) {
  124. var $container = jQuery(
  125. '#pum-popup-settings-container'
  126. );
  127. if ( 1 === $container.length ) {
  128. // Our initial presets. As we add more, consider creating JSON import system and moving to there.
  129. var popupTypes = {
  130. 'center-popup': {
  131. size: 'medium',
  132. responsive_min_width: '0%',
  133. responsive_max_width: '100%',
  134. animation_type: 'fade',
  135. animation_speed: 350,
  136. location: 'center',
  137. position_fixed: false,
  138. position_from_trigger: false,
  139. overlay_disabled: false,
  140. stackable: false,
  141. disable_reposition: false,
  142. },
  143. 'left-bottom-notice': {
  144. size: 'tiny',
  145. responsive_min_width: '0%',
  146. responsive_max_width: '100%',
  147. animation_type: 'fade',
  148. animation_speed: 350,
  149. animation_origin: 'left bottom',
  150. location: 'left bottom',
  151. position_bottom: 10,
  152. position_left: 10,
  153. position_from_trigger: false,
  154. position_fixed: true,
  155. overlay_disabled: true,
  156. stackable: true,
  157. disable_reposition: false,
  158. },
  159. 'top-bar': {
  160. size: 'custom',
  161. custom_width: '100%',
  162. custom_height_auto: true,
  163. animation_type: 'fadeAndSlide',
  164. animation_speed: 300,
  165. animation_origin: 'top',
  166. location: 'center top',
  167. position_top: 0,
  168. position_from_trigger: false,
  169. position_fixed: true,
  170. overlay_disabled: true,
  171. stackable: true,
  172. disable_reposition: false,
  173. },
  174. 'right-bottom-slidein': {
  175. size: 'custom',
  176. custom_width: '300px',
  177. custom_height_auto: true,
  178. animation_type: 'slide',
  179. animation_speed: 350,
  180. animation_origin: 'bottom',
  181. location: 'right bottom',
  182. position_bottom: 10,
  183. position_right: 10,
  184. position_from_trigger: false,
  185. position_fixed: true,
  186. overlay_disabled: true,
  187. stackable: true,
  188. disable_reposition: false,
  189. },
  190. };
  191. var popupType =
  192. e.target.dataset.popupType ||
  193. e.target.parentElement.dataset.popupType ||
  194. '';
  195. // Gather our values needed for creating new settings object.
  196. var presetValues = popupTypes.hasOwnProperty(
  197. popupType
  198. )
  199. ? popupTypes[ popupType ]
  200. : {};
  201. var args =
  202. pum_popup_settings_editor.form_args || {};
  203. var originalValues =
  204. pum_popup_settings_editor.current_values || {};
  205. var currentValues = $container.pumSerializeObject();
  206. // pumSerializeObject returns the trigger/cookie settings as strings instead of objects.
  207. // Cycle through each trigger and cookie and convert to objects.
  208. if ( currentValues.popup_settings.triggers ) {
  209. for (
  210. var i = 0;
  211. i <
  212. currentValues.popup_settings.triggers
  213. .length;
  214. i++
  215. ) {
  216. currentValues.popup_settings.triggers[
  217. i
  218. ].settings = JSON.parse(
  219. currentValues.popup_settings.triggers[
  220. i
  221. ].settings
  222. );
  223. }
  224. }
  225. if ( currentValues.popup_settings.cookies ) {
  226. for (
  227. var j = 0;
  228. j <
  229. currentValues.popup_settings.cookies.length;
  230. j++
  231. ) {
  232. currentValues.popup_settings.cookies[
  233. j
  234. ].settings = JSON.parse(
  235. currentValues.popup_settings.cookies[
  236. j
  237. ].settings
  238. );
  239. }
  240. }
  241. var newValues = Object.assign(
  242. {},
  243. originalValues,
  244. currentValues.popup_settings,
  245. presetValues
  246. );
  247. // Re-render form using updated settings.
  248. PUM_Admin.forms.render(
  249. args,
  250. newValues,
  251. $container
  252. );
  253. // Click to 'Display' so they don't jump to 'Targeting' tab upon render.
  254. document
  255. .querySelector(
  256. 'a[href="#pum-popup-settings_display"]'
  257. )
  258. .click();
  259. // Adds a notice into 'Display Presets' tab telling admin the settings have been applied.
  260. var notice = document.createElement( 'div' );
  261. notice.classList.add( 'notice', 'updated' );
  262. notice.insertBefore(
  263. document.createElement( 'p' ),
  264. notice.firstChild
  265. );
  266. notice.firstChild.innerText =
  267. 'Display settings have been updated with the ' +
  268. popupType +
  269. ' preset';
  270. var parent = document.querySelector(
  271. '#pum-popup-settings-display-subtabs_preset'
  272. );
  273. parent.insertBefore( notice, parent.firstChild );
  274. }
  275. }
  276. } );
  277. } );
  278. } )( jQuery );
  279. ( function( $, document, undefined ) {
  280. 'use strict';
  281. $( document ).on( 'click', '#popup_reset_open_count', function() {
  282. var $this = $( this );
  283. if (
  284. $this.is( ':checked' ) &&
  285. ! confirm( pum_admin_vars.I10n.confirm_count_reset )
  286. ) {
  287. $this.prop( 'checked', false );
  288. }
  289. } );
  290. } )( jQuery, document );
  291. ( function( $ ) {
  292. 'use strict';
  293. var conditions = {
  294. get_conditions: function() {
  295. return window.pum_popup_settings_editor.conditions_selectlist;
  296. },
  297. not_operand_checkbox: function( $element ) {
  298. $element = $element || $( '.pum-not-operand' );
  299. return $element.each( function() {
  300. var $this = $( this ),
  301. $input = $this.find( 'input' );
  302. $input.prop( 'checked', ! $input.is( ':checked' ) );
  303. conditions.toggle_not_operand( $this );
  304. } );
  305. },
  306. toggle_not_operand: function( $element ) {
  307. $element = $element || $( '.pum-not-operand' );
  308. return $element.each( function() {
  309. var $this = $( this ),
  310. $input = $this.find( 'input' ),
  311. // $is = $this.find('.is'),
  312. // $not = $this.find('.not'),
  313. $container = $this.parents( '.facet-target' );
  314. if ( $input.is( ':checked' ) ) {
  315. // $is.hide();
  316. // $not.show();
  317. $container.addClass( 'not-operand-checked' );
  318. } else {
  319. // $is.show();
  320. // $not.hide();
  321. $container.removeClass( 'not-operand-checked' );
  322. }
  323. } );
  324. },
  325. template: {
  326. editor: function( args ) {
  327. var data = $.extend(
  328. true,
  329. {},
  330. {
  331. groups: [],
  332. },
  333. args
  334. );
  335. data.groups = PUM_Admin.utils.object_to_array( data.groups );
  336. return PUM_Admin.templates.render(
  337. 'pum-condition-editor',
  338. data
  339. );
  340. },
  341. group: function( args ) {
  342. var data = $.extend(
  343. true,
  344. {},
  345. {
  346. index: '',
  347. facets: [],
  348. },
  349. args
  350. ),
  351. i;
  352. data.facets = PUM_Admin.utils.object_to_array( data.facets );
  353. for ( i = 0; data.facets.length > i; i++ ) {
  354. data.facets[ i ].index = i;
  355. data.facets[ i ].group = data.index;
  356. }
  357. return PUM_Admin.templates.render(
  358. 'pum-condition-group',
  359. data
  360. );
  361. },
  362. facet: function( args ) {
  363. var data = $.extend(
  364. true,
  365. {},
  366. {
  367. group: '',
  368. index: '',
  369. target: '',
  370. not_operand: false,
  371. settings: {},
  372. },
  373. args
  374. );
  375. return PUM_Admin.templates.render(
  376. 'pum-condition-facet',
  377. data
  378. );
  379. },
  380. settings: function( args, values ) {
  381. var fields = [],
  382. data = $.extend(
  383. true,
  384. {},
  385. {
  386. index: '',
  387. group: '',
  388. target: null,
  389. fields: [],
  390. },
  391. args
  392. );
  393. if (
  394. ! data.fields.length &&
  395. pum_popup_settings_editor.conditions[ args.target ] !==
  396. undefined
  397. ) {
  398. data.fields =
  399. pum_popup_settings_editor.conditions[
  400. args.target
  401. ].fields;
  402. }
  403. if ( undefined === values ) {
  404. values = {};
  405. }
  406. // Replace the array with rendered fields.
  407. _.each( data.fields, function( field, fieldID ) {
  408. field = PUM_Admin.models.field( field );
  409. if ( typeof field.meta !== 'object' ) {
  410. field.meta = {};
  411. }
  412. if ( undefined !== values[ fieldID ] ) {
  413. field.value = values[ fieldID ];
  414. }
  415. field.name =
  416. 'popup_settings[conditions][' +
  417. data.group +
  418. '][' +
  419. data.index +
  420. '][settings][' +
  421. fieldID +
  422. ']';
  423. if ( field.id === '' ) {
  424. field.id =
  425. 'popup_settings_conditions_' +
  426. data.group +
  427. '_' +
  428. data.index +
  429. '_settings_' +
  430. fieldID;
  431. }
  432. fields.push( PUM_Admin.templates.field( field ) );
  433. } );
  434. // Render the section.
  435. return PUM_Admin.templates.section( {
  436. fields: fields,
  437. } );
  438. },
  439. selectbox: function( args ) {
  440. var data = $.extend(
  441. true,
  442. {},
  443. {
  444. id: null,
  445. name: null,
  446. type: 'select',
  447. group: '',
  448. index: '',
  449. value: null,
  450. select2: true,
  451. classes: [],
  452. options: conditions.get_conditions(),
  453. },
  454. args
  455. );
  456. if ( data.id === null ) {
  457. data.id =
  458. 'popup_settings_conditions_' +
  459. data.group +
  460. '_' +
  461. data.index +
  462. '_target';
  463. }
  464. if ( data.name === null ) {
  465. data.name =
  466. 'popup_settings[conditions][' +
  467. data.group +
  468. '][' +
  469. data.index +
  470. '][target]';
  471. }
  472. return PUM_Admin.templates.field( data );
  473. },
  474. },
  475. groups: {
  476. add: function( editor, target, not_operand ) {
  477. var $editor = $( editor ),
  478. data = {
  479. index: $editor.find( '.facet-group-wrap' ).length,
  480. facets: [
  481. {
  482. target: target || null,
  483. not_operand: not_operand || false,
  484. settings: {},
  485. },
  486. ],
  487. };
  488. $editor
  489. .find( '.facet-groups' )
  490. .append( conditions.template.group( data ) );
  491. $editor.addClass( 'has-conditions' );
  492. },
  493. remove: function( $group ) {
  494. var $editor = $group.parents( '.facet-builder' );
  495. $group
  496. .prev( '.facet-group-wrap' )
  497. .find( '.and .add-facet' )
  498. .removeClass( 'disabled' );
  499. $group.remove();
  500. conditions.renumber();
  501. if ( $editor.find( '.facet-group-wrap' ).length === 0 ) {
  502. $editor.removeClass( 'has-conditions' );
  503. $( '#pum-first-condition' )
  504. .val( null )
  505. .trigger( 'change' );
  506. }
  507. },
  508. },
  509. facets: {
  510. add: function( $group, target, not_operand ) {
  511. var data = {
  512. group: $group.data( 'index' ),
  513. index: $group.find( '.facet' ).length,
  514. target: target || null,
  515. not_operand: not_operand || false,
  516. settings: {},
  517. };
  518. $group
  519. .find( '.facet-list' )
  520. .append( conditions.template.facet( data ) );
  521. },
  522. remove: function( $facet ) {
  523. var $group = $facet.parents( '.facet-group-wrap' );
  524. $facet.remove();
  525. if ( $group.find( '.facet' ).length === 0 ) {
  526. conditions.groups.remove( $group );
  527. } else {
  528. conditions.renumber();
  529. }
  530. },
  531. },
  532. renumber: function() {
  533. $( '.facet-builder .facet-group-wrap' ).each( function() {
  534. var $group = $( this ),
  535. groupIndex = $group
  536. .parent()
  537. .children()
  538. .index( $group );
  539. $group
  540. .data( 'index', groupIndex )
  541. .find( '.facet' )
  542. .each( function() {
  543. var $facet = $( this ),
  544. facetIndex = $facet
  545. .parent()
  546. .children()
  547. .index( $facet );
  548. $facet
  549. .data( 'index', facetIndex )
  550. .find( '[name]' )
  551. .each( function() {
  552. this.name = this.name.replace(
  553. /popup_settings\[conditions\]\[\d*?\]\[\d*?\]/,
  554. 'popup_settings[conditions][' +
  555. groupIndex +
  556. '][' +
  557. facetIndex +
  558. ']'
  559. );
  560. this.id = this.id.replace(
  561. /popup_settings_conditions_\d*?_\d*?_/,
  562. 'popup_settings_conditions_' +
  563. groupIndex +
  564. '_' +
  565. facetIndex +
  566. '_'
  567. );
  568. } );
  569. } );
  570. } );
  571. },
  572. };
  573. // Import this module.
  574. window.PUM_Admin = window.PUM_Admin || {};
  575. window.PUM_Admin.conditions = conditions;
  576. $( document )
  577. .on( 'pum_init', function() {
  578. conditions.renumber();
  579. conditions.toggle_not_operand();
  580. } )
  581. .on(
  582. 'select2:select pumselect2:select',
  583. '#pum-first-condition',
  584. function( event ) {
  585. var $field = $( this ),
  586. $editor = $field.parents( '.facet-builder' ).eq( 0 ),
  587. target = $field.val(),
  588. $operand = $editor.find( '#pum-first-facet-operand' ),
  589. not_operand = $operand.is( ':checked' );
  590. conditions.groups.add( $editor, target, not_operand );
  591. $field.val( null ).trigger( 'change' );
  592. $operand
  593. .prop( 'checked', false )
  594. .parents( '.facet-target' )
  595. .removeClass( 'not-operand-checked' );
  596. $( document ).trigger( 'pum_init' );
  597. }
  598. )
  599. .on( 'click', '.facet-builder .pum-not-operand', function() {
  600. conditions.not_operand_checkbox( $( this ) );
  601. } )
  602. .on( 'change', '.facet-builder .facet-target select', function(
  603. event
  604. ) {
  605. var $this = $( this ),
  606. $facet = $this.parents( '.facet' ),
  607. target = $this.val(),
  608. data = {
  609. target: target,
  610. };
  611. if ( target === '' || target === $facet.data( 'target' ) ) {
  612. return;
  613. }
  614. $facet
  615. .data( 'target', target )
  616. .find( '.facet-settings' )
  617. .html( conditions.template.settings( data ) );
  618. $( document ).trigger( 'pum_init' );
  619. } )
  620. .on(
  621. 'click',
  622. '.facet-builder .facet-group-wrap:last-child .and .add-facet',
  623. function() {
  624. conditions.groups.add(
  625. $( this )
  626. .parents( '.facet-builder' )
  627. .eq( 0 )
  628. );
  629. $( document ).trigger( 'pum_init' );
  630. }
  631. )
  632. .on(
  633. 'click',
  634. '.facet-builder .add-or .add-facet:not(.disabled)',
  635. function() {
  636. conditions.facets.add(
  637. $( this )
  638. .parents( '.facet-group-wrap' )
  639. .eq( 0 )
  640. );
  641. $( document ).trigger( 'pum_init' );
  642. }
  643. )
  644. .on( 'click', '.facet-builder .remove-facet', function() {
  645. conditions.facets.remove(
  646. $( this )
  647. .parents( '.facet' )
  648. .eq( 0 )
  649. );
  650. $( document ).trigger( 'pum_init' );
  651. } );
  652. } )( jQuery );
  653. var cookies;
  654. ( function( $, document, undefined ) {
  655. 'use strict';
  656. var I10n = pum_admin_vars.I10n,
  657. current_editor,
  658. cookies = {
  659. get_cookies: function() {
  660. return window.pum_popup_settings_editor.cookies;
  661. },
  662. get_cookie: function( event ) {
  663. var cookies = this.get_cookies(),
  664. cookie =
  665. cookies[ event ] !== 'undefined'
  666. ? cookies[ event ]
  667. : false;
  668. if ( ! cookie ) {
  669. return false;
  670. }
  671. if (
  672. cookie &&
  673. typeof cookie === 'object' &&
  674. typeof cookie.fields === 'object' &&
  675. Object.keys( cookie.fields ).length
  676. ) {
  677. cookie = this.parseFields( cookie );
  678. }
  679. return cookie;
  680. },
  681. getCookieDefaults: function( event ) {
  682. var cookie = cookies.get_cookie( event );
  683. var defaultSettings = {};
  684. for ( var tab in cookie.fields ) {
  685. if ( cookie.fields.hasOwnProperty( tab ) ) {
  686. for ( var setting in cookie.fields[ tab ] ) {
  687. if (
  688. cookie.fields[ tab ].hasOwnProperty( setting )
  689. ) {
  690. defaultSettings[ setting ] =
  691. cookie.fields[ tab ][ setting ].std;
  692. }
  693. }
  694. }
  695. }
  696. defaultSettings.name = 'pum-' + $( '#post_ID' ).val();
  697. return defaultSettings;
  698. },
  699. parseFields: function( cookie ) {
  700. _.each( cookie.fields, function( fields, tabID ) {
  701. _.each( fields, function( field, fieldID ) {
  702. cookie.fields[ tabID ][ fieldID ].name =
  703. 'cookie_settings[' + fieldID + ']';
  704. if ( cookie.fields[ tabID ][ fieldID ].id === '' ) {
  705. cookie.fields[ tabID ][ fieldID ].id =
  706. 'cookie_settings_' + fieldID;
  707. }
  708. } );
  709. } );
  710. return cookie;
  711. },
  712. parseValues: function( values, type ) {
  713. return values;
  714. },
  715. select_list: function() {
  716. var i,
  717. _cookies = PUM_Admin.utils.object_to_array(
  718. cookies.get_cookies()
  719. ),
  720. options = {};
  721. for ( i = 0; i < _cookies.length; i++ ) {
  722. options[ _cookies[ i ].id ] = _cookies[ i ].name;
  723. }
  724. return options;
  725. },
  726. /**
  727. * @deprecated
  728. *
  729. * @param event
  730. */
  731. getLabel: function( event ) {
  732. var cookie = cookies.get_cookie( event );
  733. if ( ! cookie ) {
  734. return false;
  735. }
  736. return cookie.name;
  737. },
  738. /**
  739. * @param event
  740. * @param values
  741. */
  742. getSettingsDesc: function( event, values ) {
  743. var cookie = cookies.get_cookie( event );
  744. if ( ! cookie ) {
  745. return false;
  746. }
  747. return PUM_Admin.templates.renderInline(
  748. cookie.settings_column,
  749. values
  750. );
  751. },
  752. /**
  753. * Refresh all cookie row descriptions.
  754. */
  755. refreshDescriptions: function() {
  756. $( '.pum-popup-cookie-editor table.list-table tbody tr' ).each(
  757. function() {
  758. var $row = $( this ),
  759. event = $row
  760. .find( '.popup_cookies_field_event' )
  761. .val(),
  762. values = JSON.parse(
  763. $row
  764. .find(
  765. '.popup_cookies_field_settings:first'
  766. )
  767. .val()
  768. );
  769. $row.find( 'td.settings-column' ).html(
  770. cookies.getSettingsDesc( event, values )
  771. );
  772. }
  773. );
  774. },
  775. /**
  776. * Insert a new cookie when needed.
  777. *
  778. * @param $editor
  779. * @param args
  780. */
  781. insertCookie: function( $editor, args ) {
  782. var defaultSettings = cookies.getCookieDefaults( args.event );
  783. args = $.extend(
  784. true,
  785. {},
  786. {
  787. event: 'on_popup_close',
  788. settings: defaultSettings,
  789. },
  790. args
  791. );
  792. cookies.rows.add( $editor, args );
  793. },
  794. template: {
  795. form: function( event, values, callback ) {
  796. var cookie = cookies.get_cookie( event ),
  797. modalID = 'pum_cookie_settings',
  798. firstTab = Object.keys( cookie.fields )[ 0 ];
  799. values = values || {};
  800. values.event = event;
  801. values.index = values.index >= 0 ? values.index : null;
  802. // Add hidden index & event fields.
  803. cookie.fields[ firstTab ] = $.extend(
  804. true,
  805. cookie.fields[ firstTab ],
  806. {
  807. index: {
  808. type: 'hidden',
  809. name: 'index',
  810. },
  811. event: {
  812. type: 'hidden',
  813. name: 'event',
  814. },
  815. }
  816. );
  817. if ( typeof values.key !== 'string' || values.key === '' ) {
  818. delete cookie.fields.advanced.key;
  819. }
  820. PUM_Admin.modals.reload(
  821. '#' + modalID,
  822. PUM_Admin.templates.modal( {
  823. id: modalID,
  824. title: cookie.modal_title || cookie.name,
  825. classes: 'tabbed-content',
  826. save_button:
  827. values.index !== null ? I10n.update : I10n.add,
  828. content: PUM_Admin.forms.render(
  829. {
  830. id: 'pum_cookie_settings_form',
  831. tabs: cookie.tabs || {},
  832. fields: cookie.fields || {},
  833. },
  834. values || {}
  835. ),
  836. } )
  837. );
  838. $( '#' + modalID + ' form' ).on(
  839. 'submit',
  840. callback ||
  841. function( e ) {
  842. e.preventDefault();
  843. PUM_Admin.modals.closeAll();
  844. }
  845. );
  846. },
  847. editor: function( args ) {
  848. var data = $.extend(
  849. true,
  850. {},
  851. {
  852. cookies: [],
  853. name: '',
  854. },
  855. args
  856. );
  857. data.cookies = PUM_Admin.utils.object_to_array(
  858. data.cookies
  859. );
  860. return PUM_Admin.templates.render(
  861. 'pum-cookie-editor',
  862. data
  863. );
  864. },
  865. row: function( args ) {
  866. var data = $.extend(
  867. true,
  868. {},
  869. {
  870. index: '',
  871. event: '',
  872. name: '',
  873. settings: {
  874. name: '',
  875. key: '',
  876. session: null,
  877. path: null,
  878. time: '30 days',
  879. },
  880. },
  881. args
  882. );
  883. return PUM_Admin.templates.render( 'pum-cookie-row', data );
  884. },
  885. selectbox: function( args ) {
  886. var data = $.extend(
  887. true,
  888. {},
  889. {
  890. id: null,
  891. name: null,
  892. type: 'select',
  893. group: '',
  894. index: '',
  895. value: null,
  896. select2: true,
  897. classes: [],
  898. options: cookies.select_list(),
  899. },
  900. args
  901. );
  902. if ( data.id === null ) {
  903. data.id =
  904. 'popup_settings_cookies_' + data.index + '_event';
  905. }
  906. if ( data.name === null ) {
  907. data.name =
  908. 'popup_settings[cookies][' +
  909. data.index +
  910. '][event]';
  911. }
  912. return PUM_Admin.templates.field( data );
  913. },
  914. },
  915. rows: {
  916. add: function( editor, cookie ) {
  917. var $editor = $( editor ),
  918. data = {
  919. index:
  920. cookie.index !== null && cookie.index >= 0
  921. ? cookie.index
  922. : $editor.find(
  923. 'table.list-table tbody tr'
  924. ).length,
  925. event: cookie.event,
  926. name: $editor.data( 'field_name' ),
  927. settings: cookie.settings || {},
  928. },
  929. $row = $editor.find( 'tbody tr' ).eq( data.index ),
  930. $new_row = PUM_Admin.templates.render(
  931. 'pum-cookie-row',
  932. data
  933. );
  934. if ( $row.length ) {
  935. $row.replaceWith( $new_row );
  936. } else {
  937. $editor.find( 'tbody' ).append( $new_row );
  938. }
  939. $editor.addClass( 'has-list-items' );
  940. cookies.rows.renumber();
  941. cookies.refreshDescriptions();
  942. },
  943. /**
  944. * Remove a cookie editor table row.
  945. *
  946. * @param $cookie
  947. */
  948. remove: function( $cookie ) {
  949. var $editor = $cookie.parents( '.pum-popup-cookie-editor' );
  950. $cookie.remove();
  951. cookies.rows.renumber();
  952. if (
  953. $editor.find( 'table.list-table tbody tr' ).length === 0
  954. ) {
  955. $editor.removeClass( 'has-list-items' );
  956. $( '#pum-first-cookie' )
  957. .val( null )
  958. .trigger( 'change' );
  959. }
  960. },
  961. /**
  962. * Renumber all rows for all editors.
  963. */
  964. renumber: function() {
  965. $(
  966. '.pum-popup-cookie-editor table.list-table tbody tr'
  967. ).each( function() {
  968. var $this = $( this ),
  969. index = $this
  970. .parent()
  971. .children()
  972. .index( $this );
  973. $this
  974. .attr( 'data-index', index )
  975. .data( 'index', index );
  976. $this.find( ':input, [name]' ).each( function() {
  977. if ( this.name && this.name !== '' ) {
  978. this.name = this.name.replace(
  979. /\[\d*?\]/,
  980. '[' + index + ']'
  981. );
  982. }
  983. } );
  984. } );
  985. },
  986. },
  987. };
  988. // Import this module.
  989. window.PUM_Admin = window.PUM_Admin || {};
  990. window.PUM_Admin.cookies = cookies;
  991. $( document )
  992. .on( 'pum_init', function() {
  993. cookies.refreshDescriptions();
  994. } )
  995. .on(
  996. 'select2:select pumselect2:select',
  997. '#pum-first-cookie',
  998. function() {
  999. var $this = $( this ),
  1000. $editor = $this.parents( '.pum-popup-cookie-editor' ),
  1001. event = $this.val(),
  1002. values = {
  1003. indes: $editor.find( 'table.list-table tbody tr' )
  1004. .length,
  1005. name: 'pum-' + $( '#post_ID' ).val(),
  1006. };
  1007. $this.val( null ).trigger( 'change' );
  1008. cookies.template.form( event, values, function( e ) {
  1009. var $form = $( this ),
  1010. event = $form.find( 'input#event' ).val(),
  1011. index = $form.find( 'input#index' ).val(),
  1012. values = $form.pumSerializeObject();
  1013. e.preventDefault();
  1014. if ( ! index || index < 0 ) {
  1015. index = $editor.find( 'tbody tr' ).length;
  1016. }
  1017. cookies.rows.add( $editor, {
  1018. index: index,
  1019. event: event,
  1020. settings: values.cookie_settings,
  1021. } );
  1022. PUM_Admin.modals.closeAll();
  1023. } );
  1024. }
  1025. )
  1026. .on( 'click', '.pum-popup-cookie-editor .pum-add-new', function() {
  1027. current_editor = $( this ).parents( '.pum-popup-cookie-editor' );
  1028. var template = wp.template( 'pum-cookie-add-event' );
  1029. PUM_Admin.modals.reload(
  1030. '#pum_cookie_add_event_modal',
  1031. template( { I10n: I10n } )
  1032. );
  1033. } )
  1034. .on( 'click', '.pum-popup-cookie-editor .edit', function( e ) {
  1035. var $this = $( this ),
  1036. $editor = $this.parents( '.pum-popup-cookie-editor' ),
  1037. $row = $this.parents( 'tr:first' ),
  1038. event = $row.find( '.popup_cookies_field_event' ).val(),
  1039. values = _.extend(
  1040. {},
  1041. JSON.parse(
  1042. $row.find( '.popup_cookies_field_settings:first' ).val()
  1043. ),
  1044. {
  1045. index: $row
  1046. .parent()
  1047. .children()
  1048. .index( $row ),
  1049. event: event,
  1050. }
  1051. );
  1052. e.preventDefault();
  1053. cookies.template.form( event, values, function( e ) {
  1054. var $form = $( this ),
  1055. event = $form.find( 'input#event' ).val(),
  1056. index = $form.find( 'input#index' ).val(),
  1057. values = $form.pumSerializeObject();
  1058. e.preventDefault();
  1059. if ( index === false || index < 0 ) {
  1060. index = $editor.find( 'tbody tr' ).length;
  1061. }
  1062. cookies.rows.add( $editor, {
  1063. index: index,
  1064. event: event,
  1065. settings: values.cookie_settings,
  1066. } );
  1067. PUM_Admin.modals.closeAll();
  1068. } );
  1069. } )
  1070. .on( 'click', '.pum-popup-cookie-editor .remove', function( e ) {
  1071. var $this = $( this ),
  1072. $row = $this.parents( 'tr:first' );
  1073. e.preventDefault();
  1074. if ( window.confirm( I10n.confirm_delete_cookie ) ) {
  1075. cookies.rows.remove( $row );
  1076. }
  1077. } )
  1078. .on( 'click', '.pum-field-cookie_key button.reset', function( e ) {
  1079. var $this = $( this ),
  1080. newKey = new Date().getTime().toString( 16 );
  1081. $this.siblings( 'input[type="text"]:first' ).val( newKey );
  1082. } )
  1083. .on( 'submit', '#pum_cookie_add_event_modal .pum-form', function( e ) {
  1084. var $editor = current_editor,
  1085. event = $( '#popup_cookie_add_event' ).val(),
  1086. values = {
  1087. index: $editor.find( 'table.list-table tbody tr' ).length,
  1088. name: 'pum-' + $( '#post_ID' ).val(),
  1089. path: '1',
  1090. };
  1091. e.preventDefault();
  1092. cookies.template.form( event, values, function( e ) {
  1093. var $form = $( this ),
  1094. event = $form.find( 'input#event' ).val(),
  1095. index = $form.find( 'input#index' ).val(),
  1096. values = $form.pumSerializeObject();
  1097. e.preventDefault();
  1098. if ( index === false || index < 0 ) {
  1099. index = $editor.find( 'tbody tr' ).length;
  1100. }
  1101. cookies.rows.add( $editor, {
  1102. index: index,
  1103. event: event,
  1104. settings: values.cookie_settings,
  1105. } );
  1106. PUM_Admin.modals.closeAll();
  1107. if (
  1108. typeof PUM_Admin.triggers !== 'undefined' &&
  1109. PUM_Admin.triggers.new_cookie !== false &&
  1110. PUM_Admin.triggers.new_cookie >= 0
  1111. ) {
  1112. var $trigger = PUM_Admin.triggers.current_editor
  1113. .find( 'tbody tr' )
  1114. .eq( PUM_Admin.triggers.new_cookie )
  1115. .find( '.popup_triggers_field_settings:first' ),
  1116. trigger_settings = JSON.parse( $trigger.val() );
  1117. if ( typeof trigger_settings.cookie_name === 'string' ) {
  1118. trigger_settings.cookie_name = trigger_settings.cookie_name.replace(
  1119. 'add_new',
  1120. values.cookie_settings.name
  1121. );
  1122. } else {
  1123. trigger_settings.cookie_name[
  1124. trigger_settings.cookie_name.indexOf( 'add_new' )
  1125. ] = values.cookie_settings.name;
  1126. trigger_settings.cookie_name = trigger_settings.cookie_name.filter(
  1127. function( element, index, array ) {
  1128. return element in this
  1129. ? false
  1130. : ( this[ element ] = true );
  1131. },
  1132. {}
  1133. );
  1134. }
  1135. $trigger.val( JSON.stringify( trigger_settings ) );
  1136. PUM_Admin.triggers.new_cookie = false;
  1137. PUM_Admin.triggers.refreshDescriptions();
  1138. }
  1139. } );
  1140. } );
  1141. } )( jQuery, document );
  1142. ( function( $, document, undefined ) {
  1143. 'use strict';
  1144. var I10n = pum_admin_vars.I10n;
  1145. var triggers = {
  1146. current_editor: null,
  1147. new_cookie: false,
  1148. get_triggers: function() {
  1149. return window.pum_popup_settings_editor.triggers;
  1150. },
  1151. get_trigger: function( type ) {
  1152. var triggers = this.get_triggers(),
  1153. trigger =
  1154. triggers[ type ] !== 'undefined' ? triggers[ type ] : false;
  1155. if ( ! trigger ) {
  1156. return false;
  1157. }
  1158. if (
  1159. trigger &&
  1160. typeof trigger === 'object' &&
  1161. typeof trigger.fields === 'object' &&
  1162. Object.keys( trigger.fields ).length
  1163. ) {
  1164. trigger = this.parseFields( trigger );
  1165. }
  1166. return trigger;
  1167. },
  1168. parseFields: function( trigger ) {
  1169. _.each( trigger.fields, function( fields, tabID ) {
  1170. _.each( fields, function( field, fieldID ) {
  1171. trigger.fields[ tabID ][ fieldID ].name =
  1172. 'trigger_settings[' + fieldID + ']';
  1173. if ( trigger.fields[ tabID ][ fieldID ].id === '' ) {
  1174. trigger.fields[ tabID ][ fieldID ].id =
  1175. 'trigger_settings_' + fieldID;
  1176. }
  1177. } );
  1178. } );
  1179. return trigger;
  1180. },
  1181. parseValues: function( values, type ) {
  1182. for ( var key in values ) {
  1183. if ( ! values.hasOwnProperty( key ) ) {
  1184. continue;
  1185. }
  1186. // Clean measurement fields.
  1187. if ( values.hasOwnProperty( key + '_unit' ) ) {
  1188. values[ key ] += values[ key + '_unit' ];
  1189. delete values[ key + '_unit' ];
  1190. }
  1191. }
  1192. return values;
  1193. },
  1194. select_list: function() {
  1195. var i,
  1196. _triggers = PUM_Admin.utils.object_to_array(
  1197. triggers.get_triggers()
  1198. ),
  1199. options = {};
  1200. for ( i = 0; i < _triggers.length; i++ ) {
  1201. options[ _triggers[ i ].id ] = _triggers[ i ].name;
  1202. }
  1203. return options;
  1204. },
  1205. rows: {
  1206. add: function( editor, trigger ) {
  1207. var $editor = $( editor ),
  1208. data = {
  1209. index:
  1210. trigger.index !== null && trigger.index >= 0
  1211. ? trigger.index
  1212. : $editor.find( 'table.list-table tbody tr' )
  1213. .length,
  1214. type: trigger.type,
  1215. name: $editor.data( 'field_name' ),
  1216. settings: trigger.settings || {},
  1217. },
  1218. $row = $editor.find( 'tbody tr' ).eq( data.index ),
  1219. $new_row = PUM_Admin.templates.render(
  1220. 'pum-trigger-row',
  1221. data
  1222. );
  1223. if ( $row.length ) {
  1224. $row.replaceWith( $new_row );
  1225. } else {
  1226. $editor.find( 'tbody' ).append( $new_row );
  1227. }
  1228. $editor.addClass( 'has-list-items' );
  1229. triggers.renumber();
  1230. triggers.refreshDescriptions();
  1231. },
  1232. remove: function( $trigger ) {
  1233. var $editor = $trigger.parents( '.pum-popup-trigger-editor' );
  1234. $trigger.remove();
  1235. triggers.renumber();
  1236. if (
  1237. $editor.find( 'table.list-table tbody tr' ).length === 0
  1238. ) {
  1239. $editor.removeClass( 'has-list-items' );
  1240. $( '#pum-first-trigger' )
  1241. .val( null )
  1242. .trigger( 'change' );
  1243. }
  1244. },
  1245. },
  1246. template: {
  1247. form: function( type, values, callback ) {
  1248. var trigger = triggers.get_trigger( type ),
  1249. modalID = 'pum_trigger_settings',
  1250. firstTab = Object.keys( trigger.fields )[ 0 ],
  1251. $cookies = $( '.pum-field-cookies .list-table tbody tr' );
  1252. values = values || {};
  1253. values.type = type;
  1254. values.index = values.index >= 0 ? values.index : null;
  1255. // Add hidden index & type fields.
  1256. trigger.fields[ firstTab ] = $.extend(
  1257. true,
  1258. trigger.fields[ firstTab ],
  1259. {
  1260. index: {
  1261. type: 'hidden',
  1262. name: 'index',
  1263. },
  1264. type: {
  1265. type: 'hidden',
  1266. name: 'type',
  1267. },
  1268. }
  1269. );
  1270. $cookies.each( function() {
  1271. var settings = JSON.parse(
  1272. $( this )
  1273. .find( '.popup_cookies_field_settings:first' )
  1274. .val()
  1275. );
  1276. if (
  1277. typeof trigger.fields[ firstTab ].cookie_name.options[
  1278. settings.name
  1279. ] === 'undefined'
  1280. ) {
  1281. trigger.fields[ firstTab ].cookie_name.options[
  1282. settings.name
  1283. ] = settings.name;
  1284. }
  1285. } );
  1286. PUM_Admin.modals.reload(
  1287. '#' + modalID,
  1288. PUM_Admin.templates.modal( {
  1289. id: modalID,
  1290. title: trigger.modal_title || trigger.name,
  1291. classes: 'tabbed-content',
  1292. save_button:
  1293. values.index !== null ? I10n.update : I10n.add,
  1294. content: PUM_Admin.forms.render(
  1295. {
  1296. id: 'pum_trigger_settings_form',
  1297. tabs: trigger.tabs || {},
  1298. fields: trigger.fields || {},
  1299. },
  1300. values || {}
  1301. ),
  1302. } )
  1303. );
  1304. $( '#' + modalID + ' form' ).on(
  1305. 'submit',
  1306. callback ||
  1307. function( event ) {
  1308. event.preventDefault();
  1309. PUM_Admin.modals.closeAll();
  1310. }
  1311. );
  1312. },
  1313. editor: function( args ) {
  1314. var data = $.extend(
  1315. true,
  1316. {},
  1317. {
  1318. triggers: [],
  1319. name: '',
  1320. },
  1321. args
  1322. );
  1323. data.triggers = PUM_Admin.utils.object_to_array(
  1324. data.triggers
  1325. );
  1326. return PUM_Admin.templates.render( 'pum-trigger-editor', data );
  1327. },
  1328. row: function( args ) {
  1329. var data = $.extend(
  1330. true,
  1331. {},
  1332. {
  1333. index: '',
  1334. type: '',
  1335. name: '',
  1336. settings: {
  1337. cookie_name: '',
  1338. },
  1339. },
  1340. args
  1341. );
  1342. return PUM_Admin.templates.render( 'pum-trigger-row', data );
  1343. },
  1344. selectbox: function( args ) {
  1345. var data = $.extend(
  1346. true,
  1347. {},
  1348. {
  1349. id: null,
  1350. name: null,
  1351. type: 'select',
  1352. group: '',
  1353. index: '',
  1354. value: null,
  1355. select2: true,
  1356. classes: [],
  1357. options: triggers.select_list(),
  1358. },
  1359. args
  1360. );
  1361. if ( data.id === null ) {
  1362. data.id = 'popup_settings_triggers_' + data.index + '_type';
  1363. }
  1364. if ( data.name === null ) {
  1365. data.name =
  1366. 'popup_settings[triggers][' + data.index + '][type]';
  1367. }
  1368. return PUM_Admin.templates.field( data );
  1369. },
  1370. },
  1371. /* @deprecated */
  1372. getLabel: function( type ) {
  1373. var trigger = triggers.get_trigger( type );
  1374. if ( ! trigger ) {
  1375. return false;
  1376. }
  1377. return trigger.name;
  1378. },
  1379. getSettingsDesc: function( type, values ) {
  1380. var trigger = triggers.get_trigger( type );
  1381. if ( ! trigger ) {
  1382. return false;
  1383. }
  1384. return PUM_Admin.templates.renderInline(
  1385. trigger.settings_column,
  1386. values
  1387. );
  1388. },
  1389. renumber: function() {
  1390. $( '.pum-popup-trigger-editor table.list-table tbody tr' ).each(
  1391. function() {
  1392. var $this = $( this ),
  1393. index = $this
  1394. .parent()
  1395. .children()
  1396. .index( $this );
  1397. $this.attr( 'data-index', index ).data( 'index', index );
  1398. $this.find( ':input, [name]' ).each( function() {
  1399. if ( this.name && this.name !== '' ) {
  1400. this.name = this.name.replace(
  1401. /\[\d*?\]/,
  1402. '[' + index + ']'
  1403. );
  1404. }
  1405. } );
  1406. }
  1407. );
  1408. },
  1409. refreshDescriptions: function() {
  1410. $( '.pum-popup-trigger-editor table.list-table tbody tr' ).each(
  1411. function() {
  1412. var $row = $( this ),
  1413. type = $row.find( '.popup_triggers_field_type' ).val(),
  1414. values = JSON.parse(
  1415. $row
  1416. .find( '.popup_triggers_field_settings:first' )
  1417. .val()
  1418. ),
  1419. cookie_text = PUM_Admin.triggers.cookie_column_value(
  1420. values.cookie_name
  1421. );
  1422. $row.find( 'td.settings-column' ).html(
  1423. PUM_Admin.triggers.getSettingsDesc( type, values )
  1424. );
  1425. $row.find( 'td.cookie-column code' ).text( cookie_text );
  1426. }
  1427. );
  1428. },
  1429. cookie_column_value: function( cookie_name ) {
  1430. var cookie_text = I10n.no_cookie;
  1431. if ( cookie_name instanceof Array ) {
  1432. cookie_text = cookie_name.join( ', ' );
  1433. } else if (
  1434. cookie_name !== null &&
  1435. cookie_name !== undefined &&
  1436. cookie_name !== ''
  1437. ) {
  1438. cookie_text = cookie_name;
  1439. }
  1440. return cookie_text;
  1441. },
  1442. append_click_selector_presets: function() {
  1443. var $field = $( '#extra_selectors' ),
  1444. template,
  1445. $presets;
  1446. if (
  1447. ! $field.length ||
  1448. $field.hasClass( 'pum-click-selector-presets-initialized' )
  1449. ) {
  1450. return;
  1451. }
  1452. template = PUM_Admin.templates.render(
  1453. 'pum-click-selector-presets'
  1454. );
  1455. $presets = $field
  1456. .parents( '.pum-field' )
  1457. .find( '.pum-click-selector-presets' );
  1458. if ( ! $presets.length ) {
  1459. $field.before( template );
  1460. $field.addClass( 'pum-click-selector-presets-initialized' );
  1461. $presets = $field
  1462. .parents( '.pum-field' )
  1463. .find( '.pum-click-selector-presets' );
  1464. }
  1465. $presets.position( {
  1466. my: 'right center',
  1467. at: 'right center',
  1468. of: $field,
  1469. } );
  1470. },
  1471. toggle_click_selector_presets: function() {
  1472. $( this )
  1473. .parent()
  1474. .toggleClass( 'open' );
  1475. },
  1476. reset_click_selector_presets: function( e ) {
  1477. if (
  1478. e !== undefined &&
  1479. $( e.target ).parents( '.pum-click-selector-presets' ).length
  1480. ) {
  1481. return;
  1482. }
  1483. $( '.pum-click-selector-presets' ).removeClass( 'open' );
  1484. },
  1485. insert_click_selector_preset: function() {
  1486. var $this = $( this ),
  1487. $input = $( '#extra_selectors' ),
  1488. val = $input.val();
  1489. if ( val !== '' ) {
  1490. val = val + ', ';
  1491. }
  1492. $input.val( val + $this.data( 'preset' ) );
  1493. PUM_Admin.triggers.reset_click_selector_presets();
  1494. },
  1495. };
  1496. // Import this module.
  1497. window.PUM_Admin = window.PUM_Admin || {};
  1498. window.PUM_Admin.triggers = triggers;
  1499. $( document )
  1500. .on( 'pum_init', function() {
  1501. PUM_Admin.triggers.append_click_selector_presets();
  1502. PUM_Admin.triggers.refreshDescriptions();
  1503. } )
  1504. .on(
  1505. 'click',
  1506. '.pum-click-selector-presets > span',
  1507. PUM_Admin.triggers.toggle_click_selector_presets
  1508. )
  1509. .on(
  1510. 'click',
  1511. '.pum-click-selector-presets li',
  1512. PUM_Admin.triggers.insert_click_selector_preset
  1513. )
  1514. .on( 'click', PUM_Admin.triggers.reset_click_selector_presets )
  1515. /**
  1516. * @deprecated 1.7.0
  1517. */
  1518. .on(
  1519. 'select2:select pumselect2:select',
  1520. '#pum-first-trigger',
  1521. function() {
  1522. var $this = $( this ),
  1523. $editor = $this.parents( '.pum-popup-trigger-editor' ),
  1524. type = $this.val(),
  1525. values = {};
  1526. // Set Current Editor.
  1527. PUM_Admin.triggers.current_editor = $editor;
  1528. if ( type !== 'click_open' ) {
  1529. values.cookie_name = 'pum-' + $( '#post_ID' ).val();
  1530. }
  1531. triggers.template.form( type, values, function( event ) {
  1532. var $form = $( this ),
  1533. type = $form.find( 'input#type' ).val(),
  1534. values = $form.pumSerializeObject(),
  1535. trigger_settings = triggers.parseValues(
  1536. values.trigger_settings || {}
  1537. ),
  1538. index = parseInt( values.index );
  1539. event.preventDefault();
  1540. if ( index === false || index < 0 ) {
  1541. index = $editor.find( 'tbody tr' ).length;
  1542. }
  1543. triggers.rows.add( $editor, {
  1544. index: index,
  1545. type: type,
  1546. settings: trigger_settings,
  1547. } );
  1548. PUM_Admin.modals.closeAll();
  1549. if (
  1550. trigger_settings.cookie_name !== undefined &&
  1551. trigger_settings.cookie_name !== null &&
  1552. ( trigger_settings.cookie_name === 'add_new' ||
  1553. trigger_settings.cookie_name.indexOf( 'add_new' ) >=
  1554. 0 )
  1555. ) {
  1556. PUM_Admin.triggers.new_cookie = values.index;
  1557. $(
  1558. '#pum-popup-settings-container .pum-popup-cookie-editor button.pum-add-new'
  1559. ).trigger( 'click' );
  1560. }
  1561. } );
  1562. $this.val( null ).trigger( 'change' );
  1563. }
  1564. )
  1565. // Add New Triggers
  1566. .on( 'click', '.pum-popup-trigger-editor .pum-add-new', function() {
  1567. PUM_Admin.triggers.current_editor = $( this ).parents(
  1568. '.pum-popup-trigger-editor'
  1569. );
  1570. var template = wp.template( 'pum-trigger-add-type' );
  1571. PUM_Admin.modals.reload(
  1572. '#pum_trigger_add_type_modal',
  1573. template( { I10n: I10n } )
  1574. );
  1575. } )
  1576. .on( 'click', '.pum-popup-trigger-editor .edit', function( event ) {
  1577. var $this = $( this ),
  1578. $editor = $this.parents( '.pum-popup-trigger-editor' ),
  1579. $row = $this.parents( 'tr:first' ),
  1580. type = $row.find( '.popup_triggers_field_type' ).val(),
  1581. values = _.extend(
  1582. {},
  1583. JSON.parse(
  1584. $row
  1585. .find( '.popup_triggers_field_settings:first' )
  1586. .val()
  1587. ),
  1588. {
  1589. index: $row
  1590. .parent()
  1591. .children()
  1592. .index( $row ),
  1593. type: type,
  1594. }
  1595. );
  1596. event.preventDefault();
  1597. triggers.template.form( type, values, function( event ) {
  1598. var $form = $( this ),
  1599. type = $form.find( 'input#type' ).val(),
  1600. index = $form.find( 'input#index' ).val(),
  1601. values = $form.pumSerializeObject(),
  1602. trigger_settings = triggers.parseValues(
  1603. values.trigger_settings || {}
  1604. );
  1605. // Set Current Editor.
  1606. PUM_Admin.triggers.current_editor = $editor;
  1607. event.preventDefault();
  1608. if ( index === false || index < 0 ) {
  1609. index = $editor.find( 'tbody tr' ).length;
  1610. }
  1611. triggers.rows.add( $editor, {
  1612. index: index,
  1613. type: type,
  1614. settings: trigger_settings,
  1615. } );
  1616. PUM_Admin.modals.closeAll();
  1617. if (
  1618. trigger_settings.cookie_name !== undefined &&
  1619. trigger_settings.cookie_name !== null &&
  1620. ( trigger_settings.cookie_name === 'add_new' ||
  1621. trigger_settings.cookie_name.indexOf( 'add_new' ) >= 0 )
  1622. ) {
  1623. PUM_Admin.triggers.new_cookie = values.index;
  1624. $(
  1625. '#pum-popup-settings-container .pum-popup-cookie-editor button.pum-add-new'
  1626. ).trigger( 'click' );
  1627. }
  1628. } );
  1629. } )
  1630. .on( 'click', '.pum-popup-trigger-editor .remove', function( event ) {
  1631. var $this = $( this ),
  1632. $editor = $this.parents( '.pum-popup-trigger-editor' ),
  1633. $row = $this.parents( 'tr:first' );
  1634. // Set Current Editor.
  1635. PUM_Admin.triggers.current_editor = $editor;
  1636. event.preventDefault();
  1637. if ( window.confirm( I10n.confirm_delete_trigger ) ) {
  1638. triggers.rows.remove( $row );
  1639. }
  1640. } )
  1641. .on( 'submit', '#pum_trigger_add_type_modal .pum-form', function(
  1642. event
  1643. ) {
  1644. var $editor = PUM_Admin.triggers.current_editor,
  1645. $cookie_editor = $editor
  1646. .parents( '#pum-popup-settings-triggers-subtabs_main' )
  1647. .find( '.pum-field-cookies .pum-popup-cookie-editor' ),
  1648. type = $( '#popup_trigger_add_type' ).val(),
  1649. add_cookie = $( '#popup_trigger_add_cookie' ).is( ':checked' ),
  1650. add_cookie_event = $( '#popup_trigger_add_cookie_event' ).val(),
  1651. values = {};
  1652. event.preventDefault();
  1653. if ( add_cookie ) {
  1654. values.cookie_name = 'pum-' + $( '#post_ID' ).val();
  1655. PUM_Admin.cookies.insertCookie( $cookie_editor, {
  1656. event: add_cookie_event,
  1657. settings: {
  1658. time: '1 month',
  1659. path: '1',
  1660. name: values.cookie_name,
  1661. },
  1662. } );
  1663. }
  1664. triggers.template.form( type, values, function( event ) {
  1665. var $form = $( this ),
  1666. type = $form.find( 'input#type' ).val(),
  1667. values = $form.pumSerializeObject(),
  1668. trigger_settings = triggers.parseValues(
  1669. values.trigger_settings || {}
  1670. ),
  1671. index = parseInt( values.index );
  1672. // Set Current Editor.
  1673. PUM_Admin.triggers.current_editor = $editor;
  1674. event.preventDefault();
  1675. if ( ! index || index < 0 ) {
  1676. index = $editor.find( 'tbody tr' ).length;
  1677. }
  1678. triggers.rows.add( $editor, {
  1679. index: index,
  1680. type: type,
  1681. settings: trigger_settings,
  1682. } );
  1683. PUM_Admin.modals.closeAll();
  1684. if (
  1685. trigger_settings.cookie_name !== undefined &&
  1686. trigger_settings.cookie_name !== null &&
  1687. ( trigger_settings.cookie_name === 'add_new' ||
  1688. trigger_settings.cookie_name.indexOf( 'add_new' ) >= 0 )
  1689. ) {
  1690. PUM_Admin.triggers.new_cookie = values.index;
  1691. $(
  1692. '#pum-popup-settings-container .pum-popup-cookie-editor button.pum-add-new'
  1693. ).trigger( 'click' );
  1694. }
  1695. } );
  1696. } );
  1697. } )( jQuery, document );