/*! grafana - v3.0.4 - 2016-06-01
 * Copyright (c) 2016 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","jquery","app/core/config","moment"],function(a,b,c,d){"use strict";var e=a.module("grafana.controllers");e.controller("DashboardCtrl",["$scope","$rootScope","dashboardKeybindings","timeSrv","templateValuesSrv","dynamicDashboardSrv","dashboardSrv","unsavedChangesSrv","dashboardViewStateSrv","contextSrv","$timeout",function(b,e,f,g,h,i,j,k,l,m,n){b.editor={index:0},b.panels=c.panels;var o;this.init=function(a){b.resetRow(),b.registerWindowResizeEvent(),b.onAppEvent("show-json-editor",b.showJsonEditor),b.setupDashboard(a)},b.setupDashboard=function(a){e.performance.dashboardLoadStart=(new Date).getTime(),e.performance.panelsInitialized=0,e.performance.panelsRendered=0;var c=j.create(a.dashboard,a.meta);j.setCurrent(c),g.init(c),h.init(c)["finally"](function(){i.init(c),k.init(c,b),b.dashboard=c,b.dashboardMeta=c.meta,b.dashboardViewState=l.create(b),f.shortcuts(b),b.updateSubmenuVisibility(),b.setWindowTitleAndTheme(),b.appEvent("dashboard-loaded",b.dashboard)})["catch"](function(a){a.data&&a.data.message&&(a.message=a.data.message),b.appEvent("alert-error",["Dashboard init failed","Template variables could not be initialized: "+a.message])})},b.updateSubmenuVisibility=function(){b.submenuEnabled=b.dashboard.isSubmenuFeaturesEnabled()},b.setWindowTitleAndTheme=function(){window.document.title=c.window_title_prefix+b.dashboard.title},b.broadcastRefresh=function(){e.performance.panelsRendered=0,e.$broadcast("refresh")},b.addRow=function(a,b){a.rows.push(b)},b.addRowDefault=function(){b.resetRow(),b.row.title="New row",b.addRow(b.dashboard,b.row)},b.resetRow=function(){b.row={title:"",height:"250px",editable:!0}},b.showJsonEditor=function(a,c){var d=e.$new();d.object=c.object,d.updateHandler=c.updateHandler,b.appEvent("show-dash-editor",{src:"public/app/partials/edit_json.html",scope:d})},b.onDrop=function(a,c,d){var f=b.dashboard.getPanelInfoById(a);if(d){var g=b.dashboard.getPanelInfoById(d.id);g.row.panels[g.index]=f.panel,f.row.panels[f.index]=d;var h=f.panel.span;f.panel.span=d.span,d.span=h}else f.row.panels.splice(f.index,1),f.panel.span=12-b.dashboard.rowSpan(c),c.panels.push(f.panel);e.$broadcast("render")},b.registerWindowResizeEvent=function(){a.element(window).bind("resize",function(){n.cancel(o),o=n(function(){b.$broadcast("render")},200)}),b.$on("$destroy",function(){a.element(window).unbind("resize")})},b.timezoneChanged=function(){e.$broadcast("refresh")},b.formatDate=function(a){return d(a).format("MMM Do YYYY, h:mm:ss a")}}])});