raimund@725: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
raimund@725:  * Software engineering by Intevation GmbH
raimund@725:  *
raimund@725:  * This file is Free Software under the GNU GPL (v>=3)
raimund@725:  * and comes with ABSOLUTELY NO WARRANTY! Check out
raimund@725:  * the documentation coming with IMIS-Labordaten-Application for details.
raimund@725:  */
raimund@725: 
raimund@725: /**
raimund@725:  */
raimund@725: Ext.define('Lada.view.window.FileUpload', {
raimund@725:     extend: 'Ext.window.Window',
raimund@725: 
raimund@725:     layout: 'hbox',
raimund@725: 
raimund@725:     file: null,
raimund@725: 
raimund@725:     initComponent: function() {
raimund@725:         var me = this;
raimund@725:         this.browseButton = Ext.create('Ext.ux.upload.BrowseButton', {
raimund@725:             buttonText: 'Durchsuchen...',
raimund@725:             margin: '3, 3, 3, 3'
raimund@725:         });
raimund@725:         this.fileInput = Ext.create('Ext.form.field.Text', {
raimund@725:             allowBlank: false,
raimund@725:             emptyText: 'Wählen Sie eine Datei',
raimund@725:             hideLabel: true,
raimund@725:             margin: '3, 3, 3, 3'
raimund@725:         });
raimund@725:         this.items = [
raimund@725:             this.fileInput,
raimund@725:             this.browseButton
raimund@725:         ];
raimund@725:         this.buttons = [{
raimund@725:             text: 'Speichern',
raimund@725:             handler: this.uploadFile
raimund@725:         }, {
raimund@725:             text: 'Abbrechen',
raimund@725:             handler: this.abort
raimund@725:         }];
raimund@725:         this.on('afterrender', function() {
raimund@725:             this.browseButton.fileInputEl.dom.removeAttribute('multiple', '0');
raimund@725:         }, this);
raimund@725:         this.browseButton.on('fileselected', this.fileSelected, this);
raimund@725:         this.callParent(arguments);
raimund@725:     },
raimund@725: 
raimund@725:     abort: function(button) {
raimund@725:         var win = button.up('window');
raimund@725:         win.close();
raimund@725:     },
raimund@725: 
raimund@725:     fileSelected: function(input, file) {
raimund@725:         var item = Ext.create('Ext.ux.upload.Item', {
raimund@725:             fileApiObject: file[0]
raimund@725:         });
raimund@725:         this.fileInput.setValue(item.getName());
raimund@725:         this.file = item;
raimund@725:     },
raimund@725: 
raimund@725:     uploadFile: function(button) {
raimund@725:         var win = button.up('window');
raimund@725:         var uploader = Ext.create('Ext.ux.upload.uploader.ExtJsUploader', {
raimund@725:             extraHeaders: {
raimund@725:                 'X-OPENID-PARAMS': Lada.openIDParams
raimund@725:             },
raimund@725:             method: 'POST',
dustin@727:             url: 'lada-server/import/laf'
raimund@725:         });
raimund@725:         this.mon(uploader, 'uploadsuccess', win.uploadSuccess, win);
raimund@725:         this.mon(uploader, 'uploadfailure', win.uploadFailure, win);
raimund@725:         if (button.up('window').file !== null) {
raimund@725:             uploader.uploadItem(button.up('window').file);
raimund@725:         }
raimund@725:     },
raimund@725: 
raimund@730:     uploadSuccess: function(file, response) {
raimund@725:         this.close();
raimund@730:         var win = Ext.create('Lada.view.window.ImportResponse', {
raimund@730:             data: response.response.responseText,
raimund@730:             message: response.message,
raimund@730:             fileName: file.config.fileApiObject.name,
raimund@730:             title: 'Importergebnis'
raimund@730:         });
raimund@730:         win.show();
raimund@725:     },
raimund@725: 
raimund@730:     uploadFailure: function(file, response) {
raimund@725:         this.close();
raimund@730:         var win = Ext.create('Lada.view.window.ImportResponse', {
raimund@730:             data: response.response.responseText,
raimund@730:             message: response.message,
raimund@730:             fileName: file.config.fileApiObject.name,
raimund@730:             title: 'Importergebnis'
raimund@730:         });
raimund@730:         win.show();
raimund@725:     }
raimund@725: });