envato-setup.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. jQuery(document).ready(function($){
  2. jQuery('.ta-import-content').on('click', function(e){
  3. jQuery('#importer-response').children('.res-text').html('');
  4. jQuery('#importer-response').children('.res-text').html('Processing content...');
  5. jQuery('#importer-response').find('.loadinerSearch').show();
  6. jQuery.ajax({
  7. type : "post",
  8. url : envato_setup_params.ajaxurl,
  9. data : {
  10. action:'setup_content',
  11. data:'1',
  12. },
  13. success: function(response) {
  14. if(response){
  15. jQuery('#importer-response').children('.res-text').html(response);
  16. jQuery('#importer-response').find('.loadinerSearch').hide();
  17. jQuery('#importer-response').find('.checkImg').show();
  18. jQuery('#importer-response-menu').children('.res-text').html('');
  19. jQuery('#importer-response-menu').children('.res-text').html('Creating menu...');
  20. jQuery('#importer-response-menu').find('.loadinerSearch').show();
  21. /* adding menus */
  22. jQuery.ajax({
  23. type : "post",
  24. url : envato_setup_params.ajaxurl,
  25. data : {
  26. action:'taskereasy_menu',
  27. data:'1',
  28. },
  29. success: function(responseMen) {
  30. if(responseMen){
  31. jQuery('#importer-response-menu').children('.res-text').html(responseMen);
  32. jQuery('#importer-response-menu').find('.loadinerSearch').hide();
  33. jQuery('#importer-response-menu').find('.checkImg').show();
  34. jQuery('#importer-response-homepage').children('.res-text').html('');
  35. jQuery('#importer-response-homepage').children('.res-text').html('Adding Homepage...');
  36. jQuery('#importer-response-homepage').find('.loadinerSearch').show();
  37. /* adding homepage */
  38. jQuery.ajax({
  39. type : "post",
  40. url : envato_setup_params.ajaxurl,
  41. data : {
  42. action:'taskereasy_homepage',
  43. data:'1',
  44. },
  45. success: function(responseMen) {
  46. if(responseMen){
  47. jQuery('#importer-response-homepage').children('.res-text').html(responseMen);
  48. jQuery('#importer-response-homepage').find('.loadinerSearch').hide();
  49. jQuery('#importer-response-homepage').find('.checkImg').show();
  50. jQuery('#importer-response-themeoptions').children('.res-text').html('');
  51. jQuery('#importer-response-themeoptions').children('.res-text').html('Theme Options...');
  52. jQuery('#importer-response-themeoptions').find('.loadinerSearch').show();
  53. jQuery.ajax({
  54. type : "post",
  55. url : envato_setup_params.ajaxurl,
  56. data : {
  57. action:'taskereasy_theme_options',
  58. data:'1',
  59. },
  60. success: function(responseMen) {
  61. if(responseMen){
  62. jQuery('#importer-response-themeoptions').html(responseMen);
  63. jQuery('#importer-response-themeoptions').find('.loadinerSearch').hide();
  64. jQuery('#importer-response-themeoptions').find('.checkImg').show();
  65. jQuery( ".button-next-skip" )[0].click();
  66. }
  67. }
  68. });
  69. }
  70. }
  71. });
  72. }
  73. }
  74. });
  75. }
  76. }
  77. });
  78. return false;
  79. });
  80. });
  81. var EnvatoWizard = (function($){
  82. var t;
  83. // callbacks from form button clicks.
  84. var callbacks = {
  85. install_plugins: function(btn){
  86. var plugins = new PluginManager();
  87. plugins.init(btn);
  88. },
  89. install_content: function(btn){
  90. var content = new ContentManager();
  91. content.init(btn);
  92. }
  93. };
  94. function window_loaded(){
  95. // init button clicks:
  96. $('.button-next').on( 'click', function(e) {
  97. var loading_button = dtbaker_loading_button(this);
  98. if(!loading_button){
  99. return false;
  100. }
  101. if($(this).data('callback') && typeof callbacks[$(this).data('callback')] != 'undefined'){
  102. // we have to process a callback before continue with form submission
  103. callbacks[$(this).data('callback')](this);
  104. return false;
  105. }else{
  106. loading_content();
  107. return true;
  108. }
  109. });
  110. $('.button-upload').on( 'click', function(e) {
  111. e.preventDefault();
  112. renderMediaUploader();
  113. });
  114. $('.ta-imp-demo .ta-select-demo-image').on( 'click', function() {
  115. $('.ta-imp-demo').removeClass('current');
  116. $(this).parents().addClass('current');
  117. var newcolor = $(this).find('a').data('style');
  118. $('#new_style').val(newcolor);
  119. return false;
  120. });
  121. }
  122. function loading_content(){
  123. $('.envato-setup-content').block({
  124. message: null,
  125. overlayCSS: {
  126. background: '#fff',
  127. opacity: 0.6
  128. }
  129. });
  130. }
  131. function PluginManager(){
  132. var complete;
  133. var items_completed = 0;
  134. var current_item = '';
  135. var $current_node;
  136. var current_item_hash = '';
  137. function ajax_callback(response){
  138. if(typeof response == 'object' && typeof response.message != 'undefined'){
  139. $current_node.find('span').text(response.message);
  140. if(typeof response.url != 'undefined'){
  141. // we have an ajax url action to perform.
  142. if(response.hash == current_item_hash){
  143. $current_node.find('span').text("failed");
  144. find_next();
  145. }else {
  146. current_item_hash = response.hash;
  147. jQuery.post(response.url, response, function(response2) {
  148. process_current();
  149. $current_node.find('span').text(response.message + envato_setup_params.verify_text);
  150. }).fail(ajax_callback);
  151. }
  152. }else if(typeof response.done != 'undefined'){
  153. // finished processing this plugin, move onto next
  154. find_next();
  155. }else{
  156. // error processing this plugin
  157. find_next();
  158. }
  159. }else{
  160. // error - try again with next plugin
  161. $current_node.find('span').text("Success");
  162. find_next();
  163. }
  164. }
  165. function process_current(){
  166. if(current_item){
  167. // query our ajax handler to get the ajax to send to TGM
  168. // if we don't get a reply we can assume everything worked and continue onto the next one.
  169. jQuery.post(envato_setup_params.ajaxurl, {
  170. action: 'envato_setup_plugins',
  171. wpnonce: envato_setup_params.wpnonce,
  172. slug: current_item
  173. }, ajax_callback).fail(ajax_callback);
  174. }
  175. }
  176. function find_next(){
  177. var do_next = false;
  178. if($current_node){
  179. if(!$current_node.data('done_item')){
  180. items_completed++;
  181. $current_node.data('done_item',1);
  182. }
  183. $current_node.find('.spinner').css('visibility','hidden');
  184. }
  185. var $li = $('.envato-wizard-plugins li');
  186. $li.each(function(){
  187. if(current_item == '' || do_next){
  188. current_item = $(this).data('slug');
  189. $current_node = $(this);
  190. process_current();
  191. do_next = false;
  192. }else if($(this).data('slug') == current_item){
  193. do_next = true;
  194. }
  195. });
  196. if(items_completed >= $li.length){
  197. // finished all plugins!
  198. complete();
  199. }
  200. }
  201. return {
  202. init: function(btn){
  203. $('.envato-wizard-plugins').addClass('installing');
  204. complete = function(){
  205. loading_content();
  206. window.location.href=btn.href;
  207. };
  208. find_next();
  209. }
  210. }
  211. }
  212. function ContentManager(){
  213. var complete;
  214. var items_completed = 0;
  215. var current_item = '';
  216. var $current_node;
  217. var current_item_hash = '';
  218. function ajax_callback(response) {
  219. if(typeof response == 'object' && typeof response.message != 'undefined'){
  220. $current_node.find('span').text(response.message);
  221. if(typeof response.url != 'undefined'){
  222. // we have an ajax url action to perform.
  223. if(response.hash == current_item_hash){
  224. $current_node.find('span').text("failed");
  225. find_next();
  226. }else {
  227. current_item_hash = response.hash;
  228. jQuery.post(response.url, response, ajax_callback).fail(ajax_callback); // recuurrssionnnnn
  229. }
  230. }else if(typeof response.done != 'undefined'){
  231. // finished processing this plugin, move onto next
  232. find_next();
  233. }else{
  234. // error processing this plugin
  235. find_next();
  236. }
  237. }else{
  238. // error - try again with next plugin
  239. $current_node.find('span').text("Success");
  240. find_next();
  241. }
  242. }
  243. function process_current(){
  244. if(current_item){
  245. var $check = $current_node.find('input:checkbox');
  246. if($check.is(':checked')) {
  247. console.log("Doing 2 "+current_item);
  248. // process htis one!
  249. jQuery.post(envato_setup_params.ajaxurl, {
  250. action: 'envato_setup_content',
  251. wpnonce: envato_setup_params.wpnonce,
  252. content: current_item
  253. }, ajax_callback).fail(ajax_callback);
  254. }else{
  255. $current_node.find('span').text("Skipping");
  256. setTimeout(find_next,300);
  257. }
  258. }
  259. }
  260. function find_next(){
  261. var do_next = false;
  262. if($current_node){
  263. if(!$current_node.data('done_item')){
  264. items_completed++;
  265. $current_node.data('done_item',1);
  266. }
  267. $current_node.find('.spinner').css('visibility','hidden');
  268. }
  269. var $items = $('tr.envato_default_content');
  270. var $enabled_items = $('tr.envato_default_content input:checked');
  271. $items.each(function(){
  272. if (current_item == '' || do_next) {
  273. current_item = $(this).data('content');
  274. $current_node = $(this);
  275. process_current();
  276. do_next = false;
  277. } else if ($(this).data('content') == current_item) {
  278. do_next = true;
  279. }
  280. });
  281. if(items_completed >= $items.length){
  282. // finished all items!
  283. complete();
  284. }
  285. }
  286. return {
  287. init: function(btn){
  288. $('.envato-setup-pages').addClass('installing');
  289. $('.envato-setup-pages').find('input').prop("disabled", true);
  290. complete = function(){
  291. loading_content();
  292. window.location.href=btn.href;
  293. };
  294. find_next();
  295. }
  296. }
  297. }
  298. /**
  299. * Callback function for the 'click' event of the 'Set Footer Image'
  300. * anchor in its meta box.
  301. *
  302. * Displays the media uploader for selecting an image.
  303. *
  304. * @since 0.1.0
  305. */
  306. function renderMediaUploader() {
  307. 'use strict';
  308. var file_frame, attachment;
  309. if ( undefined !== file_frame ) {
  310. file_frame.open();
  311. return;
  312. }
  313. file_frame = wp.media.frames.file_frame = wp.media({
  314. title: 'Upload Logo',//jQuery( this ).data( 'uploader_title' ),
  315. button: {
  316. text: 'Select Logo' //jQuery( this ).data( 'uploader_button_text' )
  317. },
  318. multiple: false // Set to true to allow multiple files to be selected
  319. });
  320. // When an image is selected, run a callback.
  321. file_frame.on( 'select', function() {
  322. // We set multiple to false so only get one image from the uploader
  323. attachment = file_frame.state().get('selection').first().toJSON();
  324. jQuery('.site-logo').attr('src',attachment.url);
  325. jQuery('#new_logo_id').val(attachment.id);
  326. // Do something with attachment.id and/or attachment.url here
  327. });
  328. // Now display the actual file_frame
  329. file_frame.open();
  330. }
  331. function dtbaker_loading_button(btn){
  332. var $button = jQuery(btn);
  333. if($button.data('done-loading') == 'yes')return false;
  334. var existing_text = $button.text();
  335. var existing_width = $button.outerWidth();
  336. var loading_text = '⡀⡀⡀⡀⡀⡀⡀⡀⡀⡀⠄⠂⠁⠁⠂⠄';
  337. var completed = false;
  338. $button.css('width',existing_width);
  339. $button.addClass('dtbaker_loading_button_current');
  340. var _modifier = $button.is('input') || $button.is('button') ? 'val' : 'text';
  341. $button[_modifier](loading_text);
  342. //$button.attr('disabled',true);
  343. $button.data('done-loading','yes');
  344. var anim_index = [0,1,2];
  345. // animate the text indent
  346. function moo() {
  347. if (completed)return;
  348. var current_text = '';
  349. // increase each index up to the loading length
  350. for(var i = 0; i < anim_index.length; i++){
  351. anim_index[i] = anim_index[i]+1;
  352. if(anim_index[i] >= loading_text.length)anim_index[i] = 0;
  353. current_text += loading_text.charAt(anim_index[i]);
  354. }
  355. $button[_modifier](current_text);
  356. setTimeout(function(){ moo();},60);
  357. }
  358. moo();
  359. return {
  360. done: function(){
  361. completed = true;
  362. $button[_modifier](existing_text);
  363. $button.removeClass('dtbaker_loading_button_current');
  364. $button.attr('disabled',false);
  365. }
  366. }
  367. }
  368. return {
  369. init: function(){
  370. t = this;
  371. $(window_loaded);
  372. },
  373. callback: function(func){
  374. console.log(func);
  375. console.log(this);
  376. }
  377. }
  378. })(jQuery);
  379. EnvatoWizard.init();