fl-builder-admin-settings.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. (function($){
  2. /**
  3. * Helper class for dealing with the builder's admin
  4. * settings page.
  5. *
  6. * @class FLBuilderAdminSettings
  7. * @since 1.0
  8. */
  9. FLBuilderAdminSettings = {
  10. /**
  11. * An instance of wp.media used for uploading icons.
  12. *
  13. * @since 1.4.6
  14. * @access private
  15. * @property {Object} _iconUploader
  16. */
  17. _iconUploader: null,
  18. /**
  19. * Initializes the builder's admin settings page.
  20. *
  21. * @since 1.0
  22. * @method init
  23. */
  24. init: function()
  25. {
  26. this._bind();
  27. //this._maybeShowWelcome();
  28. this._initNav();
  29. this._initNetworkOverrides();
  30. this._initLicenseSettings();
  31. this._initMultiSelects();
  32. this._initUserAccessSelects();
  33. this._initUserAccessNetworkOverrides();
  34. this._templatesOverrideChange();
  35. this._iconPro();
  36. this._alphaSettings();
  37. },
  38. /**
  39. * Binds events for the builder's admin settings page.
  40. *
  41. * @since 1.0
  42. * @access private
  43. * @method _bind
  44. */
  45. _bind: function()
  46. {
  47. $('.fl-settings-nav a').on('click', FLBuilderAdminSettings._navClicked);
  48. $('.fl-override-ms-cb').on('click', FLBuilderAdminSettings._overrideCheckboxClicked);
  49. $('.fl-ua-override-ms-cb').on('click', FLBuilderAdminSettings._overrideUserAccessCheckboxClicked);
  50. $('.fl-module-all-cb').on('click', FLBuilderAdminSettings._moduleAllCheckboxClicked);
  51. $('.fl-module-cb').on('click', FLBuilderAdminSettings._moduleCheckboxClicked);
  52. $('input[name=fl-templates-override]').on('keyup click', FLBuilderAdminSettings._templatesOverrideChange);
  53. $('input[name=fl-upload-icon]').on('click', FLBuilderAdminSettings._showIconUploader);
  54. $('.fl-delete-icon-set').on('click', FLBuilderAdminSettings._deleteCustomIconSet);
  55. $('#uninstall-form').on('submit', FLBuilderAdminSettings._uninstallFormSubmit);
  56. $( '.fl-settings-form .dashicons-editor-help' ).tipTip();
  57. $( '.subscription-form .subscribe-button' ).on( 'click', FLBuilderAdminSettings._welcomeSubscribe);
  58. $( '.advanced-group input[type=checkbox]').on('change', FLBuilderAdminSettings._advancedToggle );
  59. $( '.advanced-group input[type=text]').on('keyup', FLBuilderAdminSettings._advancedText );
  60. $( '.advanced-group h3' ).on('click', FLBuilderAdminSettings._advancedShowHide );
  61. $( '.advanced-group button' ).on('click', FLBuilderAdminSettings._advancedTextSave );
  62. },
  63. _welcomeSubscribe: function()
  64. {
  65. form = $('.subscription-form')
  66. var error = form.find('.error')
  67. var spinner = form.find('.dashicons')
  68. if( error.css('display') != 'none' ) {
  69. error.hide()
  70. }
  71. name = form.find( '.input-group-field.name').val()
  72. email = form.find( '.input-group-field.email').val()
  73. nonce = form.find( '#_wpnonce' ).val()
  74. if ( ! email || ! name ) {
  75. error.html('Please enter required fields').fadeIn()
  76. return false;
  77. }
  78. spinner.css('color', '#fff')
  79. spinner.addClass('spin')
  80. data = {
  81. 'action' : 'fl_welcome_submit',
  82. 'name' : name,
  83. 'email' : email,
  84. '_wpnonce': nonce
  85. }
  86. console.log(data)
  87. $.post(ajaxurl, data, function(response) {
  88. spinner.css( 'color', '#0a3c4b' );
  89. spinner.removeClass( 'spin' );
  90. if( response.success ) {
  91. $('.subscribe-button').hide()
  92. $('.input-group').remove()
  93. spinner.remove()
  94. $( error ).html( '<h2>' + response.data.message + '</h2>' ).fadeIn()
  95. } else {
  96. $( error ).html(response.data.message).fadeIn()
  97. }
  98. });
  99. },
  100. _advancedText: function() {
  101. button = $(this).parent().find('.save-button');
  102. button.css('display', 'contents')
  103. },
  104. _advancedTextSave: function(e) {
  105. e.preventDefault();
  106. var buttonwrap = $(this).parent()
  107. id = $(this).data('id');
  108. value = $('#' + id).val();
  109. data = {
  110. 'action' : 'fl_advanced_submit',
  111. 'setting' : id,
  112. 'type' : 'text',
  113. 'value' : value,
  114. '_wpnonce': $('#fl-advanced-nonce').val()
  115. }
  116. $.post(ajaxurl, data, function(response) {
  117. if ( response.success ) {
  118. buttonwrap.fadeOut();
  119. new Notify({
  120. status: 'success',
  121. title: 'Saved',
  122. autoclose: true,
  123. autotimeout: 1000,
  124. distance: 20,
  125. });
  126. } else {
  127. new Notify({
  128. status: 'error',
  129. title: 'Save Error',
  130. autoclose: false,
  131. distance: 20,
  132. });
  133. }
  134. })
  135. .fail( function(){
  136. new Notify({
  137. status: 'error',
  138. title: 'Save Error',
  139. autoclose: false,
  140. distance: 20,
  141. });
  142. });
  143. },
  144. _advancedToggle: function(event) {
  145. checkbox = $(this);
  146. var depend = $(this).data('depend') || false;
  147. data = {
  148. 'action' : 'fl_advanced_submit',
  149. 'setting' : checkbox.attr('name'),
  150. 'value' : checkbox.is(':checked'),
  151. '_wpnonce': $('#fl-advanced-nonce').val()
  152. }
  153. $.post(ajaxurl, data, function(response) {
  154. if ( response.success ) {
  155. $.when(
  156. new Notify({
  157. status: 'success',
  158. title: 'Saved',
  159. autoclose: true,
  160. autotimeout: 1000,
  161. distance: 20,
  162. })
  163. ).done(function(){
  164. if ( depend ) {
  165. location.reload()
  166. }
  167. });
  168. } else {
  169. new Notify({
  170. status: 'error',
  171. title: 'Save Error',
  172. autoclose: false,
  173. distance: 20,
  174. });
  175. }
  176. })
  177. .fail( function(){
  178. new Notify({
  179. status: 'error',
  180. title: 'Save Error',
  181. autoclose: false,
  182. distance: 20,
  183. });
  184. });
  185. },
  186. _advancedShowHide: function() {
  187. $(this).parent().find('.advanced-option').toggle('fast');
  188. },
  189. /**
  190. * Show the welcome page after the license has been saved.
  191. *
  192. * @since 1.7.4
  193. * @access private
  194. * @method _maybeShowWelcome
  195. */
  196. _maybeShowWelcome: function()
  197. {
  198. var onLicense = 'license' == window.location.hash.replace( '#', '' ),
  199. isUpdated = $( '.wrap .updated' ).length,
  200. licenseError = $( '.fl-license-error' ).length;
  201. if ( onLicense && isUpdated && ! licenseError ) {
  202. window.location.hash = 'welcome';
  203. }
  204. },
  205. /**
  206. * Initializes the nav for the builder's admin settings page.
  207. *
  208. * @since 1.0
  209. * @access private
  210. * @method _initNav
  211. */
  212. _initNav: function()
  213. {
  214. var links = $('.fl-settings-nav a'),
  215. hash = window.location.hash,
  216. active = hash === '' ? [] : links.filter('[href~="'+ hash +'"]');
  217. $('a.fl-active').removeClass('fl-active');
  218. $('.fl-settings-form').hide();
  219. if(hash === '' || active.length === 0) {
  220. active = links.eq(0);
  221. }
  222. active.addClass('fl-active');
  223. $('#fl-'+ active.attr('href').split('#').pop() +'-form').fadeIn();
  224. },
  225. /**
  226. * Fires when a nav item is clicked.
  227. *
  228. * @since 1.0
  229. * @access private
  230. * @method _navClicked
  231. */
  232. _navClicked: function()
  233. {
  234. if($(this).attr('href').indexOf('#') > -1) {
  235. $('a.fl-active').removeClass('fl-active');
  236. $('.fl-settings-form').hide();
  237. $(this).addClass('fl-active');
  238. $('#fl-'+ $(this).attr('href').split('#').pop() +'-form').fadeIn();
  239. }
  240. },
  241. /**
  242. * Initializes the checkboxes for overriding network settings.
  243. *
  244. * @since 1.0
  245. * @access private
  246. * @method _initNetworkOverrides
  247. */
  248. _initNetworkOverrides: function()
  249. {
  250. $('.fl-override-ms-cb').each(FLBuilderAdminSettings._initNetworkOverride);
  251. },
  252. /**
  253. * Initializes a checkbox for overriding network settings.
  254. *
  255. * @since 1.0
  256. * @access private
  257. * @method _initNetworkOverride
  258. */
  259. _initNetworkOverride: function()
  260. {
  261. var cb = $(this),
  262. content = cb.closest('.fl-settings-form').find('.fl-settings-form-content');
  263. if(this.checked) {
  264. content.show();
  265. }
  266. else {
  267. content.hide();
  268. }
  269. },
  270. /**
  271. * Fired when a network override checkbox is clicked.
  272. *
  273. * @since 1.0
  274. * @access private
  275. * @method _overrideCheckboxClicked
  276. */
  277. _overrideCheckboxClicked: function()
  278. {
  279. var cb = $(this),
  280. content = cb.closest('.fl-settings-form').find('.fl-settings-form-content');
  281. if(this.checked) {
  282. content.show();
  283. }
  284. else {
  285. content.hide();
  286. }
  287. },
  288. /**
  289. * Initializes custom multi-selects.
  290. *
  291. * @since 1.10
  292. * @access private
  293. * @method _initMultiSelects
  294. */
  295. _initMultiSelects: function()
  296. {
  297. $( 'select[multiple]' ).multiselect( {
  298. selectAll: true,
  299. texts: {
  300. deselectAll : FLBuilderAdminSettingsStrings.deselectAll,
  301. noneSelected : FLBuilderAdminSettingsStrings.noneSelected,
  302. placeholder : FLBuilderAdminSettingsStrings.select,
  303. selectAll : FLBuilderAdminSettingsStrings.selectAll,
  304. selectedOptions : FLBuilderAdminSettingsStrings.selected
  305. }
  306. } );
  307. },
  308. /**
  309. * Initializes user access select options.
  310. *
  311. * @since 1.10
  312. * @access private
  313. * @method _initUserAccessSelects
  314. */
  315. _initUserAccessSelects: function()
  316. {
  317. var config = FLBuilderAdminSettingsConfig,
  318. options = null,
  319. role = null,
  320. select = null,
  321. key = null,
  322. hidden = null;
  323. $( '.fl-user-access-select' ).each( function() {
  324. options = [];
  325. select = $( this );
  326. key = select.attr( 'name' ).replace( 'fl_user_access[', '' ).replace( '][]', '' );
  327. for( role in config.roles ) {
  328. options.push( {
  329. name : config.roles[ role ],
  330. value : role,
  331. checked : 'undefined' == typeof config.userAccess[ key ] ? false : config.userAccess[ key ][ role ]
  332. } );
  333. }
  334. select.multiselect( 'loadOptions', options );
  335. } );
  336. },
  337. /**
  338. * Initializes the checkboxes for overriding user access
  339. * network settings.
  340. *
  341. * @since 1.0
  342. * @access private
  343. * @method _initUserAccessNetworkOverrides
  344. */
  345. _initUserAccessNetworkOverrides: function()
  346. {
  347. $('.fl-ua-override-ms-cb').each(FLBuilderAdminSettings._initUserAccessNetworkOverride);
  348. },
  349. /**
  350. * Initializes a checkbox for overriding user access
  351. * network settings.
  352. *
  353. * @since 1.0
  354. * @access private
  355. * @method _initUserAccessNetworkOverride
  356. */
  357. _initUserAccessNetworkOverride: function()
  358. {
  359. var cb = $(this),
  360. select = cb.closest('.fl-user-access-setting').find('.ms-options-wrap');
  361. if(this.checked) {
  362. select.show();
  363. }
  364. else {
  365. select.hide();
  366. }
  367. },
  368. /**
  369. * Fired when a network override checkbox is clicked.
  370. *
  371. * @since 1.0
  372. * @access private
  373. * @method _overrideCheckboxClicked
  374. */
  375. _overrideUserAccessCheckboxClicked: function()
  376. {
  377. var cb = $(this),
  378. select = cb.closest('.fl-user-access-setting').find('.ms-options-wrap');
  379. if(this.checked) {
  380. select.show();
  381. }
  382. else {
  383. select.hide();
  384. }
  385. },
  386. /**
  387. * Fires when the "all" checkbox in the list of enabled
  388. * modules is clicked.
  389. *
  390. * @since 1.0
  391. * @access private
  392. * @method _moduleAllCheckboxClicked
  393. */
  394. _moduleAllCheckboxClicked: function()
  395. {
  396. if($(this).is(':checked')) {
  397. $('.fl-module-cb').prop('checked', true);
  398. } else {
  399. $('.fl-module-cb').prop('checked', false);
  400. }
  401. },
  402. /**
  403. * Fires when a checkbox in the list of enabled
  404. * modules is clicked.
  405. *
  406. * @since 1.0
  407. * @access private
  408. * @method _moduleCheckboxClicked
  409. */
  410. _moduleCheckboxClicked: function()
  411. {
  412. var allChecked = true;
  413. $('.fl-module-cb').each(function() {
  414. if(!$(this).is(':checked')) {
  415. allChecked = false;
  416. }
  417. });
  418. if(allChecked) {
  419. $('.fl-module-all-cb').prop('checked', true);
  420. }
  421. else {
  422. $('.fl-module-all-cb').prop('checked', false);
  423. }
  424. },
  425. /**
  426. * @since 1.7.4
  427. * @access private
  428. * @method _initLicenseSettings
  429. */
  430. _initLicenseSettings: function()
  431. {
  432. $( '.fl-new-license-form .button' ).on( 'click', FLBuilderAdminSettings._newLicenseButtonClick );
  433. },
  434. /**
  435. * @since 1.7.4
  436. * @access private
  437. * @method _newLicenseButtonClick
  438. */
  439. _newLicenseButtonClick: function()
  440. {
  441. $( '.fl-new-license-form' ).hide();
  442. $( '.fl-license-form' ).show();
  443. },
  444. /**
  445. * Fires when the templates override setting is changed.
  446. *
  447. * @since 1.6.3
  448. * @access private
  449. * @method _templatesOverrideChange
  450. */
  451. _templatesOverrideChange: function()
  452. {
  453. var input = $('input[name=fl-templates-override]'),
  454. val = input.val(),
  455. overrideNodes = $( '.fl-templates-override-nodes' ),
  456. toggle = false;
  457. if ( 'checkbox' == input.attr( 'type' ) ) {
  458. toggle = input.is( ':checked' );
  459. }
  460. else {
  461. toggle = '' !== val;
  462. }
  463. overrideNodes.toggle( toggle );
  464. },
  465. /**
  466. * Shows the media library lightbox for uploading icons.
  467. *
  468. * @since 1.4.6
  469. * @access private
  470. * @method _showIconUploader
  471. */
  472. _showIconUploader: function()
  473. {
  474. if(FLBuilderAdminSettings._iconUploader === null) {
  475. FLBuilderAdminSettings._iconUploader = wp.media({
  476. title: FLBuilderAdminSettingsStrings.selectFile,
  477. button: { text: FLBuilderAdminSettingsStrings.selectFile },
  478. library : { type : 'application/zip' },
  479. multiple: false
  480. });
  481. }
  482. FLBuilderAdminSettings._iconUploader.once('select', $.proxy(FLBuilderAdminSettings._iconFileSelected, this));
  483. FLBuilderAdminSettings._iconUploader.open();
  484. },
  485. /**
  486. * Callback for when an icon set file is selected.
  487. *
  488. * @since 1.4.6
  489. * @access private
  490. * @method _iconFileSelected
  491. */
  492. _iconFileSelected: function()
  493. {
  494. var file = FLBuilderAdminSettings._iconUploader.state().get('selection').first().toJSON();
  495. $( 'input[name=fl-new-icon-set]' ).val( file.id );
  496. $( '#icons-form' ).submit();
  497. },
  498. /**
  499. * Fires when the delete link for an icon set is clicked.
  500. *
  501. * @since 1.4.6
  502. * @access private
  503. * @method _deleteCustomIconSet
  504. */
  505. _deleteCustomIconSet: function()
  506. {
  507. var set = $( this ).data( 'set' );
  508. $( 'input[name=fl-delete-icon-set]' ).val( set );
  509. $( '#icons-form' ).submit();
  510. },
  511. /**
  512. * Fires when the uninstall button is clicked.
  513. *
  514. * @since 1.0
  515. * @access private
  516. * @method _uninstallFormSubmit
  517. * @return {Boolean}
  518. */
  519. _uninstallFormSubmit: function()
  520. {
  521. var result = prompt(FLBuilderAdminSettingsStrings.uninstall.replace(/&quot;/g, '"'), '');
  522. if(result == 'uninstall') {
  523. return true;
  524. }
  525. return false;
  526. },
  527. _iconPro: function() {
  528. form = $('#icons-form')
  529. checkbox = form.find('input[name=fl-enable-fa-pro]').prop('checked')
  530. light = form.find('input[value=font-awesome-5-light]').parent()
  531. duo = form.find('input[value=font-awesome-5-duotone]').parent()
  532. if ( true === checkbox ) {
  533. light.css('font-weight', '800')
  534. // light.css('color', '#0E5A71')
  535. duo.css('font-weight', '800')
  536. // duo.css('color', '#0E5A71')
  537. }
  538. },
  539. _alphaSettings: function() {
  540. form = $('#beta-form');
  541. form.find('.alpha-checkbox').on('click', function(){
  542. console.log('checked?')
  543. if ( true === $(this).prop('checked') ) {
  544. if ( confirm( 'Are you sure you want to enable Alpha releases?') ) {
  545. // do nothing
  546. } else {
  547. $(this).prop('checked',false);
  548. }
  549. }
  550. })
  551. },
  552. };
  553. /* Initializes the builder's admin settings. */
  554. $(function(){
  555. FLBuilderAdminSettings.init();
  556. });
  557. })(jQuery);