| 1 |
weForms.routeComponents.Tools = { |
| 2 |
template: '#tmpl-wpuf-tools', |
| 3 |
mixins: [weForms.mixins.Tabs, weForms.mixins.Loading], |
| 4 |
data: function() { |
| 5 |
return { |
| 6 |
activeTab: 'export', |
| 7 |
exportType: 'all', |
| 8 |
loading: false, |
| 9 |
forms: [], |
| 10 |
importButton: 'Import', |
| 11 |
currentStatus: 0, |
| 12 |
responseMessage: '', |
| 13 |
logs: [], |
| 14 |
ximport: { |
| 15 |
current: '', |
| 16 |
title: '', |
| 17 |
action: '', |
| 18 |
message: '', |
| 19 |
type: 'updated', |
| 20 |
refs: {} |
| 21 |
} |
| 22 |
}; |
| 23 |
}, |
| 24 |
|
| 25 |
computed: { |
| 26 |
|
| 27 |
isInitial: function() { |
| 28 |
return this.currentStatus === 0; |
| 29 |
}, |
| 30 |
|
| 31 |
isSaving: function() { |
| 32 |
return this.currentStatus === 1; |
| 33 |
}, |
| 34 |
|
| 35 |
isSuccess: function() { |
| 36 |
return this.currentStatus === 2; |
| 37 |
}, |
| 38 |
|
| 39 |
isFailed: function() { |
| 40 |
return this.currentStatus === 3; |
| 41 |
}, |
| 42 |
|
| 43 |
hasRefs: function() { |
| 44 |
return Object.keys(this.ximport.refs).length; |
| 45 |
}, |
| 46 |
hasLogs: function() { |
| 47 |
return Object.keys(this.logs).length; |
| 48 |
} |
| 49 |
}, |
| 50 |
|
| 51 |
created: function() { |
| 52 |
this.fetchData(); |
| 53 |
this.fetchLogs(); |
| 54 |
}, |
| 55 |
|
| 56 |
methods: { |
| 57 |
fetchLogs: function(target) { |
| 58 |
var self = this; |
| 59 |
|
| 60 |
self.startLoading(target); |
| 61 |
|
| 62 |
wp.ajax.send( 'weforms_read_logs', { |
| 63 |
data: { |
| 64 |
_wpnonce: weForms.nonce |
| 65 |
}, |
| 66 |
success: function(response) { |
| 67 |
self.stopLoading(target); |
| 68 |
self.logs = response; |
| 69 |
},error: function(){ |
| 70 |
self.stopLoading(target); |
| 71 |
self.logs = []; |
| 72 |
} |
| 73 |
}); |
| 74 |
}, |
| 75 |
deleteLogs: function(target) { |
| 76 |
var self = this; |
| 77 |
|
| 78 |
if ( confirm('Are you sure to clear the log file?') ) { |
| 79 |
|
| 80 |
self.startLoading(target); |
| 81 |
|
| 82 |
wp.ajax.send( 'weforms_delete_logs', { |
| 83 |
data: { |
| 84 |
_wpnonce: weForms.nonce |
| 85 |
}, |
| 86 |
success: function(response) { |
| 87 |
self.logs = []; |
| 88 |
self.stopLoading(target); |
| 89 |
self.fetchLogs(); |
| 90 |
}, |
| 91 |
error: function(response) { |
| 92 |
self.logs = []; |
| 93 |
self.stopLoading(target); |
| 94 |
self.fetchLogs(); |
| 95 |
} |
| 96 |
}); |
| 97 |
} |
| 98 |
}, |
| 99 |
stopLoading: function(target){ |
| 100 |
target = $(target); |
| 101 |
|
| 102 |
if (target.is('button')) { |
| 103 |
target.removeClass('updating-message').find('span').show(); |
| 104 |
}else if(target.is('span')){ |
| 105 |
target.show().parent().removeClass('updating-message'); |
| 106 |
} |
| 107 |
}, |
| 108 |
startLoading: function(target){ |
| 109 |
|
| 110 |
target = $(target); |
| 111 |
|
| 112 |
if (target.is('button')) { |
| 113 |
target.addClass('updating-message').find('span').hide(); |
| 114 |
}else if(target.is('span')){ |
| 115 |
target.hide().parent().addClass('updating-message'); |
| 116 |
} |
| 117 |
}, |
| 118 |
fetchData: function() { |
| 119 |
var self = this; |
| 120 |
|
| 121 |
this.loading = true; |
| 122 |
|
| 123 |
wp.ajax.send( 'weforms_form_names', { |
| 124 |
data: { |
| 125 |
_wpnonce: weForms.nonce |
| 126 |
}, |
| 127 |
success: function(response) { |
| 128 |
// console.log(response); |
| 129 |
self.loading = false; |
| 130 |
self.forms = response; |
| 131 |
}, |
| 132 |
error: function(error) { |
| 133 |
self.loading = false; |
| 134 |
alert(error); |
| 135 |
} |
| 136 |
}); |
| 137 |
}, |
| 138 |
|
| 139 |
importForm: function( fieldName, fileList, event ) { |
| 140 |
if ( !fileList.length ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
var formData = new FormData(); |
| 145 |
var self = this; |
| 146 |
|
| 147 |
formData.append( fieldName, fileList[0], fileList[0].name); |
| 148 |
formData.append( 'action', 'weforms_import_form' ); |
| 149 |
formData.append( '_wpnonce', weForms.nonce ); |
| 150 |
|
| 151 |
self.currentStatus = 1; |
| 152 |
|
| 153 |
$.ajax({ |
| 154 |
type: "POST", |
| 155 |
url: window.ajaxurl, |
| 156 |
data: formData, |
| 157 |
processData: false, |
| 158 |
contentType: false, |
| 159 |
success: function(response) { |
| 160 |
self.responseMessage = response.data; |
| 161 |
|
| 162 |
if ( response.success ) { |
| 163 |
self.currentStatus = 2; |
| 164 |
} else { |
| 165 |
self.currentStatus = 3; |
| 166 |
} |
| 167 |
|
| 168 |
// reset the value |
| 169 |
$(event.target).val(''); |
| 170 |
}, |
| 171 |
|
| 172 |
error: function(errResponse) { |
| 173 |
console.log(errResponse); |
| 174 |
self.currentStatus = 3; |
| 175 |
}, |
| 176 |
|
| 177 |
complete: function() { |
| 178 |
$(event.target).val(''); |
| 179 |
} |
| 180 |
}); |
| 181 |
}, |
| 182 |
|
| 183 |
importx: function(target, plugin) { |
| 184 |
var button = $(target); |
| 185 |
var self = this; |
| 186 |
|
| 187 |
self.ximport.current = plugin; |
| 188 |
button.addClass('updating-message').text( button.data('importing') ); |
| 189 |
|
| 190 |
wp.ajax.send( 'weforms_import_xforms_' + plugin, { |
| 191 |
data: { |
| 192 |
_wpnonce: weForms.nonce |
| 193 |
}, |
| 194 |
|
| 195 |
success: function(response) { |
| 196 |
self.ximport.title = response.title; |
| 197 |
self.ximport.message = response.message; |
| 198 |
self.ximport.action = response.action; |
| 199 |
self.ximport.refs = response.refs; |
| 200 |
}, |
| 201 |
|
| 202 |
error: function(error) { |
| 203 |
alert(error.message); |
| 204 |
}, |
| 205 |
|
| 206 |
complete: function() { |
| 207 |
button.removeClass('updating-message').text( button.data('original') ); |
| 208 |
} |
| 209 |
}); |
| 210 |
}, |
| 211 |
|
| 212 |
replaceX: function(target, type) { |
| 213 |
var button = $(target); |
| 214 |
var self = this; |
| 215 |
|
| 216 |
button.addClass('updating-message'); |
| 217 |
|
| 218 |
wp.ajax.send( 'weforms_import_xreplace_' + self.ximport.current, { |
| 219 |
data: { |
| 220 |
type: type, |
| 221 |
_wpnonce: weForms.nonce |
| 222 |
}, |
| 223 |
|
| 224 |
success: function(response) { |
| 225 |
if ( 'replace' === button.data('type') ) { |
| 226 |
alert( response ); |
| 227 |
} |
| 228 |
}, |
| 229 |
|
| 230 |
error: function(error) { |
| 231 |
alert( error ); |
| 232 |
}, |
| 233 |
|
| 234 |
complete: function() { |
| 235 |
self.ximport.current = ''; |
| 236 |
self.ximport.title = ''; |
| 237 |
} |
| 238 |
}); |
| 239 |
} |
| 240 |
} |
| 241 |
}; |
| 242 |
|