2012-03-23 17:57:12 +00:00
|
|
|
'use strict';
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
2012-05-31 10:32:54 +00:00
|
|
|
Ox.FileButton <f> File Button
|
2012-05-21 10:38:18 +00:00
|
|
|
options <o> Options
|
2012-06-27 08:37:12 +00:00
|
|
|
disabled <b|false> If true, the button is disabled
|
2012-06-27 10:55:38 +00:00
|
|
|
image <s|'file'> Symbol name (if type is 'image')
|
|
|
|
The default value will be 'files' if maxFiles is not 1
|
2012-06-27 09:07:34 +00:00
|
|
|
maxFiles <n|-1> Maximum number of files (or -1 for unlimited)
|
|
|
|
maxSize <n|-1> Maximum total file size in bytes (or -1 for unlimited)
|
|
|
|
title <s|''> Title of the button (and its tooltip)
|
2012-06-27 08:37:12 +00:00
|
|
|
type <s|'text'> Type of the button ('text' or 'image')
|
|
|
|
width <n|256> Width of the button in px
|
2012-05-21 10:38:18 +00:00
|
|
|
self <o> Shared private variable
|
2012-06-27 09:07:34 +00:00
|
|
|
([options[, self]]) -> <o:Ox.Element> File Button
|
|
|
|
click <!> click
|
2012-05-21 10:38:18 +00:00
|
|
|
@*/
|
2012-03-23 17:57:12 +00:00
|
|
|
Ox.FileButton = function(options, self) {
|
|
|
|
|
|
|
|
self = self || {};
|
|
|
|
var that = Ox.Element({}, self)
|
|
|
|
.defaults({
|
|
|
|
disabled: false,
|
2012-06-27 10:55:38 +00:00
|
|
|
image: options && options.maxFiles == 1 ? 'file' : 'files',
|
2012-03-23 17:57:12 +00:00
|
|
|
maxFiles: -1,
|
|
|
|
maxSize: -1,
|
|
|
|
style: 'default',
|
|
|
|
title: '',
|
|
|
|
type: 'text',
|
2012-06-27 08:37:12 +00:00
|
|
|
width: options.type == 'image' ? 16 : 256
|
2012-03-23 17:57:12 +00:00
|
|
|
})
|
|
|
|
.options(options || {})
|
2012-05-28 19:35:41 +00:00
|
|
|
.update({
|
|
|
|
disabled: function() {
|
|
|
|
self.$button.options({disabled: self.options.disabled});
|
|
|
|
self.$input[self.options.disabled ? 'hide' : 'show']();
|
|
|
|
},
|
|
|
|
title: function() {
|
|
|
|
self.$button.options({title: self.options.title});
|
|
|
|
}
|
|
|
|
})
|
2012-03-23 17:57:12 +00:00
|
|
|
.addClass('OxFileButton')
|
|
|
|
.css({width: self.options.width + 'px'});
|
|
|
|
|
|
|
|
self.files = [];
|
|
|
|
self.multiple = self.options.maxFiles != 1;
|
|
|
|
|
|
|
|
self.$button = Ox.Button({
|
|
|
|
disabled: self.options.disabled,
|
|
|
|
style: self.options.style,
|
2012-06-27 09:07:34 +00:00
|
|
|
title: self.options.type == 'image'
|
|
|
|
? self.options.image
|
|
|
|
: self.options.title,
|
2012-03-23 17:57:12 +00:00
|
|
|
type: self.options.type,
|
2012-06-27 09:07:34 +00:00
|
|
|
width: self.options.type == 'image'
|
|
|
|
? 'auto'
|
|
|
|
: self.options.width
|
2012-03-23 17:57:12 +00:00
|
|
|
})
|
|
|
|
.css({
|
|
|
|
float: 'left'
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input = renderInput();
|
2012-03-23 17:57:12 +00:00
|
|
|
self.options.disabled && self.$input.hide();
|
|
|
|
|
|
|
|
function selectFiles(e) {
|
|
|
|
var filelist = e.target.files,
|
|
|
|
files = [];
|
2012-03-24 11:12:24 +00:00
|
|
|
self.files = [];
|
2012-03-23 17:57:12 +00:00
|
|
|
Ox.loop(filelist.length, function(i) {
|
|
|
|
files.push(filelist.item(i));
|
|
|
|
});
|
|
|
|
files.sort(self.options.maxSize == -1 ? function(a, b) {
|
|
|
|
a = a.name.toLowerCase();
|
|
|
|
b = b.name.toLowerCase();
|
|
|
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
|
|
} : function(a, b) {
|
|
|
|
// if there's a max size,
|
|
|
|
// try to add small files first
|
|
|
|
return a.size - b.size;
|
|
|
|
}).forEach(function(file) {
|
|
|
|
if ((
|
|
|
|
self.options.maxFiles == -1
|
2012-03-24 10:13:33 +00:00
|
|
|
|| self.files.length < self.options.maxFiles
|
2012-03-23 17:57:12 +00:00
|
|
|
) && (
|
|
|
|
self.options.maxSize == -1
|
|
|
|
|| self.size + file.size < self.options.maxSize
|
|
|
|
)) {
|
|
|
|
self.files.push(file);
|
|
|
|
self.size += file.size;
|
|
|
|
}
|
|
|
|
});
|
2012-03-24 10:13:33 +00:00
|
|
|
self.$input = renderInput();
|
2012-03-23 17:57:12 +00:00
|
|
|
if (self.files.length) {
|
|
|
|
that.triggerEvent('click', {files: self.files});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-24 10:13:33 +00:00
|
|
|
function renderInput() {
|
|
|
|
self.$input && self.$input.remove();
|
|
|
|
return $('<input>')
|
|
|
|
.attr(
|
|
|
|
Ox.extend({
|
2012-06-27 09:07:34 +00:00
|
|
|
title: self.options.title,
|
2012-03-24 10:13:33 +00:00
|
|
|
type: 'file'
|
|
|
|
}, self.multiple ? {
|
|
|
|
multiple: true
|
|
|
|
} : {})
|
|
|
|
)
|
|
|
|
.css({
|
|
|
|
float: 'left',
|
|
|
|
width: self.options.width + 'px',
|
|
|
|
height: '16px',
|
|
|
|
marginLeft: -self.options.width + 'px',
|
|
|
|
opacity: 0
|
|
|
|
})
|
2012-05-28 14:06:22 +00:00
|
|
|
.on({
|
2012-03-24 10:13:33 +00:00
|
|
|
change: selectFiles
|
|
|
|
})
|
|
|
|
.appendTo(that);
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
blurButton <f> blurButton
|
|
|
|
@*/
|
2012-03-24 11:04:27 +00:00
|
|
|
that.blurButton = function() {
|
|
|
|
self.$input.blur();
|
|
|
|
}
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
/*@
|
|
|
|
focusButton <f> focusButton
|
|
|
|
@*/
|
2012-03-24 11:04:27 +00:00
|
|
|
that.focusButton = function() {
|
|
|
|
self.$input.focus();
|
|
|
|
};
|
|
|
|
|
2012-03-23 17:57:12 +00:00
|
|
|
return that;
|
|
|
|
|
2012-05-21 10:38:18 +00:00
|
|
|
}
|