(function($) { // Widget // [Widget][StartCol][Widget1][Widget2][...][EndCol][Widget1][Widget2][...] var startCol= -1; var col; var temp = ''; var activeWidget = -1; $.widgetize = function(options) { setHeight(); var defaults = { // Elements container:'.widgetView', shelf:'.widgetShelf', shelfName:'shelf', widget:'.widget', noShelf:'.noShelf', handle:'.widgetHandle', icon:'.icon', content:'.content', limit:'.limit', // Styles ghostClass:'ghostWidget' }; var opt = $.extend({}, defaults, options); var startFunc = function start(event, ui) { col = new Array(); col.pop(); activeWidget = $(ui.item).attr('name'); col.push(activeWidget); col.push($(opt.content,ui.item).parent().parent().attr('id')); temp = $(opt.content,ui.item).parent().parent().attr('id'); strStartCol = "#"+temp; $(strStartCol).children().each(function(){ col.push($(this).attr('name')); }); $(opt.content,ui.item).hide(); //$(opt.icon,ui.item).css('display','inline'); $(opt.icon,ui.item).show(); }; var overFunc = function over(event, ui) { $(opt.content,ui.item).hide(); //$(opt.icon,ui.item).css('display','inline'); $(opt.icon,ui.item).show(); }; var outFunc = function out(event, ui) { temp = ''; // Event is cancelable only when the widget if the widget is dropped if(!event.cancelable) { // Hide icon and show content if the content is not empty /*if(!isEmpty(ui.item[0])) { $(opt.icon,ui.item).css('display','none'); $(opt.content,ui.item).css('display','block'); }*/ } for ( var i in col ) { if(col[i] != null) temp +=col[i]+" "; } //console.debug(temp); $.post("./xhr/save_widget_config.php", {id: idEntity,type:entityType, widgetdata:temp}, function(data) { if(data == "Htos") window.location.replace("../login_page.php"); }); setHeight(); writeEmptyMessage() }; var stopFunc = function stop(event, ui) { w = ui.item[0]; temp = ''; if(inShelf(w)) { // Send query to remove the widget from UI } else { // Hide icon and show content $(opt.icon,ui.item).hide(); $(opt.content,ui.item).show(); // Send query to add/edit widget in UI // If the widget's content is empty, load it if(isEmpty(w)) $(opt.content, w).load("./xhr/load_widget.php?w="+$(w).attr('name'), function(responseText, textStatus, XMLHttpRequest) { //console.debug('Iz naow filled!'); }); // [Widget][StartCol][Widget1][Widget2][...][EndCol][Widget1][Widget2][...] col.push($(opt.widget, w.parentNode).parent().attr('id')); $(opt.widget, w.parentNode).each(function(index) { col.push($(this).attr('name')); }); for ( var i in col ) { if(col[i] != null) temp +=col[i]+" "; } //console.debug(temp); $.post("./xhr/save_widget_config.php", {id: idEntity,type:entityType, widgetdata:temp}, function(data) { if(data == "Htos") window.location.replace("../login_page.php"); }); setHeight(); writeEmptyMessage(); } }; // Allow widgets to be dropped and sorted in containers $(opt.container+':not('+opt.shelf+')').sortable({ placeholder:opt.ghostClass, connectWith:opt.container, items:opt.widget+':not('+opt.noShelf+')', containment:opt.limit, //revert:true, handle:opt.handle, start: startFunc, stop: stopFunc }); $(opt.container).disableSelection(); // Prevent the widget's text from being selected // Allow widgets to be dropped in the shelf $(opt.shelf).sortable({ placeholder:opt.ghostClass, connectWith:opt.container, items:opt.widget, containment:opt.limit, start: startFunc, over: overFunc, out: outFunc, stop: stopFunc }); $(opt.shelf).disableSelection(); // Prevent the widget's text from being selected function inShelf(w) { return $(w.parentNode).attr('name')== opt.shelfName; } function isEmpty(w) { return $(opt.content, w).html().length == 0; } function setHeight() { var tempHeight; $("#col1").css('height', 'auto'); $("#col2").css('height', 'auto'); $("#col3").css('height', 'auto'); tempHeight = $("#col1").height(); if($("#col2").height() > tempHeight) tempHeight = $("#col2").height(); if($("#col3").height() > tempHeight) tempHeight = $("#col3").height(); $("#col1").css('height', tempHeight); $("#col2").css('height', tempHeight); $("#col3").css('height', tempHeight); } function writeEmptyMessage() { $("#widget_empty_msg").hide(); if ($("#widget_list").children("div").size() == 0) { $("#widget_empty_msg").show(); } } }; // widgetize })(jQuery);