site-wide-analysis.js
3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* global yoastSiteWideAnalysisData */
import ProminentWordCalculation from "./keywordSuggestions/siteWideCalculation";
import ProminentWordCache from "./keywordSuggestions/ProminentWordCache";
import ProminentWordCachePopulator from "./keywordSuggestions/ProminentWordCachePopulator";
import RestApi from "./helpers/restApi";
let settings = yoastSiteWideAnalysisData.data;
let progressContainer, completedContainer;
let prominentWordCache;
/**
* Recalculates posts
*
* @returns {Promise} Resolves when we have recalculated posts.
*/
function recalculatePosts() {
let progressElement = jQuery( ".yoast-js-prominent-words-progress-current" );
let rootUrl = settings.restApi.root;
return new Promise( ( resolve ) => {
let postsCalculation = new ProminentWordCalculation( {
totalPosts: settings.amount.total,
recalculateAll: true,
rootUrl: rootUrl,
nonce: settings.restApi.nonce,
allProminentWordIds: settings.allWords,
listEndpoint: rootUrl + "wp/v2/posts/",
prominentWordCache,
} );
postsCalculation.on( "processedPost", ( postCount ) => {
progressElement.html( postCount );
} );
postsCalculation.start();
// Free up the variable to start another recalculation.
postsCalculation.on( "complete", resolve );
} );
}
/**
* Recalculates pages
*
* @returns {Promise} Resolves when we have recalculated pages.
*/
function recalculatePages() {
let progressElement = jQuery( ".yoast-js-prominent-words-pages-progress-current" );
let rootUrl = settings.restApi.root;
return new Promise( ( resolve ) => {
let pagesCalculation = new ProminentWordCalculation( {
totalPosts: settings.amountPages.total,
recalculateAll: true,
rootUrl: rootUrl,
nonce: settings.restApi.nonce,
allProminentWordIds: settings.allWords,
listEndpoint: rootUrl + "wp/v2/pages/",
prominentWordCache,
} );
pagesCalculation.on( "processedPost", ( postCount ) => {
progressElement.html( postCount );
} );
pagesCalculation.start();
// Free up the variable to start another recalculation.
pagesCalculation.on( "complete", resolve );
} );
}
/**
* Shows completion to the user
*
* @returns {void}
*/
function showCompletion() {
progressContainer.hide();
completedContainer.show();
}
/**
* Start recalculating.
*
* @returns {void}
*/
function startRecalculating() {
progressContainer.show();
let restApi = new RestApi( { rootUrl: settings.restApi.root, nonce: settings.restApi.nonce } );
prominentWordCache = new ProminentWordCache();
let populator = new ProminentWordCachePopulator( { cache: prominentWordCache, restApi: restApi } );
populator.populate()
.then( recalculatePosts )
.then( recalculatePages )
.then( showCompletion );
}
/**
* Initializes the site wide analysis tab.
*
* @returns {void}
*/
function init() {
jQuery( ".yoast-js-calculate-prominent-words--all" ).on( "click", function() {
startRecalculating();
jQuery( this ).hide();
} );
progressContainer = jQuery( ".yoast-js-prominent-words-progress" );
progressContainer.hide();
completedContainer = jQuery( ".yoast-js-prominent-words-completed" );
completedContainer.hide();
}
jQuery( init );