2016-12-28 14:46:42 +01:00
|
|
|
var attachments_ui = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
2018-05-05 01:12:04 +02:00
|
|
|
var attachments;
|
2019-01-17 12:05:09 +01:00
|
|
|
var upload_space_used;
|
2018-05-05 01:12:04 +02:00
|
|
|
|
2019-01-15 08:39:46 +01:00
|
|
|
exports.bytes_to_size = function (bytes, kb_with_1024_bytes) {
|
2017-12-08 20:49:00 +01:00
|
|
|
if (kb_with_1024_bytes === undefined) {
|
|
|
|
kb_with_1024_bytes = false;
|
|
|
|
}
|
2017-12-08 14:24:32 +01:00
|
|
|
var kb_size = kb_with_1024_bytes ? 1024 : 1000;
|
2017-09-17 01:20:11 +02:00
|
|
|
var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
if (bytes === 0) {
|
|
|
|
return '0 B';
|
|
|
|
}
|
2017-12-08 14:24:32 +01:00
|
|
|
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(kb_size)), 10);
|
|
|
|
var size = Math.round(bytes / Math.pow(kb_size, i));
|
2018-06-06 18:50:09 +02:00
|
|
|
if (i > 0 && size < 10) {
|
|
|
|
size = Math.round(bytes / Math.pow(kb_size, i) * 10) / 10;
|
2017-12-05 00:44:05 +01:00
|
|
|
}
|
|
|
|
return size + ' ' + sizes[i];
|
2019-01-15 08:39:46 +01:00
|
|
|
};
|
|
|
|
|
2019-01-17 12:05:09 +01:00
|
|
|
exports.percentage_used_space = function (uploads_size) {
|
|
|
|
if (page_params.realm_upload_quota === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (100 * uploads_size / page_params.realm_upload_quota).toFixed(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
function set_upload_space_stats() {
|
|
|
|
if (page_params.realm_upload_quota === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var args = {
|
|
|
|
show_upgrade_message: page_params.realm_plan_type === 2,
|
|
|
|
percent_used: exports.percentage_used_space(upload_space_used),
|
|
|
|
upload_quota: attachments_ui.bytes_to_size(page_params.realm_upload_quota, true),
|
|
|
|
};
|
|
|
|
var rendered_upload_stats_html = templates.render("upload-space-stats", args);
|
|
|
|
$("#attachment-stats-holder").html(rendered_upload_stats_html);
|
|
|
|
}
|
|
|
|
|
2019-01-15 08:39:46 +01:00
|
|
|
function delete_attachments(attachment) {
|
|
|
|
var status = $('#delete-upload-status');
|
|
|
|
channel.del({
|
|
|
|
url: '/json/attachments/' + attachment,
|
|
|
|
idempotent: true,
|
|
|
|
error: function (xhr) {
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, status);
|
|
|
|
},
|
|
|
|
success: function () {
|
|
|
|
ui_report.success(i18n.t("Attachment deleted"), status);
|
|
|
|
},
|
|
|
|
});
|
2018-05-05 00:48:29 +02:00
|
|
|
}
|
2016-12-28 14:46:42 +01:00
|
|
|
|
2018-05-05 01:12:04 +02:00
|
|
|
function render_attachments_ui() {
|
2019-01-17 12:05:09 +01:00
|
|
|
set_upload_space_stats();
|
|
|
|
|
2017-09-17 01:20:11 +02:00
|
|
|
var uploaded_files_table = $("#uploaded_files_table").expectOne();
|
2017-09-28 21:08:05 +02:00
|
|
|
var $search_input = $("#upload_file_search");
|
2017-02-24 02:31:37 +01:00
|
|
|
|
2018-06-20 18:32:39 +02:00
|
|
|
var list = list_render.create(uploaded_files_table, attachments, {
|
2017-09-28 21:07:23 +02:00
|
|
|
name: "uploaded-files-list",
|
|
|
|
modifier: function (attachment) {
|
|
|
|
return templates.render("uploaded_files_list", { attachment: attachment });
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
element: $search_input,
|
|
|
|
callback: function (item, value) {
|
|
|
|
return item.name.toLocaleLowerCase().indexOf(value) >= 0;
|
|
|
|
},
|
2017-09-28 21:08:43 +02:00
|
|
|
onupdate: function () {
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar(uploaded_files_table.closest(".progressive-table-wrapper"));
|
2017-09-28 21:08:43 +02:00
|
|
|
},
|
2017-09-28 21:07:23 +02:00
|
|
|
},
|
2018-06-21 09:16:05 +02:00
|
|
|
parent_container: $('#attachments-settings').expectOne(),
|
2017-09-28 21:07:23 +02:00
|
|
|
}).init();
|
|
|
|
|
2018-09-29 18:19:03 +02:00
|
|
|
list.sort('numeric', 'create_time');
|
|
|
|
|
2017-09-28 23:52:42 +02:00
|
|
|
list.add_sort_function("mentioned-in", function (a, b) {
|
|
|
|
var a_m = a.messages[0];
|
|
|
|
var b_m = b.messages[0];
|
|
|
|
|
|
|
|
if (!a_m) { return 1; }
|
|
|
|
if (!b_m) { return -1; }
|
|
|
|
|
|
|
|
if (a_m.id > b_m.id) {
|
|
|
|
return 1;
|
|
|
|
} else if (a_m.id === b_m.id) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
});
|
|
|
|
|
2019-01-09 14:30:35 +01:00
|
|
|
ui.reset_scrollbar(uploaded_files_table.closest(".progressive-table-wrapper"));
|
2018-05-05 00:27:25 +02:00
|
|
|
}
|
|
|
|
|
2018-05-05 01:10:46 +02:00
|
|
|
function format_attachment_data(new_attachments) {
|
|
|
|
_.each(new_attachments, function (attachment) {
|
|
|
|
var time = new XDate(attachment.create_time);
|
|
|
|
attachment.create_time_str = timerender.render_now(time).time_str;
|
2019-01-15 08:39:46 +01:00
|
|
|
attachment.size_str = exports.bytes_to_size(attachment.size);
|
2018-05-05 01:10:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-05 00:48:29 +02:00
|
|
|
exports.update_attachments = function (event) {
|
|
|
|
if (attachments === undefined) {
|
|
|
|
// If we haven't fetched attachment data yet, there's nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (event.op === 'remove' || event.op === 'update') {
|
|
|
|
attachments = attachments.filter(function (a) {
|
|
|
|
return a.id !== event.attachment.id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (event.op === 'add' || event.op === 'update') {
|
|
|
|
format_attachment_data([event.attachment]);
|
|
|
|
attachments.push(event.attachment);
|
|
|
|
}
|
2019-01-17 12:05:09 +01:00
|
|
|
upload_space_used = event.upload_space_used;
|
2018-05-05 00:48:29 +02:00
|
|
|
// TODO: This is inefficient and we should be able to do some sort
|
|
|
|
// of incremental list_render update instead.
|
|
|
|
render_attachments_ui();
|
|
|
|
};
|
|
|
|
|
2018-05-05 00:27:25 +02:00
|
|
|
exports.set_up_attachments = function () {
|
|
|
|
// The settings page must be rendered before this function gets called.
|
|
|
|
|
2018-05-05 01:05:16 +02:00
|
|
|
var uploaded_files_table = $("#uploaded_files_table").expectOne();
|
2018-05-05 00:27:25 +02:00
|
|
|
var status = $('#delete-upload-status');
|
|
|
|
loading.make_indicator($('#attachments_loading_indicator'), {text: 'Loading...'});
|
|
|
|
|
2018-05-05 01:05:16 +02:00
|
|
|
ui.set_up_scrollbar(uploaded_files_table.closest(".progressive-table-wrapper"));
|
|
|
|
|
|
|
|
$('#uploaded_files_table').on('click', '.remove-attachment', function (e) {
|
2018-05-05 00:48:29 +02:00
|
|
|
delete_attachments($(e.target).closest(".uploaded_file_row").attr('data-attachment-id'));
|
2018-05-05 01:05:16 +02:00
|
|
|
});
|
|
|
|
|
2018-05-05 00:27:25 +02:00
|
|
|
channel.get({
|
|
|
|
url: "/json/attachments",
|
|
|
|
idempotent: true,
|
|
|
|
success: function (data) {
|
|
|
|
loading.destroy_indicator($('#attachments_loading_indicator'));
|
2018-05-05 01:10:46 +02:00
|
|
|
format_attachment_data(data.attachments);
|
2018-05-05 01:12:04 +02:00
|
|
|
attachments = data.attachments;
|
2019-01-17 12:05:09 +01:00
|
|
|
upload_space_used = data.upload_space_used;
|
2018-05-05 01:12:04 +02:00
|
|
|
render_attachments_ui();
|
2018-05-05 00:27:25 +02:00
|
|
|
},
|
|
|
|
error: function (xhr) {
|
|
|
|
loading.destroy_indicator($('#attachments_loading_indicator'));
|
|
|
|
ui_report.error(i18n.t("Failed"), xhr, status);
|
|
|
|
},
|
|
|
|
});
|
2016-12-28 14:46:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = attachments_ui;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.attachments_ui = attachments_ui;
|