1
1
import { Injectable } from '@angular/core' ;
2
- import { Column , Extension , ExtensionName } from '../models/index' ;
2
+ import { Column , ExcelCopyBufferOption , Extension , ExtensionName , SelectedRange } from '../models/index' ;
3
3
import { ExtensionUtility } from './extensionUtility' ;
4
4
import { sanitizeHtmlToText } from '../services/utilities' ;
5
5
import { SharedService } from '../services/shared.service' ;
@@ -10,12 +10,15 @@ declare var $: any;
10
10
11
11
@Injectable ( )
12
12
export class CellExternalCopyManagerExtension implements Extension {
13
+ private _eventHandler : any = new Slick . EventHandler ( ) ;
13
14
private _extension : any ;
14
15
private _undoRedoBuffer : any ;
15
16
16
17
constructor ( private extensionUtility : ExtensionUtility , private sharedService : SharedService ) { }
17
18
18
19
dispose ( ) {
20
+ // unsubscribe all SlickGrid events
21
+ this . _eventHandler . unsubscribeAll ( ) ;
19
22
if ( this . _extension && this . _extension . destroy ) {
20
23
this . _extension . destroy ( ) ;
21
24
}
@@ -27,49 +30,34 @@ export class CellExternalCopyManagerExtension implements Extension {
27
30
this . extensionUtility . loadExtensionDynamically ( ExtensionName . cellExternalCopyManager ) ;
28
31
this . createUndoRedoBuffer ( ) ;
29
32
this . hookUndoShortcutKey ( ) ;
30
- let newRowIds = 0 ;
31
- const pluginOptions = {
32
- clipboardCommandHandler : ( editCommand : any ) => {
33
- this . _undoRedoBuffer . queueAndExecuteCommand . call ( this . _undoRedoBuffer , editCommand ) ;
34
- } ,
35
- dataItemColumnValueExtractor : ( item : any , columnDef : Column ) => {
36
- // when grid or cell is not editable, we will possibly evaluate the Formatter if it was passed
37
- // to decide if we evaluate the Formatter, we will use the same flag from Export which is "exportWithFormatter"
38
- if ( ! this . sharedService . gridOptions . editable || ! columnDef . editor ) {
39
- const isEvaluatingFormatter = ( columnDef . exportWithFormatter !== undefined ) ? columnDef . exportWithFormatter : ( this . sharedService . gridOptions . exportOptions && this . sharedService . gridOptions . exportOptions . exportWithFormatter ) ;
40
- if ( columnDef . formatter && isEvaluatingFormatter ) {
41
- const formattedOutput = columnDef . formatter ( 0 , 0 , item [ columnDef . field ] , columnDef , item , this . sharedService . grid ) ;
42
- if ( columnDef . sanitizeDataExport || ( this . sharedService . gridOptions . exportOptions && this . sharedService . gridOptions . exportOptions . sanitizeDataExport ) ) {
43
- let outputString = formattedOutput as string ;
44
- if ( formattedOutput && typeof formattedOutput === 'object' && formattedOutput . hasOwnProperty ( 'text' ) ) {
45
- outputString = formattedOutput . text ;
46
- }
47
- if ( outputString === null ) {
48
- outputString = '' ;
49
- }
50
- return sanitizeHtmlToText ( outputString ) ;
51
- }
52
- return formattedOutput ;
53
- }
54
- }
55
- // else use the default "dataItemColumnValueExtractor" from the plugin itself
56
- // we can do that by setting back the getter with null
57
- return null ;
58
- } ,
59
- readOnlyMode : false ,
60
- includeHeaderWhenCopying : false ,
61
- newRowCreator : ( count : number ) => {
62
- for ( let i = 0 ; i < count ; i ++ ) {
63
- const item = {
64
- id : 'newRow_' + newRowIds ++
65
- } ;
66
- this . sharedService . grid . getData ( ) . addItem ( item ) ;
67
- }
68
- }
69
- } ;
33
+
34
+ const pluginOptions = { ...this . getDefaultOptions ( ) , ...this . sharedService . gridOptions . excelCopyBufferOptions } as ExcelCopyBufferOption ;
70
35
this . sharedService . grid . setSelectionModel ( new Slick . CellSelectionModel ( ) ) ;
71
36
this . _extension = new Slick . CellExternalCopyManager ( pluginOptions ) ;
72
37
this . sharedService . grid . registerPlugin ( this . _extension ) ;
38
+
39
+ // hook to all possible events
40
+ if ( this . sharedService . grid && this . sharedService . gridOptions . excelCopyBufferOptions ) {
41
+ if ( this . sharedService . gridOptions . excelCopyBufferOptions . onExtensionRegistered ) {
42
+ this . sharedService . gridOptions . excelCopyBufferOptions . onExtensionRegistered ( this . _extension ) ;
43
+ }
44
+ this . _eventHandler . subscribe ( this . _extension . onCopyCells , ( e : any , args : { ranges : SelectedRange [ ] } ) => {
45
+ if ( this . sharedService . gridOptions . excelCopyBufferOptions && typeof this . sharedService . gridOptions . excelCopyBufferOptions . onCopyCells === 'function' ) {
46
+ this . sharedService . gridOptions . excelCopyBufferOptions . onCopyCells ( e , args ) ;
47
+ }
48
+ } ) ;
49
+ this . _eventHandler . subscribe ( this . _extension . onCopyCancelled , ( e : any , args : { ranges : SelectedRange [ ] } ) => {
50
+ if ( this . sharedService . gridOptions . excelCopyBufferOptions && typeof this . sharedService . gridOptions . excelCopyBufferOptions . onCopyCancelled === 'function' ) {
51
+ this . sharedService . gridOptions . excelCopyBufferOptions . onCopyCancelled ( e , args ) ;
52
+ }
53
+ } ) ;
54
+ this . _eventHandler . subscribe ( this . _extension . onPasteCells , ( e : any , args : { ranges : SelectedRange [ ] } ) => {
55
+ if ( this . sharedService . gridOptions . excelCopyBufferOptions && typeof this . sharedService . gridOptions . excelCopyBufferOptions . onPasteCells === 'function' ) {
56
+ this . sharedService . gridOptions . excelCopyBufferOptions . onPasteCells ( e , args ) ;
57
+ }
58
+ } ) ;
59
+ }
60
+
73
61
return this . _extension ;
74
62
}
75
63
return null ;
@@ -104,6 +92,53 @@ export class CellExternalCopyManagerExtension implements Extension {
104
92
} ;
105
93
}
106
94
95
+ /**
96
+ * @return default plugin (addon) options
97
+ */
98
+ private getDefaultOptions ( ) : ExcelCopyBufferOption {
99
+ let newRowIds = 0 ;
100
+
101
+ return {
102
+ clipboardCommandHandler : ( editCommand : any ) => {
103
+ this . _undoRedoBuffer . queueAndExecuteCommand . call ( this . _undoRedoBuffer , editCommand ) ;
104
+ } ,
105
+ dataItemColumnValueExtractor : ( item : any , columnDef : Column ) => {
106
+ // when grid or cell is not editable, we will possibly evaluate the Formatter if it was passed
107
+ // to decide if we evaluate the Formatter, we will use the same flag from Export which is "exportWithFormatter"
108
+ if ( ! this . sharedService . gridOptions . editable || ! columnDef . editor ) {
109
+ const isEvaluatingFormatter = ( columnDef . exportWithFormatter !== undefined ) ? columnDef . exportWithFormatter : ( this . sharedService . gridOptions . exportOptions && this . sharedService . gridOptions . exportOptions . exportWithFormatter ) ;
110
+ if ( columnDef . formatter && isEvaluatingFormatter ) {
111
+ const formattedOutput = columnDef . formatter ( 0 , 0 , item [ columnDef . field ] , columnDef , item , this . sharedService . grid ) ;
112
+ if ( columnDef . sanitizeDataExport || ( this . sharedService . gridOptions . exportOptions && this . sharedService . gridOptions . exportOptions . sanitizeDataExport ) ) {
113
+ let outputString = formattedOutput as string ;
114
+ if ( formattedOutput && typeof formattedOutput === 'object' && formattedOutput . hasOwnProperty ( 'text' ) ) {
115
+ outputString = formattedOutput . text ;
116
+ }
117
+ if ( outputString === null ) {
118
+ outputString = '' ;
119
+ }
120
+ return sanitizeHtmlToText ( outputString ) ;
121
+ }
122
+ return formattedOutput ;
123
+ }
124
+ }
125
+ // else use the default "dataItemColumnValueExtractor" from the plugin itself
126
+ // we can do that by setting back the getter with null
127
+ return null ;
128
+ } ,
129
+ readOnlyMode : false ,
130
+ includeHeaderWhenCopying : false ,
131
+ newRowCreator : ( count : number ) => {
132
+ for ( let i = 0 ; i < count ; i ++ ) {
133
+ const item = {
134
+ id : 'newRow_' + newRowIds ++
135
+ } ;
136
+ this . sharedService . grid . getData ( ) . addItem ( item ) ;
137
+ }
138
+ }
139
+ } ;
140
+ }
141
+
107
142
/** Attach an undo shortcut key hook that will redo/undo the copy buffer */
108
143
private hookUndoShortcutKey ( ) {
109
144
// undo shortcut
0 commit comments