1- import ModalWindow from '@shopsys/administration/src/js/utils/modalWindow' ;
21import Translator from 'bazinga-translator' ;
32import Register from '../../common/utils/Register' ;
43import FormChangeInfo from './FormChangeInfo' ;
4+ import MultiplePicker from './MultiplePicker' ;
55
6- window . FilePickerInstances = { } ;
7-
8- export default class FilePicker {
6+ export default class FilePicker extends MultiplePicker {
97 constructor ( $picker ) {
10- this . instanceId = Object . keys ( window . FilePickerInstances ) . length ;
11- window . FilePickerInstances [ this . instanceId ] = this ;
8+ super ( $picker ) ;
129
13- this . $picker = $picker ;
1410 this . $addButton = $ ( `[data-picker-target="${ $picker . attr ( 'id' ) } "]` ) ;
15- this . $itemsContainer = $picker . find ( '.js-picker-items' ) ;
16-
17- this . $addButton . click ( event => this . openPicker ( event ) ) ;
18- this . initExistingItem ( ) ;
11+ this . $addButton . click ( ( ) => this . openPickerWindow ( ) ) ;
1912 }
2013
21- initExistingItem ( ) {
22- const $existingItem = this . $itemsContainer . find ( '.js-picker-item' ) ;
23-
24- if ( $existingItem . length > 0 ) {
25- $existingItem . find ( '.js-picker-item-button-delete' ) . click ( ( ) => {
26- this . removeItem ( $existingItem ) ;
27- } ) ;
28- }
14+ getPickerWindowTitle ( ) {
15+ return Translator . trans ( 'Select file' ) ;
2916 }
3017
31- openPicker ( event ) {
32- event . preventDefault ( ) ;
33-
34- const url = this . $picker . data ( 'picker-url' ) . replace ( '__js_instance_id__' , this . instanceId ) ;
35- const iframeContent = `<iframe src="${ url } " style="width: 100%; height: 800px; border: none;"></iframe>` ;
36-
37- this . modal = new ModalWindow ( {
38- content : iframeContent ,
39- title : Translator . trans ( 'Select file' ) ,
40- size : 'xl' ,
41- buttons : [ { text : Translator . trans ( 'Cancel' ) } ] ,
42- } ) ;
43-
44- return false ;
18+ getPickerWindowButtons ( ) {
19+ return [ { text : Translator . trans ( 'Cancel' ) } ] ;
4520 }
4621
4722 addItem ( $selectedElement ) {
48- this . $itemsContainer . find ( '.js-picker-item' ) . remove ( ) ;
23+ const $existingItem = this . $itemsContainer . find ( '.js-picker-item' ) ;
24+
25+ if ( $existingItem . length > 0 ) {
26+ this . removeItem ( $existingItem ) ;
27+ }
4928
5029 const itemHtml = this . $picker . data ( 'picker-prototype' ) . replace ( / _ _ n a m e _ _ / g, 0 ) ;
5130 const $item = $ ( $ . parseHTML ( itemHtml ) ) ;
5231
5332 FilePicker . populateItemFromSelection ( $item , $selectedElement ) ;
5433
5534 this . $itemsContainer . append ( $item ) ;
56-
57- $item . find ( '.js-picker-item-button-delete' ) . click ( ( ) => {
58- this . removeItem ( $item ) ;
59- } ) ;
60-
35+ this . initItem ( $item ) ;
6136 FormChangeInfo . showInfo ( ) ;
6237
6338 if ( this . modal ?. element && typeof this . modal . element . modal === 'function' ) {
6439 this . modal . element . modal ( 'hide' ) ;
6540 }
6641 }
6742
68- removeItem ( $item ) {
69- $item . remove ( ) ;
70- FormChangeInfo . showInfo ( ) ;
71- }
72-
7343 static populateItemFromSelection ( $item , $selectedElement ) {
7444 $item . find ( '.js-picker-item-input' ) . val ( $selectedElement . data ( 'picker-id' ) ) ;
7545 $item . find ( '.js-picker-item-thumbnail' ) . html ( $selectedElement . data ( 'picker-thumbnail' ) ) ;
@@ -85,7 +55,7 @@ export default class FilePicker {
8555 }
8656
8757 static onClickSelectFile ( instanceId , $btnElement ) {
88- const pickerInstance = window . parent . FilePickerInstances [ instanceId ] ;
58+ const pickerInstance = window . parent . PickerInstances [ instanceId ] ;
8959
9060 if ( ! pickerInstance ) {
9161 console . error ( `FilePicker instance ${ instanceId } not found.` ) ;
0 commit comments