Skip to content

Commit

Permalink
Add current user filter widget (redo from other branch)
Browse files Browse the repository at this point in the history
  • Loading branch information
mduvernon committed Oct 20, 2023
1 parent bbb4f01 commit b040841
Show file tree
Hide file tree
Showing 13 changed files with 587 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import DashboardPageWidgetVO from "./DashboardPageWidgetVO";
import AbstractVO from "../../VO/abstract/AbstractVO";
import VOFieldRefVO from "./VOFieldRefVO";

/**
* CurrentUserFilterWidgetOptionsVO
*/
export default class CurrentUserFilterWidgetOptionsVO extends AbstractVO {

public static VO_FIELD_REF_PLACEHOLDER_CODE_PREFIX: string = "CurrentUserFilterWidgetOptions.vo_field_ref.placeholder.";
public static VO_FIELD_REF_ADVANCED_MODE_PLACEHOLDER_CODE_PREFIX: string = "CurrentUserFilterWidgetOptions.vo_field_ref.advanced_mode_placeholder.";

public static get_selected_fields(page_widget: DashboardPageWidgetVO): { [api_type_id: string]: { [field_id: string]: boolean } } {
let res: { [api_type_id: string]: { [field_id: string]: boolean } } = {};

let options: CurrentUserFilterWidgetOptionsVO = (page_widget && page_widget.json_options) ? JSON.parse(page_widget.json_options) : null;
if ((!options) || (!options.vo_field_ref)) {
return res;
}

if ((!options.vo_field_ref.api_type_id) || (!options.vo_field_ref.field_id)) {
return res;
}

if (!res[options.vo_field_ref.api_type_id]) {
res[options.vo_field_ref.api_type_id] = {};
}

res[options.vo_field_ref.api_type_id][options.vo_field_ref.field_id] = true;

return res;
}

public constructor(
public vo_field_ref?: VOFieldRefVO,
public hide_filter?: boolean,
) {
super();
}

public get_placeholder_name_code_text(page_widget_id: number): string {

if ((!this.vo_field_ref) || (!page_widget_id)) {
return null;
}

return CurrentUserFilterWidgetOptionsVO.VO_FIELD_REF_PLACEHOLDER_CODE_PREFIX +
`${page_widget_id}.` +
`${this.vo_field_ref.api_type_id}.` +
this.vo_field_ref.field_id;
}

public get_advanced_mode_placeholder_code_text(page_widget_id: number): string {

if ((!this.vo_field_ref) || (!page_widget_id)) {
return null;
}

return CurrentUserFilterWidgetOptionsVO.VO_FIELD_REF_ADVANCED_MODE_PLACEHOLDER_CODE_PREFIX + page_widget_id + '.' + this.vo_field_ref.api_type_id + '.' + this.vo_field_ref.field_id;
}

public async get_all_exportable_name_code_and_translation(page_id: number, page_widget_id: number): Promise<{ [current_code_text: string]: string }> {
let res: { [exportable_code_text: string]: string } = {};

let placeholder_name_code_text: string = this.get_placeholder_name_code_text(page_widget_id);
if (placeholder_name_code_text) {

res[placeholder_name_code_text] =
CurrentUserFilterWidgetOptionsVO.VO_FIELD_REF_PLACEHOLDER_CODE_PREFIX +
'{{IMPORT:' + DashboardPageWidgetVO.API_TYPE_ID + ':' + page_widget_id + '}}' +
'.' + this.vo_field_ref.api_type_id + '.' + this.vo_field_ref.field_id;
}

let advanced_mode_placeholder_code_text: string = this.get_advanced_mode_placeholder_code_text(page_widget_id);
if (advanced_mode_placeholder_code_text) {

res[advanced_mode_placeholder_code_text] =
CurrentUserFilterWidgetOptionsVO.VO_FIELD_REF_ADVANCED_MODE_PLACEHOLDER_CODE_PREFIX +
'{{IMPORT:' + DashboardPageWidgetVO.API_TYPE_ID + ':' + page_widget_id + '}}' +
'.' + this.vo_field_ref.api_type_id + '.' + this.vo_field_ref.field_id;
}
return res;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class DashboardWidgetVO implements IDistantVOBase, IWeightedItem,
public static WIDGET_NAME_monthfilter: string = 'monthfilter';
public static WIDGET_NAME_advanceddatefilter: string = 'advanceddatefilter';
public static WIDGET_NAME_yearfilter: string = 'yearfilter';
public static WIDGET_NAME_currentuserfilter: string = 'currentuserfilter';
public static WIDGET_NAME_validationfilters: string = 'validationfilters';
public static WIDGET_NAME_savefavoritesfilters: string = 'savefavoritesfilters';
public static WIDGET_NAME_showfavoritesfilters: string = 'showfavoritesfilters';
Expand Down
11 changes: 11 additions & 0 deletions src/shared/modules/DashboardBuilder/vos/FieldFiltersVO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ContextFilterVO from "../../ContextFilter/vos/ContextFilterVO";

/**
* FieldFiltersVO
* - Used to describe api_field_filters of dashboard and dashboard_pages
* - api_field_filters is a JSON object of field filters
* - the actual api_type_id key may be a custom api_type_id (e.g. field_filters of dates)
*/
export default class FieldFiltersVO {
[api_type_id: string]: { [field_id: string]: ContextFilterVO }
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import SupervisionWidgetOptions from './widgets/supervision_widget/options/Super
import TableWidgetOptions from './widgets/table_widget/options/TableWidgetOptions';
import VarPieChartWidgetOptions from './widgets/var_pie_chart_widget/options/VarPieChartWidgetOptions';
import VarWidgetOptions from './widgets/var_widget/options/VarWidgetOptions';
import CurrentUserFilterWidgetOptionsVO from '../../../../shared/modules/DashboardBuilder/vos/CurrentUserFilterWidgetOptionsVO';
import VOFieldRefVO from '../../../../shared/modules/DashboardBuilder/vos/VOFieldRefVO';
import UserVO from '../../../../shared/modules/AccessPolicy/vos/UserVO';

export default class DashboardBuilderVueModuleBase extends VueModuleBase {

Expand Down Expand Up @@ -94,6 +97,7 @@ export default class DashboardBuilderVueModuleBase extends VueModuleBase {
await this.initializeWidget_MonthFilter();
await this.initializeWidget_YearFilter();
await this.initializeWidget_AdvancedDateFilter();
await this.initializeWidget_CurrentUserFilter();

await this.initializeWidget_VarPieChart();

Expand Down Expand Up @@ -310,6 +314,36 @@ export default class DashboardBuilderVueModuleBase extends VueModuleBase {
Vue.component('Advanceddatefilterwidgeticoncomponent', () => import('./widgets/advanced_date_filter_widget/icon/AdvancedDateFilterWidgetIconComponent'));
}

private async initializeWidget_CurrentUserFilter() {
let CurrentUserFilter = new DashboardWidgetVO();

CurrentUserFilter.default_height = 5;
CurrentUserFilter.default_width = 2;
CurrentUserFilter.name = DashboardWidgetVO.WIDGET_NAME_currentuserfilter;
CurrentUserFilter.widget_component = 'Currentuserfilterwidgetcomponent';
CurrentUserFilter.options_component = 'Currentuserfilterwidgetoptionscomponent';
CurrentUserFilter.weight = 6;
CurrentUserFilter.default_background = '#f5f5f5';
CurrentUserFilter.icon_component = 'Currentuserfilterwidgeticoncomponent';
CurrentUserFilter.is_filter = true;

await DashboardBuilderWidgetsController.getInstance().registerWidget(
CurrentUserFilter,
() => new CurrentUserFilterWidgetOptionsVO(
new VOFieldRefVO().from({
api_type_id: UserVO.API_TYPE_ID,
field_id: "id"
}),
true
),
CurrentUserFilterWidgetOptionsVO.get_selected_fields
);

Vue.component('Currentuserfilterwidgetcomponent', () => import('./widgets/current_user_filter_widget/CurrentUserFilterWidgetComponent'));
Vue.component('Currentuserfilterwidgetoptionscomponent', () => import('./widgets/current_user_filter_widget/options/CurrentUserFilterWidgetOptionsComponent'));
Vue.component('Currentuserfilterwidgeticoncomponent', () => import('./widgets/current_user_filter_widget/icon/CurrentUserFilterWidgetIconComponent'));
}

private async initializeWidget_VarPieChart() {
let VarPieChart = new DashboardWidgetVO();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.current_user_filter_widget_component
template(v-if='vo_field_ref')
.vo_field_ref_label {{ vo_field_ref_label }}
.vo_field_ref_value
template(v-if='is_type_string')
input(v-model='current_data_filter.string_value' type='text' disabled='disabled')
template(v-else-if='is_type_number')
input(v-model='current_data_filter.numeric_value' type='text' disabled='disabled')
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.current_user_filter_widget_component {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import Component from 'vue-class-component';
import { Prop, Watch } from 'vue-property-decorator';
import CurrentUserFilterWidgetOptionsVO from '../../../../../../shared/modules/DashboardBuilder/vos/CurrentUserFilterWidgetOptionsVO';
import DashboardPageWidgetVO from '../../../../../../shared/modules/DashboardBuilder/vos/DashboardPageWidgetVO';
import ContextFilterVO from '../../../../../../shared/modules/ContextFilter/vos/ContextFilterVO';
import VOFieldRefVO from '../../../../../../shared/modules/DashboardBuilder/vos/VOFieldRefVO';
import UserVO from '../../../../../../shared/modules/AccessPolicy/vos/UserVO';
import { ModuleTranslatableTextGetter } from '../../../InlineTranslatableText/TranslatableTextStore';
import { ModuleDashboardPageAction } from '../../page/DashboardPageStore';
import ConsoleHandler from '../../../../../../shared/tools/ConsoleHandler';
import VueComponentBase from '../../../VueComponentBase';
import VOFieldRefVOTypeHandler from '../../../../../../shared/modules/DashboardBuilder/handlers/VOFieldRefVOTypeHandler';
import ContextFilterVOManager from '../../../../../../shared/modules/ContextFilter/manager/ContextFilterVOManager';
import VOsTypesManager from '../../../../../../shared/modules/VO/manager/VOsTypesManager';
import DataFilterOption from '../../../../../../shared/modules/DataRender/vos/DataFilterOption';
import ModuleTableField from '../../../../../../shared/modules/ModuleTableField';
import './CurrentUserFilterWidgetComponent.scss';

@Component({
template: require('./CurrentUserFilterWidgetComponent.pug'),
components: {}
})
export default class CurrentUserFilterWidgetComponent extends VueComponentBase {

@ModuleDashboardPageAction
private set_active_field_filter: (param: { vo_type: string, field_id: string, active_field_filter: ContextFilterVO }) => void;

@ModuleTranslatableTextGetter
private get_flat_locale_translations: { [code_text: string]: string };

@Prop({ default: null })
private page_widget: DashboardPageWidgetVO;

private widget_options: CurrentUserFilterWidgetOptionsVO = null;

private current_data_filter: DataFilterOption = null; // the current user data filter
private user: UserVO = null; // the current user

/**
* Watch on page_widget
*
* @returns {void}
*/
@Watch('page_widget', { immediate: true })
private onchange_page_widget_options(): void {
this.user = this.data_user;
this.widget_options = this.get_widget_options();

this.init_data_filter();
}

/**
* Watch on widget_options
* - Shall happen first on component init or each time widget_options changes
* - Initialize the active_field_filter with the context_filter
*
* @returns {void}
*/
@Watch('widget_options', { immediate: true, deep: true })
private async onchange_widget_options(): Promise<void> {

if (!this.widget_options) {
return;
}

this.init_data_filter();
}

/**
* init_data_filter
* - Initialize the active_field_filter with the context_filter
*
* @returns {void}
*/
private init_data_filter(): void {

if (!this.widget_options) {
return;
}

this.current_data_filter = this.create_data_filter(this.vo_field_ref.field_id);

const context_filter = ContextFilterVOManager.get_context_filter_from_data_filter_option(
this.current_data_filter,
null,
this.field,
this.vo_field_ref
);

this.set_active_field_filter({
field_id: this.vo_field_ref.field_id,
vo_type: this.vo_field_ref.api_type_id,
active_field_filter: context_filter,
});
}

/**
* get_widget_options
* - Get widget options from page_widget json_options
*
* @returns {CurrentUserFilterWidgetOptionsVO}
*/
private get_widget_options(): CurrentUserFilterWidgetOptionsVO {
if (!this.page_widget) {
return null;
}

let options: CurrentUserFilterWidgetOptionsVO = null;
try {
if (this.page_widget.json_options?.length > 0) {
options = JSON.parse(this.page_widget.json_options) as CurrentUserFilterWidgetOptionsVO;
options = options ? new CurrentUserFilterWidgetOptionsVO().from(options) : null;
}
} catch (error) {
ConsoleHandler.error(error);
}

return options;
}

/**
* create_data_filter
* - Create a DataFilterOption from a value and a field_id (vo_field_ref)
*
* @param {string} field_id The field_id of the vo_field_ref may be id or name
* @returns {DataFilterOption}
*/
private create_data_filter(field_id: string): DataFilterOption {
let val: number = this.user[field_id]; // it may be id or login or name

if (!val) {
return null;
}

const data_filter = new DataFilterOption(
DataFilterOption.STATE_SELECTED,
this.t(this.field?.field_label ?? field_id),
val
);

if (this.is_type_string) {
data_filter.string_value = val.toString();
}

if (this.is_type_number) {
data_filter.numeric_value = val;
}

return data_filter;
}

get vo_field_ref(): VOFieldRefVO {
let options: CurrentUserFilterWidgetOptionsVO = this.widget_options;

if ((!options) || (!options.vo_field_ref)) {
return null;
}

return new VOFieldRefVO().from(options.vo_field_ref);
}

get field(): ModuleTableField<any> {
if (!this.vo_field_ref) {
return null;
}

const moduletable = VOsTypesManager.moduleTables_by_voType[this.vo_field_ref.api_type_id];
return moduletable.get_field_by_id(this.vo_field_ref.field_id);
}

get vo_field_ref_label(): string {
if ((!this.widget_options) || (!this.vo_field_ref)) {
return null;
}

return this.get_flat_locale_translations[this.vo_field_ref.get_translatable_name_code_text(this.page_widget.id)];
}

get is_type_string(): boolean {
return VOFieldRefVOTypeHandler.is_type_string(this.vo_field_ref);
}

get is_type_number(): boolean {
return VOFieldRefVOTypeHandler.is_type_number(this.vo_field_ref);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.current_user_filter_widget_icon
template(v-if='!widget')
template(v-else)
.fa-stack.fa-lg.fa-fw(aria-hidden='true')
i.fa-solid.fa-user.fa-stack-2x
i.fa-solid.fa-bullseye.fa-stack-1x
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.current_user_filter_widget_icon {
i.fa-solid.fa-bullseye.fa-stack-1x {
position: absolute;
left: 0px;
bottom: 0px;
width: auto;
height: auto;
color: dodgerblue;
background: transparent;
padding: 1px 3px;
margin: 0;
line-height: 1.1em;
}
}
Loading

0 comments on commit b040841

Please sign in to comment.