From 8a17da1a9161ec912453ee187ba99ba426d45ef8 Mon Sep 17 00:00:00 2001 From: Aleksey V Zapparov Date: Wed, 1 Aug 2012 22:37:57 +0200 Subject: [PATCH] Refactor work with hash --- demo/js/demo.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/demo/js/demo.js b/demo/js/demo.js index 7d4deb9b..888c67a9 100644 --- a/demo/js/demo.js +++ b/demo/js/demo.js @@ -1,8 +1,11 @@ +/*global window, document, location, CodeMirror, jsyaml, inspect, base64, hasher*/ + + window.runDemo = function runDemo() { 'use strict'; var source, result, initial, permalink, timer1, timer2 = null, - hash = location.hash.toString(); + fallback = document.getElementById('source').value || ''; // add sexy constructor jsyaml.addConstructor('!sexy', function (node) { @@ -32,22 +35,17 @@ window.runDemo = function runDemo() { } } - function updateSource(fallback) { + function updateSource() { var yaml; if (location.hash && '#yaml=' === location.hash.toString().slice(0,6)) { yaml = base64.decode(location.hash.slice(6)); } - source.setValue(yaml || fallback || ''); + source.setValue(yaml || fallback); parse(); } - function handleHashChange(newHash) { - hash = newHash; - updateSource(); - } - permalink = document.getElementById('permalink'); source = CodeMirror.fromTextArea(document.getElementById('source'), { @@ -83,12 +81,12 @@ window.runDemo = function runDemo() { }); // initial source - updateSource(document.getElementById('source').value); + updateSource(); // start monitor hash change hasher.prependHash = ''; - hasher.changed.add(handleHashChange); - hasher.initialized.add(handleHashChange); + hasher.changed.add(updateSource); + hasher.initialized.add(updateSource); hasher.init(); };