2012-03-23 16:55:55 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-05-22 13:14:40 +00:00
|
|
|
Ox.FileInput <f:Ox.Element> File Input
|
|
|
|
([options[, self]]) -> <o> File Input
|
2012-05-21 10:38:18 +00:00
|
|
|
options <o> Options
|
|
|
|
self <o> Shared private variable
|
|
|
|
@*/
|
|
|
|
|
2012-03-23 16:55:55 +00:00
|
|
|
Ox.FileInput = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
|
|
|
maxFiles: -1,
|
|
|
|
maxLines: -1,
|
|
|
|
maxSize: -1,
|
2012-03-23 17:20:36 +00:00
|
|
|
value: [],
|
2012-03-23 16:55:55 +00:00
|
|
|
width: 256
|
|
|
|
})
|
|
|
|
.options(options || {})
|
|
|
|
.addClass('OxFileInput')
|
|
|
|
.css({width: self.options.width + 'px'});
|
|
|
|
|
|
|
|
self.multiple = self.options.maxFiles != 1;
|
2012-03-23 17:57:12 +00:00
|
|
|
self.size = getSize();
|
2012-03-23 16:55:55 +00:00
|
|
|
|
|
|
|
self.$bar = Ox.Bar({size: 14})
|
2012-03-23 17:57:12 +00:00
|
|
|
.css(
|
|
|
|
Ox.extend({
|
|
|
|
width: self.options.width - 2 + 'px'
|
|
|
|
}, self.multiple && self.options.value.length ? {
|
|
|
|
borderBottomLeftRadius: 0,
|
|
|
|
borderBottomRightRadius: 0
|
|
|
|
} : {})
|
|
|
|
)
|
2012-03-23 16:55:55 +00:00
|
|
|
.appendTo(that);
|
|
|
|
|
2012-03-23 17:57:12 +00:00
|
|
|
self.$title = $('<div>')
|
2012-03-23 16:55:55 +00:00
|
|
|
.css({
|
|
|
|
float: 'left',
|
|
|
|
width: self.options.width - 102 + 'px',
|
|
|
|
paddingLeft: '4px',
|
|
|
|
overflow: 'hidden',
|
|
|
|
textOverflow: 'ellipsis'
|
|
|
|
})
|
2012-03-23 17:57:12 +00:00
|
|
|
.html(getTitleText())
|
2012-03-23 16:55:55 +00:00
|
|
|
.appendTo(self.$bar);
|
|
|
|
|
|
|
|
self.$size = $('<div>')
|
|
|
|
.css({
|
|
|
|
float: 'left',
|
|
|
|
width: '64px',
|
|
|
|
height: '14px',
|
|
|
|
paddingRight: '16px',
|
|
|
|
textAlign: 'right'
|
|
|
|
})
|
2012-03-23 17:57:12 +00:00
|
|
|
.html(getSizeText())
|
2012-03-23 16:55:55 +00:00
|
|
|
.appendTo(self.$bar);
|
|
|
|
|
|
|
|
self.$button = Ox.Button({
|
|
|
|
style: 'symbol',
|
|
|
|
title: 'add',
|
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.css({
|
|
|
|
float: 'left',
|
|
|
|
marginTop: '-1px'
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
click: clearFile
|
|
|
|
})
|
|
|
|
.appendTo(self.$bar);
|
|
|
|
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input = renderInput();
|
2012-03-23 16:55:55 +00:00
|
|
|
|
|
|
|
if (self.multiple) {
|
|
|
|
self.$files = $('<div>')
|
|
|
|
.addClass('OxFiles')
|
|
|
|
.css({
|
|
|
|
width: self.options.width - 2 + 'px',
|
|
|
|
height: getHeight()
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
2012-03-23 17:57:12 +00:00
|
|
|
self.options.value.length == 0 && self.$files.hide();
|
2012-03-23 16:55:55 +00:00
|
|
|
self.$list = Ox.TextList({
|
|
|
|
columns: [
|
|
|
|
{
|
|
|
|
id: 'name',
|
|
|
|
visible: true,
|
|
|
|
width: self.options.width - 94
|
|
|
|
},
|
|
|
|
{
|
|
|
|
align: 'right',
|
|
|
|
format: function(value) {
|
|
|
|
return Ox.formatValue(value, 'B');
|
|
|
|
},
|
|
|
|
id: 'size',
|
|
|
|
visible: true,
|
|
|
|
width: 64
|
|
|
|
},
|
|
|
|
{
|
|
|
|
align: 'right',
|
|
|
|
format: function(value, data) {
|
|
|
|
return Ox.Button({
|
|
|
|
style: 'symbol',
|
|
|
|
title: 'close',
|
|
|
|
type: 'image'
|
|
|
|
})
|
|
|
|
.attr({title: 'Remove File'})
|
|
|
|
.css({margin: '-1px -4px 0 0'})
|
|
|
|
.bindEvent({
|
|
|
|
click: function() {
|
|
|
|
self.$list.options({selected: [value]});
|
|
|
|
removeFiles([value]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
id: 'id',
|
|
|
|
unique: true,
|
|
|
|
visible: true,
|
|
|
|
width: 28
|
|
|
|
}
|
|
|
|
],
|
2012-03-23 17:57:12 +00:00
|
|
|
items: getItems(),
|
2012-03-23 16:55:55 +00:00
|
|
|
sort: [{key: 'name', operator: '+'}]
|
|
|
|
})
|
|
|
|
.css({
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
width: self.options.width - 2 + 'px',
|
|
|
|
height: '64px',
|
|
|
|
})
|
|
|
|
.bindEvent({
|
|
|
|
'delete': function(data) {
|
|
|
|
removeFiles(data.ids);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.appendTo(self.$files);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addFiles(e) {
|
|
|
|
var filelist = e.target.files,
|
|
|
|
files = [];
|
|
|
|
Ox.loop(filelist.length, function(i) {
|
|
|
|
files.push(filelist.item(i));
|
|
|
|
});
|
|
|
|
files.sort(self.options.maxSize == -1 ? function(a, b) {
|
2012-03-23 17:20:36 +00:00
|
|
|
a = a.name.toLowerCase();
|
|
|
|
b = b.name.toLowerCase();
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
2012-03-23 16:55:55 +00:00
|
|
|
} : function(a, b) {
|
2012-03-23 17:20:36 +00:00
|
|
|
// if there's a max size,
|
|
|
|
// try to add small files first
|
2012-03-23 16:55:55 +00:00
|
|
|
return a.size - b.size;
|
|
|
|
}).forEach(function(file) {
|
2012-03-24 10:13:33 +00:00
|
|
|
Ox.print('::', file, exists(file), self.options.value)
|
2012-03-23 16:55:55 +00:00
|
|
|
if (!exists(file) && (
|
|
|
|
self.options.maxFiles == -1
|
2012-03-23 17:20:36 +00:00
|
|
|
|| self.options.value.length < self.options.maxFiles
|
2012-03-23 17:57:12 +00:00
|
|
|
) && (
|
|
|
|
self.options.maxSize == -1
|
|
|
|
|| self.size + file.size < self.options.maxSize
|
2012-03-23 16:55:55 +00:00
|
|
|
)) {
|
2012-03-23 17:57:12 +00:00
|
|
|
self.options.value.push(file);
|
|
|
|
self.size += file.size;
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
|
|
|
});
|
2012-03-23 17:57:12 +00:00
|
|
|
self.$title.html(getTitleText());
|
|
|
|
self.$size.html(getSizeText());
|
2012-03-23 16:55:55 +00:00
|
|
|
if (self.multiple) {
|
|
|
|
self.$bar.css({
|
|
|
|
borderBottomLeftRadius: 0,
|
|
|
|
borderBottomRightRadius: 1
|
|
|
|
});
|
|
|
|
self.$files.css({height: getHeight()}).show();
|
|
|
|
self.$list.options({items: getItems()});
|
|
|
|
if (
|
2012-03-23 17:20:36 +00:00
|
|
|
self.options.value.length == self.options.maxFiles
|
2012-03-23 16:55:55 +00:00
|
|
|
|| self.size == self.options.maxSize
|
|
|
|
) {
|
|
|
|
self.$button.options({disabled: true});
|
|
|
|
}
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input = renderInput();
|
2012-03-23 16:55:55 +00:00
|
|
|
} else {
|
|
|
|
self.$button.options({title: 'close'}).attr({title: 'Clear'});
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input.remove();
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
2012-03-23 17:20:36 +00:00
|
|
|
that.triggerEvent('change', {value: self.options.value});
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function clearFile() {
|
2012-03-23 17:20:36 +00:00
|
|
|
self.options.value = [];
|
2012-03-23 16:55:55 +00:00
|
|
|
self.size = 0;
|
2012-03-23 17:57:12 +00:00
|
|
|
self.$title.html(getTitleText());
|
|
|
|
self.$size.html(getSizeText());
|
2012-03-23 16:55:55 +00:00
|
|
|
self.$button.options({title: 'add'}).attr({title: ''});
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input = renderInput();
|
2012-03-23 17:20:36 +00:00
|
|
|
that.triggerEvent('change', {value: self.options.value});
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function exists(file) {
|
2012-03-23 17:20:36 +00:00
|
|
|
return self.options.value.some(function(f) {
|
2012-03-23 16:55:55 +00:00
|
|
|
return f.name == file.name
|
|
|
|
&& f.size == file.size
|
|
|
|
&& Ox.isEqual(f.lastModifiedDate, file.lastModifiedDate);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeight() {
|
|
|
|
return (
|
|
|
|
self.options.maxLines == -1
|
2012-03-23 17:20:36 +00:00
|
|
|
? self.options.value.length
|
|
|
|
: Math.min(self.options.value.length, self.options.maxLines)
|
2012-03-23 16:55:55 +00:00
|
|
|
) * 16 + 'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
function getItems() {
|
2012-03-23 17:20:36 +00:00
|
|
|
return self.options.value.map(function(file, i) {
|
2012-03-23 16:55:55 +00:00
|
|
|
return {name: file.name, size: file.size, id: i};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSize() {
|
2012-03-23 17:57:12 +00:00
|
|
|
return self.options.value.reduce(function(prev, curr) {
|
|
|
|
return prev + curr.size;
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSizeText() {
|
2012-03-23 16:55:55 +00:00
|
|
|
return self.size ? Ox.formatValue(self.size, 'B') : '';
|
|
|
|
}
|
|
|
|
|
2012-03-23 17:57:12 +00:00
|
|
|
function getTitleText() {
|
2012-03-23 17:20:36 +00:00
|
|
|
var length = self.options.value.length
|
2012-03-23 16:55:55 +00:00
|
|
|
return length == 0
|
|
|
|
? 'No file' + (self.multiple ? 's' : '') + ' selected'
|
|
|
|
: self.multiple
|
2012-03-23 17:57:12 +00:00
|
|
|
? length + ' file' + (length == 1 ? '' : 's')
|
2012-03-23 17:20:36 +00:00
|
|
|
: self.options.value[0].name;
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function removeFiles(ids) {
|
2012-03-23 17:20:36 +00:00
|
|
|
self.options.value = self.options.value.filter(function(v, i) {
|
2012-03-23 16:55:55 +00:00
|
|
|
return ids.indexOf(i) == -1;
|
|
|
|
});
|
2012-03-23 17:57:12 +00:00
|
|
|
self.size = getSize();
|
2012-03-23 17:20:36 +00:00
|
|
|
if (self.options.value.length == 0) {
|
2012-03-23 16:55:55 +00:00
|
|
|
self.$bar.css({
|
|
|
|
borderBottomLeftRadius: '8px',
|
|
|
|
borderBottomRightRadius: '8px'
|
|
|
|
});
|
|
|
|
self.$files.hide();
|
|
|
|
}
|
2012-03-23 17:57:12 +00:00
|
|
|
self.$title.html(getTitleText());
|
|
|
|
self.$size.html(getSizeText());
|
2012-03-23 16:55:55 +00:00
|
|
|
self.$list.options({items: getItems(), selected: []});
|
|
|
|
self.$files.css({height: getHeight()});
|
2012-03-23 17:20:36 +00:00
|
|
|
that.triggerEvent('change', {value: self.options.value});
|
2012-03-23 16:55:55 +00:00
|
|
|
}
|
|
|
|
|
2012-03-24 10:13:33 +00:00
|
|
|
function renderInput() {
|
|
|
|
self.$input && self.$input.remove();
|
|
|
|
return $('<input>')
|
|
|
|
.attr(
|
|
|
|
Ox.extend({
|
|
|
|
title: self.multiple ? 'Add Files' : 'Select File',
|
|
|
|
type: 'file'
|
|
|
|
}, self.multiple ? {
|
|
|
|
multiple: true
|
|
|
|
} : {})
|
|
|
|
)
|
|
|
|
.css({
|
|
|
|
float: 'left',
|
|
|
|
width: '16px',
|
|
|
|
height: '14px',
|
|
|
|
margin: '-1px -7px 0 -16px',
|
|
|
|
opacity: 0
|
|
|
|
})
|
|
|
|
.bind({
|
|
|
|
change: addFiles
|
|
|
|
})
|
|
|
|
.appendTo(self.$bar);
|
|
|
|
}
|
|
|
|
|
2012-03-23 16:55:55 +00:00
|
|
|
return that;
|
|
|
|
};
|