diff options
-rw-r--r-- | dependencies.js | 41 | ||||
-rw-r--r-- | getFilename.php | 27 | ||||
-rw-r--r-- | index.html | 52 |
3 files changed, 78 insertions, 42 deletions
diff --git a/dependencies.js b/dependencies.js index 17c2184..c7ba39a 100644 --- a/dependencies.js +++ b/dependencies.js @@ -1,23 +1,26 @@ -var dir = ["data_structures_correct"]; -var fileextension = "."; +$.getScript('math.js'); -var script = document.createElement("script"); - script.src = "math.js"; - document.head.appendChild(script); -$.each(dir, function(index, value) { - $.ajax({ // http://stackoverflow.com/a/18480589 - url: value, - success: function(data) { - $(data).find("a:contains(" + fileextension + ")").each(function() { - var filename = this.href.replace(window.location.host, ""); - filename = filename.replace("https://", value); - filename = filename.replace("http://", value); - var script = document.createElement("script"); - script.src = filename; - document.head.appendChild(script); - }); - } - }); +$.ajax({ + type: "POST", + + // Invoke getFilename.php + url: "getFilename.php", + + // Receive the resultant filenames from the php script in JSON format + dataType: "json", + + // add url for the required folder + data:{ url: "/data_structures_correct/"}, + success: function (data) { + + /* + * data will have the required filenames in the mentioned folder + * For each url, add the script to the body div element with getScript function + */ + for (var i = 0, length = data.length; i < length; ++i) { + $.getScript(data[i]); + } + } }); function AfficheBlock() { diff --git a/getFilename.php b/getFilename.php new file mode 100644 index 0000000..3355ac3 --- /dev/null +++ b/getFilename.php @@ -0,0 +1,27 @@ +<?php + +/* + * @jiteshjha + * getFilename returns the names of all the files in a given folder + */ + +// Store the filenames +$filenameArray = []; + +// Get the folder to be operated +$url = $_POST['url']; + +// Open the file directory and get the filehandle +// Reference: http://php.net/manual/en/function.opendir.php +$handle = opendir(dirname(realpath(__FILE__)).$url); + +// For each file in the folder, push the filename to filenameArray +while($file = readdir($handle)){ + if($file !== '.' && $file !== '..'){ + array_push($filenameArray, "$url$file"); + } +} + +// Return the file name array in JSON format +echo json_encode($filenameArray); +?> @@ -2142,32 +2142,38 @@ </body> <!-- It's good if this part happens after the entire page has loaded--> <script type="text/javascript"> + // Preload all images - var dir = ["blocks", "images"]; - var fileextension = "."; - var blockImages = []; - $.each(dir, function(index, value) { - $.ajax({ // http://stackoverflow.com/a/18480589 - url: value, - success: function(data) { - $(data).find("a:contains(" + fileextension + ")").each(function() { - var filename = this.href.replace(window.location.host, ""); - filename = filename.replace("https://", value); - filename = filename.replace("http://", value); - blockImages.push(filename); - }); - // Prevent multi-threading and have function within call! - function preload(sources) { - var images = []; - for (var i = 0, length = sources.length; i < length; ++i) { - images[i] = new Image(); - images[i].src = sources[i]; - } + var dir = ["/blocks/", "/images/", "/palettes/"]; + for(addr in dir) { + $.ajax({ + type: "POST", + + // Invoke getFilename.php + url: "getFilename.php", + + // Receive the resultant filenames from the php script in JSON format + dataType: "json", + + // add url for the required folder + data:{ url: dir[addr] }, + success: function (data) { + function preload(sources) { + + /* + * sources will have the required filenames in the mentioned folder + * For each image url, make a new image to enable preloading + */ + for (var i = 0, length = sources.length; i < length; ++i) { + var image = new Image(); + image.src = sources[i]; } - preload(blockImages); } - }); - }); + preload(data); + } + }); + } + //Find out more here: http://stackoverflow.com/questions/12843418/jquery-ui-accordion-expand-collapse-all $(window).load(function() { var headers = $('#sidebarContainer .accordion-header'); |