Add and implement configurable actions after reaching the first or a saved message

This commit is contained in:
chylex 2016-10-25 17:17:45 +02:00
parent 4087684913
commit 939720dfa9
3 changed files with 76 additions and 9 deletions

View File

@ -16,8 +16,21 @@ var GUI = (function(){
}
}
if (settings && type === "gui" && detail === "settings"){
settings.ui.cbAutoscroll.checked = STATE.settings.autoscroll;
if (settings){
var force = type === "gui" && detail === "settings";
if (force){
settings.ui.cbAutoscroll.checked = STATE.settings.autoscroll;
settings.ui.optsAfterFirstMsg[STATE.settings.afterFirstMsg].checked = true;
settings.ui.optsAfterSavedMsg[STATE.settings.afterSavedMsg].checked = true;
}
if (type === "setting" || force){
var autoscrollRev = !STATE.settings.autoscroll;
Object.values(settings.ui.optsAfterFirstMsg).forEach(ele => ele.disabled = autoscrollRev);
Object.values(settings.ui.optsAfterSavedMsg).forEach(ele => ele.disabled = autoscrollRev);
}
}
};
@ -161,21 +174,49 @@ var GUI = (function(){
settings.ele.id = "dht-cfg";
settings.ele.innerHTML = [
"<label><input id='dht-cfg-autoscroll' type='checkbox'> Autoscroll</label>"
"<label><input id='dht-cfg-autoscroll' type='checkbox'> Autoscroll</label><br>",
"<br>",
"<label>After reaching the first message in channel...</label><br>",
"<label><input id='dht-cfg-afm-nothing' name='dht-afm' type='radio'> Do Nothing</label><br>",
"<label><input id='dht-cfg-afm-pause' name='dht-afm' type='radio'> Pause Tracking</label><br>",
"<br>",
"<label>After reaching a previously saved message...</label><br>",
"<label><input id='dht-cfg-asm-nothing' name='dht-asm' type='radio'> Do Nothing</label><br>",
"<label><input id='dht-cfg-asm-pause' name='dht-asm' type='radio'> Pause Tracking</label><br>",
].join("");
// elements
settings.ui = {
cbAutoscroll: DOM.id("dht-cfg-autoscroll")
cbAutoscroll: DOM.id("dht-cfg-autoscroll"),
optsAfterFirstMsg: {},
optsAfterSavedMsg: {}
};
settings.ui.optsAfterFirstMsg[CONSTANTS.AUTOSCROLL_ACTION_NOTHING] = DOM.id("dht-cfg-afm-nothing");
settings.ui.optsAfterFirstMsg[CONSTANTS.AUTOSCROLL_ACTION_PAUSE] = DOM.id("dht-cfg-afm-pause");
settings.ui.optsAfterSavedMsg[CONSTANTS.AUTOSCROLL_ACTION_NOTHING] = DOM.id("dht-cfg-asm-nothing");
settings.ui.optsAfterSavedMsg[CONSTANTS.AUTOSCROLL_ACTION_PAUSE] = DOM.id("dht-cfg-asm-pause");
// events
settings.ui.cbAutoscroll.addEventListener("change", () => {
STATE.settings.autoscroll = settings.ui.cbAutoscroll.checked;
});
Object.keys(settings.ui.optsAfterFirstMsg).forEach(key => {
settings.ui.optsAfterFirstMsg[key].addEventListener("click", () => {
STATE.settings.afterFirstMsg = key;
});
});
Object.keys(settings.ui.optsAfterSavedMsg).forEach(key => {
settings.ui.optsAfterSavedMsg[key].addEventListener("click", () => {
STATE.settings.afterSavedMsg = key;
});
});
setupStateChanged("settings");
},

View File

@ -1,3 +1,8 @@
var CONSTANTS = {
AUTOSCROLL_ACTION_NOTHING: "optNothing",
AUTOSCROLL_ACTION_PAUSE: "optPause",
};
var STATE = (function(){
var stateChangedEvents = [];
@ -33,8 +38,8 @@ var STATE = (function(){
*/
SETTINGS.prototype._reset = function(){
this._autoscroll = true;
this._afterFirstMsg = "optPause";
this._afterSavedMsg = "optPause";
this._afterFirstMsg = CONSTANTS.AUTOSCROLL_ACTION_PAUSE;
this._afterSavedMsg = CONSTANTS.AUTOSCROLL_ACTION_PAUSE;
};
/*

View File

@ -4,10 +4,26 @@ DISCORD.setupMessageRequestHook((channel, messages) => {
if (info.id == channel){ // Discord has a bug where the message request may be sent without switching channels
STATE.addDiscordChannel(info.server, info.type, channel, info.channel);
STATE.addDiscordMessages(channel, messages);
var hasUpdatedFile = STATE.addDiscordMessages(channel, messages);
if (STATE.settings.autoscroll){
DOM.setTimer(() => DISCORD.loadOlderMessages(), 0);
DOM.setTimer(() => {
var action = CONSTANTS.AUTOSCROLL_ACTION_NOTHING;
if (!hasUpdatedFile){
action = STATE.settings.afterSavedMsg;
}
else if (!DISCORD.hasMoreMessages()){
action = STATE.settings.afterFirstMsg;
}
if (action === CONSTANTS.AUTOSCROLL_ACTION_PAUSE){
STATE.toggleTracking();
}
else{
DISCORD.loadOlderMessages();
}
}, 0);
}
}
}
@ -15,7 +31,12 @@ DISCORD.setupMessageRequestHook((channel, messages) => {
STATE.onStateChanged((type, detail) => {
if (type === "tracking" && detail && STATE.settings.autoscroll){
DISCORD.loadOlderMessages();
if (DISCORD.hasMoreMessages()){
DISCORD.loadOlderMessages();
}
else if (STATE.settings.afterFirstMsg === CONSTANTS.AUTOSCROLL_ACTION_PAUSE){
DOM.setTimer(() => STATE.toggleTracking(), 200); // give the user visual feedback after clicking the button before switching off
}
}
});