Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Refactor array move into util service.
Browse files Browse the repository at this point in the history
Moved container move logic into ContainerService.
Moved dashboard initialization to ExplorerService.
  • Loading branch information
jmuharsky committed Aug 10, 2015
1 parent 6eb3767 commit 5bcff52
Show file tree
Hide file tree
Showing 26 changed files with 1,078 additions and 376 deletions.
3 changes: 3 additions & 0 deletions client/app.js
Expand Up @@ -27,6 +27,7 @@ goog.require('p3rf.perfkit.explorer.components.config.ConfigService');
goog.require('p3rf.perfkit.explorer.components.container.ContainerConfigDirective');
goog.require('p3rf.perfkit.explorer.components.container.ContainerDirective');
goog.require('p3rf.perfkit.explorer.components.container.ContainerToolbarDirective');
goog.require('p3rf.perfkit.explorer.components.container.ContainerService');
goog.require('p3rf.perfkit.explorer.components.dashboard.DashboardConfigDirective');
goog.require('p3rf.perfkit.explorer.components.dashboard.DashboardCtrl');
goog.require('p3rf.perfkit.explorer.components.dashboard.DashboardDataService');
Expand Down Expand Up @@ -148,6 +149,8 @@ explorer.application.module.service('sidebarTabService',
explorer.components.explorer.sidebar.SidebarTabService);
explorer.application.module.service('configService',
explorer.components.config.ConfigService);
explorer.application.module.service('containerService',
explorer.components.container.ContainerService);
explorer.application.module.service('dashboardDataService',
explorer.components.dashboard.DashboardDataService);
explorer.application.module.service('dashboardService',
Expand Down
16 changes: 10 additions & 6 deletions client/components/code_editor/code-editor-controller_test.js
Expand Up @@ -30,7 +30,8 @@ describe('CodeEditorCtrl', function() {
const ExplorerStateService = explorer.components.explorer.ExplorerStateService;
const WidgetType = explorer.models.WidgetType;

var ctrl, scope, rootScope, dashboardSvc, widgetFactorySvc, explorerStateSvc;
var ctrl, scope, rootScope, containerSvc, dashboardSvc, widgetFactorySvc,
explorerStateSvc;
var container, widget;
var $state;
var ctrlPrototype =
Expand All @@ -39,10 +40,11 @@ describe('CodeEditorCtrl', function() {
beforeEach(module('explorer'));

beforeEach(inject(function(
$rootScope, $controller, _dashboardService_, _explorerStateService_,
_widgetFactoryService_, errorService, _$state_) {
$rootScope, $controller, _explorerService_, _dashboardService_,
_explorerStateService_, _widgetFactoryService_, errorService, _$state_) {
errorService.logToConsole = false;

explorerService = _explorerService_;
dashboardSvc = _dashboardService_;
explorerStateSvc = _explorerStateService_;
widgetFactorySvc = _widgetFactoryService_;
Expand All @@ -59,7 +61,8 @@ describe('CodeEditorCtrl', function() {
explorer.components.code_editor.CodeEditorCtrl,
{$scope: scope});

dashboardSvc.newDashboard();
explorerService.newDashboard();

rootScope.$apply();

container = explorerStateSvc.containers.selected;
Expand Down Expand Up @@ -108,7 +111,7 @@ describe('CodeEditorCtrl', function() {
it('should update the currentJson property with the content of the ' +
'current object.',
function() {
dashboardSvc.newDashboard();
explorerService.newDashboard();
rootScope.$apply();

var newWidget = new ChartWidgetConfig(widgetFactorySvc);
Expand Down Expand Up @@ -151,7 +154,8 @@ describe('CodeEditorCtrl', function() {
it('should update the current object with the content of the currentJson ' +
'property.',
function() {
dashboardSvc.newDashboard();
explorerService.newDashboard();

rootScope.$apply();

var model = {obj: 'model'};
Expand Down
244 changes: 244 additions & 0 deletions client/components/container/container-service.js
@@ -0,0 +1,244 @@
/**
* @copyright Copyright 2015 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileoverview containerService is an angular service that provides
* management and manipulation (not state) of containers in a dashboard.
*
* @author joemu@google.com (Joe Allan Muharsky)
*/

goog.provide('p3rf.perfkit.explorer.components.container.ContainerService');

goog.scope(function() {
const explorer = p3rf.perfkit.explorer;



/**
* See module docstring for more information about purpose and usage.
*
* @ngInject
* @export
*/
explorer.components.container.ContainerService = class {
constructor(arrayUtilService, dashboardService, explorerStateService) {
/** @export {!ArrayUtilService} */
this.arrayUtilSvc = arrayUtilService;

/** @export {!DashboardService} */
this.dashboardSvc = dashboardService;

/** @export {!ExplorerStateService} */
this.explorerStateSvc = explorerStateService;
};

/**
* Checks for a valid container and executes the specified move function.
*
* @param {!Function.<!Array, !ContainerWidgetModel>} moveFunction The
* function to execute.
* @param {?ContainerWidgetModel=} opt_container The target for the move,
* if provided. Otherwise, the selected container is used.
*/
move_(moveFunction, opt_container) {
if (!goog.isDefAndNotNull(opt_container) &&
!goog.isDefAndNotNull(this.dashboardSvc.selectedContainer)) {
throw new Error(
moveFunction.name + ' failed: no container specified or selected.');
}

moveFunction(
this.dashboardSvc.current.model.children,
opt_container || this.dashboardSvc.selectedContainer
);
};

/**
* Moves the specified or selected container to the first position.
*
* @param {?ContainerWidgetModel=} opt_container The target for the move,
* if provided. Otherwise, the selected container is used.
* @export
*/
moveFirst(opt_container) {
this.move_(
angular.bind(arrayUtilSvc, arrayUtilSvc.moveFirst), opt_container);
};

/**
* Moves the specified or selected container to the previous position.
*
* @param {?ContainerWidgetModel=} opt_container The target for the move,
* if provided. Otherwise, the selected container is used.
* @export
*/
movePrevious(opt_container) {
this.move_(
angular.bind(arrayUtilSvc, arrayUtilSvc.movePrevious), opt_container);
};

/**
* Moves the specified or selected container to the next position.
*
* @param {?ContainerWidgetModel=} opt_container The target for the move,
* if provided. Otherwise, the selected container is used.
* @export
*/
moveNext(opt_container) {
this.move_(
angular.bind(arrayUtilSvc, arrayUtilSvc.moveNext), opt_container);
};

/**
* Moves the specified or selected container to the last position.
*
* @param {?ContainerWidgetModel=} opt_container The target for the move,
* if provided. Otherwise, the selected container is used.
* @export
*/
moveLast(opt_container) {
this.move_(
angular.bind(arrayUtilSvc, arrayUtilSvc.moveLast), opt_container);
};


/**
* Adds a new container with one new widget and optionally selects it.
*
* @param {boolean=} opt_autoCreateWidget If true, automatically adds
* a widget to the container.
* @param {boolean=} opt_autoSelect If true, automatically selects
* the widget and/or container.
* @return {!ContainerWidgetConfig}
* @export
*/
insert(opt_autoCreateWidget = true, opt_autoSelect = true) {
let container = this.dashboardSvc.newContainer(opt_autoCreateWidget);

this.dashboardSvc.containers.push(container);
this.explorerStateSvc.containers.add(container);

if (opt_autoSelect) {
this.dashboardSvc.selectContainer(container);
}

return container;
};


/**
* Adds a new container with one new widget and optionally selects it.
*
* @param {number} index
* @param {boolean=} opt_autoCreateWidget If true, automatically adds
* a widget to the container.
* @param {boolean=} opt_autoSelect If true, automatically selects
* the widget and/or container.
* @return {!ContainerWidgetConfig}
* @export
*/
insertAt(index, opt_autoCreateWidget = true, opt_autoSelect = true) {
let container = this.dashboardSvc.newContainer(opt_autoCreateWidget);

goog.array.insertAt(this.dashboardSvc.containers, container, index);
this.explorerStateSvc.containers.add(container);

if (opt_autoSelect) {
this.dashboardSvc.selectContainer(container);
}

return container;
};


/**
* Adds a new container and inserts it after the provided one.
*
* @param {?ContainerWidgetConfig=} opt_container The container that will
* precede the newly created one. If not provided, the selected
* container is used.
* @export
*/
insertAfter(
opt_container, opt_autoCreateWidget = true, opt_autoSelect = true) {
let container = this.dashboardSvc.newContainer(opt_autoCreateWidget);
let targetContainer = opt_container || this.dashboardSvc.selectedContainer;

if (!goog.isDefAndNotNull(targetContainer)) {
throw new Error(
'insertAfter failed: No container provided or selected.');
};

this.arrayUtilSvc.insertAfter(
this.dashboardSvc.containers, container, targetContainer);

if (opt_autoSelect) {
this.dashboardSvc.selectContainer(container);
}

return container;
}


/**
* Adds a new container and inserts it before the provided one.
*
* @param {?ContainerWidgetConfig=} opt_container The container that will
* follow the newly created one. If not provided, the selected
* container is used.
*/
insertBefore(
opt_container, opt_autoCreateWidget = true, opt_autoSelect = true) {
let container = this.dashboardSvc.newContainer(opt_autoCreateWidget);
let targetContainer = opt_container || this.dashboardSvc.selectedContainer;

if (!goog.isDefAndNotNull(targetContainer)) {
throw new Error(
'insertBefore failed: No container provided or selected.');
};

this.arrayUtilSvc.insertBefore(
this.dashboardSvc.containers, container, targetContainer);

if (opt_autoSelect) {
this.dashboardSvc.selectContainer(container);
}

return container;
}

/**
* Removes the specified container from the curernt dashboard.
*
* @param {?ContainerWidgetConfig=} opt_container The container that will
* follow the newly created one. If not provided, the selected
* container is used.
* @export
*/
remove(opt_container) {
let targetContainer = opt_container || this.dashboardSvc.selectedContainer;

if (this.dashboardSvc.selectedWidget.state().parent === targetContainer) {
this.unselectWidget();
}

this.move_(
angular.bind(arrayUtilSvc, arrayUtilSvc.remove), targetContainer);
delete this.explorerStateSvc.containers.all[targetContainer.model.id];
}
};
const ContainerService = explorer.components.util.ContainerService;

}); // goog.scope

0 comments on commit 5bcff52

Please sign in to comment.