...
 
......@@ -11,24 +11,36 @@ function APIRequest(method, url, data)
APIRequest.prototype.overrideMimeType = function(mime)
{
if (this.rq !== null)
return this;
this.mime = mime;
return this;
};
APIRequest.prototype.setHeader = function(k, v)
{
if (this.rq !== null)
return this;
this.headers[k] = v;
return this;
};
APIRequest.prototype.setResponseType = function(type)
{
if (this.rq !== null)
return this;
this.responseType = type;
return this;
};
APIRequest.prototype.send = function()
{
if (this.rq !== null)
return Promise.reject("Already sending.");
this.rq = new XMLHttpRequest();
return new Promise((resolve, reject) => {
......@@ -36,19 +48,20 @@ APIRequest.prototype.send = function()
if (this.rq.readyState !== XMLHttpRequest.DONE)
return;
if (this.rq.status !== 0
&& this.rq.status !== 200
&& this.rq.status !== 204)
if (((this.rq.status / 100) | 0) !== 2)
{
reject(this.rq);
this.rq = null;
return;
}
resolve(this.rq);
this.rq = null;
};
this.rq.onerror = (evt) => {
reject(this.rq);
this.rq = null;
};
this.rq.open(this.method, this.url, true);
......@@ -65,7 +78,7 @@ APIRequest.prototype.send = function()
this.rq.overrideMimeType(this.mime);
this.rq.responseType = this.responseType;
this.rq.send(this.data);
});
};
......@@ -19,7 +19,7 @@ ctx.storage = new DBStorage('DiscUp://' + window.location.pathname);
ctx.cur_channel = null;
ctx.is_loading_msgs = false;
ctx.last_displayed_msg_idx = 0;
ctx.first_displayed_msg_idx = 0;
ctx.first_displayed_msg_idx = -1;
ctx.auto_display_msg_interval_id = -1;
function fetch_json_object_promise(name)
......@@ -377,23 +377,14 @@ function format_msg_attachments(msg)
{
let img_w = attachment.width;
let img_h = attachment.height;
let img_scale = 1;
let img_scale_w = cfg.img_max_width / img_w;
let img_scale_h = cfg.img_max_height / img_h;
if (img_w > cfg.img_max_width)
{
let img_scale = cfg.img_max_width / img_w;
img_w = img_scale * img_w;
img_h = img_scale * img_h;
}
if (img_h > cfg.img_max_height)
{
let img_scale = cfg.img_max_height / img_h;
img_w = img_scale * img_w;
img_h = img_scale * img_h;
}
img_scale = Math.min(img_scale, img_scale_w, img_scale_h);
img_w |= 0;
img_h |= 0;
img_w = (img_scale * img_w) | 0;
img_h = (img_scale * img_h) | 0;
msg_attachment_obj_html = '<img class="message_attachment_object" width="' + img_w + '" height="' + img_h + '" src="' + attachment_url + '">';
}
......@@ -419,7 +410,7 @@ function create_msg_chunk_elem(guild, users, messages, first_msg_idx, last_msg_i
elem_msg_chunk.dataset.firstMsgIdx = first_msg_idx;
elem_msg_chunk.dataset.lastMsgIdx = last_msg_idx;
messages = messages.slice(last_msg_idx, first_msg_idx);
messages = messages.slice(last_msg_idx, first_msg_idx + 1);
let msgs_html = messages.reduceRight((accum, msg, msg_idx) => {
let msg_html = '';
......@@ -575,7 +566,7 @@ Promise.all([load_guild, load_users]).then((values) => {
ctx.cur_channel = channel.id;
ctx.is_loading_msgs = true;
ctx.last_displayed_msg_idx = 0;
ctx.first_displayed_msg_idx = 0;
ctx.first_displayed_msg_idx = -1;
let load_messages = fetch_json_object_promise('channel-' + channel.id);
load_messages.then((messages) => {
......