Sketchy/Web/js/main.js

131 lines
4.1 KiB
JavaScript
Raw Normal View History

2014-02-01 18:18:29 +00:00
"use strict";
$(document).ready(function() {
2014-02-05 13:02:07 +00:00
yepnope({
test: window.mobile,
2014-02-19 21:36:39 +00:00
yep : ['js/mobile.js', 'js/libs/color-picker-touch.js'],
nope: ['js/libs/color-picker.js']
2014-02-05 13:02:07 +00:00
})
$(window).resize(sizeAndPos);
2014-02-09 15:38:14 +00:00
function save() {
2014-02-20 10:09:44 +00:00
var f = c.getImageData(0, 0, width(), height());
2014-02-09 15:38:14 +00:00
switch(save.background) {
case 'white': {
c.fillStyle = 'white';
c.globalCompositeOperation = 'destination-over';
c.fillRect(0, 0, width(), height());
c.fillStyle = settings.color;
c.globalCompositeOperation = settings.composite;
break;
}
case 'current color': {
c.fillStyle = settings.bg;
2014-02-09 15:38:14 +00:00
c.globalCompositeOperation = 'destination-over';
c.fillRect(0, 0, width(), height());
c.globalCompositeOperation = settings.composite;
break;
}
}
var data = $c[0].toDataURL();
if( save.type == 'sketchy project' ) {
var list = JSON.parse(localStorage.getItem('projects'));
2014-02-18 12:01:32 +00:00
var index;
if( list && list.some(function(a, i) { if( a.name == save['file name'] ) {index = i; return true} return false }) ) {
if( confirm('A sketch with this name already exists. Do you want to overwrite ' + save['file name'] + '?') ) {
2014-02-18 12:01:32 +00:00
console.log(index);
list[index] = {
name: save['file name'],
data: data,
2014-02-18 12:01:32 +00:00
points: window.points,
settings: settings
}
localStorage.setItem('projects', JSON.stringify(list));
2014-02-09 15:38:14 +00:00
}
}
else
list ? list.push({
name: save['file name'],
data: data,
points: window.points,
settings: settings
}) : list = [{
name: save['file name'],
data: data,
points: window.points,
settings: settings
}];
localStorage.setItem('projects', JSON.stringify(list));
2014-02-09 15:38:14 +00:00
} else {
2014-05-02 15:13:10 +00:00
$('<a href="' + data + '" download="' + save['file name'] + '.png"></a>').click();
//window.open(data, '_blank').focus();
2014-02-09 15:38:14 +00:00
}
2014-02-20 10:09:44 +00:00
c.putImageData(f, 0, 0);
2014-02-09 15:38:14 +00:00
}
function load() {
var file = JSON.parse(localStorage.getItem('projects')).filter(function(a) { return a.name == load.file })[0];
2014-02-09 15:38:14 +00:00
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}];
2014-02-18 12:01:32 +00:00
$c.first().css('background', file.settings.bg);
window.settings.bg = file.settings.bg;
2014-02-09 15:38:14 +00:00
}
}
window.load = load;
window.save = save;
2014-02-10 12:12:30 +00:00
// TODO: Check for Update
2014-02-09 15:38:14 +00:00
/*var request = navigator.mozApps.getInstalled();
2014-02-09 15:38:14 +00:00
request.onsuccess = function() {
2014-02-09 16:22:38 +00:00
var app = this.result[0];
2014-02-09 17:42:55 +00:00
var latest = $.ajax({url:'manifest-web.webapp'});
2014-02-09 18:28:45 +00:00
var selfApp = navigator.mozApps.getSelf();
selfApp.onsuccess = function() {
2014-02-09 18:28:45 +00:00
if(this.result) {
latest.onload = function() {
if( this.response ) {
var lapp = JSON.parse(this.response);
2014-02-10 11:20:58 +00:00
alert(lapp.version);
alert(app.manifest.version);
2014-02-09 18:28:45 +00:00
if( lapp.version != app.manifest.version &&
2014-02-09 18:30:35 +00:00
confirm('A new version of this app is available, do you want to update?')) {
2014-02-10 11:39:57 +00:00
var ins = navigator.mozApps.install();
2014-02-09 18:28:45 +00:00
ins.onsuccess = function() {
alert('The app was installed successfuly');
}
ins.onerror = function() {
2014-02-09 18:39:33 +00:00
alert('There was an error installing app - ' + this.error.name)
2014-02-09 18:28:45 +00:00
}
}
2014-02-09 17:42:55 +00:00
}
}
}
}
2014-02-09 17:42:55 +00:00
if( !app && confirm('Do you want to Install this app?') ) {
2014-02-09 18:00:37 +00:00
var ins = navigator.mozApps.install('http://mdibaiee.github.io/Sketchy/Web/manifest-web.webapp');
2014-02-09 16:11:39 +00:00
ins.onsuccess = function() {
alert('The app was installed successfuly');
}
ins.onerror = function() {
alert('There was an error installing app')
2014-02-09 17:57:42 +00:00
console.log(this.error);
2014-02-09 16:11:39 +00:00
}
2014-02-09 16:02:07 +00:00
}
2014-02-09 15:38:14 +00:00
}
2014-02-09 16:02:07 +00:00
request.onerror = function() {
alert('An error occured while trying to check for updates');
}*/
2014-02-09 16:02:07 +00:00
2014-02-01 18:18:29 +00:00
})