Skip to content
Snippets Groups Projects
Commit 1a643e91 authored by Skriv's avatar Skriv
Browse files

rbuttonJS

parent 270e81ab
No related branches found
No related tags found
No related merge requests found
...@@ -12,22 +12,22 @@ Group of radiobutton will be created based on variable name. Checked state will ...@@ -12,22 +12,22 @@ Group of radiobutton will be created based on variable name. Checked state will
Macro.add('rbutton', { Macro.add('rbutton', {
handler() { handler() {
if (this.args.length < 2) { if (this.args.length < 2) {
const errors = []; const errors = [];
if (this.args.length < 1) { errors.push('variable name'); } if (this.args.length < 1) { errors.push('variable name'); }
if (this.args.length < 2) { errors.push('checked value'); } if (this.args.length < 2) { errors.push('checked value'); }
return this.error(`no ${errors.join(' or ')} specified`); return this.error(`no ${errors.join(' or ')} specified`);
} }
// Ensure that the variable name argument is a string. // Ensure that the variable name argument is a string.
if (typeof this.args[0] !== 'string') { if (typeof this.args[0] !== 'string') {
return this.error('variable name argument is not a string'); return this.error('variable name argument is not a string');
} }
const varName = this.args[0].trim(); const varName = this.args[0].trim();
// Try to ensure that we receive the variable's name (incl. sigil), not its value. // Try to ensure that we receive the variable's name (incl. sigil), not its value.
if (varName[0] !== '$' && varName[0] !== '_') { if (varName[0] !== '$' && varName[0] !== '_') {
return this.error(`variable name "${this.args[0]}" is missing its sigil ($ or _)`); return this.error(`variable name "${this.args[0]}" is missing its sigil ($ or _)`);
} }
const initValue = Wikifier.getValue(this.args[0]); const initValue = Wikifier.getValue(this.args[0]);
...@@ -48,59 +48,59 @@ Macro.add('rbutton', { ...@@ -48,59 +48,59 @@ Macro.add('rbutton', {
Setup and initialize the group counter. Setup and initialize the group counter.
*/ */
if (!TempState.hasOwnProperty(this.name)) { if (!TempState.hasOwnProperty(this.name)) {
TempState[this.name] = {}; TempState[this.name] = {};
} }
if (!TempState[this.name].hasOwnProperty(varId)) { if (!TempState[this.name].hasOwnProperty(varId)) {
TempState[this.name][varId] = 0; TempState[this.name][varId] = 0;
} }
/* /*
Setup and append the input element to the output buffer. Setup and append the input element to the output buffer.
*/ */
jQuery(el) jQuery(el)
.attr({ .attr({
id : `${this.name}-${varId}-${TempState[this.name][varId]++}`, id : `${this.name}-${varId}-${TempState[this.name][varId]++}`,
name : `${this.name}-${varId}`, name : `${this.name}-${varId}`,
type : 'radio', type : 'radio',
tabindex : 0 // for accessibility tabindex : 0 // for accessibility
}) })
.addClass(`macro-${this.name}`) .addClass(`macro-${this.name}`)
.on('change', function () { .on('change', function () {
if (this.checked) { if (this.checked) {
Wikifier.setValue(varName, checkValue); Wikifier.setValue(varName, checkValue);
if (replaceID.length > 0 && replaceText.length > 0){ if (replaceID.length > 0 && replaceText.length > 0){
var replaceEl = document.getElementById(replaceID);
//alert (replaceEl);
if (replaceEl !== null) {
replaceEl.innerHTML = replaceText;
}
var replaceEl = document.getElementById(replaceID);
//alert (replaceEl);
if (replaceEl !== null) {
replaceEl.innerHTML = replaceText;
} }
} }
} })
}) .ready (function () {
.ready (function () {
//alert ("DOM finished"); //alert ("DOM finished");
if (el.checked && replaceID.length > 0 && replaceText.length > 0){ if (el.checked && replaceID.length > 0 && replaceText.length > 0){
var replaceEl = document.getElementById(replaceID); var replaceEl = document.getElementById(replaceID);
//alert (replaceEl); //alert (replaceEl);
if (replaceEl !== null) { if (replaceEl !== null) {
replaceEl.innerHTML = replaceText; replaceEl.innerHTML = replaceText;
} }
} }
}) })
.appendTo(this.output); .appendTo(this.output);
/* /*
Set the story variable to the checked value and the input element to checked, if requested. Set the story variable to the checked value and the input element to checked, if requested.
*/ */
if (initValue === checkValue) { if (initValue === checkValue) {
el.checked = true; el.checked = true;
Wikifier.setValue(varName, checkValue); Wikifier.setValue(varName, checkValue);
} }
} }
}); });
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment