Skip to content

Commit

Permalink
Refactor work with hash
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Aug 1, 2012
1 parent 02dcfb2 commit 8a17da1
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions demo/js/demo.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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'), {
Expand Down Expand Up @@ -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();
};

Expand Down

0 comments on commit 8a17da1

Please sign in to comment.