Skip to content

Commit a35fe04

Browse files
authored
Remember monitoring view (#4949)
1 parent 8f3bf06 commit a35fe04

5 files changed

Lines changed: 84 additions & 49 deletions

File tree

scripts/apps/monitoring/directives/MonitoringView.ts

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {gettext} from 'core/utils';
33
import {AuthoringWorkspaceService} from 'apps/authoring/authoring/services/AuthoringWorkspaceService';
44
import {IDesk, ISuperdeskQuery, IUser} from 'superdesk-api';
55
import {appConfig} from 'appConfig';
6+
import {sdApi} from 'api';
7+
import {getMonitoringViewOptions} from './utils';
68

79
const PAGE_SIZE = 50;
810

@@ -23,7 +25,7 @@ interface IScope extends ng.IScope {
2325
group: any;
2426
refreshGroup(group?: any, triggeredByScrolling?: boolean);
2527
isActiveGroup: (group: any) => boolean;
26-
switchView: (value: any, swimlane?: any) => void;
28+
switchView: (value: any, options?: {programmatic?: boolean}) => void;
2729
gettext: (text: any, params?: any) => any;
2830
toggleFilter: () => void;
2931
addResourceUpdatedEventListener: (callback: any) => void;
@@ -35,6 +37,7 @@ interface IScope extends ng.IScope {
3537
afterWorkspaceRename: () => void;
3638
initWorkspaceRename: (workspace) => void;
3739
workspaceToRename: any;
40+
availableViews: Array<{id: string; label: string; icon: string}>;
3841
}
3942

4043
/**
@@ -130,7 +133,17 @@ export function MonitoringView(
130133
pageTitle.setUrl(_.capitalize(gettext(scope.type)));
131134

132135
scope.shouldRefresh = true;
133-
scope.view = 'compact'; // default view
136+
137+
scope.availableViews = getMonitoringViewOptions({
138+
swimlaneViewEnabled:
139+
!scope.monitoring.singleGroup
140+
&& scope.type === 'monitoring'
141+
&& scope.monitoring.hasSwimlaneView,
142+
compactViewEnabled: scope.compactViewEnabled,
143+
});
144+
145+
// will be initialized by programmatic call to `scope.switchView `
146+
scope.view = undefined;
134147

135148
scope.workspaces = workspaces;
136149
scope.$watch('workspaces.active', (workspace) => {
@@ -199,38 +212,32 @@ export function MonitoringView(
199212
});
200213
}
201214

202-
if (currentDesk && currentDesk.monitoring_default_view) {
203-
switch (currentDesk.monitoring_default_view) {
204-
case 'list':
205-
scope.switchView('compact');
206-
break;
207-
case 'list-compact':
208-
scope.switchView('compact-configurable');
209-
break;
210-
case 'swimlane':
211-
scope.switchView('compact', true);
212-
break;
213-
case 'photogrid':
214-
scope.switchView('photogrid');
215-
break;
216-
default:
217-
scope.switchView('compact'); // list by default
218-
break;
219-
}
220-
}
215+
const availableViewIds = new Set(scope.availableViews.map(({id}) => id));
216+
const deskPreference =
217+
scope.availableViews.find(({id}) => id === currentDesk?.monitoring_default_view)?.id;
218+
const userPreference: string = sdApi.preferences.get('monitoring:view')?.view;
219+
const appliedPreference = deskPreference ?? userPreference;
220+
221+
scope.switchView(
222+
availableViewIds.has(appliedPreference) ? appliedPreference : 'compact',
223+
{programmatic: true},
224+
);
221225
});
222226

223227
scope.numberOfColumns = 1;
224228

225-
/**
226-
* Toggle viewColumn to switch views between swimlane and list
227-
* @param {Boolean} value
228-
* @param {Boolean} swimlane
229-
*/
230-
scope.switchView = function(value, swimlane) {
231-
scope.monitoring.switchViewColumn(swimlane, true);
229+
scope.switchView = function(value: string, options?: {programmatic?: boolean}) {
230+
const isSwimlane = value === 'swimlane2';
231+
232+
const programmatic = options?.programmatic === true;
233+
234+
scope.monitoring.switchViewColumn(isSwimlane, !programmatic);
232235
scope.view = value;
233-
scope.swimlane = swimlane;
236+
scope.swimlane = isSwimlane;
237+
238+
if (!programmatic) {
239+
sdApi.preferences.update('monitoring:view', {view: value});
240+
}
234241
};
235242

236243
/**

scripts/apps/monitoring/directives/utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,42 @@ export const getTagsConfig = (filterIds: Array<string>): ITagConfig => {
7171
},
7272
}), {});
7373
};
74+
75+
export function getMonitoringViewOptions(
76+
options: {
77+
compactViewEnabled: boolean;
78+
swimlaneViewEnabled: boolean;
79+
},
80+
): Array<{id: string; label: string; icon: string}> {
81+
const availableViews: Array<{id: string; label: string; icon: string}> = [];
82+
83+
availableViews.push({
84+
id: 'compact',
85+
label: gettext('List view'),
86+
icon: 'list-view',
87+
});
88+
89+
if (options.compactViewEnabled) {
90+
availableViews.push({
91+
id: 'compact-configurable',
92+
label: gettext('Compact View'),
93+
icon: 'unordered-list',
94+
});
95+
}
96+
97+
if (options.swimlaneViewEnabled) {
98+
availableViews.push({
99+
id: 'swimlane2',
100+
label: gettext('Swimlane View'),
101+
icon: 'kanban-view',
102+
});
103+
}
104+
105+
availableViews.push({
106+
id: 'photogrid',
107+
label: gettext('Photo Grid View'),
108+
icon: 'grid-view',
109+
});
110+
111+
return availableViews;
112+
}

scripts/apps/monitoring/tests/monitoring.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ describe('monitoring', () => {
99
beforeEach(window.module('superdesk.core.services.pageTitle'));
1010
beforeEach(window.module('superdesk.templates-cache'));
1111

12-
beforeEach(inject((session, $rootScope) => {
12+
beforeEach(inject((session, $rootScope, preferencesService) => {
1313
session.start({token: 's1', _id: 'foo', href: ''}, {_id: 'foo'});
14+
15+
// prevent crash; actual implementation crashes since preferences aren't loaded when unit test runs
16+
spyOn(preferencesService, 'getSync').and.returnValue(undefined);
17+
1418
$rootScope.$digest();
1519
}));
1620

scripts/apps/monitoring/views/monitoring-view.html

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,9 @@ <h2 class="subnav__page-title sd-flex-no-grow sd-empty" ng-hide="monitoring.sing
184184
<i ng-class="{'icon-list-view': !monitoring.viewColumn && (!view || view === 'compact'), 'icon-grid-view': view === 'photogrid', 'icon-kanban-view': monitoring.viewColumn, 'icon-unordered-list': view === 'compact-configurable'}"></i>
185185
</button>
186186
<ul class="dropdown__menu">
187-
<li>
188-
<button ng-click="switchView('compact')">
189-
<i class="icon-list-view"></i>{{ 'List View' | translate }}
190-
</button>
191-
</li>
192-
<li ng-if="compactViewEnabled">
193-
<button ng-click="switchView('compact-configurable')">
194-
<i class="icon-unordered-list"></i>{{ 'Compact View' | translate }}
195-
</button>
196-
</li>
197-
<li ng-if="!monitoring.singleGroup && type === 'monitoring' && monitoring.hasSwimlaneView">
198-
<button ng-click="switchView('swimlane2', true)">
199-
<i class="icon-kanban-view"></i>{{ 'Swimlane View' | translate }}
200-
</button>
201-
</li>
202-
<li>
203-
<button ng-click="switchView('photogrid')">
204-
<i class="icon-grid-view"></i>{{ 'Photo Grid View' | translate }}
187+
<li ng-repeat="availableView in availableViews">
188+
<button ng-click="switchView(availableView.id)">
189+
<i class="icon-{{availableView.icon}}"></i>{{availableView.label}}
205190
</button>
206191
</li>
207192
</ul>

scripts/core/superdesk-api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ declare module 'superdesk-api' {
15621562
desk_metadata?: {[key: string]: any};
15631563
content_profiles: {[key: IContentProfile['_id']]: any};
15641564
desk_language?: string;
1565-
monitoring_default_view?: 'list' | 'list-compact' | 'swimlane' | 'photogrid';
1565+
monitoring_default_view?: string;
15661566
default_content_profile: string;
15671567
default_content_template: string;
15681568
slack_channel_name?: string;

0 commit comments

Comments
 (0)