Math.uuid.js added. openjscad: it loads csd and itself via xhr. the relative path is wrong in simple.html, because simple.html/filename -> simple.html/csd.js. openjscad loads ../csd.js and ../openjscad.js now. but in index.html and the other demos wont work now.

This commit is contained in:
Denis Knauf 2012-03-17 22:59:19 +01:00
parent 6bb529834f
commit 47955a0b3d
3 changed files with 197 additions and 73 deletions

92
Math.uuid.js Normal file
View file

@ -0,0 +1,92 @@
/*!
Math.uuid.js (v1.4)
http://www.broofa.com
mailto:robert@broofa.com
Copyright (c) 2010 Robert Kieffer
Dual licensed under the MIT and GPL licenses.
*/
/*
* Generate a random uuid.
*
* USAGE: Math.uuid(length, radix)
* length - the desired number of characters
* radix - the number of allowable values for each character.
*
* EXAMPLES:
* // No arguments - returns RFC4122, version 4 ID
* >>> Math.uuid()
* "92329D39-6F5C-4520-ABFC-AAB64544E172"
*
* // One argument - returns ID of the specified length
* >>> Math.uuid(15) // 15 character ID (default base=62)
* "VcydxgltxrVZSTV"
*
* // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
* >>> Math.uuid(8, 2) // 8 character ID (base=2)
* "01001010"
* >>> Math.uuid(8, 10) // 8 character ID (base=10)
* "47473046"
* >>> Math.uuid(8, 16) // 8 character ID (base=16)
* "098F4D35"
*/
(function() {
// Private array of chars to use
var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
Math.uuid = function (len, radix) {
var chars = CHARS, uuid = [], i;
radix = radix || chars.length;
if (len) {
// Compact form
for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
} else {
// rfc4122, version 4 form
var r;
// rfc4122 requires these characters
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
// Fill in random data. At i==19 set the high bits of clock sequence as
// per rfc4122, sec. 4.1.5
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | Math.random()*16;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
};
// A more performant, but slightly bulkier, RFC4122v4 solution. We boost performance
// by minimizing calls to random()
Math.uuidFast = function() {
var chars = CHARS, uuid = new Array(36), rnd=0, r;
for (var i = 0; i < 36; i++) {
if (i==8 || i==13 || i==18 || i==23) {
uuid[i] = '-';
} else if (i==14) {
uuid[i] = '4';
} else {
if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
r = rnd & 0xf;
rnd = rnd >> 4;
uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
}
}
return uuid.join('');
};
// A more compact, but less performant, RFC4122v4 solution:
Math.uuidCompact = function() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
};
})();

View file

@ -308,8 +308,8 @@ OpenJsCad.javaScriptToSolidSync = function(script, mainParameters, debugging) {
// callback: should be function(error, csg) // callback: should be function(error, csg)
OpenJsCad.javaScriptToSolidASync = function(script, mainParameters, callback) { OpenJsCad.javaScriptToSolidASync = function(script, mainParameters, callback) {
var baselibraries = [ var baselibraries = [
"csg.js", "../csg.js",
"openjscad.js" "../openjscad.js"
]; ];
var baseurl = document.location + ""; var baseurl = document.location + "";
var workerscript = ""; var workerscript = "";

View file

@ -1,27 +1,27 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<script src="lightgl.js"></script> <script src="../lightgl.js"></script>
<script src="csg.js"></script> <script src="../csg.js"></script>
<script src="openjscad.js"></script> <script src="../openjscad.js"></script>
<script src="jquery.js"></script> <script src="../jquery.js"></script>
<script src="coffee-script.js"></script> <script src="../coffee-script.js"></script>
<script src="../Math.uuid.js"></script>
<style> <style>
body { body {
font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif; font: 14px/20px 'Helvetica Neue Light', HelveticaNeue-Light, 'Helvetica Neue', Helvetica, Arial, sans-serif;
} }
pre, code, textarea { pre, code, textarea, canvas {
font: 12px/20px Monaco, monospace; font: 12px/20px Monaco, monospace;
border: 1px solid #CCC; border: 1px solid #CCC;
border-radius: 3px; border-radius: 3px;
background: #F9F9F9; background: #F9F9F9;
padding: 0 3px; padding: 0;
margin: 0;
color: #555; color: #555;
} }
pre, textarea {
padding: 10px;
}
pre { pre {
padding: 10px;
width: 100%; width: 100%;
} }
textarea:focus { textarea:focus {
@ -30,88 +30,120 @@
canvas { canvas {
cursor: move; cursor: move;
} }
header > *{ header {
padding: 0;
margin: 0;
position: fixed;
top: 0;
right: 0;
left: 0;
border-bottom: 1px solid black;
background: #bbb;
height: 1.2em;
}
header > * {
display: inline-block; display: inline-block;
} }
body {
margin: 1.3em 0 0 0;
padding: 0;
}
</style> </style>
<link rel="stylesheet" href="openjscad.css" type="text/css"> <link rel="stylesheet" href="openjscad.css" type="text/css">
<script> <script type="text/coffeescript">
# Show all exceptions to the user:
OpenJsCad.AlertUserOfUncaughtExceptions()
gProcessor = revision = null
var gProcessor=null; window.prepareCode = (lang, code) ->
if 'CoffeeScript' == lang
code = CoffeeScript.compile code, bare: 'on'
console.group 'code'
console.log code
console.groupEnd()
code
window.getCode = -> $('#code').val()
window.getLang = -> $('#lang').val()
window.getJsCode = ->
prepareCode getLang(), getCode()
// Show all exceptions to the user: window.updateSolid = ->
OpenJsCad.AlertUserOfUncaughtExceptions(); gProcessor.setJsCad getJsCode()
function onload() { # http://pallieter.org/Projects/insertTab/
gProcessor = new OpenJsCad.Processor(document.getElementById("viewer")); window.insertTab = (o, e) ->
updateSolid(); kC = e.keyCode ? e.charCode ? e.which
} if kC == 9 && !e.shiftKey && !e.ctrlKey && !e.altKey
oS = o.scrollTop
if o.setSelectionRange
sS = o.selectionStart
sE = o.selectionEnd
o.value = "#{o.value.substring 0, sS}\t#{o.value.substr sE}"
o.setSelectionRange sS + 1, sS + 1
o.focus()
else if o.createTextRange
document.selection.createRange().text = "\t"
e.returnValue = false
o.scrollTop = oS
if e.preventDefault
e.preventDefault()
true
function getCode() { window.currentFilename = -> /\/([^\/]*?)$/.exec(window.location.pathname)[1]
var code = document.getElementById('code').value;
var lang = document.getElementById('lang').value;
if( 'CoffeeScript' === lang)
code = CoffeeScript.compile( code, { bare: 'on'});
return code;
}
function updateSolid() { window.loadFile = (fn) ->
gProcessor.setJsCad( getCode()); rev = localStorage[fn]
} JSON.parse localStorage[rev]
/* http://pallieter.org/Projects/insertTab/ */ window.writeFile = (fn, lang, code) ->
function insertTab(o, e) { rev = Math.uuid()
var kC = e.keyCode ? e.keyCode : e.charCode ? e.charCode : e.which; localStorage[rev] = JSON.stringify [fn, revision, getLang(), getCode()]
if (kC == 9 && !e.shiftKey && !e.ctrlKey && !e.altKey) { localStorage[fn] = revision = rev
var oS = o.scrollTop;
if (o.setSelectionRange) { # Store the file-history (for undo) in the browser-storage:
var sS = o.selectionStart; window.edit_history = (event) ->
var sE = o.selectionEnd; fn = currentFilename()
o.value = o.value.substring(0, sS) + "\t" + o.value.substr(sE); code = getCode()
o.setSelectionRange(sS + 1, sS + 1); lang = getLang()
o.focus(); [_,_, oldlang, oldcode] = loadFile fn
} writeFile fn, lang, code unless oldcode == code and oldlang == lang
else if (o.createTextRange) { true
document.selection.createRange().text = "\t";
e.returnValue = false; CSG.loadFile = (fn) ->
} [fn, rev, lang, code] = loadFile()
o.scrollTop = oS; eval prepareCode( lang, code)
if (e.preventDefault)
e.preventDefault(); window.resizeCode = ->
return false; $code = $ '#code'
} $code.css
return true; width: "#{$('body').innerWidth() - $('#viewer').outerWidth() - $code.outerWidth() + $code.width()}px"
} height: '600px'
$ ->
$('#code').
on( 'keydown', (e)-> insertTab this, e).
on( 'keyup', edit_history)
[fn, rev, lang, code] = loadFile currentFilename()
$('#lang').val lang
$('#code').val code
gProcessor = new OpenJsCad.Processor $("#viewer")[0]
updateSolid()
resizeCode()
hist = [];
function edit_history(event) {
}
</script> </script>
<title>OpenJsCad</title> <title>OpenJsCad</title>
</head> </head>
<body onload="onload()"> <body>
<header> <header>
<h1>OpenJsCad</h1> <a href='#' onclick="updateSolid(); return false;">Update</a>
[<a href='#' onclick="updateSolid(); return false;">Update</a>]
</header> </header>
<div id="viewer" style="float:right;"></div> <div id="viewer" style="float:right;"></div>
<div id="editor"> <div id="editor">
<textarea id="code" name="code" onkeydown="r=insertTab(this, event);edit_history(event);return r">main = -> <textarea id="code" name="code"></textarea>
resolution = 24; # increase to get smoother corners (will get slow!)
cube1 = CSG.roundedCube center: [0,0,0], radius: [10,10,10], roundradius: 2, resolution: resolution
sphere1 = CSG.sphere center: [5, 5, 5], radius: 10, resolution: resolution
sphere2 = sphere1.translate [12, 5, 0]
sphere3 = CSG.sphere center: [20, 0, 0], radius: 30, resolution: resolution
result = cube1
result = result.union sphere1
result = result.subtract sphere2
result = result.intersect sphere3
result</textarea>
<br> <br>
<select id="lang" name="lang"> <select id="lang" name="lang">
<option>JavaScript</option> <option>JavaScript</option>