(function(){
var loading = null,
site_url = null;
$("document").ready(function(){
loading = $("#loading");
bindHandlers();
})
var bindHandlers = function(){
$("#find-channels").on("click",function(){
site_url = $("#site-url-wrapper input[type=text]").val();
$("#site-url-wrapper").fadeOut();
getChannelList();
})
}
var getChannelList = function(){
displayLoading(true,"Fetching channels from " + site_url);
$.ajax({
url : "/admin/feeds/get_channel_list",
data : {"url" : site_url},
dataType : "json",
type : "get"
}).done(function(channels){
$.each(channels.channels,function(index,channel){
var ch = $("
");
ch.on("click",function(){
getFeedList(channel);
})
$("#channels").append(ch);
})
setTimeout(function(){
displayLoading(false);
setTimeout(function(){$("#channels").fadeIn();},500);
},1000);
})
}
var getFeedList = function(channel){
$("#channels").fadeOut(function(){
$("#channels").html("");
displayLoading(true,"Fetching feed list for " + channel.title);
$.ajax({
url : "/admin/feeds/get_feed_list",
data : {"url" : site_url, "channel" : channel.key, "feed_list_url" : channel.url},
dataType : "json",
type : "get"
}).done(function(data){
$.each(data.feeds, function(index,feed){
var klass = (feed.subscribed ? "active" : "");
var f = $("
" + feed.title_translations[I18n.locale] + "
");
f.on("click",function(){
if($(this).hasClass("active")){
if(confirm("Are you sure, you want to unsubscribe from " + feed.title_translations[I18n.locale] + "?")){
unsubscribeFeed($(this),feed);
}
}else{
subscribeFeed($(this),feed,channel);
}
})
$("#channels").append(f);
})
setTimeout(function(){
displayLoading(false);
setTimeout(function(){$("#channels").fadeIn();},500);
},1000);
})
})
}
var unsubscribeFeed = function(dom,feed){
dom.removeClass("active");
$.ajax({
url : "/admin/feeds/unsubscribe",
data : {"feed_uid": feed.uid},
dataType : "json",
type : "post"
}).done(function(){
$("#selectCategoryModal").modal("hide");
})
}
var subscribeFeed = function(dom,feed,channel){
var subscribe_button = $("#subscribe-button"),
select = $("#local-category");
$("#selectCategoryModal").modal("show");
subscribe_button.attr("disabled","disabled");
$.ajax({
url : "/admin/feeds/get_category_list",
data : {"channel" : channel.key},
dataType : "json",
type : "get"
}).done(function(data){
select.html("");
$.each(data.categories,function(index,category){
select.append("");
})
subscribe_button.removeAttr("disabled");
})
subscribe_button.unbind("click");
subscribe_button.on("click",function(){
dom.addClass("active");
$.ajax({
url : "/admin/feeds/subscribe",
data : {"url" : site_url, "feed" : feed, "channel" : channel.title, "channel_key" : channel.key, "category" : select.val()},
dataType : "json",
type : "post"
}).done(function(){
$("#selectCategoryModal").modal("hide");
})
})
}
var displayLoading = function(display, msg){
if(display){
loading.find(".progress_msg").text(msg);
loading.fadeIn();
}else{
loading.find(".progress_msg").text("");
loading.fadeOut();
}
}
})();