Refactor wp-details and wp-show as derived classes

pull/4643/head
Oliver Günther 8 years ago
parent aa6b750136
commit d4c49159a4
  1. 66
      frontend/app/components/routing/wp-details/wp-details.controller.ts
  2. 198
      frontend/app/components/routing/wp-show/wp-show.controller.ts
  3. 118
      frontend/app/components/routing/wp-view-base/wp-view-base.controller.ts

@ -26,62 +26,20 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {scopedObservable} from "../../../helpers/angular-rx-utils";
import {WorkPackageResource} from "../../api/api-v3/hal-resources/work-package-resource.service";
import {WorkPackageEditModeStateService} from "../../wp-edit/wp-edit-mode-state.service";
import {wpControllersModule} from '../../../angular-modules';
import {WorkPackageViewController} from '../wp-view-base/wp-view-base.controller';
angular
.module('openproject.workPackages.controllers')
.controller('WorkPackageDetailsController', WorkPackageDetailsController);
export class WorkPackageDetailsController extends WorkPackageViewController {
function WorkPackageDetailsController($scope,
$state,
$rootScope,
$q,
I18n,
PathHelper,
WorkPackageService,
NotificationsService,
wpEditModeState:WorkPackageEditModeStateService,
wpCacheService) {
$scope.wpEditModeState = wpEditModeState;
$scope.I18n = I18n;
var deferred = $q.defer();
$scope.initializedWorkPackage = deferred.promise;
scopedObservable($scope, wpCacheService.loadWorkPackage($state.params.workPackageId))
.subscribe((wp:WorkPackageResource) => {
$scope.workPackage = wp;
wp.schema.$load();
WorkPackageService.cache().put('preselectedWorkPackageId', wp.id);
$scope.focusAnchorLabel = getFocusAnchorLabel(
$state.current.url.replace(/\//, ''),
wp
);
$scope.showStaticPagePath = PathHelper.workPackagePath(wp);
deferred.resolve();
});
$scope.onWorkPackageSave = function () {
$rootScope.$emit('workPackagesRefreshInBackground');
};
constructor(public $injector,
public $scope,
public $state) {
super($injector, $scope, $state.params['workPackageId']);
}
$scope.canViewWorkPackageWatchers = function() {
return !!($scope.workPackage && $scope.workPackage.watchers !== undefined);
public onWorkPackageSave() {
this.$rootScope.$emit('workPackagesRefreshInBackground');
};
function getFocusAnchorLabel(tab, workPackage) {
var tabLabel = I18n.t('js.work_packages.tabs.' + tab),
params = {
tab: tabLabel,
type: workPackage.type.name,
subject: workPackage.subject
};
return I18n.t('js.label_work_package_details_you_are_here', params);
}
}
wpControllersModule.controller('WorkPackageDetailsController', WorkPackageDetailsController);

@ -26,52 +26,76 @@
// See doc/COPYRIGHT.rdoc for more details.
// ++
import {scopedObservable} from "../../../helpers/angular-rx-utils";
import {WorkPackageResource} from "../../api/api-v3/hal-resources/work-package-resource.service";
function WorkPackageShowController($scope,
$rootScope,
$state,
$window,
PERMITTED_MORE_MENU_ACTIONS,
I18n,
PathHelper,
WorkPackageService,
WorkPackageAuthorization,
HookService,
AuthorisationService,
wpCacheService,
wpEditModeState) {
$scope.wpEditModeState = wpEditModeState;
scopedObservable($scope, wpCacheService.loadWorkPackage($state.params.workPackageId))
.subscribe((wp: WorkPackageResource) => {
$scope.workPackage = wp;
wp.schema.$load();
AuthorisationService.initModelAuth('work_package', $scope.workPackage);
var authorization = new WorkPackageAuthorization($scope.workPackage);
$scope.permittedActions = angular.extend(getPermittedActions(authorization, PERMITTED_MORE_MENU_ACTIONS),
getPermittedPluginActions(authorization));
$scope.actionsAvailable = Object.keys($scope.permittedActions).length > 0;
// END stuff copied from details toolbar directive...
$scope.I18n = I18n;
$scope.$parent.preselectedWorkPackageId = $scope.workPackage.id;
$scope.maxDescriptionLength = 800;
$scope.projectIdentifier = $scope.workPackage.project.identifier;
// initialization
setWorkPackageScopeProperties($scope.workPackage);
import {WorkPackageEditModeStateService} from "../../wp-edit/wp-edit-mode-state.service";
import {wpControllersModule} from '../../../angular-modules';
import {WorkPackageViewController} from '../wp-view-base/wp-view-base.controller';
import {WorkPackageResourceInterface} from '../../api/api-v3/hal-resources/work-package-resource.service';
import {UserResource} from '../../api/api-v3/hal-resources/user-resource.service';
import {HalResource} from '../../api/api-v3/hal-resources/hal-resource.service';
export class WorkPackageShowController extends WorkPackageViewController {
// Permitted actions for WP toolbar
public permittedActions:any;
public actionsAvailable:boolean;
// Watcher properties
public isWatched:boolean;
public displayWatchButton:boolean;
public watchers:any;
// Properties
public type:HalResource;
public author:UserResource;
public authorPath:string;
public authorActive:boolean;
public attachments:any;
constructor(public $injector,
public $scope,
public $state,
public $window,
public HookService,
public AuthorisationService,
public WorkPackageAuthorization,
public PERMITTED_MORE_MENU_ACTIONS) {
super($injector, $scope, $state.params['workPackageId']);
}
protected init() {
super.init();
this.AuthorisationService.initModelAuth('work_package', this.workPackage);
var authorization = new this.WorkPackageAuthorization(this.workPackage);
this.permittedActions = angular.extend(this.getPermittedActions(authorization, this.PERMITTED_MORE_MENU_ACTIONS),
this.getPermittedPluginActions(authorization));
this.actionsAvailable = Object.keys(this.permittedActions).length > 0;
// initialization
this.setWorkPackageScopeProperties(this.workPackage);
}
public deleteSelectedWorkPackage() {
var promise = this.WorkPackageService.performBulkDelete([this.workPackage.id], true);
promise.success(function () {
this.$state.go('work-packages.list', { projectPath: this.projectIdentifier });
});
}
public triggerMoreMenuAction(action, link) {
switch (action) {
case 'delete':
this.deleteSelectedWorkPackage();
break;
default:
this.$window.location.href = link;
break;
}
};
// stuff copied from details toolbar directive...
function getPermittedActions(authorization, permittedMoreMenuActions) {
private getPermittedActions(authorization, permittedMoreMenuActions) {
var permittedActions = authorization.permittedActionsWithLinks(permittedMoreMenuActions);
var augmentedActions = { };
@ -84,9 +108,9 @@ function WorkPackageShowController($scope,
return augmentedActions;
}
function getPermittedPluginActions(authorization) {
private getPermittedPluginActions(authorization) {
var pluginActions = [];
angular.forEach(HookService.call('workPackageDetailsMoreMenu'), function(action) {
angular.forEach(this.HookService.call('workPackageDetailsMoreMenu'), function(action) {
pluginActions = pluginActions.concat(action);
});
@ -105,91 +129,27 @@ function WorkPackageShowController($scope,
return augmentedPluginActions;
}
function deleteSelectedWorkPackage() {
var promise = WorkPackageService.performBulkDelete([$scope.workPackage.id], true);
promise.success(function() {
$state.go('work-packages.list', {projectPath: $scope.projectIdentifier});
});
}
$scope.triggerMoreMenuAction = function(action, link) {
switch (action) {
case 'delete':
deleteSelectedWorkPackage();
break;
default:
$window.location.href = link;
break;
}
};
function outputMessage(message, isError) {
$scope.$emit('flashMessage', {
isError: !!isError,
text: message
});
}
function outputError(error) {
outputMessage(error.message || I18n.t('js.work_packages.error.general'), true);
}
$scope.outputMessage = outputMessage; // expose to child controllers
$scope.outputError = outputError; // expose to child controllers
function setWorkPackageScopeProperties(workPackage){
$scope.isWatched = workPackage.hasOwnProperty('unwatch');
$scope.displayWatchButton = workPackage.hasOwnProperty('unwatch') ||
workPackage.hasOwnProperty('watch');
private setWorkPackageScopeProperties(wp:WorkPackageResourceInterface) {
this.isWatched = wp.hasOwnProperty('unwatch');
this.displayWatchButton = wp.hasOwnProperty('unwatch') || wp.hasOwnProperty('watch');
// watchers
if(workPackage.watchers) {
$scope.watchers = workPackage.watchers.elements;
if (wp.watchers) {
this.watchers = (wp.watchers as any).elements;
}
$scope.showStaticPagePath = PathHelper.workPackagePath($scope.workPackage.id);
// Type
$scope.type = workPackage.type;
this.type = wp.type;
// Author
$scope.author = workPackage.author;
$scope.authorPath = $scope.author.showUserPath;
$scope.authorActive = $scope.author.isActive;
this.author = wp.author;
this.authorPath = this.author.showUserPath;
this.authorActive = this.author.isActive;
// Attachments
$scope.attachments = workPackage.attachments.elements;
$scope.focusAnchorLabel = getFocusAnchorLabel(
$state.current.url.replace(/\//, ''),
$scope.workPackage
);
}
$scope.canViewWorkPackageWatchers = function() {
return !!($scope.workPackage && $scope.workPackage.watchers !== undefined);
};
// toggles
$scope.toggleStates = {
hideFullDescription: true,
hideAllAttributes: true
};
function getFocusAnchorLabel(tab, workPackage) {
var tabLabel = I18n.t('js.work_packages.tabs.' + tab),
params = {
tab: tabLabel,
type: workPackage.type.name,
subject: workPackage.subject
};
return I18n.t('js.label_work_package_details_you_are_here', params);
this.attachments = wp.attachments.elements;
}
}
angular
.module('openproject.workPackages.controllers')
.controller('WorkPackageShowController', WorkPackageShowController);
wpControllersModule.factory('WorkPackageShowController', WorkPackageShowController);

@ -0,0 +1,118 @@
//-- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
//++
import {WorkPackageResourceInterface} from '../../api/api-v3/hal-resources/work-package-resource.service';
import {scopedObservable} from '../../../helpers/angular-rx-utils';
import {WorkPackageCacheService} from '../../work-packages/work-package-cache.service';
import {KeepTabService} from '../../wp-panels/keep-tab/keep-tab.service';
import {wpControllersModule} from '../../../angular-modules';
import {WorkPackageEditModeStateService} from '../../wp-edit/wp-edit-mode-state.service';
export class WorkPackageViewController {
protected $q:ng.IQService;
protected $state:ng.ui.IStateService;
protected $rootScope:ng.IRootScopeService;
protected keepTab:KeepTabService;
protected wpCacheService:WorkPackageCacheService;
protected wpEditModeState:WorkPackageEditModeStateService;
protected WorkPackageService;
protected PathHelper:op.PathHelper;
protected I18n:op.I18n;
// Helper promise to detect when the controller has been initialized
// (when a WP has loaded).
public initialized:ng.IPromise<any>;
// Work package resource to be loaded from the cache
public workPackage:WorkPackageResourceInterface;
public projectIdentifier:string;
public focusAnchorLabel:string;
public showStaticPagePath:string;
constructor(public $injector, public $scope, protected workPackageId) {
this.$inject('$q', '$state', 'keepTab', 'wpCacheService', 'WorkPackageService',
'wpEditModeState', 'PathHelper', 'I18n');
var deferred = this.$q.defer();
this.initialized = deferred.promise;
scopedObservable($scope, this.wpCacheService.loadWorkPackage(workPackageId))
.subscribe((wp:WorkPackageResourceInterface) => {
this.workPackage = wp;
deferred.resolve();
});
}
protected $inject(...args:string[]) {
args.forEach(field => {
this[field] = this.$injector.get(field);
});
}
/**
* Initialize controller after workPackage resource has been loaded.
*/
protected init() {
// Ensure the schema is being loaded as soon as possible
this.workPackage.schema.$load();
// Set elements
this.projectIdentifier = this.workPackage.project.identifier;
// Preselect this work package for future list operations
this.showStaticPagePath = this.PathHelper.workPackagePath(this.workPackage);
this.WorkPackageService.cache().put('preselectedWorkPackageId', this.workPackage.id);
// Listen to tab changes to update the tab label
scopedObservable(this.$scope, this.keepTab.observable).subscribe((tabs:any) => {
this.updateFocusAnchorLabel(tabs.active);
});
}
/**
* Recompute the current tab focus label
*/
public updateFocusAnchorLabel(tabName:string):string {
const tabLabel = this.I18n.t('js.label_work_package_details_you_are_here', {
tab: this.I18n.t('js.work_packages.tabs.' + tabName),
type: this.workPackage.type.name,
subject: this.workPackage.subject
});
return this.focusAnchorLabel = tabLabel;
}
public get canViewWorkPackageWatchers() {
return !!(this.workPackage && this.workPackage.watchers);
}
}
wpControllersModule.controller('WorkPackageViewController', WorkPackageViewController);
Loading…
Cancel
Save