Skip to content

Commit

Permalink
fix(core/pageTitle): Use promise based API for showing/hiding spinner (
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen authored and Justin Reynolds committed May 5, 2017
1 parent f3a64a3 commit bedfdb6
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 122 deletions.
3 changes: 2 additions & 1 deletion app/scripts/modules/core/core.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {INSIGHT_NGMODULE} from './insight/insight.module';
import {REPLACE_FILTER} from './filter/replace.filter';
import {PIPELINE_TEMPLATE_MODULE} from './pipeline/config/templates/pipelineTemplate.module';
import {HEALTH_COUNTS_COMPONENT} from './healthCounts/healthCounts.component';
import {CORE_PAGETITLE_SERVICE} from './pageTitle/pageTitle.service';

require('../../../fonts/spinnaker/icons.css');

Expand Down Expand Up @@ -109,7 +110,7 @@ module.exports = angular
require('./notification/types/slack/slack.notification.type.module.js'),
require('./notification/types/sms/sms.notification.type.module.js'),

require('./pageTitle/pageTitle.service.js'),
CORE_PAGETITLE_SERVICE,

require('./pipeline/pipelines.module.js'),
require('./pipeline/config/stages/bake/bakeStage.module.js'),
Expand Down
112 changes: 0 additions & 112 deletions app/scripts/modules/core/pageTitle/pageTitle.service.js

This file was deleted.

15 changes: 7 additions & 8 deletions app/scripts/modules/core/pageTitle/pageTitle.service.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';
var pageTitleServiceModule = require('./pageTitle.service').CORE_PAGETITLE_SERVICE;

describe('Service: pageTitleService', function() {

beforeEach(
window.module(
require('./pageTitle.service')
)
window.module(pageTitleServiceModule)
);

beforeEach(window.inject(function (pageTitleService, $stateParams, $rootScope) {
Expand All @@ -22,7 +21,7 @@ describe('Service: pageTitleService', function() {
expect(document.title).toBe('');

this.pageTitleService.handleRoutingStart();
expect(scope.routing).toBe(true);
expect(scope.routing).toBeTruthy();
expect(document.title).toBe('Spinnaker: Loading...');
});
});
Expand All @@ -32,11 +31,11 @@ describe('Service: pageTitleService', function() {
var scope = this.$rootScope;

this.pageTitleService.handleRoutingStart();
expect(scope.routing).toBe(true);
expect(scope.routing).toBeTruthy();
expect(document.title).toBe('Spinnaker: Loading...');

this.pageTitleService.handleRoutingError();
expect(scope.routing).toBe(false);
this.pageTitleService.handleRoutingError({ type: 1 });
expect(scope.routing).toBeFalsy();
expect(document.title).toBe('Spinnaker: Error');

});
Expand All @@ -45,7 +44,7 @@ describe('Service: pageTitleService', function() {
describe('handleRoutingSuccess', function() {

afterEach(function() {
expect(this.$rootScope.routing).toBe(false);
expect(this.$rootScope.routing).toBeFalsy();
});

it('falls back to "Spinnaker" when nothing configured', function() {
Expand Down
136 changes: 136 additions & 0 deletions app/scripts/modules/core/pageTitle/pageTitle.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { module, IScope } from 'angular';
import { TransitionService, Rejection, RejectType, StateParams } from '@uirouter/core';

interface IMainConfig {
field: string;
label: string;
}

interface ISectionConfig {
title: string;
}

interface IDetailsConfig {
title: string;
nameParam?: string;
accountParam?: string;
regionParam?: string;
}

interface IStatePageData {
pageTitleMain?: IMainConfig;
pageTitleSection?: ISectionConfig;
pageTitleDetails?: IDetailsConfig;
}

interface IPageDataParts {
main: string;
section: string;
details: string;
}

export class PageTitleService {
public static $inject = [ '$rootScope', '$stateParams', '$transitions' ];

private previousPageTitle = 'Spinnaker';

constructor(private $rootScope: IScope, private $stateParams: StateParams, $transitions: TransitionService) {
$rootScope.routing = 0;

$transitions.onStart({}, transition => {
this.handleRoutingStart();
const onSuccess = () => this.handleRoutingSuccess(transition.to().data);
const onReject = (err: Rejection) => this.handleRoutingError(err);
transition.promise.then(onSuccess, onReject);
});
}

public handleRoutingStart(): void {
this.$rootScope.routing++;
this.previousPageTitle = document.title;
document.title = 'Spinnaker: Loading...';
}

public handleRoutingError(rejection: Rejection): void {
this.$rootScope.routing--;
const cancelled = rejection.type === RejectType.ABORTED;
document.title = cancelled ? this.previousPageTitle : 'Spinnaker: Error';
}

public handleRoutingSuccess(config: IStatePageData): void {
const parts: IPageDataParts = this.configurePageTitle(config);
let title = parts.main || 'Spinnaker';
if (parts.section) {
title += ' · ' + parts.section;
}
if (parts.details) {
title += ' · ' + parts.details;
}
this.$rootScope.routing = false;
document.title = title;
}

public resolveStateParams(config: IDetailsConfig): string {
if (!config) {
return null;
}

const { $stateParams } = this;
const { title, nameParam, accountParam, regionParam } = config;

let result = title;

if (nameParam) {
result += ': ' + $stateParams[nameParam];
}

if (accountParam || regionParam) {
result += ' (';
if (accountParam && regionParam) {
result += $stateParams[accountParam] + ':' + $stateParams[regionParam];
} else {
result += $stateParams[accountParam] || $stateParams[regionParam];
}
result += ')';
}

return result;
}

public configureSection(sectionConfig: ISectionConfig): string {
return this.resolveStateParams(sectionConfig);
}

public configureDetails(detailsConfig: IDetailsConfig): string {
return this.resolveStateParams(detailsConfig);
}

public configureMain(mainConfig: IMainConfig): string {
const { $stateParams } = this;
let main = null;
if (!mainConfig) {
return main;
}
if (mainConfig.field) {
main = $stateParams[mainConfig.field];
}
if (mainConfig.label) {
main = mainConfig.label;
}
return main;
}

public configurePageTitle(data: IStatePageData = {}): IPageDataParts {
return {
main: this.configureMain(data.pageTitleMain),
section: this.configureSection(data.pageTitleSection),
details: this.configureDetails(data.pageTitleDetails)
};
}
}

export const CORE_PAGETITLE_SERVICE = 'spinnaker.core.pageTitle.service';

module(CORE_PAGETITLE_SERVICE, [require('angular-ui-router').default])
.service('pageTitleService', PageTitleService)
.run(['pageTitleService', (pts: PageTitleService) => pts]);
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {CLUSTER_FILTER_SERVICE} from 'core/cluster/filter/clusterFilter.service'
import {CACHE_INITIALIZER_SERVICE} from 'core/cache/cacheInitializer.service';
import {OVERRIDE_REGISTRY} from 'core/overrideRegistry/override.registry';
import {RECENT_HISTORY_SERVICE} from 'core/history/recentHistory.service';
import {CORE_PAGETITLE_SERVICE} from 'core/pageTitle/pageTitle.service';

module.exports = angular.module('spinnaker.search.infrastructure.controller', [
require('./infrastructureSearch.service.js'),
RECENT_HISTORY_SERVICE,
require('../searchResult/searchResult.directive.js'),
require('core/pageTitle/pageTitle.service.js'),
CORE_PAGETITLE_SERVICE,
require('./project/infrastructureProject.directive.js'),
require('../searchRank.filter.js'),
CLUSTER_FILTER_SERVICE,
Expand Down

0 comments on commit bedfdb6

Please sign in to comment.