From 36ef8fd63f29f232f432155d194599ef5203937d Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Sun, 16 Feb 2014 18:42:22 +0330 Subject: [PATCH] Moved Sketchy Projects to "projects" array in localStorage --- Mobile/js/mobile.js | 77 +++++++++++++++++++++++++++------------------ Web/js/desktop.css | 0 Web/js/desktop.js | 15 +++++---- Web/js/main.css | 0 Web/js/main.js | 23 +++++++++++--- 5 files changed, 74 insertions(+), 41 deletions(-) create mode 100644 Web/js/desktop.css create mode 100644 Web/js/main.css diff --git a/Mobile/js/mobile.js b/Mobile/js/mobile.js index cafa2ac..9be3917 100755 --- a/Mobile/js/mobile.js +++ b/Mobile/js/mobile.js @@ -51,39 +51,52 @@ window.save = function() { } } var data = $c[0].toDataURL(); - if( save.type == 'sketchy project' ) { - if( localStorage.getItem(save['file name']) ) { + if( save.type == 'sketchy project' ) { + 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})); - } 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') + 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(); } + + 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() { - var file = JSON.parse(localStorage.getItem(load.file)); - 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); - 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}]; + 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() { + c.clearRect(0, 0, width(), height()); + c.drawImage(img, 0, 0); + 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}]; + } + } + + 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( - $('
  • ') - ); - } - if( localStorage.length < 1 ) { + var list = JSON.parse(localStorage.getItem('projects')); + if( !list || list.length < 1 ) { $('#load ol').append( $('

    No Sketch found.

    ') ); + return; + } + for( var i = 0, len = list.length; i < len; i++ ) { + $('#load ol').append( + $('
  • ') + ); } $confirm.find('li').off('tap').tap(function(e) { $(this).parent().find('li[aria-selected]').removeAttr('aria-selected'); diff --git a/Web/js/desktop.css b/Web/js/desktop.css new file mode 100644 index 0000000..e69de29 diff --git a/Web/js/desktop.js b/Web/js/desktop.js index 6163294..1b3bc18 100755 --- a/Web/js/desktop.js +++ b/Web/js/desktop.js @@ -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( - $('
  • ') - ); - } - if( localStorage.length < 1 ) { + var list = JSON.parse(localStorage.getItem('projects')); + if( !list || list.length < 1 ) { $('#load ol').append( $('

    No sketch found.

    ') ); + return; } + for( var i = 0, len = list.length; i < len; i++ ) { + $('#load ol').append( + $('
  • ') + ); + } + $confirm.find('li').off('click').click(function(e) { $(this).parent().find('li[aria-selected]').removeAttr('aria-selected'); $(this).attr('aria-selected', 'true'); diff --git a/Web/js/main.css b/Web/js/main.css new file mode 100644 index 0000000..e69de29 diff --git a/Web/js/main.js b/Web/js/main.js index d0f0aeb..0b9ef86 100755 --- a/Web/js/main.js +++ b/Web/js/main.js @@ -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() {