Moved Sketchy Projects to "projects" array in localStorage

This commit is contained in:
Mahdi Dibaiee 2014-02-16 18:42:22 +03:30
parent 6d32146ca8
commit 36ef8fd63f
5 changed files with 74 additions and 41 deletions

View File

@ -52,31 +52,39 @@ window.save = function() {
}
var data = $c[0].toDataURL();
if( save.type == 'sketchy project' ) {
if( localStorage.getItem(save['file name']) ) {
var list = JSON.parse(localStorage.getItem('projects'));
if( list && list.some(function(a) { return a.name == save['file name'] }) ) {
if( confirm('A sketch with this name already exists. Do you want to overwrite ' + save['file name']) + '?' ) {
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
list.push({
name: save['file name'],
data: data,
points: window.points
})
localStorage.setItem('projects', JSON.stringify(list));
}
}
else
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
list ? list.push({
name: save['file name'],
data: data,
points: window.points
}) : list = [{
name: save['file name'],
data: data,
points: window.points
}];
localStorage.setItem('projects', JSON.stringify(list));
} else {
var file = dataToBlob($c[0].toDataURL());
var pics = navigator.getDeviceStorage('pictures');
var r = pics.addNamed(file, save['file name'] + '.png');
r.onsuccess = function() {
alert('Your sketch was successfuly saved to ' + this.result);
}
r.onerror = function() {
alert('Something bad happened when we tried to save your file\n Possible problems: \n Duplicate name \n Permission problems')
}
window.open(data, '_blank').focus();
}
c.putImageData(window.points.history[window.points.history.last].data, 0, 0);
}
window.load = function() {
var file = JSON.parse(localStorage.getItem(load.file));
var file = JSON.parse(localStorage.getItem('projects')).filter(function(a) { return a.name == load.file })[0];
var img = document.createElement('img');
img.src = file.data;
console.log(file.data);
img.onload = function() {
c.clearRect(0, 0, width(), height());
c.drawImage(img, 0, 0);
@ -85,6 +93,11 @@ window.load = function() {
}
}
if( localStorage.getItem('sawTips') != settings.version ) {
$('.tour').removeClass('hidden');
localStorage.setItem('sawTips', settings.version);
}
$('.menu').tap(function() {
$('#menu').toggleClass('pulled');
})
@ -94,15 +107,17 @@ window.load = function() {
$('.load').tap(function() {
$('#load').removeClass('hidden');
$('#load li').remove();
for( var i = 0, len = localStorage.length; i < len; i++ ) {
$('#load ol').append(
$('<li><label><span>' + localStorage.key(i) + '</span></label></li>')
);
}
if( localStorage.length < 1 ) {
var list = JSON.parse(localStorage.getItem('projects'));
if( !list || list.length < 1 ) {
$('#load ol').append(
$('<p>No Sketch found.</p>')
);
return;
}
for( var i = 0, len = list.length; i < len; i++ ) {
$('#load ol').append(
$('<li><label><span>' + list[i].name + '</span></label></li>')
);
}
$confirm.find('li').off('tap').tap(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');

0
Web/js/desktop.css Normal file
View File

View File

@ -11,16 +11,19 @@ $(window).resize(sizeAndPos);
$('.load').click(function() {
$('#load').removeClass('hidden');
$('#load li').remove();
for( var i = 0, len = localStorage.length; i < len; i++ ) {
$('#load ol').append(
$('<li><label><span>' + localStorage.key(i) + '</span></label></li>')
);
}
if( localStorage.length < 1 ) {
var list = JSON.parse(localStorage.getItem('projects'));
if( !list || list.length < 1 ) {
$('#load ol').append(
$('<p>No sketch found.</p>')
);
return;
}
for( var i = 0, len = list.length; i < len; i++ ) {
$('#load ol').append(
$('<li><label><span>' + list[i].name + '</span></label></li>')
);
}
$confirm.find('li').off('click').click(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');

0
Web/js/main.css Normal file
View File

View File

@ -72,13 +72,28 @@ $(document).ready(function() {
var data = $c[0].toDataURL();
if( save.type == 'sketchy project' ) {
if( localStorage.getItem(save['file name']) ) {
var list = JSON.parse(localStorage.getItem('projects'));
if( list && list.some(function(a) { return a.name == save['file name'] }) ) {
if( confirm('A sketch with this name already exists. Do you want to overwrite ' + save['file name']) + '?' ) {
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
list.push({
name: save['file name'],
data: data,
points: window.points
})
localStorage.setItem('projects', JSON.stringify(list));
}
}
else
localStorage.setItem(save['file name'], JSON.stringify({data: data, points: window.points}));
list ? list.push({
name: save['file name'],
data: data,
points: window.points
}) : list = [{
name: save['file name'],
data: data,
points: window.points
}];
localStorage.setItem('projects', JSON.stringify(list));
} else {
window.open(data, '_blank').focus();
}
@ -87,7 +102,7 @@ $(document).ready(function() {
}
function load() {
var file = JSON.parse(localStorage.getItem(load.file));
var file = JSON.parse(localStorage.getItem('projects')).filter(function(a) { return a.name == load.file })[0];
var img = document.createElement('img');
img.src = file.data;
img.onload = function() {