Skip to content

Commit

Permalink
Prefs file changed from EncryptedLocalStorage to applicationStorage
Browse files Browse the repository at this point in the history
(should be less prone to crashes)
- More responsive file browsing by pre-caching nested folders
  • Loading branch information
matthew-dean committed Oct 6, 2014
1 parent 4de73d7 commit f963b15
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
Binary file modified builds/Crunch.1.9.2.air
Binary file not shown.
80 changes: 63 additions & 17 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,44 @@ else {
openCSSafterCrunch: true
}
};
var storedPrefs = air.EncryptedLocalStore.getItem("state");

if(storedPrefs != null) {
var val = storedPrefs.readUTFBytes(storedPrefs.length);
$.extend(true, App, JSON.parse(val));
copyPaths();
var prefsPath = air.File.applicationStorageDirectory;
var prefsFile = prefsPath.resolvePath("prefs.json");

if(prefsFile.exists) {
var stream = new air.FileStream();
stream.open(prefsFile, air.FileMode.READ);
var storedPrefs = stream.readUTFBytes(stream.bytesAvailable);
stream.close();
$.extend(true, App, JSON.parse(storedPrefs));
copyPaths();
}

function updateAppState() {
var str = JSON.stringify(App);
var bytes = new air.ByteArray();
bytes.writeUTFBytes(str);
air.EncryptedLocalStore.setItem("state", bytes);
copyPaths();

var stream = new air.FileStream();
stream.open(prefsFile, air.FileMode.WRITE);
stream.writeUTFBytes(str);
stream.close();
//copyPaths();
}

//var storedPrefs = air.EncryptedLocalStore.getItem("state");

// if(storedPrefs != null) {
// var val = storedPrefs.readUTFBytes(storedPrefs.length);
// $.extend(true, App, JSON.parse(val));
// copyPaths();
// }

// function updateAppState() {
// var str = JSON.stringify(App);
// var bytes = new air.ByteArray();
// bytes.writeUTFBytes(str);
// air.EncryptedLocalStore.setItem("state", bytes);
// //copyPaths();
// }


function applyAppSetting(pref) {
switch(pref) {
case 'filemonitoring':
Expand Down Expand Up @@ -95,7 +118,7 @@ else {
}
$.each(App.openFiles, function(idx, val) {
if(!root.resolvePath(idx).exists) {
delete App.openFiles[idx];
delete App.openFiles[idx];
update = true;
}
});
Expand Down Expand Up @@ -583,11 +606,12 @@ else {
var lastCrunch;

// Intercept AJAX requests because AIR doesn't use them
$.mockjaxSettings.logging = false;
$.mockjax({
url : 'dir.html',
status : 200,
response : function(settings) {
this.responseText = getTree(settings.data.path);
this.responseText = getTree(settings.data.path, true);
}
});

Expand Down Expand Up @@ -858,7 +882,14 @@ else {

setTimeout(function() {
try {
fileSelect.browseForSave("Save As");
// Try fixing opening directories on Mac
if(fileSelect.parent) {
fileSelect.parent.browseForSave("Save As");
}
else {
fileSelect.browseForSave("Save As");
}

fileSelect.addEventListener(air.Event.SELECT, saveData);
if(filemonitored)
fileSelect.addEventListener(air.Event.CANCEL, reWatch);
Expand Down Expand Up @@ -937,7 +968,7 @@ else {
return $('<div/>').html(value).text();
}

function getTree(treePath) {
function getTree(treePath, end) {
var target = Paths.project.resolvePath(treePath);
var files = target.getDirectoryListing();
var tree = '<ul>';
Expand All @@ -956,7 +987,11 @@ else {
tree += ' class="jstree-leaf file css"';
else
tree += ' class="jstree-leaf file"';
tree += ' title="' + files[i].nativePath + '"><a href="#">' + files[i].name + '</a></li>';
tree += ' title="' + files[i].nativePath + '"><a href="#">' + files[i].name + '</a>'
if(files[i].isDirectory && !end) {
tree += getTree(files[i].nativePath, true);
}
tree += '</li>';
}
}
tree += '</ul>';
Expand All @@ -977,7 +1012,8 @@ else {
var directory = Paths.project;

var tree = '<li id="root" class="jstree-open" title="' + directory.nativePath + '"><a href="#">' + directory.name + '</a>' + getTree(directory.nativePath) + '</li>';
$("#filelist").jstree({
var $fileTree = $('#filelist');
$fileTree.jstree({
"core" : {
"initially_open" : ["root"],
"animation" : 100
Expand All @@ -1002,7 +1038,17 @@ else {
"icons" : true
},
"plugins" : ["themes", "html_data", "ui", "types"]
}).bind("open_node.jstree", 'li', function (e, data) {
var ref = $.jstree._reference($fileTree);

$(data.args[0][0]).parent().find('li.jstree-closed').each(function() {
var $node = $(this);
if($node.find('ul').length === 0) {
ref.load_node_html(this);
}
});
});

$('#project').addClass("show").find("#open-project").removeClass('big');
$('#refresh').removeAttr('disabled').click(function() {
$('#filelist').jstree('refresh', -1);
Expand Down

1 comment on commit f963b15

@matthew-dean
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also fixes the file open dialog opening to the correct project-relative directory

Please sign in to comment.