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

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