2013-07-29 17:29:25 +02:00
|
|
|
var avatar = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
function is_image_format(file) {
|
|
|
|
var type = file.type;
|
2013-08-01 17:47:48 +02:00
|
|
|
if (!type) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-29 17:29:25 +02:00
|
|
|
|
|
|
|
var supported_types = [
|
|
|
|
'image/jpeg',
|
|
|
|
'image/png',
|
|
|
|
'image/gif',
|
|
|
|
'image/svg+xml'
|
|
|
|
];
|
2013-07-29 23:37:51 +02:00
|
|
|
return _.indexOf(supported_types, type) >= 0;
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
exports.build_bot_create_widget = function () {
|
2013-07-29 17:29:25 +02:00
|
|
|
|
2013-07-29 20:39:38 +02:00
|
|
|
// We have to do strange gyrations with the file input to clear it,
|
|
|
|
// where we replace it wholesale, so we generalize the file input with
|
|
|
|
// a callback function.
|
|
|
|
var get_file_input = function () {
|
|
|
|
return $('#bot_avatar_file_input');
|
|
|
|
};
|
|
|
|
|
2013-07-29 21:17:34 +02:00
|
|
|
var file_name_field = $('#bot_avatar_file');
|
|
|
|
var input_error = $('#bot_avatar_file_input_error');
|
|
|
|
var clear_button = $('#bot_avatar_clear_button');
|
|
|
|
var upload_button = $('#bot_avatar_upload_button');
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
return exports.build_widget(
|
2013-07-29 21:31:21 +02:00
|
|
|
get_file_input,
|
|
|
|
file_name_field,
|
|
|
|
input_error,
|
|
|
|
clear_button,
|
|
|
|
upload_button
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
exports.build_bot_edit_widget = function (li) {
|
2013-07-29 16:27:18 +02:00
|
|
|
var get_file_input = function () {
|
|
|
|
return li.find('.edit_bot_avatar_file_input');
|
|
|
|
};
|
|
|
|
|
|
|
|
var file_name_field = li.find('.edit_bot_avatar_file');
|
|
|
|
var input_error = li.find('.edit_bot_avatar_error');
|
|
|
|
var clear_button = li.find('.edit_bot_avatar_clear_button');
|
|
|
|
var upload_button = li.find('.edit_bot_avatar_upload_button');
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
return exports.build_widget(
|
2013-07-29 16:27:18 +02:00
|
|
|
get_file_input,
|
|
|
|
file_name_field,
|
|
|
|
input_error,
|
|
|
|
clear_button,
|
|
|
|
upload_button
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2013-08-01 15:43:48 +02:00
|
|
|
exports.build_widget = function (
|
2013-07-29 21:31:21 +02:00
|
|
|
get_file_input, // function returns a jQuery file input object
|
|
|
|
file_name_field, // jQuery object to show file name
|
|
|
|
input_error, // jQuery object for error text
|
|
|
|
clear_button, // jQuery button to clear last upload choice
|
|
|
|
upload_button // jQuery button to open file dialog
|
|
|
|
) {
|
|
|
|
|
2013-07-30 14:39:05 +02:00
|
|
|
function accept(file) {
|
2013-07-29 21:17:34 +02:00
|
|
|
file_name_field.text(file.name);
|
|
|
|
input_error.hide();
|
|
|
|
clear_button.show();
|
|
|
|
upload_button.hide();
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
|
|
|
|
2013-07-30 14:39:05 +02:00
|
|
|
function clear() {
|
2013-07-29 20:39:38 +02:00
|
|
|
var control = get_file_input();
|
2013-07-29 17:29:25 +02:00
|
|
|
var new_control = control.clone(true);
|
|
|
|
control.replaceWith(new_control);
|
2013-07-29 21:17:34 +02:00
|
|
|
file_name_field.text('');
|
|
|
|
clear_button.hide();
|
|
|
|
upload_button.show();
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 19:31:12 +02:00
|
|
|
clear_button.on('click', function (e) {
|
2013-07-30 14:39:05 +02:00
|
|
|
clear();
|
2013-07-29 17:29:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2013-07-29 21:17:34 +02:00
|
|
|
upload_button.on('drop', function (e) {
|
2013-07-29 17:29:25 +02:00
|
|
|
var files = e.dataTransfer.files;
|
|
|
|
if (files === null || files === undefined || files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-29 20:39:38 +02:00
|
|
|
get_file_input().get(0).files = files;
|
2013-07-29 17:29:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2013-07-31 19:31:12 +02:00
|
|
|
get_file_input().on('change', function (e) {
|
2013-07-29 17:29:25 +02:00
|
|
|
if (e.target.files.length === 0) {
|
2013-07-29 21:17:34 +02:00
|
|
|
input_error.hide();
|
2013-07-29 17:29:25 +02:00
|
|
|
} else if (e.target.files.length === 1) {
|
|
|
|
var file = e.target.files[0];
|
|
|
|
if (file.size > 5*1024*1024) {
|
2013-07-29 21:17:34 +02:00
|
|
|
input_error.text('File size must be < 5Mb.');
|
|
|
|
input_error.show();
|
2013-07-30 14:39:05 +02:00
|
|
|
clear();
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
|
|
|
else if (!is_image_format(file)) {
|
2013-07-29 21:17:34 +02:00
|
|
|
input_error.text('File type is not supported.');
|
|
|
|
input_error.show();
|
2013-07-30 14:39:05 +02:00
|
|
|
clear();
|
2013-07-29 17:29:25 +02:00
|
|
|
} else {
|
2013-07-30 14:39:05 +02:00
|
|
|
accept(file);
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2013-07-29 21:17:34 +02:00
|
|
|
input_error.text('Please just upload one file.');
|
2013-07-29 17:29:25 +02:00
|
|
|
}
|
2013-07-31 19:31:12 +02:00
|
|
|
});
|
2013-07-29 17:29:25 +02:00
|
|
|
|
2013-07-31 19:31:12 +02:00
|
|
|
upload_button.on('click', function (e) {
|
2013-07-29 20:39:38 +02:00
|
|
|
get_file_input().trigger('click');
|
2013-07-29 17:29:25 +02:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
|
2013-07-31 19:33:00 +02:00
|
|
|
function close() {
|
|
|
|
clear();
|
|
|
|
clear_button.off('click');
|
|
|
|
upload_button.off('drop');
|
|
|
|
get_file_input().off('change');
|
|
|
|
upload_button.off('click');
|
|
|
|
}
|
|
|
|
|
2013-07-31 17:07:34 +02:00
|
|
|
return {
|
|
|
|
// Call back to clear() in situations like adding bots, when
|
|
|
|
// we want to use the same widget over and over again.
|
2013-07-31 19:33:00 +02:00
|
|
|
clear: clear,
|
|
|
|
// Call back to close() when you are truly done with the widget,
|
|
|
|
// so you can release handlers.
|
|
|
|
close: close
|
2013-07-31 17:07:34 +02:00
|
|
|
};
|
2013-07-29 17:29:25 +02:00
|
|
|
};
|
|
|
|
|
2013-10-28 15:49:38 +01:00
|
|
|
exports.build_user_avatar_widget = function (upload_function) {
|
|
|
|
var get_file_input = function () {
|
|
|
|
return $('#user_avatar_file_input').expectOne();
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports.build_direct_upload_widget(
|
|
|
|
get_file_input,
|
|
|
|
$("#user_avatar_file_input_error").expectOne(),
|
|
|
|
$("#user_avatar_upload_button").expectOne(),
|
|
|
|
upload_function
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.build_direct_upload_widget = function (
|
|
|
|
get_file_input, // function returns a jQuery file input object
|
|
|
|
input_error, // jQuery object for error text
|
|
|
|
upload_button, // jQuery button to open file dialog
|
|
|
|
upload_function
|
|
|
|
) {
|
|
|
|
|
|
|
|
function accept(file) {
|
|
|
|
input_error.hide();
|
|
|
|
upload_function(get_file_input());
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear() {
|
|
|
|
var control = get_file_input();
|
|
|
|
var new_control = control.clone(true);
|
|
|
|
control.replaceWith(new_control);
|
|
|
|
}
|
|
|
|
|
|
|
|
upload_button.on('drop', function (e) {
|
|
|
|
var files = e.dataTransfer.files;
|
|
|
|
if (files === null || files === undefined || files.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
get_file_input().get(0).files = files;
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
get_file_input().on('change', function (e) {
|
|
|
|
if (e.target.files.length === 0) {
|
|
|
|
input_error.hide();
|
|
|
|
} else if (e.target.files.length === 1) {
|
|
|
|
var file = e.target.files[0];
|
|
|
|
if (file.size > 5*1024*1024) {
|
|
|
|
input_error.text('File size must be < 5Mb.');
|
|
|
|
input_error.show();
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
else if (!is_image_format(file)) {
|
|
|
|
input_error.text('File type is not supported.');
|
|
|
|
input_error.show();
|
|
|
|
clear();
|
|
|
|
} else {
|
|
|
|
accept(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input_error.text('Please just upload one file.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
upload_button.on('click', function (e) {
|
|
|
|
get_file_input().trigger('click');
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-07-29 17:29:25 +02:00
|
|
|
return exports;
|
|
|
|
|
|
|
|
}());
|