Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract more settings code and simplify some of the handlers. #167

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/js/class_hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -3061,7 +3061,7 @@ class Puzzle_hex extends Puzzle {
return th;
}
check_conflict(current_sol) {
if (UserSettings.conflict_detection > 1) {
if (UserSettings.show_conflicts) {
// User has disabled conflict detection.
this.conflict_cells = [];
return;
Expand Down
18 changes: 9 additions & 9 deletions docs/js/class_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class Puzzle {
this.reset_pause_layer();

// set the style and font
if (UserSettings.color_theme == 1) {
if (UserSettings.color_theme == THEME_LIGHT) {
pause_ctx.fillStyle = Color.BLUE;
} else {
pause_ctx.fillStyle = Color.WHITE;
Expand Down Expand Up @@ -7643,15 +7643,15 @@ class Puzzle {
if (number !== "") {
// S submode is 5, M submode is 6
// dynamic (i.e. upto 5 digits larger size and then smaller size)
if (UserSettings.sudoku_centre_size === 1) {
if (UserSettings.sudoku_centre_size === SUDOKU_CENTRE_AUTO) {
if (number.length > 5) {
this[this.mode.qa].number[k] = [number, this.mode[this.mode.qa][this.mode[this.mode.qa].edit_mode][1], "5"];
} else {
this[this.mode.qa].number[k] = [number, this.mode[this.mode.qa][this.mode[this.mode.qa].edit_mode][1], "6"];
}
} else if (UserSettings.sudoku_centre_size === 2) { // all large
} else if (UserSettings.sudoku_centre_size === SUDOKU_CENTRE_LARGE) { // all large
this[this.mode.qa].number[k] = [number, this.mode[this.mode.qa][this.mode[this.mode.qa].edit_mode][1], "6"];
} else if (UserSettings.sudoku_centre_size === 3) { // all small
} else if (UserSettings.sudoku_centre_size === SUDOKU_CENTRE_SMALL) { // all small
this[this.mode.qa].number[k] = [number, this.mode[this.mode.qa][this.mode[this.mode.qa].edit_mode][1], "5"];
}
} else {
Expand Down Expand Up @@ -9665,9 +9665,9 @@ class Puzzle {
if (this.ondown_key === "mousedown") { // do only star when on laptop
this.re_combi_star_reduced(num);
} else {
if (UserSettings.starbattle_dots === 3) {
if (UserSettings.starbattle_dots === STAR_DOTS_DISABLED) {
num = this.coord_p_edgex_star(x, y, 0);
} else if (UserSettings.starbattle_dots === 2) {
} else if (UserSettings.starbattle_dots === STAR_DOTS_LOW) {
num = this.coord_p_edgex_star(x, y, 0.2);
}
this.re_combi_star(num); // Behave as normal when ipad and phone
Expand Down Expand Up @@ -9747,9 +9747,9 @@ class Puzzle {
this.re_combi_akari_downright(num);
break;
case "star":
if (UserSettings.starbattle_dots === 3) {
if (UserSettings.starbattle_dots === STAR_DOTS_DISABLED) {
num = this.coord_p_edgex_star(x, y, 0);
} else if (UserSettings.starbattle_dots === 2) {
} else if (UserSettings.starbattle_dots === STAR_DOTS_LOW) {
num = this.coord_p_edgex_star(x, y, 0.2);
}
this.re_combi_star_downright(num);
Expand Down Expand Up @@ -12541,7 +12541,7 @@ class Puzzle {
}

check_conflict(current_sol) {
if (UserSettings.conflict_detection > 1) {
if (UserSettings.show_conflicts) {
// User has disabled conflict detection.
this.conflict_cells = [];
return;
Expand Down
2 changes: 1 addition & 1 deletion docs/js/class_square.js
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ class Puzzle_square extends Puzzle {
set_font_style(this.ctx, 0.7 * this.size.toString(10), this[pu].number[i][1]);

// if some numbers present in the corner (like Killer sudoku etc) then displace the numbers slightly lower to avoid overlap
let offset = (UserSettings.sudoku_normal_size === 2) ? 0.16 : 0.06;
let offset = UserSettings.sudoku_normal_bottom ? 0.16 : 0.06;
this.ctx.text(this[pu].number[i][0], p_x, p_y + offset * factor * this.size, this.size * 0.8);

break;
Expand Down
2 changes: 1 addition & 1 deletion docs/js/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@ function load(urlParam, type = 'url', origurl = null) {
if (rtext[13]) {
let parsedValue = JSON.parse(rtext[13]);
if (parsedValue === "true" || parsedValue === 1) {
UserSettings.custom_colors_on = 2;
UserSettings.custom_colors_on = true;
}
}

Expand Down
5 changes: 2 additions & 3 deletions docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ onload = function() {
selectContainer.appendChild(liteModeButton);

window.addEventListener('beforeunload', function(e) {
if (UserSettings.reload_button === 1) {
if (UserSettings.reload_button) {
// Cancel the event
e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
// Chrome requires returnValue to be set
Expand Down Expand Up @@ -2139,7 +2139,7 @@ onload = function() {
}

document.getElementById("reload_button").onchange = function() {
UserSettings.reload_button = this.value;
UserSettings.reload_button = parseInt(this.value, 10) === 1;
}

document.getElementById("allow_local_storage").onchange = function() {
Expand Down Expand Up @@ -2264,7 +2264,6 @@ onload = function() {
// Conflict detection
document.getElementById("conflict_detection_opt").onchange = function() {
UserSettings.conflict_detection = this.value;
pu.redraw();
}

// Enable or Disable Shortcuts
Expand Down
49 changes: 26 additions & 23 deletions docs/js/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const THEME_LIGHT = 1;
const THEME_DARK = 2;
const SUDOKU_CENTRE_AUTO = 1;
const SUDOKU_CENTRE_LARGE = 2;
const SUDOKU_CENTRE_SMALL = 3;
const STAR_DOTS_HIGH = 1;
const STAR_DOTS_LOW = 2;
const STAR_DOTS_DISABLED = 3;

function getCookie(name) {
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
Expand Down Expand Up @@ -86,10 +92,15 @@ const UserSettings = {
} else {
deleteCookie('conflict_detection');
}

pu.redraw();
},
get conflict_detection() {
return this._conflict_detection;
},
get show_conflicts() {
return this._conflict_detection > 1
},

// Star Battle Dot handling
_starbattle_dots: 1,
Expand Down Expand Up @@ -129,6 +140,9 @@ const UserSettings = {
get sudoku_normal_size() {
return this._sudoku_normal_size;
},
get sudoku_normal_bottom() {
return this._sudoku_normal_size === 2;
},

_sudoku_centre_size: 1,
set sudoku_centre_size(newValue) {
Expand All @@ -144,12 +158,10 @@ const UserSettings = {

_custom_colors_on: false,
set custom_colors_on(newValue) {
if (typeof newValue === 'string') {
const valueInt = newValue ? parseInt(newValue, 10) : 1;
this._custom_colors_on = (valueInt === 2);
} else {
this._custom_colors_on = !!newValue;
}
const stringValue = String(newValue);
const valueInt = (stringValue === 'true' || stringValue === '2') ? 2 : 1;

this._custom_colors_on = (valueInt === 2);

if (this._custom_colors_on) {
// On
Expand All @@ -159,7 +171,7 @@ const UserSettings = {
// Off
document.getElementById('style_special').style.display = 'none';
}
document.getElementById("custom_color_opt").value = this._custom_colors_on ? '2' : '1';
document.getElementById("custom_color_opt").value = valueInt;

pu.redraw();
},
Expand All @@ -170,18 +182,8 @@ const UserSettings = {
// This setting is for whether the user wants local storage to be used at all, ever
_local_storage: true,
set local_storage(newValue) {
let valueInt;
if (typeof newValue === 'boolean') {
valueInt = newValue ? 1 : 4;
} else {
valueString = String(newValue);

if (valueString.match(/[14]/)) {
valueInt = parseInt(newValue, 10);
} else if (valueString.match(/true|false/i)) {
valueInt = valueString === 'true' ? 1 : 4;
}
}
const valueString = String(newValue);
const valueInt = (valueString === 'true' || valueString === '1') ? 1 : 4;

this._local_storage = (valueInt === 1);

Expand Down Expand Up @@ -209,17 +211,18 @@ const UserSettings = {

_reload_button: 2,
set reload_button(newValue) {
if (newValue === "ON") { newValue = 1; }
if (newValue === "OFF") { newValue = 2; }
const valueString = String(newValue);
let valueInt = 2;

if (valueString === "ON" || valueString === 'true' || valueString === '1') { valueInt = 1; }

const valueInt = newValue ? parseInt(newValue, 10) : 2;
this._reload_button = valueInt;

document.getElementById("reload_button").value = valueInt;
this.attemptSave();
},
get reload_button() {
return this._reload_button;
return this._reload_button === 1;
},

_shortcuts_enabled: 1,
Expand Down