|
# error handling |
|
error = |
|
throw: () -> |
|
# our pen url error will be based on whether it can find the html |
|
$(".intro").removeClass "hidden" |
|
$(".intro .error").html """ |
|
<h2>Could not load pen.</h2> |
|
<p>Make sure it is the right format.</p> |
|
""" |
|
clear: () -> |
|
$(".intro .error").html "" |
|
|
|
# toggling the intro and pre elements |
|
toggleElements = () -> |
|
$(".intro").addClass "hidden" |
|
$("#_pre").addClass "active" |
|
|
|
|
|
|
|
# primary draw function |
|
drawPen = (_path) -> |
|
_code = [] |
|
_current_code = 0 |
|
# get pen files |
|
$.when( |
|
$.get("#{_path}.html", (html) -> |
|
# setting html string |
|
_code[0] = { type: "html", string: html } |
|
# TODO: strip script tags from html |
|
return |
|
).fail( () -> |
|
# fire off our error |
|
error.throw() |
|
), $.get("#{_path}.css", (css) -> |
|
# setting css string |
|
_code[1] = { type: "css", string: css } |
|
return |
|
), $.get("#{_path}.js", (js) -> |
|
# setting js string |
|
_code[2] = { type: "js", string: js } |
|
return |
|
, "text") # important to specify text, overrides jquery's auto detect on script |
|
).then -> |
|
# clear any existing errors |
|
error.clear() |
|
# toggle the elements |
|
toggleElements() |
|
|
|
# select our primary elements |
|
$pre = document.getElementById "_pre" |
|
$style = document.getElementById "_style" |
|
$html = document.getElementById "_html" |
|
$script = document.getElementById "_script" |
|
|
|
|
|
# creating a script and running it |
|
runJavascript = () -> |
|
console.log "Running the Javascript" |
|
script_tag = document.createElement "script" |
|
script_tag.innerHTML = _code[2].string |
|
$script.appendChild script_tag |
|
|
|
# removing all css and readding |
|
refreshCSS = () -> |
|
console.log "Refreshing the CSS" |
|
_styles = $style.innerHTML |
|
$style.innerHTML = "" |
|
$style.innerHTML = _styles |
|
|
|
|
|
# ongoing html string |
|
__html = "" |
|
# write a single character |
|
writeChar = (which, type) -> |
|
# Writing Javascript |
|
if type == "js" |
|
# do nothing for now |
|
# Writing CSS |
|
else if type == "css" |
|
$style.innerHTML += which |
|
# Writing HTML |
|
else # if type == "html" |
|
__html += which |
|
# need this to make it render as html |
|
el = document.createElement("div") |
|
el.innerHTML = __html |
|
$html.innerHTML = "" |
|
$html.appendChild el |
|
|
|
# add character to pre |
|
$pre.innerHTML += which |
|
|
|
|
|
# writing all the codez |
|
writeCode = (code, index, interval) -> |
|
# if first character |
|
if index == 0 |
|
if _current_code != 0 then $pre.innerHTML += "\n\n\n\n" |
|
if code.type == "html" then $pre.innerHTML += "<!-- HTML -->" |
|
if code.type == "css" then $pre.innerHTML += "/* CSS */" |
|
if code.type == "js" then $pre.innerHTML += "// Javascript" |
|
$pre.innerHTML += "\n\n" |
|
# if we're still going |
|
if index < code.string.length |
|
# keep the pre scrolled to the bottom |
|
$pre.scrollTop = $pre.scrollHeight |
|
# write the character |
|
writeChar code.string[index++], code.type |
|
# call this again |
|
setTimeout (-> |
|
writeCode code, index, interval |
|
), interval |
|
else |
|
# if javascript, run it |
|
if code.type == "js" then runJavascript() |
|
# if css, remove all then add it back to refresh |
|
if code.type == "css" then refreshCSS() |
|
# increment up the code array index |
|
_current_code++ |
|
# if we have more code to write, move to next code block |
|
if _current_code < _code.length |
|
writeCode(_code[_current_code], 0, interval) |
|
|
|
|
|
# initiate the script |
|
writeCode(_code[0], 0, 16) |
|
|
|
|
|
# clicking an option |
|
$(".intro a").click () -> |
|
_path = $(this).data "path" |
|
drawPen(_path) |
|
return false |
|
|
|
# providing a url |
|
$("#custom-pen-button").click () -> |
|
_path = $("#custom-pen-path").val() |
|
# if there is a value in the input |
|
if _path != "" |
|
error.clear() |
|
drawPen(_path) |
|
else |
|
error.throw() |
|
|
|
|
|
jakealbaughSignature() |