Sketchy/Mobile/js/mobile.js

344 lines
10 KiB
JavaScript
Raw Normal View History

2014-02-05 13:02:07 +00:00
"use strict";
2014-02-07 12:20:19 +00:00
$(document).ready(function() {
// Open External Links in browser
$('*').click(function(e) {
e.preventDefault();
})
2014-02-07 14:48:26 +00:00
$('a[href^="http"]').tap(function(e) {
2014-02-07 12:20:19 +00:00
e.preventDefault();
var href = $(this).attr('href');
var view = new MozActivity({
name: 'view',
data: {
type: 'url',
url: href
}
})
})
2014-02-07 14:48:26 +00:00
$('a[href^="mailto"]').tap(function(e) {
e.preventDefault();
var mail = /mailto:(.*)/.exec($(this).attr('href'))[1];
var mail = new MozActivity({
name: 'new',
data: {
type: 'mail',
url: mail
}
})
})
2014-02-07 12:50:47 +00:00
window.save = function() {
2014-02-05 13:02:07 +00:00
switch(save.background) {
case 'white': {
c.fillStyle = 'white';
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
c.fillStyle = settings.color;
c.globalCompositeOperation = settings.composite;
2014-02-05 13:02:07 +00:00
break;
}
case 'current color': {
c.fillStyle = settings.bg;
2014-02-05 13:02:07 +00:00
c.globalCompositeOperation = 'destination-over';
2014-02-05 16:38:28 +00:00
c.fillRect(0, 0, width(), height());
c.globalCompositeOperation = settings.composite;
2014-02-05 13:02:07 +00:00
break;
}
}
var data = $c[0].toDataURL();
if( save.type == 'sketchy project' ) {
var list = JSON.parse(localStorage.getItem('projects'));
if( list && list.some(function(a) { return a.name == save['file name'] }) ) {
2014-02-09 15:38:14 +00:00
if( confirm('A sketch with this name already exists. Do you want to overwrite ' + save['file name']) + '?' ) {
list.push({
name: save['file name'],
data: data,
points: window.points
})
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
}) : list = [{
name: save['file name'],
data: data,
points: window.points
}];
localStorage.setItem('projects', JSON.stringify(list));
} else {
window.open(data, '_blank').focus();
2014-02-09 15:38:14 +00:00
}
c.putImageData(window.points.history[window.points.history.last].data, 0, 0);
2014-02-05 13:02:07 +00:00
}
2014-02-09 15:38:14 +00:00
window.load = function() {
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);
2014-02-09 15:38:14 +00:00
}
2014-02-05 13:02:07 +00:00
2014-02-07 12:20:19 +00:00
$('.menu').tap(function() {
2014-02-04 15:02:41 +00:00
$('#menu').toggleClass('pulled');
})
2014-02-07 12:20:19 +00:00
$('.save').tap(function() {
2014-02-05 13:02:07 +00:00
$('#save').removeClass('hidden');
2014-02-04 15:02:41 +00:00
})
2014-02-09 15:38:14 +00:00
$('.load').tap(function() {
$('#load').removeClass('hidden');
$('#load li').remove();
var list = JSON.parse(localStorage.getItem('projects'));
if( !list || list.length < 1 ) {
2014-02-09 15:38:14 +00:00
$('#load ol').append(
$('<p>No Sketch found.</p>')
2014-02-09 15:38:14 +00:00
);
return;
2014-02-09 15:38:14 +00:00
}
for( var i = 0, len = list.length; i < len; i++ ) {
2014-02-09 15:38:14 +00:00
$('#load ol').append(
$('<li><label><span>' + list[i].name + '</span></label></li>')
2014-02-09 15:38:14 +00:00
);
}
$confirm.find('li').off('tap').tap(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
})
})
$('#pro').tap(function() {
2014-02-09 15:38:14 +00:00
$('#save ol:nth-of-type(2) li').each(function() {
if( $(this).find('span').html() !== 'Transparent' ) {
$(this).addClass('hidden');
$(this).removeAttr('aria-selected');
}
else $(this).attr('aria-selected', 'true');
})
})
$('#exp').tap(function() {
2014-02-09 15:38:14 +00:00
$('#save ol:nth-of-type(2) li').removeClass('hidden');
})
2014-02-05 13:02:07 +00:00
$c.last().on('touchstart', function(e) {
2014-02-04 15:02:41 +00:00
var xy = relative(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
startPoint(xy.x, xy.y);
window.active = true;
2014-02-05 13:02:07 +00:00
}).on('touchmove', function(e) {
2014-02-04 15:02:41 +00:00
if (!window.active || settings.type == 'line') return;
var xy = relative(e.changedTouches[0].pageX, e.changedTouches[0].pageY);
drawPoint(xy.x, xy.y);
2014-02-05 13:02:07 +00:00
}).on('touchend', function(e) {
2014-02-04 15:02:41 +00:00
window.active = false;
2014-02-06 14:40:10 +00:00
if( settings.type == 'eraser' ) return;
if( settings.type == 'shape' ) {
var s = settings.comShape;
o.clear();
c.beginPath();
c.fillStyle = settings.color;
c.strokeStyle = settings.color;
c.lineWidth = settings.lineWidth / 20;
switch(s.type) {
case 'circle': {
c.arc(s.x, s.y, s.radius, 0, 2*Math.PI);
break;
}
case 'rectangle': {
c.rect(s.x, s.y, s.w, s.h)
break;
}
case 'triangle': {
c.moveTo(s.start.x + s.dix, s.start.y);
c.lineTo(s.x, s.y);
c.lineTo(s.start.x, s.y);
c.lineTo(s.start.x + s.dix, s.start.y);
break;
}
}
if( settings.fill ) c.fill();
if( settings.stroke ) c.stroke();
}
if( settings.type == 'line' ) return;
2014-02-04 15:02:41 +00:00
if(window.points.history.last < window.points.history.length-1) {
window.points.history.splice(window.points.history.last+1);
}
2014-02-04 15:02:41 +00:00
window.points.history.push({
2014-02-05 16:38:28 +00:00
data: c.getImageData(0, 0, width(), height()),
2014-02-04 15:02:41 +00:00
points: window.points.slice(0)
})
window.points.history.last = window.points.history.length-1;
}).on('longTap', function(e) {
if( points[points.length-1].type == 'line' ) {
window.active = false;
points[points.length-1].type = '';
points[points.length-1].start = undefined;
finishLine();
}
2014-02-04 15:02:41 +00:00
})
// Value Selector
2014-02-05 13:02:07 +00:00
var $single = $('form[data-type="value-selector"].single');
2014-02-04 15:02:41 +00:00
2014-02-07 12:20:19 +00:00
$single.find('li').tap(function(e) {
2014-02-05 13:02:07 +00:00
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
2014-02-04 15:02:41 +00:00
$(this).attr('aria-selected', 'true');
2014-02-05 13:02:07 +00:00
var key = $(this).parents('form').attr('id'),
value = $(this).find('label span').html().toLowerCase(),
target = $(this).attr('data-target');
2014-02-05 13:02:07 +00:00
window.settings[key] = value;
2014-02-04 15:02:41 +00:00
2014-02-05 13:02:07 +00:00
$('button[id="set' + key + '"] span').html(value[0].toUpperCase() + value.substr(1));
if( target ) {
$('#menu div.options > div').addClass('hidden');
$('#menu div.options > .general, #menu div.options > .'+target).removeClass('hidden');
}
2014-02-04 15:02:41 +00:00
$(this).parents('form').addClass('hidden');
})
$single.find('button').tap(function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
$(this).parents('form').addClass('hidden');
2014-02-04 15:02:41 +00:00
})
2014-02-05 13:02:07 +00:00
// Confirm
var $confirm = $('form[data-type="value-selector"].confirm');
2014-02-09 15:38:14 +00:00
$confirm.each(function() {
$(this).find('li').tap(function(e) {
2014-02-09 15:38:14 +00:00
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
2014-02-05 13:02:07 +00:00
})
$(this).find('button').last().tap(function(e) {
2014-02-09 15:38:14 +00:00
e.preventDefault();
var v = $(this).parents('form').attr('id');
$(this).parents('form').find('h1').each(function(i) {
if( i > 0 ) {
var key = $(this).html().toLowerCase();
var value = $(this).parent().find('ol:nth-of-type('+i+') li[aria-selected] span').html();
if( key !== 'file name' && key !== 'file' ) value = value.toLowerCase();
window[v][key] = value;
}
})
$(this).parents('form').addClass('hidden');
window[v]();
})
$(this).find('button').first().tap(function(e) {
2014-02-09 15:38:14 +00:00
e.preventDefault();
$(this).parents('form').addClass('hidden');
})
2014-02-05 13:02:07 +00:00
})
2014-02-04 15:02:41 +00:00
// Value Selector Callers
var $btn = $('button[id^="set"]');
$btn.each(function() {
var target = /set(.*)/.exec($(this).attr('id'))[1];
// Exception for Color
if( target == 'color' || target == 'bg' ) {
2014-02-07 12:20:19 +00:00
return $(this).tap(function() {
2014-02-05 13:02:07 +00:00
$('.picker').removeClass('hidden');
$('.picker').attr('data-caller', target);
2014-02-05 13:02:07 +00:00
})
}
2014-02-07 12:20:19 +00:00
$(this).tap(function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
$('form[id="' + target + '"]').removeClass('hidden');
})
})
// Seekbar
2014-02-05 13:02:07 +00:00
var sliderLeft;
2014-02-04 15:02:41 +00:00
$('div[role="slider"] button').on('touchstart', function() {
$(this).attr('data-moving','true');
2014-02-05 13:02:07 +00:00
if( !sliderLeft ) sliderLeft = $('div[role="slider"] button').offset().left;
2014-02-04 15:02:41 +00:00
}).on('touchmove', function(e) {
2014-02-05 13:02:07 +00:00
if( $(this).attr('data-moving') ) {
var x = parseInt(e.changedTouches[0].pageX - sliderLeft - 15);
2014-02-06 16:18:11 +00:00
var $c = $('.'+$(this).parents('div[role="slider"]').attr('class'));
var progress = $c.find('progress');
2014-02-05 13:02:07 +00:00
var max = +progress.attr('max');
var min = +progress.attr('min');
if( x <= max && x >= min ) {
2014-02-06 16:18:11 +00:00
$c.find('button').css('left', x+'%');
progress.attr('value', x);
var key = $c.attr('class');
2014-02-05 13:02:07 +00:00
settings[key] = x;
$('#'+ key +' span').html(x);
2014-02-04 15:02:41 +00:00
}
}
}).on('touchend', function() {
$(this).removeAttr('data-moving');
})
2014-02-05 13:02:07 +00:00
$('.fill, .stroke').tap(function() {
var s = $('.'+$(this).attr('class')).find('span');
if( s.html() == 'Yes' ) {
s.html('No');
settings[$(this).attr('class')] = false;
} else {
s.html('Yes');
settings[$(this).attr('class')] = true;
}
})
$('.close, .tour button').tap(function() {
$(this).parent().addClass('hidden');
})
2014-02-05 13:02:07 +00:00
// Color Picker
2014-02-07 12:20:19 +00:00
$('.close').tap(function() {
$(this).parent().addClass('hidden');
2014-02-05 13:02:07 +00:00
})
// Bottom
2014-02-07 12:20:19 +00:00
$('#clear').tap(function() {
2014-02-05 16:38:28 +00:00
c.clearRect(0, 0, width(), height());
2014-02-06 13:00:58 +00:00
var h = window.points.history;
2014-02-05 16:38:28 +00:00
window.points = [];
2014-02-06 13:00:58 +00:00
window.points.history = h;
2014-02-05 13:02:07 +00:00
if(window.points.history.last < window.points.history.length-1) {
window.points.history.splice(window.points.history.last+1);
}
window.points.history.push({
2014-02-05 16:38:28 +00:00
data: c.getImageData(0, 0, width(), height()),
2014-02-05 13:02:07 +00:00
points: []
})
window.points.history.last = window.points.history.length-1;
})
2014-02-07 12:20:19 +00:00
$('#undo').tap(undo);
$('#redo').tap(redo);
$('#about').tap(function() {
$('.about').removeClass('hidden');
})
2014-02-05 13:02:07 +00:00
2014-02-07 12:20:19 +00:00
});