fl-builder-global-import-export.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ( function( $ ) {
  2. /**
  3. * @since 2.6
  4. * @class FLBuilderGlobalImportExport
  5. */
  6. FLBuilderGlobalImportExport = {
  7. _settingsUploader: null,
  8. /**
  9. * Initializes custom exports for the builder.
  10. *
  11. * @since 1.8
  12. * @access private
  13. * @method _init
  14. */
  15. _init: function()
  16. {
  17. $('body').on( 'click', '#fl-import-export-form input.export', FLBuilderGlobalImportExport._exportClicked);
  18. $('body').on( 'click', '#fl-import-export-form input.import', FLBuilderGlobalImportExport._importClicked);
  19. $('body').on( 'click', '#fl-import-export-form input.reset', FLBuilderGlobalImportExport._resetClicked);
  20. FLBuilderGlobalImportExport.bindChecks();
  21. },
  22. _exportClicked: function() {
  23. nonce = $('#fl-import-export-form').find('#_wpnonce').val();
  24. data = {
  25. global_all: $('#fl-import-export-form input.global_all').prop('checked'),
  26. admin: $('#fl-import-export-form input.admin').prop('checked'),
  27. global: $('#fl-import-export-form input.global').prop('checked'),
  28. styles: $('#fl-import-export-form input.styles').prop('checked'),
  29. colors: $('#fl-import-export-form input.colors').prop('checked'),
  30. }
  31. // generate data file.
  32. FLBuilderGlobalImportExport.ajax( {
  33. action: 'export_global_settings',
  34. data: data,
  35. _wpnonce: nonce,
  36. }, function ( response ) {
  37. switch( response.success ) {
  38. case false:
  39. break;
  40. case true:
  41. data = response.data;
  42. settings = data.settings;
  43. var filename = '';
  44. $.each( data.selected, function( e,i ) {
  45. if ( 'global_all' === e && 'true' === i ) {
  46. filename += 'all-';
  47. return false;
  48. }
  49. if ( 'true' === i ) {
  50. filename += e + '-';
  51. }
  52. });
  53. date = new Date();
  54. day = date.getDate();
  55. month = date.getMonth() + 1;
  56. year = date.getFullYear();
  57. filename = 'bb-settings-' + filename + `${year}-${month}-${day}` + '.txt';
  58. var blob = new Blob( [settings], { type: "application/octetstream" } );
  59. //Check the Browser type and download the File.
  60. var isIE = false || !!document.documentMode;
  61. if (isIE) {
  62. window.navigator.msSaveBlob(blob, filename);
  63. } else {
  64. var url = window.URL || window.webkitURL;
  65. link = url.createObjectURL(blob);
  66. var a = $("<a />");
  67. a.attr("download", filename);
  68. a.attr("href", link);
  69. $("body").append(a);
  70. a[0].click();
  71. $("body").remove(a);
  72. }
  73. break;
  74. }
  75. });
  76. },
  77. _importClicked: function() {
  78. if(FLBuilderGlobalImportExport._settingsUploader === null) {
  79. FLBuilderGlobalImportExport._settingsUploader = wp.media({
  80. title: 'Import Settings',
  81. button: { text: FLBuilderAdminImportExportConfig.select },
  82. library : { type : 'text/plain' },
  83. multiple: false
  84. });
  85. _wpPluploadSettings['defaults']['multipart_params']['fl_global_import']= 'json';
  86. FLBuilderGlobalImportExport._settingsUploader.on( 'select', function() {
  87. var selection = FLBuilderGlobalImportExport._settingsUploader.state().get('selection');
  88. var attachment_id = selection.map( function( attachment ) {
  89. attachment = attachment.toJSON();
  90. return attachment.id;
  91. }).join();
  92. txt = 'Are you sure you want to import settings?';
  93. if ( confirm( txt ) ) {
  94. FLBuilderGlobalImportExport._importSettings(attachment_id);
  95. }
  96. });
  97. }
  98. FLBuilderGlobalImportExport._settingsUploader.open();
  99. },
  100. _importSettings: function(attachment_id) {
  101. nonce = $('#fl-import-export-form').find('#_wpnonce').val();
  102. FLBuilderGlobalImportExport.ajax( {
  103. action: 'import_global_settings',
  104. _wpnonce: nonce,
  105. importid: attachment_id
  106. }, function ( response ) {
  107. switch( response.success ) {
  108. case false:
  109. alert( 'There was an error :(')
  110. break;
  111. case true:
  112. alert( 'Success!');
  113. location.reload();
  114. break;
  115. };
  116. });
  117. },
  118. _resetClicked: function() {
  119. nonce = $('#fl-import-export-form').find('#_wpnonce').val();
  120. txt = 'Are you sure you want to reset all settings?';
  121. if ( confirm(txt) ) {
  122. FLBuilderGlobalImportExport.ajax( {
  123. action: 'reset_global_settings',
  124. _wpnonce: nonce,
  125. }, function ( response ) {
  126. switch( response.success ) {
  127. case false:
  128. alert( 'There was an error :(')
  129. break;
  130. case true:
  131. alert( 'Success!');
  132. location.reload();
  133. break;
  134. };
  135. });
  136. }
  137. },
  138. /**
  139. * Makes an AJAX request.
  140. *
  141. * @since 1.0
  142. * @method ajax
  143. * @param {Object} data An object with data to send in the request.
  144. * @param {Function} callback A function to call when the request is complete.
  145. */
  146. ajax: function(data, callback) {
  147. // Send the request.
  148. $.post(ajaxurl, data, function(response) {
  149. if(typeof callback !== 'undefined') {
  150. callback.call(this, response);
  151. }
  152. });
  153. },
  154. bindChecks: function() {
  155. $('body').on( 'change', '#fl-import-export-form input.global_all', function(){
  156. checked = $(this).prop('checked')
  157. if ( ! checked ) {
  158. $('#fl-import-export-form .extra').fadeIn();
  159. } else {
  160. $('#fl-import-export-form .extra').fadeOut();
  161. }
  162. });
  163. }
  164. }
  165. $( FLBuilderGlobalImportExport._init );
  166. } )( jQuery );