define([ 'dojo/_base/declare', 'dojo/_base/lang', 'dojo/date/stamp', '../store', '../app/Router', '../app/Page', '../widgets/ObjectBox', 'dojo/text!./templates/sessions.html' ],function(declare,lang,dateStamp,store,Router,Page,ObjectBox,template){ return declare([Page],{ templateString: template, templateActions: null, sessionActions: null, startup: function() { if ( this._started ) { return; } this.inherited(arguments); this.templateActions = { "Edit": function(obj){ Router.go('/session/'+store.getIdentity(obj)); }, "Delete": lang.hitch(this,function(obj){ store.remove(store.getIdentity(obj),store.getRevision(obj)) .then(lang.hitch(this,function(){ this._refresh(); })); }), "Publish": lang.hitch(this,this._publishSession) }; this.sessionActions = { "Facilitate": function(obj){ Router.go('run',{uid:store.getIdentity(obj)}); }, "Delete": lang.hitch(this,function(obj){ store.remove(store.getIdentity(obj),store.getRevision(obj)) .then(lang.hitch(this,function(){ this._refresh(); })); }) }; this._refresh(); }, _refresh: function() { this.containerTemplates.set('content',''); this.containerSessions.set('content',''); this._refreshByType('SessionTemplate',this.containerTemplates.domNode,this.templateActions); this._refreshByType('SessionInstance',this.containerSessions.domNode,this.sessionActions); }, _refreshByType: function(type,container,actions) { store.query("_design/default/_view/by_type",{key:type}) .forEach(lang.hitch(this,function(obj){ var widget = new ObjectBox({ actions: actions }).placeAt(container, "last"); widget.startup(); widget.set('value',obj); })); }, onAddSessionTemplate: function(){ Router.go('/session/new'); }, _publishSession: function(sessionTemplate) { var session = lang.clone(sessionTemplate); delete session[store.idProperty]; delete session[store.revProperty]; session.type = "SessionInstance"; session.publicationDate = store.timestamp(); session.creator = "Igor Mayer"; store.add(session) .then(lang.hitch(this,function(){ this._refresh(); this.tabContainer.selectChild(this.sessionsTab); })); } }); });