Skip to content
Snippets Groups Projects
Commit bba28bd4 authored by Pregmodder's avatar Pregmodder
Browse files

Merge branch 'fix' into 'pregmod-master'

Cleanup

See merge request pregmodfan/fc-pregmod!8592
parents 37ef4953 d6bb7be7
No related branches found
No related tags found
No related merge requests found
/* This is modified radiobutton macro, for automatic checked state setup*/
/* Usage (be sure to use quotes around parameters):
<<rbutton "$variable" "value">>
Or:
<<rbutton "$variable" "value" "HTML_element_ID" "Text to replace with, inside html element with ID from previous parameter. <br> HTML tags allowed.">>
Group of radiobutton will be created based on variable name. Checked state will be set up if variable contain value matched with second parameter. Full form of macro call can be used to display extended description of selected value.
*/
Macro.add('rbutton', {
handler() {
if (this.args.length < 2) {
const errors = [];
if (this.args.length === 0) { errors.push('variable name'); }
if (this.args.length < 2) { errors.push('checked value'); }
return this.error(`no ${errors.join(' or ')} specified`);
}
// Ensure that the variable name argument is a string.
if (typeof this.args[0] !== 'string') {
return this.error('variable name argument is not a string');
}
const varName = this.args[0].trim();
// Try to ensure that we receive the variable's name (incl. sigil), not its value.
if (varName[0] !== '$' && varName[0] !== '_') {
return this.error(`variable name "${this.args[0]}" is missing its sigil ($ or _)`);
}
const initValue = Wikifier.getValue(this.args[0]);
const varId = Util.slugify(varName);
const checkValue = this.args[1];
const el = document.createElement('input');
let replaceID = "";
let replaceText = "";
if (typeof this.args[2] === 'string') {
replaceID = this.args[2];
}
if (typeof this.args[3] === 'string') {
replaceText = this.args[3];
}
/*
Setup and initialize the group counter.
*/
if (!TempState.hasOwnProperty(this.name)) {
TempState[this.name] = {};
}
if (!TempState[this.name].hasOwnProperty(varId)) {
TempState[this.name][varId] = 0;
}
/*
Setup and append the input element to the output buffer.
*/
jQuery(el)
.attr({
id: `${this.name}-${varId}-${TempState[this.name][varId]++}`,
name: `${this.name}-${varId}`,
type: 'radio',
tabindex: 0 // for accessibility
})
.addClass(`macro-${this.name}`)
.on('change', function() {
if (this.checked) {
Wikifier.setValue(varName, checkValue);
if (replaceID.length > 0 && replaceText.length > 0) {
let replaceEl = document.getElementById(replaceID);
// alert (replaceEl);
if (replaceEl !== null) {
replaceEl.innerHTML = replaceText;
}
}
}
})
.ready(function() {
// alert ("DOM finished");
if (el.checked && replaceID.length > 0 && replaceText.length > 0) {
let replaceEl = document.getElementById(replaceID);
// alert (replaceEl);
if (replaceEl !== null) {
replaceEl.innerHTML = replaceText;
}
}
})
.appendTo(this.output);
/*
Set the story variable to the checked value and the input element to checked, if requested.
*/
if (initValue === checkValue) {
el.checked = true;
Wikifier.setValue(varName, checkValue);
}
}
});
Macro.add("textbox2", {
handler: function() {
if (this.args.length < 2) {
const e = [];
return this.args.length < 1 && e.push("variable name"), this.args.length < 2 && e.push("default value"), this.error(`no ${e.join(" or ")} specified`);
}
if (typeof this.args[0] !== "string") { return this.error("variable name argument is not a string"); }
const t = this.args[0].trim();
if (t[0] !== "$" && t[0] !== "_") { return this.error(`variable name "${this.args[0]}" is missing its sigil ($ or _)`); }
Config.debug && this.debugView.modes({
block: true
});
const r = Util.slugify(t);
const a = this.args[1];
const isNumber = typeof(a) === "number";
const inputElement = document.createElement("input");
let autofocus = false;
let passage = void 0;
let setargs = null;
if (this.args.length > 3) {
passage = this.args[2];
autofocus = this.args[3] === "autofocus";
if (!autofocus) {
setargs = this.args[3];
}
} else if (this.args.length > 2) {
if (this.args[2] === "autofocus") {
autofocus = true;
} else {
passage = this.args[2];
}
}
if (passage !== (void 0) && typeof(passage) === "object") {
passage = passage.link;
}
if (!passage) {
passage = State.passage;
}
function gotoPassage() {
if (passage) {
const currentScrollPosition = window.pageYOffset;
const currentPassage = State.passage;
if (setargs) {
Scripting.evalTwineScript(setargs);
}
Engine.play(passage);
if (currentPassage === passage) {
Scripting.evalJavaScript(`window.scrollTo(0, ${currentScrollPosition});`);
}
}
}
function valueToNumberIfSame(v) {
if (!isNumber) {
return v;
} // Do nothing
try {
return parseInt(v, 10);
} catch (error) {
return v;
}
}
jQuery(inputElement).attr({
id: `${this.name}-${r}`,
name: `${this.name}-${r}`,
// type: isNumber ? "number" : "text", /* TODO: - hide spinner if we do this */
tabindex: 0
}).addClass(`macro-${this.name}`)
.on("change", function() {
State.setVar(t, valueToNumberIfSame(this.value));
}).on("blur", function() {
State.setVar(t, valueToNumberIfSame(this.value));
// eslint-disable-next-line eqeqeq
if (this.value != a) { // If the value has actually changed, reload the page. Note != and not !== because types might be different
gotoPassage();
}
})
.on("keypress", function (e) {
// FIXME: deprecated method
e.which === 13 && (e.preventDefault(), State.setVar(t, valueToNumberIfSame(this.value)), gotoPassage());
}).appendTo(this.output), State.setVar(t, a), inputElement.value = a, autofocus && (inputElement.setAttribute("autofocus", "autofocus"), postdisplay[`#autofocus:${inputElement.id}`] = function(e) {
delete postdisplay[e], setTimeout(function() {
return inputElement.focus();
}, Engine.minDomActionDelay);
});
}
});
/* Nicked off greyelf, works for replace textboxes */
globalThis.setReplaceTextboxMaxLength = function(storyVarName, maxLength) {
const textboxId = `#textbox-${Util.slugify(storyVarName)}`;
$(textboxId)
.attr("maxlength", maxLength)
.css({
"min-width": "initial",
"width": `${maxLength}em`,
"padding": "3px 2px"
});
};
/* Nicked off TheMadExile, works for non-replace textboxes */
globalThis.setTextboxMaxLength = function(storyVarName, maxLength) {
const textboxId = `#textbox-${Util.slugify(storyVarName)}`;
postdisplay[`${textboxId}-maxlength`] = function(taskName) {
delete postdisplay[taskName];
$(textboxId)
.attr("maxlength", maxLength)
.css({
"min-width": "initial",
"width": `${maxLength}em`,
"padding": "3px 2px"
});
};
};
......@@ -452,7 +452,7 @@ App.UI.DOM.formatException = function formatException(ex, recursion = false) {
* In case the normal rendering failed attempts to provide some information on it.
* In the worst case gives back a default error message.
*
* @param exception
* @param {Error} exception
* @returns {DocumentFragment|HTMLParagraphElement}
*/
function failSafe(exception) {
......
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