kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/kaka.farm
Log | Files | Refs | README

commit e9d2121dbc24e0bbc39cf96ec33861cce6180500
parent 753ea24a49748b00d361bfd62e7a614b91c7fd1d
Author: Yuval Langer <yuvallangerontheroad@gmail.com>
Date:   Sun, 12 Jul 2020 21:27:21 +0300

Move Sokoban web-related javascript code into its own javascript file.

Diffstat:
Ahtml/sokoban/sokoban-web.js | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mhtml/sokoban/sokoban.html | 62++------------------------------------------------------------
2 files changed, 59 insertions(+), 60 deletions(-)

diff --git a/html/sokoban/sokoban-web.js b/html/sokoban/sokoban-web.js @@ -0,0 +1,57 @@ +'use strict'; + + +(function() { + function load_game() { + return JSON.parse(window.localStorage.getItem('game')); + }; + + + function save_game(game) { + window.localStorage.setItem('game', JSON.stringify(game)); + }; + + + function main() { + let game = load_game() || sokoban.new_game(); + + let text_element = document.getElementById('text'); + text_element.innerText = sokoban.game_to_text(game); + + [ + ['up', sokoban.action_step_up], + ['down', sokoban.action_step_down], + ['left', sokoban.action_step_left], + ['right', sokoban.action_step_right], + ['next_level', sokoban.action_next_level], + ['restart_level', sokoban.action_restart_level], + ['undo', sokoban.action_undo], + ['save', save_game], + ].forEach(([element_id, action_function])=>{ + let button = document.getElementById(element_id); + console.debug(button) + function click_handler() { + console.debug('click: ' + element_id); + action_function(game); + text_element.innerText = sokoban.game_to_text(game); + }; + button.addEventListener('click', click_handler); + }); + + [ + ['new', sokoban.new_game], + ['load', load_game], + ].forEach(([element_id, action_function])=>{ + let button = document.getElementById(element_id); + console.debug(button) + function click_handler() { + console.debug('click: ' + element_id); + game = action_function(); + text_element.innerText = sokoban.game_to_text(game); + }; + button.addEventListener('click', click_handler); + }); + }; + + window.addEventListener('load', main); +})(); diff --git a/html/sokoban/sokoban.html b/html/sokoban/sokoban.html @@ -10,67 +10,9 @@ window.kakalog_activated = true; </script> - <script type="text/javascript" src="kakalog.js"></script> + <script type="text/javascript" src="../kakalog.js"></script> <script type="text/javascript" src="sokoban.js"></script> - <script type="text/javascript"> -'use strict'; - - -(function() { - function load_game() { - return JSON.parse(window.localStorage.getItem('game')); - }; - - - function save_game(game) { - window.localStorage.setItem('game', JSON.stringify(game)); - }; - - - function main() { - let game = load_game() || sokoban.new_game(); - - let text_element = document.getElementById('text'); - text_element.innerText = sokoban.game_to_text(game); - - [ - ['up', sokoban.action_step_up], - ['down', sokoban.action_step_down], - ['left', sokoban.action_step_left], - ['right', sokoban.action_step_right], - ['next_level', sokoban.action_next_level], - ['restart_level', sokoban.action_restart_level], - ['undo', sokoban.action_undo], - ['save', save_game], - ].forEach(([element_id, action_function])=>{ - let button = document.getElementById(element_id); - console.debug(button) - function click_handler() { - console.debug('click: ' + element_id); - action_function(game); - text_element.innerText = sokoban.game_to_text(game); - }; - button.addEventListener('click', click_handler); - }); - - [ - ['new', sokoban.new_game], - ['load', load_game], - ].forEach(([element_id, action_function])=>{ - let button = document.getElementById(element_id); - console.debug(button) - function click_handler() { - console.debug('click: ' + element_id); - game = action_function(); - text_element.innerText = sokoban.game_to_text(game); - }; - button.addEventListener('click', click_handler); - }); - }; - - window.addEventListener('load', main); -})(); - </script> + <script type="text/javascript" src="sokoban-web.js"></script> </head> <body> <pre id="text"></pre>