Skip to content

Commit 40a5df3

Browse files
author
Andrew Valums
committed
Added listElement option to qq.FileUploader to specify separate file list
1 parent e3377a6 commit 40a5df3

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

client/fileuploader.js

+21-27
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,9 @@ qq.FileUploader = function(o){
478478
// additional options
479479
qq.extend(this._options, {
480480
element: null,
481-
481+
// if set, will be used instead of qq-upload-list in template
482+
listElement: null,
483+
482484
template: '<div class="qq-uploader">' +
483485
'<div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>' +
484486
'<div class="qq-upload-button">Upload a file</div>' +
@@ -492,8 +494,8 @@ qq.FileUploader = function(o){
492494
'<span class="qq-upload-size"></span>' +
493495
'<a class="qq-upload-cancel" href="#">Cancel</a>' +
494496
'<span class="qq-upload-failed-text">Failed</span>' +
495-
'</li>',
496-
497+
'</li>',
498+
497499
classes: {
498500
// used to get elements from templates
499501
button: 'qq-upload-button',
@@ -516,10 +518,12 @@ qq.FileUploader = function(o){
516518
qq.extend(this._options, o);
517519

518520
this._element = this._options.element;
519-
this._element.innerHTML = this._options.template;
520-
this._classes = this._options.classes;
521+
this._element.innerHTML = this._options.template;
522+
this._listElement = this._options._listElement || this._find(this._element, 'list');
521523

522-
this._button = this._createUploadButton(this._getElement('button'));
524+
this._classes = this._options.classes;
525+
526+
this._button = this._createUploadButton(this._find(this._element, 'button'));
523527

524528
this._bindCancelEvent();
525529
this._setupDragDrop();
@@ -531,19 +535,9 @@ qq.extend(qq.FileUploader.prototype, qq.FileUploaderBasic.prototype);
531535
qq.extend(qq.FileUploader.prototype, {
532536
/**
533537
* Gets one of the elements listed in this._options.classes
534-
* First optional element is root for search (this._element default)
535-
* 1. this._getElement('button');
536-
* 2. this._getElement(item, 'file');
537538
**/
538-
_getElement: function(parent, type){
539-
if (typeof parent == 'string'){
540-
// parent was not passed
541-
type = parent;
542-
parent = this._element;
543-
}
544-
545-
var element = qq.getByClass(parent, this._options.classes[type])[0];
546-
539+
_find: function(parent, type){
540+
var element = qq.getByClass(parent, this._options.classes[type])[0];
547541
if (!element){
548542
throw new Error('element not found ' + type);
549543
}
@@ -552,7 +546,7 @@ qq.extend(qq.FileUploader.prototype, {
552546
},
553547
_setupDragDrop: function(){
554548
var self = this,
555-
dropArea = this._getElement('drop');
549+
dropArea = this._find(this._element, 'drop');
556550

557551
var dz = new qq.UploadDropZone({
558552
element: dropArea,
@@ -598,7 +592,7 @@ qq.extend(qq.FileUploader.prototype, {
598592
qq.FileUploaderBasic.prototype._onProgress.apply(this, arguments);
599593

600594
var item = this._getItemByFileId(id);
601-
var size = this._getElement(item, 'size');
595+
var size = this._find(item, 'size');
602596
size.style.display = 'inline';
603597

604598
var text;
@@ -615,8 +609,8 @@ qq.extend(qq.FileUploader.prototype, {
615609

616610
// mark completed
617611
var item = this._getItemByFileId(id);
618-
qq.remove(this._getElement(item, 'cancel'));
619-
qq.remove(this._getElement(item, 'spinner'));
612+
qq.remove(this._find(item, 'cancel'));
613+
qq.remove(this._find(item, 'spinner'));
620614

621615
if (result.success){
622616
qq.addClass(item, this._classes.success);
@@ -628,14 +622,14 @@ qq.extend(qq.FileUploader.prototype, {
628622
var item = qq.toElement(this._options.fileTemplate);
629623
item.qqFileId = id;
630624

631-
var fileElement = this._getElement(item, 'file');
625+
var fileElement = this._find(item, 'file');
632626
qq.setText(fileElement, this._formatFileName(fileName));
633-
this._getElement(item, 'size').style.display = 'none';
627+
this._find(item, 'size').style.display = 'none';
634628

635-
this._getElement('list').appendChild(item);
629+
this._listElement.appendChild(item);
636630
},
637631
_getItemByFileId: function(id){
638-
var item = this._getElement('list').firstChild;
632+
var item = this._listElement.firstChild;
639633

640634
// there can't be txt nodes in dynamically created list
641635
// and we can use nextSibling
@@ -649,7 +643,7 @@ qq.extend(qq.FileUploader.prototype, {
649643
**/
650644
_bindCancelEvent: function(){
651645
var self = this,
652-
list = this._getElement('list');
646+
list = this._listElement;
653647

654648
qq.attach(list, 'click', function(e){
655649
e = e || window.event;

tests/separate-file-list.htm

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5+
<link href="../client/fileuploader.css" rel="stylesheet" type="text/css">
6+
</head>
7+
<body>
8+
9+
<div id="demo"></div>
10+
<ul id="separate-list"></ul>
11+
12+
<script src="../client/fileuploader.js" type="text/javascript"></script>
13+
<script>
14+
function createUploader(){
15+
var uploader = new qq.FileUploader({
16+
element: document.getElementById('demo'),
17+
listElement: document.getElementById('separate-list'),
18+
action: '../client/do-nothing.htm'
19+
});
20+
}
21+
window.onload = createUploader;
22+
</script>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)