MCPcopy
hub / github.com/jscad/OpenJSCAD.org / fetchExample

Function fetchExample

src/ui/examples.js:92–140  ·  view source on GitHub ↗
(filename, url, {memFs, gProcessor, gEditor})

Source from the content-addressed store, hash-verified

90}
91
92function fetchExample (filename, url, {memFs, gProcessor, gEditor}) {
93 memFs = []
94
95 const hasExtension = filename.match(/\.[^\/]+$/)
96 if (!hasExtension) // -- has no extension, ie folder referenced
97 {
98 if (!filename.match(/\/$/)) {
99 filename += '/' // add tailing /
100 }
101 filename += 'main.jscad'
102 }
103
104 // FIXME: same as in drag-drop !!
105 function onConversionDone (data) {
106 if ('filename' in data && 'source' in data) {
107 // console.log("editor"+data.source+']')
108 putSourceInEditor(gEditor, data.source, data.filename)
109 }
110 if ('filename' in data && 'converted' in data) {
111 // console.log("processor: "+data.filename+" ["+data.converted+']')
112 if ('cache' in data && data.cache === true) {
113 // FIXME: cannot do it from here, bloody globals
114 // saveScript(data.filename, data.converted)
115 }
116 gProcessor.setJsCad(data.converted, data.filename, data.baseurl)
117 }
118 }
119
120 if (1) { // doesn't work off-line yet
121 var xhr = new XMLHttpRequest()
122 xhr.open('GET', url || filename, true)
123 if (filename.match(/\.(stl|gcode)$/i)) {
124 xhr.overrideMimeType('text/plain; charset=x-user-defined') // our pseudo binary retrieval (works with Chrome)
125 }
126 gProcessor.setStatus('loading', filename)
127 xhr.onload = function () {
128 const source = this.responseText
129 const baseurl = url ? url.replace(/\/[^\/]+$/, '/') : gProcessor.baseurl
130 filename = url ? url.replace(/^.+\//, '') : filename
131
132 // FIXME: refactor : same code as ui/drag-drop
133 gProcessor.setStatus('converting', filename)
134 const worker = createConversionWorker(onConversionDone)
135 // NOTE: cache: false is set to allow evaluation of 'include' statements
136 worker.postMessage({version, baseurl, source, filename, cache: false})
137 }
138 xhr.send()
139 }
140}
141
142const url = require('url')
143

Callers 5

loadViaProxyFunction · 0.85
loadLocalFunction · 0.85
loadRemoteFunction · 0.85
loadInitialExampleFunction · 0.85
onLoadExampleClickedFunction · 0.85

Calls 1

createConversionWorkerFunction · 0.85

Tested by

no test coverage detected