-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadmin.js
165 lines (147 loc) · 4.33 KB
/
admin.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
(function ($) {
$(document).ready(function () {
var features = $('.wprds-feature')
var indexingOptions = $('.indexing-options')
var startIndexing = $('.start-indexing')
var resumeIndexing = $('.resume-indexing')
features.on( 'click', '.save-settings', function( event ) {
event.preventDefault();
var $this = $( this )
if ( $this.hasClass( 'disabled' ) ) {
return;
}
var feature = $this.attr( 'data-feature' );
var $feature = features.find( '.wprds-feature-' + feature );
var settings = {};
var $settings = $feature.find('.setting-field');
$settings.each(function() {
var type = $( this ).attr( 'type' );
var name = $( this ).attr( 'data-field-name' );
var value = $( this ).attr( 'value' );
if ( 'radio' === type ) {
if ( this.checked ) {
settings[ name ] = value;
}
} else {
settings[ name ] = value;
}
});
console.log(settings)
$this.parent().addClass( 'saving' );
$.ajax( {
method: 'post',
url: ajaxurl,
data: {
action: 'wp_redisearch_save_feature',
feature: feature,
nonce: wpRds.nonce,
settings: settings
}
} ).done( function( response ) {
setTimeout( function() {
$this.parent().removeClass( 'saving' );
if ( '1' === settings.active ) {
$feature.addClass( 'feature-active' );
} else {
$feature.removeClass( 'feature-active' );
}
if ( response.data.reindex ) {
dropIndex();
}
}, 700 );
} ).error( function() {
setTimeout( function() {
$feature.removeClass( 'saving' );
$feature.removeClass( 'feature-active' );
}, 700 );
} );
});
if (indexingOptions.attr('data-num-docs') == indexingOptions.attr('data-num-posts')) {
resumeIndexing.css('display', 'none')
}
startIndexing.click(function (e) {
e.preventDefault()
startIndexing.css('display', 'none')
dropIndex()
})
resumeIndexing.click(function (e) {
e.preventDefault()
resumeIndexing.css('display', 'none')
addToIndex()
})
function dropIndex() {
let postData = {
action: 'wp_redisearch_drop_index'
}
jQuery.ajax({
data: postData,
type: "post",
url: wpRds.ajaxUrl
})
.done(function (res) {
if (res.data) {
addToIndex()
}
})
.error(function (err) {
console.log(err)
})
}
function addToIndex() {
let postData = {
action: 'wp_redisearch_add_to_index'
}
jQuery.ajax({
data: postData,
type: "post",
url: wpRds.ajaxUrl
})
.done(function (res) {
if (res.data.offset < res.data.found_posts) {
updateProgressBar(res.data.offset, res.data.found_posts)
addToIndex()
} else {
updateProgressBar(res.data.found_posts, res.data.found_posts)
startIndexing.css('display', 'inline-block')
writeToDisk()
}
})
.error(function (err) {
console.log(err)
})
}
function writeToDisk() {
let postData = {
action: 'wp_redisearch_write_to_disk'
}
jQuery.ajax({
data: postData,
type: "post",
url: wpRds.ajaxUrl
})
.done(function (res) {
console.log('Data successfully written to the disk ', res.data)
})
.error(function (err) {
console.log('Error writing to the disk ', err)
})
}
function updateProgressBar(numDocs, numPosts) {
let pBar = $("#indexBar")
let statNumDoc = $('#statNumDoc')
let statNumPosts = $('#statNumPosts')
statNumDoc.text(numDocs)
statNumPosts.text(numPosts)
let width = (numDocs / numPosts) * 100
pBar.css('width', width + '%')
}
renderProgressBar()
function renderProgressBar() {
let pBar = $("#indexBar")
let numDocs = pBar.attr('data-num-docs')
let numPosts = pBar.attr('data-num-posts')
let width = (numDocs / numPosts) * 100
pBar.css('width', width + '%')
}
});
} )( jQuery );