(function($) {
$.fn.forum = function(settings){
var defaults = {
tag: "test",
url: "http://api.tagmalion.com/",
template: "http://jsrepeater.devprog.com/forumtemplates/basic/basic.js",
host: null
};
var options = $.extend(defaults, settings);
if ($.fn.forum.controllerInstance == null) {
$.fn.forum.controllerInstance = new $.fn.forum.controller(options);
}
return this.each(function() {
$.fn.forum.controllerInstance.settings = options;
$.fn.forum.controllerInstance.container = this;
if (options.template != null)
{
$.getJSON(options.template + "?cachebust=" + new Date() + "&format=json&jsoncallback=?");
} else {
$.fn.forum.controllerInstance.initialise(this);
}
//$('head').append('')
//this.innerHTML = $.fn.forum.controllerInstance.settings.template;
//$.fn.forum.controllerInstance.initialise(this);
});
}
$.forum = {};
$.forum.insertTemplate = function(data){
$('head').append('')
$.fn.forum.controllerInstance.container.innerHTML = data.template;
$.fn.forum.controllerInstance.initialise();
}
$.fn.forum.controllerInstance = null;
$.fn.forum.controller = function(settings){
this.settings = settings;
}
var controller = $.fn.forum.controller;
controller.prototype.initialise = function() {
$('.tmf_listThreads').bind('click', function(e) { $.fn.forum.controllerInstance.listPosts(); } );
$('.tmf_addPost').bind('click', function(e) { $.fn.forum.controllerInstance.showAddPost(); } );
$('.tmf_cancelPost').bind('click', function(e) { $.fn.forum.controllerInstance.cancelAddPost(this.form); e.stopPropagation(); e.preventDefault(); return false; } );
$('.tmf_cancelReply').bind('click', function(e) { $.fn.forum.controllerInstance.cancelReplyPost(this.form); e.stopPropagation(); e.preventDefault(); return false; } );
$('.tmf_post').bind('click', function(e) { $.fn.forum.controllerInstance.post(this.form); e.stopPropagation(); e.preventDefault(); return false; } );
$('.tmf_reply').bind('click', function(e) { $.fn.forum.controllerInstance.reply(this.form); e.stopPropagation(); e.preventDefault(); return false; } );
$('.tmf_renewVerification').bind('click', function(e) { $.fn.forum.controllerInstance.requestVerification(); } );
this.hideContainers();
this.listPosts();
}
controller.prototype.showAddPost = function() {
this.requestVerification();
this.hideContainers();
$('.tmf_postView').show();
}
controller.prototype.showReplyPost = function(id, threadId) {
this.requestVerification();
this.hideContainers();
$('#tmf_ReplyToID').attr("value", id);
$('#tmf_threadId').attr("value", threadId);
$('.tmf_replyView').show();
}
controller.prototype.cancelAddPost = function(frm) {
this.clearForm(frm);
this.hideContainers();
$('.tmf_listView').show();
}
controller.prototype.cancelReplyPost = function(frm) {
this.clearForm(frm);
this.hideContainers();
$('.tmf_threadView').show();
}
controller.prototype.post = function(frm){
if (!this.validateForm(frm)) { return false; }
var self = this;
var host = (this.settings.host != null) ? this.settings.host : "";
$.getJSON(this.settings.url + "forum/post?cachebust=" + new Date() + "&format=json&jsoncallback=?",
{"tag" : this.settings.tag, "name" : frm.tmf_name.value, "subject" : frm.tmf_subject.value,
"content" : frm.tmf_content.value, "verificationID" : frm.tmf_verificationID.value, "verificationKey" : frm.tmf_verificationKey.value, "host" : host},
function(data) {
self.checkPostResponse(data, function(data) { setTimeout(function() { self.clearForm(frm); self.listPosts(); }, 1000); });
});
}
controller.prototype.reply = function(frm){
if (!this.validateForm(frm)) { return false; }
var self = this;
var id = frm.tmf_ReplyToID.value;
var threadId = frm.tmf_threadId.value;
var host = (this.settings.host != null) ? this.settings.host : "";
$.getJSON(this.settings.url + "forum/reply?cachebust=" + new Date() + "&format=json&jsoncallback=?",
{"id" : id, "name" : frm.tmf_name.value, "subject" : frm.tmf_subject.value,
"content" : frm.tmf_content.value, "verificationID" : frm.tmf_verificationID.value, "verificationKey" : frm.tmf_verificationKey.value, "host" : host},
function(data) {
self.checkPostResponse(data, function(data) { setTimeout(function() { self.clearForm(frm); self.displayThread(threadId); }, 1000); });
});
}
controller.prototype.checkPostResponse = function(data, callback){
if (data.Error != undefined){
if (data.Error == 'Invalid Verification'){
this.requestVerification();
alert("Wrong verification code, please try again");
}
else {
alert(data.Error);
}
} else {
callback(data);
}
}
controller.prototype.validateForm = function(frm){
if (frm.tmf_name.value.length == 0) { alert("Name required"); return false; }
if (frm.tmf_subject.value.length == 0) { alert("Subject required"); return false; }
if (frm.tmf_content.value.length == 0) { alert("Content required"); return false; }
if (frm.tmf_verificationKey.value.length == 0) { alert("Please enter the verification code"); return false; }
return true;
}
controller.prototype.clearForm = function(frm){
frm.tmf_name.value = "";
frm.tmf_subject.value = "";
frm.tmf_content.value = "";
frm.tmf_verificationKey.value = "";
}
controller.prototype.hideContainers = function(){
$('.tmf_listView, .tmf_threadView, .tmf_postView, .tmf_replyView').hide();
}
controller.prototype.requestVerification = function(){
var self = this;
$.getJSON(this.settings.url + "verify/request?cachebust=" + new Date() + "&format=json&jsoncallback=?",
{},
function(Data) {
$('.tmf_verificationImage').attr('src', self.settings.url + "verify/image?cachebust=" + new Date() + "&id=" + Data.id);
$('.tmf_verificationID').attr('value', Data.id);
});
}
controller.prototype.listPosts = function() {
var self = this;
$.getJSON(this.settings.url + "forum/list?cachebust=" + new Date() + "&format=json&jsoncallback=?",
{"tag" : this.settings.tag},
function(Data) {
self.hideContainers();
$('.tmf_listView').show();
$('#tmf_ListPosts').fillTemplate(Data);
$('.tmf_threadLink').bind('click', function(e) { $.fn.forum.controllerInstance.displayThread($(this).attr('tmf_dataId')); } );
});
}
controller.prototype.displayThread = function(id){
var self = this;
$.getJSON(this.settings.url + "forum/thread?cachebust=" + new Date() + "&format=json&jsoncallback=?",
{"id" : id},
function(Data) {
self.hideContainers();
$('.tmf_threadView').show();
$('#tmf_ListThread').fillTemplate({"replies" : Data});
$('.tmf_addReply').bind('click', function(e) { $.fn.forum.controllerInstance.showReplyPost($(this).attr('tmf_dataId'), $(this).attr('tmf_threadId')); e.stopPropagation(); e.preventDefault(); return false; } );
});
}
})(jQuery);