| Conditions | 1 | 
| Paths | 1 | 
| Total Lines | 120 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import progressbar from 'progressbar.js';  | 
            ||
| 3 | var GFImage = function() { | 
            ||
| 4 | |||
| 5 | var $ = jQuery;  | 
            ||
| 6 | |||
| 7 | var gFImage = this;  | 
            ||
| 8 | |||
| 9 | 	var element = $('#generate_fimage_wrapper'); | 
            ||
| 10 | |||
| 11 | 	var postNoFImageIds = element.data('post_no_fimage_ids'); | 
            ||
| 12 | |||
| 13 | var totalPostNoFImage = postNoFImageIds.length;  | 
            ||
| 14 | |||
| 15 | 	var securityToken = element.data('nonce'); | 
            ||
| 16 | |||
| 17 | 	var informationWrapper = $('.generate-fimage-information'); | 
            ||
| 18 | |||
| 19 | var progressBarID = '#generate_fimage_progressbar';  | 
            ||
| 20 | |||
| 21 | var progressBar;  | 
            ||
| 22 | |||
| 23 | 	var generateButton = $('#generate_fimage_button'); | 
            ||
| 24 | |||
| 25 | 	var saveButton = $('#save_form_button'); | 
            ||
| 26 | |||
| 27 | 	this.init = function() { | 
            ||
| 28 | gFImage._progressBarInit();  | 
            ||
| 29 | gFImage._onClickGButton();  | 
            ||
| 30 | }  | 
            ||
| 31 | |||
| 32 | 	this._progressBarInit = function() { | 
            ||
| 33 | 		var ProgressBar = require('progressbar.js'); | 
            ||
| 34 | 		progressBar = new ProgressBar.Line(progressBarID, { | 
            ||
| 35 | easing: 'easeInOut'  | 
            ||
| 36 | });  | 
            ||
| 37 | }  | 
            ||
| 38 | |||
| 39 | 	this._onClickGButton = function() { | 
            ||
| 40 | 		generateButton.click(function(event){ | 
            ||
| 41 | event.preventDefault();  | 
            ||
| 42 | var warningResult = gFImage._warning();  | 
            ||
| 43 | 			if(warningResult) { | 
            ||
| 44 | gFImage._toggleButton([generateButton, saveButton], 'disable');  | 
            ||
| 45 | gFImage._ajaxUpdateFImage(postNoFImageIds[0]);  | 
            ||
| 46 | }  | 
            ||
| 47 | });  | 
            ||
| 48 | }  | 
            ||
| 49 | |||
| 50 | 	this._toggleButton = function(arrayElement, status) { | 
            ||
| 51 | 		arrayElement.forEach(function(el){ | 
            ||
| 52 | 			switch(status) { | 
            ||
| 53 | case 'disable':  | 
            ||
| 54 | 					el.prop('disabled', true); | 
            ||
| 55 | break;  | 
            ||
| 56 | case 'enable':  | 
            ||
| 57 | 					el.prop('disabled', false); | 
            ||
| 58 | break;  | 
            ||
| 59 | default:  | 
            ||
| 60 | 					el.prop('disabled', false); | 
            ||
| 61 | }  | 
            ||
| 62 | });  | 
            ||
| 63 | }  | 
            ||
| 64 | |||
| 65 | 	this._ajaxUpdateFImage = function(postId) { | 
            ||
| 66 | 		$.ajax({ | 
            ||
| 67 | url: '/wp-admin/admin-ajax.php?action=wpdfi_generate_feature_image',  | 
            ||
| 68 | method: 'POST',  | 
            ||
| 69 |             data: { | 
            ||
| 70 | post_id: postId,  | 
            ||
| 71 | security: securityToken  | 
            ||
| 72 | },  | 
            ||
| 73 |             success: function(res) { | 
            ||
| 74 | |||
| 75 | var response = JSON.parse(res);  | 
            ||
| 76 | gFImage._updateLogAfterAjax(response);  | 
            ||
| 77 | var arrayIndex = postNoFImageIds.indexOf(postId);  | 
            ||
| 78 | gFImage._updateProgressBar(arrayIndex);  | 
            ||
| 79 | gFImage._continueAjax(arrayIndex);  | 
            ||
| 80 | |||
| 81 | }  | 
            ||
| 82 | })  | 
            ||
| 83 | }  | 
            ||
| 84 | |||
| 85 | 	this._updateProgressBar = function(arrayIndex) { | 
            ||
| 86 | /* Javascript array index start at 0, so we need to plus 1 to get the correct value for divide purpose. */  | 
            ||
| 87 | var realIndex = arrayIndex + 1;  | 
            ||
| 88 | var currentPercent = realIndex/totalPostNoFImage;  | 
            ||
| 89 | var currentPercentText = (currentPercent*100).toFixed(2) + '%';  | 
            ||
| 90 | progressBar.set(currentPercent);  | 
            ||
| 91 | progressBar.setText(currentPercentText);  | 
            ||
| 92 | }  | 
            ||
| 93 | |||
| 94 | 	this._continueAjax = function(arrayIndex) { | 
            ||
| 95 | /* Javascript array index start at 0, so we need to minus 1 from total posts to get the last index. */  | 
            ||
| 96 | var lastIndex = totalPostNoFImage - 1;  | 
            ||
| 97 | var isLastIndex = (arrayIndex == lastIndex);  | 
            ||
| 98 | /* If the current index is not the last index, continue run Ajax request on the next index. */  | 
            ||
| 99 | 		if(!isLastIndex) { | 
            ||
| 100 | gFImage._ajaxUpdateFImage(postNoFImageIds[arrayIndex + 1]);  | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | 	this._updateLogAfterAjax = function(response) { | 
            ||
| 105 | 		switch(response.status) { | 
            ||
| 106 | case true:  | 
            ||
| 107 |     			informationWrapper.append('<p>' + response.namePT + ' with ID ' + response.postId +' is updated feature image successfully</p>'); | 
            ||
| 108 | break;  | 
            ||
| 109 | case false:  | 
            ||
| 110 |     			informationWrapper.append('<p>' + response.namePT + ' with ID ' + response.postId +' because conditions are not match.</p>'); | 
            ||
| 111 | break;  | 
            ||
| 112 | default:  | 
            ||
| 113 |     			informationWrapper.append('<p>' + response.namePT + ' with ID ' + response.postId +' has something wrong!</p>'); | 
            ||
| 114 | }  | 
            ||
| 115 | }  | 
            ||
| 116 | |||
| 117 | 	this._warning = function() { | 
            ||
| 118 | var warningText = 'Are you sure you want to generate all feature image with the values in the "Sections" tab? Make sure to backup your database before click "OK".';  | 
            ||
| 119 | return confirm(warningText);  | 
            ||
| 120 | }  | 
            ||
| 121 | |||
| 122 | }  | 
            ||
| 123 | |||
| 124 | export default GFImage;  |