Sketchy/Web/js/desktop.js

254 lines
7.3 KiB
JavaScript
Raw Normal View History

2014-02-05 13:02:07 +00:00
"use strict";
$(window).resize(sizeAndPos);
2014-02-04 15:02:41 +00:00
$('.menu').click(function() {
$('#menu').toggleClass('pulled');
})
$('.save').click(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').click(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
);
}
2014-02-09 15:38:14 +00:00
$confirm.find('li').off('click').click(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
})
2014-02-09 16:02:07 +00:00
$('#pro').click(function() {
$('#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').click(function() {
$('#save ol:nth-of-type(2) li').removeClass('hidden');
})
2014-02-09 15:38:14 +00:00
})
$('#pro').click(function() {
$('#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').click(function() {
$('#save ol:nth-of-type(2) li').removeClass('hidden');
})
2014-02-05 13:02:07 +00:00
$c.last().on('mousedown', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
var xy = relative(e.pageX, e.pageY);
startPoint(xy.x, xy.y);
window.active = true;
2014-02-05 13:02:07 +00:00
}).on('mousemove', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
if (!window.active || settings.type == 'line') return;
var xy = relative(e.pageX, e.pageY);
drawPoint(xy.x, xy.y);
}).on('mouseup mouseleave', function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
window.active = false;
2014-02-06 14:40:10 +00:00
if( settings.type == 'eraser' ) 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);
}
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
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;
})
// Value Selector
2014-02-05 13:02:07 +00:00
// Single
2014-02-09 15:38:14 +00:00
var $single = $('form.single');
2014-02-04 15:02:41 +00:00
2014-02-05 13:02:07 +00:00
$single.find('li').click(function(e) {
$(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'),
2014-02-12 10:23:31 +00:00
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');
})
2014-02-05 13:02:07 +00:00
$single.submit(function(e) {
2014-02-04 15:02:41 +00:00
e.preventDefault();
$(this).addClass('hidden');
})
2014-02-05 13:02:07 +00:00
// Confirm
2014-02-09 15:38:14 +00:00
var $confirm = $('form.confirm');
2014-02-05 13:02:07 +00:00
2014-02-09 15:38:14 +00:00
$confirm.each(function() {
$(this).find('li').click(function(e) {
$(this).parent().find('li[aria-selected]').removeAttr('aria-selected');
$(this).attr('aria-selected', 'true');
2014-02-05 13:02:07 +00:00
})
2014-02-09 15:38:14 +00:00
$(this).find('button').last().click(function(e) {
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().click(function(e) {
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];
2014-02-05 13:02:07 +00:00
// Exception for Color
if( target == 'color' || target == 'bg' ) {
2014-02-05 13:02:07 +00:00
return $(this).click(function() {
$('.picker').removeClass('hidden');
$('.picker').attr('data-caller', target);
2014-02-05 13:02:07 +00:00
})
}
$(this).click(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('mousedown', 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('mousemove', function(e) {
if( $(this).attr('data-moving') ) {
2014-02-05 13:02:07 +00:00
var x = parseInt(e.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('mouseup mouseleave', function() {
$(this).removeAttr('data-moving');
})
2014-02-05 13:02:07 +00:00
$('.fill, .stroke').click(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;
}
})
2014-02-05 13:02:07 +00:00
$('.close, .tour button').click(function() {
2014-02-07 12:20:19 +00:00
$(this).parent().addClass('hidden');
2014-02-05 13:02:07 +00:00
})
// Bottom
$('#clear').click(function() {
2014-02-12 10:23:31 +00:00
c.clear();
var h = window.points.history;
2014-02-05 16:38:28 +00:00
window.points = [];
2014-02-12 10:23:31 +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;
})
$('#undo').click(undo);
$('#redo').click(redo);
2014-02-07 12:20:19 +00:00
$('#about').click(function() {
$('.about').removeClass('hidden');
})