User:PetraMagna/momotalk-choice.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
PetraMagna (talk | contribs) draft; WIP |
PetraMagna (talk | contribs) m add documentation |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
const momotalk_options = function() { |
const momotalk_options = function() { |
||
// wait for jQuery to become available |
|||
function defer(method) { |
function defer(method) { |
||
if (window.jQuery) { |
if (window.jQuery) { |
||
Line 9: | Line 9: | ||
} |
} |
||
// specifies what to do after the user clicks on a sensei reply option |
|||
⚫ | |||
function momotalk_option_click(group_num, option_num) { |
|||
const container = $("#momotalk-sensei-option-group-" + group_num); |
|||
container.children('.momotalk-reply-option').each(function(num, option) { |
|||
const index = num + 1; |
|||
// note that addClass and removeClass become nop if the corresponding jquery objects don't exist |
|||
// highlight selected option |
|||
if (index === option_num) { |
|||
$(option).addClass("momotalk-selected-option"); |
|||
} else { |
|||
$(option).removeClass("momotalk-selected-option"); |
|||
} |
|||
// hide student text corresponding to the options that are not selected |
|||
$(".momotalk-student-option-" + group_num + "-" + index).each(function(i, student_reply) { |
|||
if (index === option_num) { |
|||
$(student_reply).removeClass("momotalk-hidden-student-text"); |
|||
} else { |
|||
$(student_reply).addClass("momotalk-hidden-student-text"); |
|||
} |
|||
}); |
|||
}); |
|||
} |
|||
⚫ | |||
$(".momotalk-reply-container").each(function(i, container) { |
$(".momotalk-reply-container").each(function(i, container) { |
||
// skip containers without an id |
|||
const container_id = container.id; |
|||
if (container_id == null) { |
|||
return; |
|||
} |
|||
// extract group number from the id |
|||
const container_id_split = container_id.split("-"); |
|||
const group_num = container_id_split[container_id_split.length - 1]; |
|||
// register onclick function for each option |
|||
$(container).children('.momotalk-reply-option').each(function(option_num, option) { |
|||
option.onclick = function() { |
|||
momotalk_option_click(group_num, option_num + 1); |
|||
}; |
|||
// by default, the first reply option is selected |
|||
if (option_num === 0) { |
|||
option.click(); |
|||
} |
|||
}); |
|||
}); |
}); |
||
} |
} |
||
function momotalk_main( |
function momotalk_main() { |
||
$(document).ready(function() { |
$(document).ready(function() { |
||
const title_split = mw.config.get('wgTitle').toLowerCase().split("/"); |
const title_split = mw.config.get('wgTitle').toLowerCase().split("/"); |
Latest revision as of 03:56, 8 December 2023
const momotalk_options = function() {
// wait for jQuery to become available
function defer(method) {
if (window.jQuery) {
method();
} else {
setTimeout(function() { defer(method) }, 50);
}
}
// specifies what to do after the user clicks on a sensei reply option
function momotalk_option_click(group_num, option_num) {
const container = $("#momotalk-sensei-option-group-" + group_num);
container.children('.momotalk-reply-option').each(function(num, option) {
const index = num + 1;
// note that addClass and removeClass become nop if the corresponding jquery objects don't exist
// highlight selected option
if (index === option_num) {
$(option).addClass("momotalk-selected-option");
} else {
$(option).removeClass("momotalk-selected-option");
}
// hide student text corresponding to the options that are not selected
$(".momotalk-student-option-" + group_num + "-" + index).each(function(i, student_reply) {
if (index === option_num) {
$(student_reply).removeClass("momotalk-hidden-student-text");
} else {
$(student_reply).addClass("momotalk-hidden-student-text");
}
});
});
}
function momotalk_init() {
$(".momotalk-reply-container").each(function(i, container) {
// skip containers without an id
const container_id = container.id;
if (container_id == null) {
return;
}
// extract group number from the id
const container_id_split = container_id.split("-");
const group_num = container_id_split[container_id_split.length - 1];
// register onclick function for each option
$(container).children('.momotalk-reply-option').each(function(option_num, option) {
option.onclick = function() {
momotalk_option_click(group_num, option_num + 1);
};
// by default, the first reply option is selected
if (option_num === 0) {
option.click();
}
});
});
}
function momotalk_main() {
$(document).ready(function() {
const title_split = mw.config.get('wgTitle').toLowerCase().split("/");
// don't do anything if this is not a MomoTalk page
if (mw.config.get('wgNamespaceNumber') === 0 && title_split.length === 2 && title_split[1] === 'momotalk') {
momotalk_init();
} else {
console.log("Not MomoTalk page. Exit.");
}
});
}
defer(momotalk_main);
};
momotalk_options();