在 sencha extjs 中疯狂拖放
crazy drag&drop in sencha extjs
我是sencha extjs的初学者。
我的sencha extjs拖放有问题,我的代码是基于官方的拖放示例,但是不能完全工作,也就是说,它只能通过左右拖放文件来工作和自下而上,当我从上到下拖动时,它变得疯狂。
注意:官方示例通过从面板的任一侧拖动文件来工作。
Code for the view
Ext.define('KitchenSink.view.drag.File', {
extend: 'Ext.panel.Panel',
xtype: 'drag.file',
controller: 'drag.file',
requires: [
'Ext.layout.container.Fit'
],
title: 'File Drag',
draggable: true,
width: 500,
height: 300,
bodyPadding: 5,
layout: 'fit',
bodyCls: 'drag-file-ct',
html: '<div class="drag-file-label">' +
'Drop some files here' +
'</div>' +
'<div class="drag-file-icon"></div>'
});
Code for controllerView
Ext.define('KitchenSink.view.drag.FileViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.drag.file',
requires: ['Ext.drag.Target'],
afterRender: function(view) {
var body = view.body;
if (window.File && window.FileList && window.FileReader) {
this.target = new Ext.drag.Target({
element: body,
listeners: {
scope: this,
dragenter: this.onDragEnter,
dragleave: this.onDragLeave,
drop: this.onDrop
}
});
} else {
body.down('.drag-file-label').setHtml(
'File dragging is not supported by your browser'
);
body.el.addCls('nosupport');
}
},
onDragEnter: function() {
this.getView().body.addCls('active');
//console.log("Enter");
},
onDragLeave: function() {
this.getView().body.removeCls('active');
//console.log("Fuera");
},
onDrop: function(target, info) {
var view = this.getView(),
body = view.body,
icon = body.down('.drag-file-icon');
body.removeCls('active').addCls('dropped');
icon.addCls('fa-spin');
var me = this,
files = info.files,
len = files.length,
s;
if (len > 1) {
s = 'Dropped ' + len + ' files.';
} else {
s = 'Dropped ' + files[0].name;
}
Ext.toast({
html: s,
closable: false,
align: 't',
slideInDuration: 400,
minWidth: 400
});
me.timer = Ext.defer(function() {
if (!view.destroyed) {
icon.removeCls('fa-spin');
icon.fadeOut({
callback: function() {
body.removeCls('dropped');
icon.setOpacity(1);
icon.show();
}
});
}
me.timer = null;
}, 2000);
},
destroy: function() {
Ext.undefer(this.timer);
this.target = Ext.destroy(this.target);
this.callParent();
}
});
一开始我在 fiddle 中也遇到了这个问题,但后来我用元素 id asdasd
替换了目标元素 body
并且它起作用了。好像是关于图层的。
我认为问题在于您错过了额外的 CSS 规则。例如,您必须抑制所有指针事件,否则在图标容器顶部拖动将导致触发 "dragleave" 事件。
这是您需要的整套规则,以及 fiddle 示例:
Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2hac
.drag-file-ct .x-innerhtml {
pointer-events: none;
}
.drag-file-fadeout {
opacity: 0;
-webkit-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.drag-file-label {
font-size: 18px;
padding-top: 30px;
text-align: center;
pointer-events: none;
}
.drag-file-icon {
font-family: FontAwesome;
display: block;
font-size: 64px;
margin-top: 50px;
text-align: center;
pointer-events: none;
}
.active .drag-file-icon {
display: block;
}
.active .drag-file-icon:after {
content: "\f058";
color: #8BC34A;
}
.dropped .drag-file-icon {
display: block;
}
.dropped .drag-file-icon:after {
content: "\f013";
color: #9E9E9E;
}
.nosupport .drag-file-icon {
display: block;
}
.nosupport .drag-file-icon:after {
content: "\f119";
color: #9E9E9E;
}
我是sencha extjs的初学者。
我的sencha extjs拖放有问题,我的代码是基于官方的拖放示例,但是不能完全工作,也就是说,它只能通过左右拖放文件来工作和自下而上,当我从上到下拖动时,它变得疯狂。
注意:官方示例通过从面板的任一侧拖动文件来工作。
Code for the view
Ext.define('KitchenSink.view.drag.File', {
extend: 'Ext.panel.Panel',
xtype: 'drag.file',
controller: 'drag.file',
requires: [
'Ext.layout.container.Fit'
],
title: 'File Drag',
draggable: true,
width: 500,
height: 300,
bodyPadding: 5,
layout: 'fit',
bodyCls: 'drag-file-ct',
html: '<div class="drag-file-label">' +
'Drop some files here' +
'</div>' +
'<div class="drag-file-icon"></div>'
});
Code for controllerView
Ext.define('KitchenSink.view.drag.FileViewController', {
extend: 'Ext.app.ViewController',
alias: 'controller.drag.file',
requires: ['Ext.drag.Target'],
afterRender: function(view) {
var body = view.body;
if (window.File && window.FileList && window.FileReader) {
this.target = new Ext.drag.Target({
element: body,
listeners: {
scope: this,
dragenter: this.onDragEnter,
dragleave: this.onDragLeave,
drop: this.onDrop
}
});
} else {
body.down('.drag-file-label').setHtml(
'File dragging is not supported by your browser'
);
body.el.addCls('nosupport');
}
},
onDragEnter: function() {
this.getView().body.addCls('active');
//console.log("Enter");
},
onDragLeave: function() {
this.getView().body.removeCls('active');
//console.log("Fuera");
},
onDrop: function(target, info) {
var view = this.getView(),
body = view.body,
icon = body.down('.drag-file-icon');
body.removeCls('active').addCls('dropped');
icon.addCls('fa-spin');
var me = this,
files = info.files,
len = files.length,
s;
if (len > 1) {
s = 'Dropped ' + len + ' files.';
} else {
s = 'Dropped ' + files[0].name;
}
Ext.toast({
html: s,
closable: false,
align: 't',
slideInDuration: 400,
minWidth: 400
});
me.timer = Ext.defer(function() {
if (!view.destroyed) {
icon.removeCls('fa-spin');
icon.fadeOut({
callback: function() {
body.removeCls('dropped');
icon.setOpacity(1);
icon.show();
}
});
}
me.timer = null;
}, 2000);
},
destroy: function() {
Ext.undefer(this.timer);
this.target = Ext.destroy(this.target);
this.callParent();
}
});
一开始我在 fiddle 中也遇到了这个问题,但后来我用元素 id asdasd
替换了目标元素 body
并且它起作用了。好像是关于图层的。
我认为问题在于您错过了额外的 CSS 规则。例如,您必须抑制所有指针事件,否则在图标容器顶部拖动将导致触发 "dragleave" 事件。
这是您需要的整套规则,以及 fiddle 示例: Fiddle: https://fiddle.sencha.com/#view/editor&fiddle/2hac
.drag-file-ct .x-innerhtml {
pointer-events: none;
}
.drag-file-fadeout {
opacity: 0;
-webkit-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.drag-file-label {
font-size: 18px;
padding-top: 30px;
text-align: center;
pointer-events: none;
}
.drag-file-icon {
font-family: FontAwesome;
display: block;
font-size: 64px;
margin-top: 50px;
text-align: center;
pointer-events: none;
}
.active .drag-file-icon {
display: block;
}
.active .drag-file-icon:after {
content: "\f058";
color: #8BC34A;
}
.dropped .drag-file-icon {
display: block;
}
.dropped .drag-file-icon:after {
content: "\f013";
color: #9E9E9E;
}
.nosupport .drag-file-icon {
display: block;
}
.nosupport .drag-file-icon:after {
content: "\f119";
color: #9E9E9E;
}