/*! grafana - v4.5.1 - 2017-09-19
 * Copyright (c) 2017 Torkel Ödegaard; Licensed Apache-2.0 */

define(["angular","lodash","jquery","app/core/config"],function(a,b,c,d){"use strict";var e=a.module("grafana.services");e.factory("dashboardViewStateSrv",["$location","$timeout",function(a,e){function f(b){var c=this;c.state={},c.panelScopes=[],c.$scope=b,c.dashboard=b.dashboard,b.onAppEvent("$routeUpdate",function(){var a=c.getQueryStringState();c.needsSync(a)&&c.update(a,!0)}),b.onAppEvent("panel-change-view",function(a,b){c.update(b)}),b.onAppEvent("panel-initialized",function(a,b){c.registerPanel(b.scope)}),a.replace(),this.update(this.getQueryStringState()),this.expandRowForPanel()}return f.prototype.expandRowForPanel=function(){if(this.state.panelId){var a=this.$scope.dashboard.getPanelInfoById(this.state.panelId);a&&(a.row.collapse=!1)}},f.prototype.needsSync=function(a){return b.isEqual(this.state,a)===!1},f.prototype.getQueryStringState=function(){var b=a.search();return b.panelId=parseInt(b.panelId)||null,b.fullscreen=!!b.fullscreen||null,b.edit="true"===b.edit||b.edit===!0||null,b.editview=b.editview||null,b.orgId=d.bootData.user.orgId,b},f.prototype.serializeToUrl=function(){var a=b.clone(this.state);return a.fullscreen=!!this.state.fullscreen||null,a.edit=!!this.state.edit||null,a},f.prototype.update=function(c,d){c.toggle&&(delete c.toggle,this.state.fullscreen&&c.fullscreen&&this.state.edit===c.edit&&(c.fullscreen=!c.fullscreen)),this.editStateChanged=c.edit!==this.state.edit,b.extend(this.state,c),this.dashboard.meta.fullscreen=this.state.fullscreen,this.state.fullscreen||(this.state.fullscreen=null,this.state.edit=null,this.dashboard.meta.soloMode||(this.state.panelId=null)),this.state.edit||delete this.state.tab,d!==!0&&a.search(this.serializeToUrl()),this.syncState()},f.prototype.syncState=function(){if(0!==this.panelScopes.length)if(this.dashboard.meta.fullscreen){var a=this.getPanelScope(this.state.panelId);if(!a)return;if(this.fullscreenPanel){if(this.fullscreenPanel===a&&this.editStateChanged===!1)return;this.leaveFullscreen(!1)}a.ctrl.editModeInitiated||a.ctrl.initEditMode(),a.ctrl.fullscreen||this.enterFullscreen(a)}else this.fullscreenPanel&&this.leaveFullscreen(!0)},f.prototype.getPanelScope=function(a){return b.find(this.panelScopes,function(b){return b.ctrl.panel.id===a})},f.prototype.leaveFullscreen=function(a){var b=this,c=b.fullscreenPanel.ctrl;return c.editMode=!1,c.fullscreen=!1,c.dashboard.editMode=this.oldDashboardEditMode,this.$scope.appEvent("panel-fullscreen-exit",{panelId:c.panel.id}),!!a&&void e(function(){b.oldTimeRange!==c.range?b.$scope.broadcastRefresh():b.$scope.$broadcast("render"),delete b.fullscreenPanel})},f.prototype.enterFullscreen=function(a){var b=a.ctrl;b.editMode=this.state.edit&&this.dashboard.meta.canEdit,b.fullscreen=!0,this.oldDashboardEditMode=this.dashboard.editMode,this.oldTimeRange=b.range,this.fullscreenPanel=a,this.dashboard.editMode=!1,c(window).scrollTop(0),this.$scope.appEvent("panel-fullscreen-enter",{panelId:b.panel.id}),e(function(){b.render()})},f.prototype.registerPanel=function(a){var c=this;c.panelScopes.push(a),c.dashboard.meta.soloMode||c.state.panelId===a.ctrl.panel.id&&(c.state.edit?a.ctrl.editPanel():a.ctrl.viewPanel());var d=a.$on("$destroy",function(){c.panelScopes=b.without(c.panelScopes,a),d()})},{create:function(a){return new f(a)}}}])});