Created
October 12, 2017 13:15
-
-
Save Tafhim/a7e35c360676d66a9038501fef9c5500 to your computer and use it in GitHub Desktop.
Front end methods for the bbPress to DWQA conversion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var author_list; | |
var author_index = 0; | |
var forum_index = 0; | |
var topic_index = 0; | |
var reply_index = 0; | |
var forum_list = []; | |
var topic_list = []; | |
var reply_list = []; | |
var json; | |
function handleData(json_data) { | |
//console.log(json_data); | |
authors = jQuery(json_data.authors)[0]; | |
forum_list = json.forums; | |
author_list = []; | |
jQuery.each(authors, function(k, v) { | |
if (k != '') | |
author_list.push(v); | |
}); | |
jQuery('#author-table-cell').html(0 + '/' + author_list.length); | |
author_supplier(); | |
} | |
function locate_author_id(email_key) { | |
return json.authors[email_key].new_id; | |
} | |
function author_supplier() { | |
if ( author_index >= author_list.length ) { | |
//console.log( author_list ); | |
jQuery(document).trigger('imbbp2dw_authors_ready'); | |
return; | |
} | |
jQuery.ajax({ | |
url: admin_url + '?imbbp2dw_ensure_author=1', | |
method: 'POST', | |
data: { imbbp2dw_author_details: JSON.stringify(author_list[author_index]) }, | |
success: function(res) { | |
try { | |
author_data = JSON.parse(res); | |
} catch (e) { | |
console.log('Author creation failure. ' + e); | |
} | |
if (author_data.status == 'error') { | |
console.log(author_data.message); | |
} else { | |
jQuery(document).trigger('imbb2dw_author_ready', [author_data.id]); | |
} | |
} | |
}); | |
} | |
function forum_supplier() { | |
if ( forum_index >= forum_list.length ) { | |
jQuery(document).trigger('imbbp2dw_forums_ready'); | |
return; | |
} | |
jQuery.ajax({ | |
url: admin_url + '?imbbp2dw_ensure_forum=1', | |
method: 'POST', | |
data: { imbbp2dw_forum_details: JSON.stringify(forum_list[forum_index].info) }, | |
success: function(res) { | |
try { | |
forum_data = JSON.parse(res); | |
} catch (e) { | |
console.log('Forum creation failure. ' + e); | |
} | |
if (forum_data.status == 'error') { | |
console.log(forum_data.message); | |
} else { | |
jQuery(document).trigger('imbbp2dw_forum_ready', [forum_data.id]); | |
} | |
} | |
}); | |
} | |
function topic_supplier() { | |
if ( topic_index >= topic_list.length ) { | |
jQuery(document).trigger('imbbp2dw_topics_ready'); | |
return; | |
} | |
jQuery.ajax({ | |
url: admin_url + '?imbbp2dw_ensure_topic=1', | |
method: 'POST', | |
data: { imbbp2dw_topic_details: JSON.stringify(topic_list[topic_index].info) }, | |
success: function(res) { | |
try { | |
topic_data = JSON.parse(res); | |
} catch (e) { | |
console.log('Topic creation failure. ' + e); | |
} | |
if (topic_data.status == 'error') { | |
console.log(topic_data); | |
} else { | |
jQuery(document).trigger('imbbp2dw_topic_ready', [topic_data.id]); | |
} | |
} | |
}); | |
} | |
function reply_supplier() { | |
if ( reply_index >= reply_list.length ) { | |
jQuery(document).trigger('imbbp2dw_replies_ready'); | |
return; | |
} | |
jQuery.ajax({ | |
url: admin_url + '?imbbp2dw_ensure_reply=1', | |
method: 'POST', | |
data: { imbbp2dw_reply_details: JSON.stringify(reply_list[reply_index].info) }, | |
success: function(res) { | |
try { | |
reply_data = JSON.parse(res); | |
} catch (e) { | |
console.log('Reply creation failure. ' + e); | |
} | |
if (reply_data.status == 'error') { | |
console.log(reply_data); | |
} else { | |
jQuery(document).trigger('imbbp2dw_reply_ready', [reply_data.id]); | |
} | |
} | |
}); | |
} | |
jQuery(document).ready(function($) { | |
jQuery(document).on('imbb2dw_author_ready', function(e, id) { | |
author_list[author_index].new_id = id; | |
em = author_list[author_index].user_email; | |
json.authors[em].new_id = id; | |
author_index++; | |
$('#author-table-cell').html(author_index + '/' + author_list.length); | |
author_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_authors_ready', function(e) { | |
forum_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_forum_ready', function(e, id) { | |
forum_list[forum_index].info.new_id = id; | |
for (i=0 ; i<forum_list[forum_index].topics.length ; i++) { | |
forum_list[forum_index].topics[i].info.parent_forum = id; | |
forum_list[forum_index].topics[i].info.author_id = locate_author_id(forum_list[forum_index].topics[i].info.author_email); | |
topic_list.push(forum_list[forum_index].topics[i]); | |
forum_list[forum_index].topics[i] = null; | |
} | |
forum_index++; | |
$('#forums-table-cell').html(forum_index + '/' + forum_list.length); | |
forum_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_forums_ready', function(e) { | |
topic_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_topic_ready', function(e, id) { | |
topic_list[topic_index].info.new_id = id; | |
for (i=0 ; i<topic_list[topic_index].replies.length ; i++) { | |
topic_list[topic_index].replies[i].info.parent_topic = id; | |
topic_list[topic_index].replies[i].info.parent_topic_resolved = (topic_list[topic_index].info['root_status'] == 'closed' ? 'yes' : 'no'); | |
topic_list[topic_index].replies[i].info.author_id = locate_author_id(topic_list[topic_index].replies[i].info.author_email); | |
reply_list.push(topic_list[topic_index].replies[i]); | |
topic_list[topic_index].replies[i] = null; | |
} | |
topic_index++; | |
$('#topics-table-cell').html(topic_index + '/' + topic_list.length); | |
topic_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_topics_ready', function(e) { | |
reply_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_reply_ready', function(e, id) { | |
reply_list[reply_index].info.new_id = id; | |
reply_index++; | |
$('#replies-table-cell').html(reply_index + '/' + reply_list.length); | |
reply_supplier(); | |
}); | |
jQuery(document).on('imbbp2dw_replies_ready', function(e) { | |
json = forum_list = topic_list = reply_list = author_list = forum_index = topic_index = author_index = null; | |
alert( 'All done!!' ); | |
}); | |
$(document).on('submit', '#im_bbp2dw_json_form', function(e) { | |
e.preventDefault(); | |
file_input = $('#im_json_file'); | |
//console.log(file_input); | |
file = file_input[0].files[0]; | |
fr = new FileReader(); | |
fr.onload = (function (theFile) { | |
return function (e) { | |
//console.log('e readAsText = ', e); | |
//console.log('e readAsText target = ', e.target); | |
try { | |
json = JSON.parse(e.target.result); | |
//console.log('json global var has been set to parsed json of this file here it is unevaled = \n' + JSON.stringify(json)); | |
handleData(json); | |
} catch (ex) { | |
alert('ex when trying to parse json = ' + ex); | |
} | |
} | |
})(file); | |
//console.log(fr.readAsText(file)); | |
fr.readAsText(file); | |
//console.log(JSON.stringify(json)); | |
//console.log(fr.readAsDataURL(file)); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment