var XWiki=(function(a){a.Dashboard=Class.create({initialize:function(b){this.element=b;
this.gadgetsClass="XWiki.GadgetClass";
this.readMetadata();
this.displayWarning();
this.edited=false;
this.removed=new Array();
this.element.addClassName("dashboard-edit");
this.containers=b.select(".gadget-container");
this.createDragAndDrops();
this.addGadgetsHandlers();
this.addNewGadgetHandler();
this.addNewContainerHandler();
document.observe("xwiki:actions:save",this.saveChanges.bindAsEventListener(this))
},readMetadata:function(){this.editURL=this.element.down(".metadata .editurl").readAttribute("href");
this.removeURL=this.element.down(".metadata .removeurl").readAttribute("href");
this.addURL=this.element.down(".metadata .addurl").readAttribute("href");
this.sourcePage=this.element.down(".metadata .sourcepage").innerHTML;
this.sourceSpace=this.element.down(".metadata .sourcespace").innerHTML;
this.sourceWiki=this.element.down(".metadata .sourcewiki").innerHTML;
this.sourceURL=this.element.down(".metadata .sourceurl").readAttribute("href")
},displayWarning:function(){if(a.currentDocument.page!=this.sourcePage||a.currentDocument.space!=this.sourceSpace||a.currentDocument.wiki!=this.sourceWiki){var e=new Element("div",{"class":"box warningmessage differentsource"});
var c="$escapetool.javascript($msg.get('dashboard.actions.edit.differentsource.information'))";
var b="$escapetool.javascript($msg.get('dashboard.actions.edit.differentsource.warning'))";
var d=new Element("a",{href:this.sourceURL});
d.update(this.sourceWiki+":"+this.sourceSpace+"."+this.sourcePage);
e.insert(c);
e.insert(d);
e.insert(b);
this.element.insert({top:e})
}},_getContainerId:function(b){return b.readAttribute("id").substring(16)
},_getGadgetId:function(b){return b.readAttribute("id").substring(7)
},_getMacroCallComment:function(b){return"<!--"+b+"--><!--stopmacro-->"
},_parseMacroCallComment:function(c){var b=c.indexOf("-->");
if(c.startsWith("<!--startmacro:")&&b>0){return c.substr(4,b-4)
}else{return null
}},_insertPlaceholder:function(b){if(b.down(".gadget")||b.down(".gadget-placeholder")){return
}var c=new Element("div",{"class":"gadget-placeholder"}).update('$escapetool.javascript($msg.get("dashboard.gadget.actions.drop"))');
b.insert(c)
},_removePlaceholder:function(c){var b=c.select(".gadget-placeholder");
b.each(function(d){d.remove()
})
},_doOnStartDrag:function(){this.containers.each(this._insertPlaceholder)
},_doOnEndDrag:function(){this.containers.each(this._removePlaceholder)
},createDragAndDrops:function(){var c=new Array();
this.containers.each(function(e){c.push(e.readAttribute("id"))
});
c.each(function(e){this.makeSortable(e,c,this.onMoveGadget.bind(this))
}.bind(this));
var d=this._doOnStartDrag.bind(this);
var b=this._doOnEndDrag.bind(this);
Draggables.addObserver({onStart:d,onEnd:b})
},makeSortable:function(b,c,d){Sortable.create(b,{tag:"div",only:"gadget",handle:"gadget-title",overlap:"vertical",scroll:window,containment:c,dropOnEmpty:true,constraint:false,ghosting:false,hoverclass:"gadget-container-hover-highlight",onUpdate:d})
},addGadgetsHandlers:function(){this.element.select(".gadget").each(function(f){var b=new Element("div",{"class":"settings",title:'$escapetool.javascript($msg.get("dashboard.gadget.actions.tooltip"))'});
var e=f.down(".gadget-title");
if(!e){return
}var g=new Element("div",{"class":"remove action",title:'$escapetool.javascript($msg.get("dashboard.gadget.actions.delete.tooltip"))'});
g.observe("click",this.onRemoveGadget.bindAsEventListener(this));
var d=new Element("div",{"class":"edit action",title:'$escapetool.javascript($msg.get("dashboard.gadget.actions.edit.tooltip"))'});
d.observe("click",this.onEditGadgetClick.bindAsEventListener(this));
var c=new Element("div",{"class":"settings-menu"});
c.hide();
c.insert(d);
c.insert(g);
b.hide();
e.insert(b);
e.insert(c);
b.observe("click",function(h){c.toggle();
b.toggleClassName("settings-open")
});
f.observe("mouseover",function(){b.show()
});
f.observe("mouseout",function(i){var h=i.relatedTarget||i.toElement;
if((i.element()==f||i.element().up(".gadget"))&&(h==null||h.up(".gadget")==null)){i.stop();
b.hide();
c.hide();
b.removeClassName("settings-open")
}})
}.bind(this))
},addNewGadgetHandler:function(){var b=new Element("div",{"class":"addgadget",title:"$escapetool.javascript($msg.get('dashboard.actions.add.tooltip'))"});
b.update("$escapetool.javascript($msg.get('dashboard.actions.add.button'))");
b.observe("click",this.onAddGadgetClick.bindAsEventListener(this));
var c=this.element.down(".differentsource");
if(c){c.insert({after:b})
}else{this.element.insert({top:b})
}b.insert({after:new Element("div",{"class":"clearfloats"})})
},onAddGadgetClick:function(b){Wysiwyg.onModuleLoad(function(){if(!this.gadgetWizard){this.gadgetWizard=new a.GadgetWizard()
}this.gadgetWizard.add(this.onAddGadgetComplete.bind(this))
}.bind(this))
},onAddGadgetComplete:function(e){var d=this._getMacroCallComment(e.content);
var b=this.containers.length;
var f=this.containers.last().select(".gadget").length+1;
var c=new Hash();
c.set("classname",this.gadgetsClass);
c.set(this.gadgetsClass+"_title",e.title);
c.set(this.gadgetsClass+"_content",d);
c.set("RequiresHTMLConversion",this.gadgetsClass+"_content");
c.set(this.gadgetsClass+"_content_syntax","xwiki/2.0");
c.set(this.gadgetsClass+"_position",b+", "+f);
this._x_notification=new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.actions.add.loading'))","inprogress");
new Ajax.Request(this.addURL,{parameters:c,onSuccess:function(g){window.location.reload()
}.bind(this),onFailure:function(g){var h=g.statusText;
if(g.statusText==""||g.status==12031){h="Server not responding"
}this._x_notification.replace(new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.actions.add.failed'))"+h,"error",{timeout:5}))
}.bind(this),on0:function(g){g.request.options.onFailure(g)
}.bind(this)})
},onEditGadgetClick:function(b){var g=b.element().up(".gadget");
if(g){var c=g.down(".metadata");
if(!c){return
}var j=c.down(".isMacro");
if(j&&j.innerHTML=="true"){var d=this._getGadgetId(g);
var k,e;
var h=c.down(".title");
if(h){k=h.innerHTML
}var l=c.down(".content");
if(l){var f=l.innerHTML;
e=this._parseMacroCallComment(f)
}Wysiwyg.onModuleLoad(function(){if(!this.gadgetWizard){this.gadgetWizard=new a.GadgetWizard()
}this.gadgetWizard.edit(e,k,function(m){this.onEditGadgetComplete(d,m)
}.bind(this))
}.bind(this))
}else{var i=new a.widgets.ModalPopup("$escapetool.javascript($msg.get('dashboard.gadget.actions.edit.error.notmacro'))",{},{title:"$escapetool.javascript($msg.get('dashboard.gadget.actions.edit.error.notmacro.title'))"});
i.showDialog()
}}},onEditGadgetComplete:function(b,e){var d=this._getMacroCallComment(e.content);
var c=new Hash();
c.set(this.gadgetsClass+"_"+b+"_title",e.title);
c.set(this.gadgetsClass+"_"+b+"_content",d);
c.set("RequiresHTMLConversion",this.gadgetsClass+"_"+b+"_content");
c.set(this.gadgetsClass+"_"+b+"_content_syntax","xwiki/2.0");
c.set("ajax","1");
this._x_notification=new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.gadget.actions.edit.loading'))","inprogress");
new Ajax.Request(this.editURL,{parameters:c,onSuccess:function(f){window.location.reload()
}.bind(this),onFailure:function(f){var g=f.statusText;
if(f.statusText==""||f.status==12031){g="Server not responding"
}this._x_notification.replace(new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.gadget.actions.edit.failed'))"+g,"error",{timeout:5}))
}.bind(this),on0:function(f){f.request.options.onFailure(f)
}.bind(this)})
},onRemoveGadget:function(d){var c=d.element();
var e=c.up(".gadget");
if(!e){return
}var b=this._getGadgetId(e);
this.removed.push(b);
new a.widgets.ConfirmedAjaxRequest(this.removeURL,{parameters:{classname:encodeURIComponent(this.gadgetsClass),classid:encodeURIComponent(b),ajax:"1"},onCreate:function(){c.disabled=true
},onSuccess:function(f){e.remove()
}.bind(this)},{confirmationText:"$escapetool.javascript($msg.get('dashboard.gadget.actions.delete.confirm'))",progressMessageText:"$escapetool.javascript($msg.get('dashboard.gadget.actions.delete.inProgress'))",successMessageText:"$escapetool.javascript($msg.get('dashboard.gadget.actions.delete.done'))",failureMessageText:"$escapetool.javascript($msg.get('dashboard.gadget.actions.delete.failed'))"})
},addNewContainerHandler:function(){var b=new Element("div",{"class":"addcontainer",title:"$escapetool.javascript($msg.get('dashboard.actions.columns.add.tooltip'))"});
b.update("$escapetool.javascript($msg.get('dashboard.actions.columns.add.button'))");
b.observe("click",this.onAddColumn.bindAsEventListener(this));
var c=this.element.down(".addgadget");
c.insert({before:b})
},onAddColumn:function(c){var h=this.containers.last();
var e=this._getContainerId(h);
var j=1+parseInt(e,10);
var d="gadgetcontainer_"+j;
var b=new Element("div",{"class":h.readAttribute("class"),id:d});
h.removeClassName("last-column");
h.insert({after:b});
this.containers.push(b);
var i=new Array();
this.containers.each(function(k){i.push(k.readAttribute("id"))
});
this.createDragAndDrops();
var g=new Element("link",{href:'$xwiki.getSkinFile("uicomponents/container/columns.css", true)?columns='+this.containers.length,type:"text/css",rel:"stylesheet"});
var f=$(document.getElementsByTagName("head")[0]);
f.insert(g)
},doEditGadgets:function(c){var b=this.prepareEditParameters();
b.set("ajax","1");
new Ajax.Request(this.editURL,{parameters:b,onSuccess:function(d){this.edited=false;
if(c){c()
}}.bind(this),onFailure:function(d){var e=d.statusText;
if(d.statusText==""||d.status==12031){e="Server not responding"
}this._x_notification=new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.actions.edit.failed'))"+e,"error",{timeout:5});
if(c){c()
}}.bind(this),on0:function(d){d.request.options.onFailure(d)
}.bind(this)})
},onMoveGadget:function(b){this.edited=true
},saveChanges:function(c){if(!this.edited){return
}c.stop();
c.memo.originalEvent.stop();
var b=c.memo.originalEvent.element();
this._x_edit_notification=new a.widgets.Notification("$escapetool.javascript($msg.get('dashboard.actions.save.loading'))","inprogress");
this.doEditGadgets(function(){if(!this.edited){b.click()
}if(this._x_edit_notification){this._x_edit_notification.hide()
}}.bind(this))
},prepareEditParameters:function(){var b=new Hash();
this.containers.each(function(d){var c=this._getContainerId(d);
d.select(".gadget").each(function(h,g){var e=this._getGadgetId(h);
var f=this.gadgetsClass+"_"+e+"_position";
var i=c+", "+(g+1);
b.set(f,i)
},this)
},this);
return b
}});
return a
}(XWiki||{}));
document.observe("xwiki:dom:loaded",function(c){if(XWiki.contextaction=="inline"){var b=$$(".dashboard")[0];
if(b){var a=new XWiki.Dashboard(b)
}}});