Skip to content

Commit 86b1214

Browse files
Ghislain BeaulacGhislain Beaulac
Ghislain Beaulac
authored and
Ghislain Beaulac
committed
feat(translate): make ngx-translate an optional dependency
1 parent f9f4de4 commit 86b1214

14 files changed

+137
-99
lines changed

src/app/examples/grid-menu.component.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class GridMenuComponent implements OnInit {
1616
<li>You can change the Grid Menu icon, for example "fa-ellipsis-v"&nbsp;&nbsp;<span class="fa fa-ellipsis-v"></span>&nbsp;&nbsp;(which is shown in this example)</li>
1717
<li>By default the Grid Menu shows all columns which you can show/hide them</li>
1818
<li>You can configure multiple custom "commands" to show up in the Grid Menu and use the "onGridMenuCommand()" callback</li>
19-
<li>Doing a "right+click" over any column header will also provide a way to show/hide a column (via the Column Picker Plugin)</li>
19+
<li>Doing a "right + click" over any column header will also provide a way to show/hide a column (via the Column Picker Plugin)</li>
2020
<li><i class="fa fa-arrow-down"></i> You can also show the Grid Menu anywhere on your page</li>
2121
</ul>
2222
`;

src/app/modules/angular-slickgrid/components/angular-slickgrid.component.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import 'slickgrid/slick.grid';
77
import 'slickgrid/slick.dataview';
88

99
// ...then everything else...
10-
import { AfterViewInit, Component, ElementRef, EventEmitter, Inject, Injectable, Input, Output, OnDestroy, OnInit } from '@angular/core';
10+
import { AfterViewInit, Component, ElementRef, EventEmitter, Inject, Injectable, Input, Output, OnDestroy, OnInit, Optional } from '@angular/core';
1111
import { TranslateService } from '@ngx-translate/core';
1212
import { GlobalGridOptions } from './../global-grid-options';
1313
import { titleCase, unsubscribeAllObservables } from './../services/utilities';
@@ -168,7 +168,7 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
168168
private resizer: ResizerService,
169169
private sharedService: SharedService,
170170
private sortService: SortService,
171-
private translate: TranslateService,
171+
@Optional() private translate: TranslateService,
172172
@Inject('config') private forRootConfig: GridOption
173173
) { }
174174

@@ -400,16 +400,18 @@ export class AngularSlickgridComponent implements AfterViewInit, OnDestroy, OnIn
400400

401401
bindDifferentHooks(grid: any, gridOptions: GridOption, dataView: any) {
402402
// on locale change, we have to manually translate the Headers, GridMenu
403-
this.subscriptions.push(
404-
this.translate.onLangChange.subscribe((event) => {
405-
if (gridOptions.enableTranslate) {
406-
this.extensionService.translateColumnHeaders();
407-
this.extensionService.translateColumnPicker();
408-
this.extensionService.translateGridMenu();
409-
this.extensionService.translateHeaderMenu();
410-
}
411-
})
412-
);
403+
if (this.translate && this.translate.onLangChange) {
404+
this.subscriptions.push(
405+
this.translate.onLangChange.subscribe((event) => {
406+
if (gridOptions.enableTranslate) {
407+
this.extensionService.translateColumnHeaders();
408+
this.extensionService.translateColumnPicker();
409+
this.extensionService.translateGridMenu();
410+
this.extensionService.translateHeaderMenu();
411+
}
412+
})
413+
);
414+
}
413415

414416
// if user entered some Columns "presets", we need to reflect them all in the grid
415417
if (gridOptions.presets && Array.isArray(gridOptions.presets.columns) && gridOptions.presets.columns.length > 0) {

src/app/modules/angular-slickgrid/constants.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
export class Constants {
2+
static TEXT_ALL_SELECTED = 'All Selected';
23
static TEXT_CANCEL = 'Cancel';
34
static TEXT_CLEAR_ALL_FILTERS = 'Clear All Filters';
45
static TEXT_CLEAR_ALL_SORTING = 'Clear All Sorting';
6+
static TEXT_CONTAINS = 'Contains';
57
static TEXT_COLUMNS = 'Columns';
68
static TEXT_COMMANDS = 'Commands';
9+
static TEXT_EQUALS = 'Equals';
10+
static TEXT_ENDS_WITH = 'Ends With';
711
static TEXT_EXPORT_IN_CSV_FORMAT = 'Export in CSV format';
812
static TEXT_EXPORT_IN_TEXT_FORMAT = 'Export in Text format (Tab delimited)';
913
static TEXT_FORCE_FIT_COLUMNS = 'Force fit columns';
@@ -13,11 +17,14 @@ export class Constants {
1317
static TEXT_REMOVE_FILTER = 'Remove Filter';
1418
static TEXT_REMOVE_SORT = 'Remove Sort';
1519
static TEXT_SAVE = 'Save';
20+
static TEXT_SELECT_ALL = 'Select All';
1621
static TEXT_SYNCHRONOUS_RESIZE = 'Synchronous resize';
1722
static TEXT_SORT_ASCENDING = 'Sort Ascending';
1823
static TEXT_SORT_DESCENDING = 'Sort Descending';
24+
static TEXT_STARTS_WITH = 'Starts With';
1925
static TEXT_TOGGLE_FILTER_ROW = 'Toggle Filter Row';
2026
static TEXT_TOGGLE_PRE_HEADER_ROW = 'Toggle Pre-Header Row';
27+
static TEXT_X_OF_Y_SELECTED = '# of % selected';
2128
static VALIDATION_REQUIRED_FIELD = 'Field is required';
2229
static VALIDATION_EDITOR_VALID_NUMBER = 'Please enter a valid number';
2330
static VALIDATION_EDITOR_VALID_INTEGER = 'Please enter a valid integer number';

src/app/modules/angular-slickgrid/extensions/extensionUtility.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Optional } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import { Constants } from '../constants';
44
import { ExtensionName } from '../models/index';
@@ -8,7 +8,11 @@ declare function require(name: string);
88

99
@Injectable()
1010
export class ExtensionUtility {
11-
constructor(private sharedService: SharedService, private translate: TranslateService) { }
11+
constructor(private sharedService: SharedService, @Optional() private translate: TranslateService) {
12+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
13+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
14+
}
15+
}
1216

1317
/**
1418
* Remove a column from the grid by it's index in the grid
@@ -127,7 +131,7 @@ export class ExtensionUtility {
127131
if (Array.isArray(items)) {
128132
for (const item of items) {
129133
if (item[inputKey]) {
130-
item[outputKey] = this.translate.instant(item[inputKey]);
134+
item[outputKey] = this.translate && this.translate && this.translate.instant && this.translate.instant(item[inputKey]);
131135
}
132136
}
133137
}

src/app/modules/angular-slickgrid/extensions/gridMenuExtension.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Optional } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import { Constants } from '../constants';
44
import {
@@ -37,8 +37,11 @@ export class GridMenuExtension implements Extension {
3737
private filterService: FilterService,
3838
private sharedService: SharedService,
3939
private sortService: SortService,
40-
private translate: TranslateService,
40+
@Optional() private translate: TranslateService,
4141
) {
42+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
43+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
44+
}
4245
this._eventHandler = new Slick.EventHandler();
4346
}
4447

src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Optional } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import { Constants } from '../constants';
44
import {
@@ -31,8 +31,11 @@ export class HeaderMenuExtension implements Extension {
3131
private filterService: FilterService,
3232
private sharedService: SharedService,
3333
private sortService: SortService,
34-
private translate: TranslateService,
34+
@Optional() private translate: TranslateService,
3535
) {
36+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.enableTranslate && (!this.translate || !this.translate.instant)) {
37+
throw new Error('[Angular-Slickgrid] requires "ngx-translate" to be installed and configured when the grid option "enableTranslate" is enabled.');
38+
}
3639
this._eventHandler = new Slick.EventHandler();
3740
}
3841

src/app/modules/angular-slickgrid/filters/compoundDateFilter.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
SearchTerm,
1414
} from './../models/index';
1515
import Flatpickr from 'flatpickr';
16+
import { Optional } from '@angular/core';
1617

1718
// use Flatpickr from import or 'require', whichever works first
1819
declare function require(name: string): any;
@@ -35,7 +36,7 @@ export class CompoundDateFilter implements Filter {
3536
columnDef: Column;
3637
callback: FilterCallback;
3738

38-
constructor(private translate: TranslateService) { }
39+
constructor(@Optional() private translate: TranslateService) { }
3940

4041
/** Getter for the Grid Options pulled through the Grid Object */
4142
private get gridOptions(): GridOption {
@@ -123,7 +124,7 @@ export class CompoundDateFilter implements Filter {
123124
private buildDatePickerInput(searchTerm?: SearchTerm) {
124125
const inputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.type || FieldType.dateIso);
125126
const outputFormat = mapFlatpickrDateFormatWithFieldType(this.columnDef.outputType || this.columnDef.type || FieldType.dateUtc);
126-
let currentLocale = this.translate.currentLang || 'en';
127+
let currentLocale = this.translate && this.translate.currentLang || 'en';
127128
if (currentLocale.length > 2) {
128129
currentLocale = currentLocale.substring(0, 2);
129130
}
@@ -180,12 +181,12 @@ export class CompoundDateFilter implements Filter {
180181
private getOptionValues(): { operator: OperatorString, description: string }[] {
181182
return [
182183
{ operator: '' as OperatorString, description: '' },
183-
{ operator: '=' as OperatorString, description: '' },
184-
{ operator: '<' as OperatorString, description: '' },
185-
{ operator: '<=' as OperatorString, description: '' },
186-
{ operator: '>' as OperatorString, description: '' },
187-
{ operator: '>=' as OperatorString, description: '' },
188-
{ operator: '<>' as OperatorString, description: '' }
184+
{ operator: '=' as OperatorString, description: '=' },
185+
{ operator: '<' as OperatorString, description: '<' },
186+
{ operator: '<=' as OperatorString, description: '<=' },
187+
{ operator: '>' as OperatorString, description: '>' },
188+
{ operator: '>=' as OperatorString, description: '>=' },
189+
{ operator: '<>' as OperatorString, description: '<>' }
189190
];
190191
}
191192

src/app/modules/angular-slickgrid/filters/compoundInputFilter.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Constants } from './../constants';
12
import { TranslateService } from '@ngx-translate/core';
23
import { FieldType } from './../models/index';
34
import {
@@ -149,19 +150,19 @@ export class CompoundInputFilter implements Filter {
149150
switch (type) {
150151
case FieldType.string:
151152
optionValues = [
152-
{ operator: '' as OperatorString, description: this.translate.instant('CONTAINS') },
153-
{ operator: '=' as OperatorString, description: this.translate.instant('EQUALS') },
154-
{ operator: 'a*' as OperatorString, description: this.translate.instant('STARTS_WITH') },
155-
{ operator: '*z' as OperatorString, description: this.translate.instant('ENDS_WITH') },
153+
{ operator: '' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('CONTAINS') || Constants.TEXT_CONTAINS },
154+
{ operator: '=' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('EQUALS') || Constants.TEXT_EQUALS },
155+
{ operator: 'a*' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('STARTS_WITH') || Constants.TEXT_STARTS_WITH },
156+
{ operator: '*z' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('ENDS_WITH') || Constants.TEXT_CONTAINS },
156157
/*
157-
{ operator: 'IN' as OperatorString, description: this.translate.instant('IN_COLLECTION_SEPERATED_BY_COMMA') },
158-
{ operator: 'NIN' as OperatorString, description: this.translate.instant('NOT_IN_COLLECTION_SEPERATED_BY_COMMA') },
158+
{ operator: 'IN' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('IN_COLLECTION_SEPERATED_BY_COMMA') },
159+
{ operator: 'NIN' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('NOT_IN_COLLECTION_SEPERATED_BY_COMMA') },
159160
*/
160161
];
161162
break;
162163
default:
163164
optionValues = [
164-
{ operator: '' as OperatorString, description: this.translate.instant('CONTAINS') },
165+
{ operator: '' as OperatorString, description: this.translate && this.translate.instant && this.translate.instant('CONTAINS') || Constants.TEXT_CONTAINS },
165166
{ operator: '=' as OperatorString, description: '' },
166167
{ operator: '<' as OperatorString, description: '' },
167168
{ operator: '<=' as OperatorString, description: '' },

src/app/modules/angular-slickgrid/filters/filterFactory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable } from '@angular/core';
1+
import { Injectable, Optional } from '@angular/core';
22
import { Filter } from '../models/filter.interface';
33
import { ColumnFilter } from '../models';
44
import { SlickgridConfig } from '../slickgrid-config';
@@ -12,7 +12,7 @@ export class FilterFactory {
1212
*/
1313
private _options: any;
1414

15-
constructor(private config: SlickgridConfig, private translate: TranslateService, private collectionService: CollectionService) {
15+
constructor(private config: SlickgridConfig, @Optional() private translate: TranslateService, private collectionService: CollectionService) {
1616
this._options = this.config.options;
1717
}
1818

src/app/modules/angular-slickgrid/filters/nativeSelectFilter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
OperatorString,
99
SearchTerm,
1010
} from './../models/index';
11+
import { Optional } from '@angular/core';
1112

1213
// using external non-typed js libraries
1314
declare var $: any;
@@ -21,7 +22,7 @@ export class NativeSelectFilter implements Filter {
2122
columnDef: Column;
2223
callback: FilterCallback;
2324

24-
constructor(private translate: TranslateService) { }
25+
constructor(@Optional() private translate: TranslateService) { }
2526

2627
get operator(): OperatorType | OperatorString {
2728
return (this.columnDef && this.columnDef.filter && this.columnDef.filter.operator) || OperatorType.equal;

0 commit comments

Comments
 (0)