Объект
Мой первый объект
HTML
<input type="file" multiple="multiple" name="files[]" accept="image" required="required" title="Необходимо выбрать минимум один файл" id="files" />
<ul id="list_files"></ul>
<span id="clear_all">Очистить все</span>
CSS
#list_files li {
margin-bottom: 5px;
}
#list_files i {
background-color: #FE0303;
border-radius: 10px 10px 10px 10px;
color: #FFFFFF;
cursor: pointer;
font-size: 14px;
font-style: normal;
margin-left: 5px;
padding: 0 5px 1px;
}
#clear_all {
cursor: pointer;
color: #FE0303;
}
#clear_all:hover {
text-decoration: underline;
}
JavaScript
var data = {
data: [],
length: function() {
return this.data.length;
},
set: function(x) {
this.data.push(x);
return this;
},
clear: function() {
this.data.length = 0;
return this;
},
del: function(x) {
return this.data.splice(x, 1);
},
uni: function(x) {
var i = this.data.length;
while (i--) {
if (this.data[i].name === x.name) {
return true;
}
}
return false;
}
};
var $files = $('#files');
$files.change(function() {
var error = '';
var type = ["image/jpeg", "image/jpg", "image/gif", "image/png"];
$.each(this.files, function(i, file) {
console.log(file);
if (data.length() > 9) {
error += 'Разрешено не более 10 файлов\n\r';
} else if ($.inArray(file.type, type) == -1) {
error += 'Недопустимое разрешение файла - ' + file.type + '\n\r';
} else if (file.size > 2097152) {
error += file.name + ' - размер файла больше 2Мб\n\r';
} else if (data.uni(file) === false) {
data.set(file);
$('#list_files').append('<li>' + file.name + '<i data-file="' + data.length() + '">x</i></li>');
}
});
if (error.length) alert(error);
});
$('#list_files').on('click', 'i', function() {
data.del($(this).data('file') - 1);
$(this).parent().remove();
});
$('#clear_all').click(function() {
$('#list_files li').remove();
data.clear();
$files.replaceWith($files.val('').clone(true));
$files = $('#files');
});