More robust/Less shitty QR status.

This commit is contained in:
Nicolas Stepien 2013-02-26 18:32:14 +01:00
parent 5711a85c8c
commit 28a472adec
2 changed files with 65 additions and 58 deletions

View File

@ -5684,6 +5684,8 @@
$.on(d, 'ThreadUpdate', function() { $.on(d, 'ThreadUpdate', function() {
if (g.DEAD) { if (g.DEAD) {
return QR.abort(); return QR.abort();
} else {
return QR.status();
} }
}); });
if (Conf['Persistent QR']) { if (Conf['Persistent QR']) {
@ -5786,11 +5788,8 @@
} }
return QR.notifications = []; return QR.notifications = [];
}, },
status: function(data) { status: function() {
var disabled, status, value; var disabled, status, value;
if (data == null) {
data = {};
}
if (!QR.nodes) { if (!QR.nodes) {
return; return;
} }
@ -5799,9 +5798,9 @@
disabled = true; disabled = true;
QR.cooldown.auto = false; QR.cooldown.auto = false;
} }
value = data.progress || QR.cooldown.seconds || value; value = QR.req ? QR.req.progress : QR.cooldown.seconds || value;
status = QR.nodes.status; status = QR.nodes.status;
status.value = QR.cooldown.auto ? value ? "Auto " + value : 'Auto' : value || 'Submit'; status.value = !value ? 'Submit' : QR.cooldown.auto ? "Auto " + value : value;
return status.disabled = disabled || false; return status.disabled = disabled || false;
}, },
cooldown: { cooldown: {
@ -6519,15 +6518,15 @@
if (e != null) { if (e != null) {
e.preventDefault(); e.preventDefault();
} }
if (QR.req) {
QR.abort();
return;
}
if (QR.cooldown.seconds) { if (QR.cooldown.seconds) {
QR.cooldown.auto = !QR.cooldown.auto; QR.cooldown.auto = !QR.cooldown.auto;
QR.status(); QR.status();
return; return;
} }
if (QR.ajax) {
QR.abort();
return;
}
reply = QR.replies[0]; reply = QR.replies[0];
if (reply === QR.selected) { if (reply === QR.selected) {
reply.forceSave(); reply.forceSave();
@ -6576,9 +6575,6 @@
if (!QR.cooldown.auto && $.x('ancestor::div[@id="qr"]', d.activeElement)) { if (!QR.cooldown.auto && $.x('ancestor::div[@id="qr"]', d.activeElement)) {
d.activeElement.blur(); d.activeElement.blur();
} }
QR.status({
progress: '...'
});
post = { post = {
resto: threadID, resto: threadID,
name: reply.name, name: reply.name,
@ -6595,11 +6591,9 @@
recaptcha_response_field: response recaptcha_response_field: response
}; };
callbacks = { callbacks = {
onload: function() { onload: QR.response,
return QR.response(this);
},
onerror: function() { onerror: function() {
delete QR.ajax; delete QR.req;
QR.cooldown.auto = false; QR.cooldown.auto = false;
QR.status(); QR.status();
return QR.error($.el('a', { return QR.error($.el('a', {
@ -6613,22 +6607,23 @@
form: $.formData(post), form: $.formData(post),
upCallbacks: { upCallbacks: {
onload: function() { onload: function() {
return QR.status({ QR.req.progress = '...';
progress: '...' return QR.status();
});
}, },
onprogress: function(e) { onprogress: function(e) {
return QR.status({ QR.req.progress = "" + (Math.round(e.loaded / e.total * 100)) + "%";
progress: "" + (Math.round(e.loaded / e.total * 100)) + "%" return QR.status();
});
} }
} }
}; };
return QR.ajax = $.ajax($.id('postForm').parentNode.action, callbacks, opts); QR.req = $.ajax($.id('postForm').parentNode.action, callbacks, opts);
QR.req.progress = '...';
return QR.status();
}, },
response: function(req) { response: function() {
var ban, board, err, h1, persona, postID, reply, threadID, tmpDoc, _, _base, _ref, _ref1; var ban, board, err, h1, persona, postID, reply, req, threadID, tmpDoc, _, _base, _ref, _ref1;
delete QR.ajax; req = QR.req;
delete QR.req;
tmpDoc = d.implementation.createHTMLDocument(''); tmpDoc = d.implementation.createHTMLDocument('');
tmpDoc.documentElement.innerHTML = req.response; tmpDoc.documentElement.innerHTML = req.response;
if (ban = $('.banType', tmpDoc)) { if (ban = $('.banType', tmpDoc)) {
@ -6700,9 +6695,9 @@
return QR.status(); return QR.status();
}, },
abort: function() { abort: function() {
if (QR.ajax) { if (QR.req) {
QR.ajax.abort(); QR.req.abort();
delete QR.ajax; delete QR.req;
QR.notifications.push(new Notification('info', 'QR upload aborted.', 5)); QR.notifications.push(new Notification('info', 'QR upload aborted.', 5));
} }
return QR.status(); return QR.status();

View File

@ -36,7 +36,10 @@ QR =
$.on d, 'drop', QR.dropFile $.on d, 'drop', QR.dropFile
$.on d, 'dragstart dragend', QR.drag $.on d, 'dragstart dragend', QR.drag
$.on d, 'ThreadUpdate', -> $.on d, 'ThreadUpdate', ->
QR.abort() if g.DEAD if g.DEAD
QR.abort()
else
QR.status()
QR.persist() if Conf['Persistent QR'] QR.persist() if Conf['Persistent QR']
@ -108,19 +111,25 @@ QR =
notification.close() notification.close()
QR.notifications = [] QR.notifications = []
status: (data={}) -> status: ->
return unless QR.nodes return unless QR.nodes
if g.DEAD if g.DEAD
value = 404 value = 404
disabled = true disabled = true
QR.cooldown.auto = false QR.cooldown.auto = false
value = data.progress or QR.cooldown.seconds or value
value = if QR.req
QR.req.progress
else
QR.cooldown.seconds or value
{status} = QR.nodes {status} = QR.nodes
status.value = status.value = unless value
if QR.cooldown.auto 'Submit'
if value then "Auto #{value}" else 'Auto' else if QR.cooldown.auto
else "Auto #{value}"
value or 'Submit' else
value
status.disabled = disabled or false status.disabled = disabled or false
cooldown: cooldown:
@ -727,15 +736,16 @@ QR =
submit: (e) -> submit: (e) ->
e?.preventDefault() e?.preventDefault()
if QR.req
QR.abort()
return
if QR.cooldown.seconds if QR.cooldown.seconds
QR.cooldown.auto = !QR.cooldown.auto QR.cooldown.auto = !QR.cooldown.auto
QR.status() QR.status()
return return
if QR.ajax
QR.abort()
return
reply = QR.replies[0] reply = QR.replies[0]
reply.forceSave() if reply is QR.selected reply.forceSave() if reply is QR.selected
if g.BOARD.ID is 'f' if g.BOARD.ID is 'f'
@ -781,10 +791,6 @@ QR =
# Unfocus the focused element if it is one within the QR and we're not auto-posting. # Unfocus the focused element if it is one within the QR and we're not auto-posting.
d.activeElement.blur() d.activeElement.blur()
# Starting to upload might take some time.
# Provide some feedback that we're starting to submit.
QR.status progress: '...'
post = post =
resto: threadID resto: threadID
name: reply.name name: reply.name
@ -801,10 +807,9 @@ QR =
recaptcha_response_field: response recaptcha_response_field: response
callbacks = callbacks =
onload: -> onload: QR.response
QR.response @
onerror: -> onerror: ->
delete QR.ajax delete QR.req
# Connection error, or # Connection error, or
# CORS disabled error on www.4chan.org/banned # CORS disabled error on www.4chan.org/banned
QR.cooldown.auto = false QR.cooldown.auto = false
@ -817,16 +822,23 @@ QR =
form: $.formData post form: $.formData post
upCallbacks: upCallbacks:
onload: -> onload: ->
# Upload done, waiting for response. # Upload done, waiting for server response.
QR.status progress: '...' QR.req.progress = '...'
QR.status()
onprogress: (e) -> onprogress: (e) ->
# Uploading... # Uploading...
QR.status progress: "#{Math.round e.loaded / e.total * 100}%" QR.req.progress = "#{Math.round e.loaded / e.total * 100}%"
QR.status()
QR.ajax = $.ajax $.id('postForm').parentNode.action, callbacks, opts QR.req = $.ajax $.id('postForm').parentNode.action, callbacks, opts
# Starting to upload might take some time.
# Provide some feedback that we're starting to submit.
QR.req.progress = '...'
QR.status()
response: (req) -> response: ->
delete QR.ajax {req} = QR
delete QR.req
tmpDoc = d.implementation.createHTMLDocument '' tmpDoc = d.implementation.createHTMLDocument ''
tmpDoc.documentElement.innerHTML = req.response tmpDoc.documentElement.innerHTML = req.response
@ -916,8 +928,8 @@ QR =
QR.status() QR.status()
abort: -> abort: ->
if QR.ajax if QR.req
QR.ajax.abort() QR.req.abort()
delete QR.ajax delete QR.req
QR.notifications.push new Notification 'info', 'QR upload aborted.', 5 QR.notifications.push new Notification 'info', 'QR upload aborted.', 5
QR.status() QR.status()