orbit-basic/app/assets/javascripts/orbitdesktop.js

435 lines
15 KiB
JavaScript
Executable File

// JavaScript Document
//harry
var orbitDesktop = function(dom){
orbitDesktopAPI.apply(this);
o = this;
this.contentHolder = dom;
this.themesettings = "";
this.theme = "default";
// this.themefolder = "themes";
this.transitionTime = 500;
this.currenthtml = "desktop.html";
this.currentface = "home";
this.desktopData = {"home":"","settings":"","work":"","favorite":"","apps_manager":"","sections":""};
this.tp = "";
this.initialize = function(){
var theme = o.theme;
$.getJSON("/desktop/get_desktop_settings",{id:o.desktopId},function(desktopSettings){
if(desktopSettings){
theme = desktopSettings.theme;
o.theme = theme;
loadTheme();
}else{loadTheme();}
})
var loadTheme = function(){
$.getJSON("/"+o.themefolder+"/"+theme+"/settings/"+theme+".json",function(ts){
o.themesettings = eval(ts);
$('head').append( $('<link rel="stylesheet" id="dyn_css" type="text/css" />').attr('href', "/"+o.themefolder+"/"+theme+"/css/"+ts.css));
$(document).ready(function(){o.loadWallpaper();o.bindDesktopEvents();o.loadIconCache();o.initializeDesktop();});
})
}
};
this.changeTheme = function(theme){
o.theme = theme;
$.getJSON("/"+o.themefolder+"/"+theme+"/settings/"+theme+".json",function(ts){
o.themesettings = eval(ts);
$('head').find("#dyn_css").remove();
$('head').append( $('<link rel="stylesheet" id="dyn_css" type="text/css" />').attr('href', "/"+o.themefolder+"/"+theme+"/css/"+ts.css));
o.loadWallpaper();
o.loadIconCache();
})
};
this.bindDesktopEvents = function(){
$("a#d_app_manager").click(function(){
var target = $(this).attr("href");
if(o.currenthtml!=target){
o.desktopData[o.currentface] = $(o.contentHolder).html();
$("#content").hide("drop",o.transitionTime,function(){
o.initializeAppSearch(target);
});
}
});
$("a#d_desktop").click(function(){
var target = $(this).attr("href");
if(o.currenthtml!=target){
o.desktopData[o.currentface] = $(o.contentHolder).html();
$("#content").hide("drop",o.transitionTime,function(){
o.initializeDesktop(target);
});
}
});
$("a#d_sections").click(function(){
var target = $(this).attr("href");
if(o.currenthtml!=target){
o.desktopData[o.currentface] = $(o.contentHolder).html();
$("#content").hide("drop",o.transitionTime,function(){
o.initializeSectionsManager(target);
});
}
});
$("a#d_settings").click(function(){
var target = $(this).attr("href");
if(o.currenthtml!=target){
o.desktopData[o.currentface] = $(o.contentHolder).html();
$("#content").hide("drop",o.transitionTime,function(){
o.initializeSettings(target);
});
}
});
$(window).resize(function(){
var ww = $(window).width();
$("img#thmbackground").attr({"width":ww});
});
var $widget_fn = $('.widget_fn'),$fn_des = $('.fn_des');
$widget_fn.hover(function(){
var fn_name = $(this).find('img').attr('alt'),nth = $(this).parents('.d_cate').index(),des_left = $('.dock_child').eq(nth).width();
$(this).addClass('thmc1');
$fn_des.text(fn_name).css({'top':nth * 60,'left': des_left + 60}).stop(true, true).fadeIn();
},function(){
$(this).removeClass('thmc1');
$fn_des.stop(true, true).fadeOut();
});
};
this.initializeDesktop = function(target){
if(!target)target = "desktop";
o.currenthtml = target;
o.currentface = "home";
var extraspace = 300;
var currentElement, elementParent, targetParent, tempElement, widthcheck;
widthcheck = false;
var bindHandlers = function(){
var groupWrapperWidth = 0;
var groupWrapperHeight = 0;
$(".element").mousedown(function(){
currentElement = $(this);
elementParent = $(this).parent();
})
$(".group").mouseenter(function(){
targetParent = $(this);
})
$(".group").each(function(){groupWrapperWidth+=$(this).width();})
groupWrapperWidth+=200;
groupWrapperHeight = $(".group").height() + 20;
groupWrapperHeight = (groupWrapperHeight > ($(window).height()-extraspace))? $(window).height()-extraspace:groupWrapperHeight;
$("#group_wrapper").css("width",groupWrapperWidth);
$(".grp").sortable({
connectWith: ".grp",
tolerance: 'pointer' ,
revert:true,
stop:function(){
o.tp = targetParent.height();
var currentElementWidth = currentElement.width();
var targetElementWidth = targetParent.width();
if(targetParent.height()>540){
//targetParent.width(currentElement.width());
var newWidth = targetElementWidth+currentElementWidth;
targetParent.css("max-width",newWidth);
var newParentWidth = $("#group_wrapper").width()+currentElementWidth;
$("#group_wrapper").css("width",newParentWidth);
}else if(targetParent.height()<540){
var newWidth = targetElementWidth - (540-targetElementWidth);
targetParent.css("max-width",newWidth);
var newParentWidth = $("#group_wrapper").width()+currentElementWidth;
$("#group_wrapper").css("width",newParentWidth);
}
}
});
$('.tinycanvas').tinyscrollbar({ axis: 'x'});
$("div.scrollbar").hover(function(){
$(this).removeClass('op01');
}, function(){
$(this).addClass('op01');
});
// simple dropdown menu
var $sdm = $('.sdm');
if( !$sdm.children('.sdm_o') ){
return;
} else {
$sdm.hover(function(){
$(this).addClass('thmc2');
}, function(){
$(this).removeClass('thmc2');
});
}
}
var loadTiles = function(id){
$("div#group_wrapper").empty();
$.getJSON("/desktop/getgroups",{sectionid:id},function(groups){
var tilecolors = o.themesettings.tilecolor;
var opacity = ["op05","op06","op07","op08","op09"];
$.each(groups,function(i,group){
group.sort(o.sortJSON("position",true,parseInt));
var $group = $('<div class="group"><ul class="grp ui-sortable"></ul></div>');
$.each(group,function(j,tile){
var tilecolor = tilecolors[Math.floor(Math.random()*tilecolors.length)];
var op = opacity[Math.floor(Math.random()*opacity.length)];
if(tile.data_category == "app")
$li = $('<li class="element '+tile.shape+' hp vp" data-category="'+tile.data_category+'" data-content="'+tile.data_content+'"><span class="tile '+tilecolor+' '+op+'"></span><a href="" class="appicon"><img src="" alt=""></a><h1 class="appname thmtxt">'+tile.title+'</h1></li>');
else
$li = $('<li class="element '+tile.shape+' hp vp" data-category="'+tile.data_category+'" data-content="'+tile.data_content+'"><span class="tile '+tilecolor+' '+op+'"></span><h1 class="appname thmtxt">'+tile.title+'</h1><div class="appholder">test content</div></li>');
$group.find("ul").append($li);
})
$("div#group_wrapper").append($group);
})
bindHandlers();
o.initializeWidgets();
})
}
var loadSectionList = function(){
$.getJSON("/desktop/getsectionlist",{desktopid:o.desktopId},function(sectionlist){
$.each(sectionlist,function(i,section){
var tempstyle="";
if(i==0){
tempstyle = "style='display:none;'";
$("span#section_heading").text(section.name);
}
$("ul#section_list").append($('<li class="section_name" '+tempstyle+'><a class="admtxt hp w2 hh2" onclick="return false;" href="'+section._id+'" >'+section.name+'</a></li>'));
})
bindSecondaryHandlers();
})
}
var bindSecondaryHandlers = function(){
$("ul#section_list li.section_name").click(function(){
$("li.section_name").show();
$(this).hide();
$("span#section_heading").text($(this).text());
loadTiles($(this).find("a").attr("href"));
})
}
if(!o.desktopData[o.currentface]){
$(o.contentHolder).empty().load("/desktop/"+target,function(){
loadTiles(o.sectionId);
loadSectionList();
});
}else{
$(o.contentHolder).html(o.desktopData[o.currentface]);
bindHandlers();
o.initializeWidgets();
bindSecondaryHandlers();
}
};
this.tempFunc = function(th){
//o.notify("This is test notification!!","alert",2)
console.log(th);
$.post("/desktop/save_desktop_settings",{"id":o.desktopId,"theme":th},function(){
o.notify("Settings Saved!!","success");
})
}
this.initializeAppSearch = function(target){
o.currenthtml = target;
o.currentface = "apps_manager";
var searchArray,allApps;
var bindHandlers = function(){
$("#group_wrapper").css("min-width",$(window).width()-100);
$(".group_search").css("width",$(window).width()-200);
//for textbox search
$("#searchbox").focus(function(){$(this).val("");}).keyup(function(e){
if($(this).val()){
$(".search_result").empty();
$("#seperator").hide();
searchArray = $(".element:containsi("+$(this).val()+")");
if(searchArray.length>0){
$("#seperator").show();
searchArray.each(function(){
var $newelement = $('<div class="search element w1 h1 hp vp thmc2" data-category="desktop">'+$(this).html()+'</div>');
$(".search_result").prepend($newelement);
})
}
}else{$("#seperator").hide();$(".search_result").empty();}
}).blur(function(){$(this).val("Search");});
//for Alphabet sorting
$("a#alphabet_sort_btn").click(function(){
switch ($(this).attr("href")){
case "ascending":
$(this).attr("href","descending").find(".thmtxt").text("Alphabet [Z-A]");
allApps = $(".group_search .element").sort(sortAscending);
break;
case "descending":
$(this).attr("href","ascending").find(".thmtxt").text("Alphabet [A-Z]");
allApps = $(".group_search .element").sort(sortDescending);
break;
}
$(".group_search").html(allApps);
return false;
})
$('.tinycanvas').tinyscrollbar({ axis: 'x'});
}
if(!o.desktopData[o.currentface]){
$(o.contentHolder).empty().load("/desktop/"+target,function(){
bindHandlers();
});
}else{
$(o.contentHolder).html(o.desktopData[o.currentface]);
bindHandlers();
}
};
this.initializeSectionsManager = function(target){
o.currenthtml = target;
o.currentface = "sections";
var elementParent,element;
var bindHandlers = function(){
$(".group").width(530);
$("#group_wrapper .element").sortable({
start:function(){
var $elementParent = $(this).parent().parent();
elementParent = $elementParent;
element = $(this);
$elementParent.find("div.section_label ul li").fadeIn(500);
},
stop:function(){
elementParent.find("div.section_label ul li:not(:nth-child(1))").fadeOut(500);
}
});
$(".section_label ul li:not(:nth-child(1))").droppable({
drop:function(){
if($("#"+$(this).attr("data-category")+" .element").length>=24){
o.notify("Section is full.","alert");
}
else
$("#"+$(this).attr("data-category")).append(element);
},
over:function(){
$(this).find('span.tile').removeClass('op06');
},
out:function(){
$(this).find('span.tile').addClass('op06');
}
});
$('.tinycanvas').tinyscrollbar({ axis: 'x'});
var $sdm = $('.sdm');
if( !$sdm.children('.sdm_o') ){
return;
} else {
$sdm.hover(function(){
$(this).addClass('thmc2');
}, function(){
$(this).removeClass('thmc2');
});
}
};
if(!o.desktopData[o.currentface]){
$(o.contentHolder).empty().load("/desktop/"+target,function(){
bindHandlers();
});
}else{
$(o.contentHolder).html(o.desktopData[o.currentface]);
bindHandlers();
}
};
this.initializeSettings = function(target){
o.currenthtml = target;
o.currentface = "settings";
var bindHandlers = function(){
}
var loadThemePage = function(){
var bindHandlers = function(){
$(".theme_list > li").click(function(){
o.changeTheme($(this).find('.theme_name').text());
$(".theme_list > li").removeClass('thmc1').data('clicked',null).find('.theme_name').removeClass('thmtxt');
$(this).addClass('thmc1').data('clicked',true).find('.theme_name').addClass('thmtxt');
od.tempFunc($(this).text());
});
$('.tinycanvas').each(function(){
var h = $(this).parent().height(),
sh = $(this).siblings('.s_tab').height();
$(this).css({'height': h-sh-24}).tinyscrollbar({ axis: 'y'});
});
var $anchor = $('.s_tab a[href*=#]'),
$tabcontent = $('.st_c');
$anchor.each(function(){
$(this).click(function(){
var target = $(this).attr('href');
$anchor.removeClass('thmc2 thmtxt admtxt admbg').addClass('admtxt admbg');
$(this).toggleClass('thmc2 thmtxt admtxt admbg');
$tabcontent.hide();
$(target).show();
$('.tinycanvas').tinyscrollbar_update();
return false;
});
});
$('.color-picker').miniColors();
$('.theme_list li:first-child').data('clicked',true); // this line just for demo
$('.theme_list li').hover(function(){
$(this).addClass('thmc1').find('.theme_name').addClass('thmtxt');
}, function(){
var c = 'thmc1',
t = 'thmtxt';
if( $(this).data('clicked') ){
t = '', c = '';
}
$(this).removeClass(c).find('.theme_name').removeClass(t);
});
}
$("div#panel_r").load("/desktop/settingthemes/",function(){
bindHandlers();
})
}
$(o.contentHolder).empty().load("/desktop/"+target,function(){
loadThemePage();
});
};
this.loadWallpaper = function(wallpaper){
if(!wallpaper)wallpaper = o.themesettings.background;
var ww = $(window).width();
var wh = $(window).height();
if(o.themesettings.background)
$("img#thmbackground").attr({"src":"/"+o.themefolder+"/"+o.theme+"/images/"+wallpaper,"width":ww}).css({"position":"fixed","top":"0px","left":"0px","z-index":"-1","height":"auto"});
else
$("img#thmbackground").attr({"src":"/"+"images/"+wallpaper,"width":ww}).css({"position":"fixed","top":"0px","left":"0px","z-index":"-1","height":"auto"});
$("div#bgover").css({"position":"fixed","top":"0px","left":"0px","z-index":"-1","width":ww,"height":wh});
};
this.loadIconCache = function(){
var imgs = $("ul.docklist img");
$.each(imgs,function(){
var setting_name = $(this).attr("id").replace("_icon","");
$(this).attr("src","/"+o.themefolder+"/"+o.theme+"/images/"+o.themesettings.icons[setting_name])
})
};
this.initializeWidgets = function(){
var elements = $("#group_wrapper li.element");
$.each(elements,function(){
var widget = $(this);
if(widget.attr("data-category")=="widget"){
var widgename = widget.attr("data-content");
$.getScript("/desktop_widgets/"+widgename+"/"+widgename+".js",function(){
widget.find("div.appholder").load("/desktop_widgets/"+widgename+"/index.html.erb");
});
// $(this).find("div.appholder").append( $('<link rel="stylesheet" id="dyn_css" type="text/css" />').attr('href', "/desktop_widgets/"+widgename+"/css/"+widgename+".css"));
}
})
}
o.initialize();
}
orbitDesktop.prototype.themefolder = "themes";
orbitDesktop.prototype.widgetfolder = "desktop_widgets";
orbitDesktop.prototype.desktopId = "1";
orbitDesktop.prototype.notifyImgPath = "temp";
var uselessfunction = function(){
$.post("/desktop/temp_func",{sectionid:"4f83e7bbbd98eb041600001d"});
}