define(["cilantro/define/chart","cilantro/define/form","cilantro/utils/frontdesk","cilantro/lib/base"],function(a,b,c){var d=Base.extend({constructor:function(a,b){this.viewset=a,this.fd=new c,this.view=$('<div class="view"></div>'),this.name=b;var d=this.view,e=$(this);d.bind("UpdateElementEvent",jQuery.proxy(this.updateElement,this)),d.bind("HideDependentsEvent ShowDependentsEvent",jQuery.proxy(this.toggleDependents,this)),d.bind("UpdateQueryButtonClicked",jQuery.proxy(this.constructQuery,this)),d.bind("UpdateDSEvent GainedFocusEvent LostFocusEvent RegisterElementsEvent",jQuery.proxy(this.notifyChildren,this)),$(this).bind("GainedFocusEvent LostFocusEvent UpdateQueryButtonClicked HideDependentsEvent RegisterElementsEvent ShowDependentsEvent UpdateDSEvent",jQuery.proxy(this.eventPassThru,this)),this.render()},render:function(){var c=this.viewset,e=this.view,f=this.fd;c.elements[0].type!=="chart"&&e.append("<h2>"+this.name+"</h2>"),$.each(this.viewset.elements,function(g,h){var i=null;switch(h.type){case"form":i=new b(h,c.concept_id),e.append(i.dom),f.checkIn(g,i.dom),f.checkOut(g);break;case"chart":var j=h.data.datatype;j==="number"?(i=new a.LineChart(h,c.concept_id),e.append(i.dom),f.checkIn(g,i.dom),f.checkOut(g)):j==="nullboolean"||j==="boolean"?(i=new a.PieChart(h,c.concept_id),e.append(i.dom),f.checkIn(g,i.dom),f.checkOut(g)):(i=new a.BarChart(h,c.concept_id),e.append(i.dom),f.checkIn(g,i.dom),f.checkOut(g));break;case"custom":h.css&&d.loadCss(h.css),f.checkIn(g);var k=c.elements[0].type==="chart"?0:1;require([h.js],function(a){var b=new a(h,c.concept_id);e.children().length-1<g+k?e.append(b.dom):$(e.children()[g]).prepend(b.dom),f.checkOut(g,b.dom)});break;default:e.append($("<p>Undefined View!</p>"))}})},updateElement:function(a){var b=this.fd;$.each(this.viewset.elements,function(c){b.leaveMessage(c,"triggerHandler",a)}),a.stopPropagation()},toggleDependents:function(a){var b=this.fd;a.target===this.view.children().get(0)&&$.each(this.viewset.elements,function(c){if(c===0)return;b.leaveMessage(c,"triggerHandler",a)}),a.stopPropagation()},constructQuery:function(a){this.view.trigger("ConstructQueryEvent"),a.stopPropagation()},notifyChildren:function(a,b){var c=this.fd;$.each(this.viewset.elements,function(d){c.leaveMessage(d,"triggerHandler",a,b)}),a.stopPropagation()},eventPassThru:function(a,b){this.view.triggerHandler(a,b),a.stopPropagation()},dom:function(){return this.view}},{loadCss:function(a){var b=document.createElement("link");b.type="text/css",b.rel="stylesheet",b.href=a,document.getElementsByTagName("head")[0].appendChild(b)}});return d})