mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
ported to static site, removed _ext folder, split hbs files into partials
This commit is contained in:
79
js/util/ajax.js
Normal file
79
js/util/ajax.js
Normal file
@ -0,0 +1,79 @@
|
||||
//GET: ajax(String url, Function success [,timeout])
|
||||
//POST: ajax(String url, Object postData, Function success [,timeout])
|
||||
Util.ajax = function (url, arg2, arg3, arg4) {
|
||||
if (typeof arg2 == 'function') {
|
||||
var success = arg2;
|
||||
var timeout = arg3 || 10000;
|
||||
}
|
||||
else {
|
||||
var postData = arg2;
|
||||
var success = arg3;
|
||||
var timeout = arg4 || 10000;
|
||||
}
|
||||
|
||||
var start = new Date();
|
||||
console.log('AJAX - STARTING REQUEST', url, '(' + timeout + ')');
|
||||
|
||||
//start new request
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
var result;
|
||||
|
||||
//try to parse as json
|
||||
try {
|
||||
result = JSON.parse(this.response);
|
||||
console.log('AJAX - COMPLETE ('+(new Date()-start)+'ms) - json:', result);
|
||||
}
|
||||
catch (e) {
|
||||
result = this.response;
|
||||
console.log('AJAX - COMPLETE ('+(new Date()-start)+'ms) - string:', this.response, e);
|
||||
}
|
||||
|
||||
//return result
|
||||
success(result);
|
||||
|
||||
xhr = null;
|
||||
}
|
||||
else if (this.readyState == 4) {
|
||||
console.log('ajax failed', this.readyState, this.status);
|
||||
success({ error: 'failure' });
|
||||
}
|
||||
};
|
||||
|
||||
xhr.ontimeout = function(e) {
|
||||
console.log('ajax request timed out')
|
||||
success({ error: 'timeout' });
|
||||
};
|
||||
|
||||
if (postData) {
|
||||
//post request
|
||||
console.log('post data: ', postData instanceof FormData, postData);
|
||||
|
||||
//the the input isn't already formdata, convert it to form data
|
||||
if (!(postData instanceof FormData)) {
|
||||
console.log('converting to form data');
|
||||
|
||||
var formData = new FormData();
|
||||
|
||||
for (var key in postData) {
|
||||
formData.append(key, postData[key]);
|
||||
}
|
||||
|
||||
postData = formData;
|
||||
}
|
||||
|
||||
//send to server
|
||||
xhr.open("POST", url, true);
|
||||
xhr.timeout = timeout;
|
||||
xhr.send(postData);
|
||||
}
|
||||
else {
|
||||
//get request
|
||||
xhr.open("GET", url, true);
|
||||
xhr.timeout = timeout;
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
return xhr;
|
||||
}
|
10
js/util/getSetText.js
Normal file
10
js/util/getSetText.js
Normal file
@ -0,0 +1,10 @@
|
||||
//get text of specified element
|
||||
function getText(elementId) {
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
return element.textContent;
|
||||
}
|
||||
|
||||
function setText(elementId, text) {
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.textContent = text;
|
||||
}
|
12
js/util/getSetValue.js
Normal file
12
js/util/getSetValue.js
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
|
||||
function getValue(elementId) {
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
console.log("setting: " + elementId + ": " + element.value);
|
||||
return element.value;
|
||||
}
|
||||
|
||||
function setValue(elementId, value) {
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.value = value;
|
||||
}
|
21
js/util/hexToRgb.js
Normal file
21
js/util/hexToRgb.js
Normal file
@ -0,0 +1,21 @@
|
||||
//put in a hex color code (f464b2 or #f464b2) string
|
||||
//and get an rgb color object {r:0,g:0,b:0}
|
||||
//divisor is an optional argument, which makes it so you can get values other than 0-255
|
||||
|
||||
function hexToRgb(hex, divisor) {
|
||||
//if divisor isn't set, set it to one (so it has no effect)
|
||||
divisor = divisor || 1;
|
||||
|
||||
//split given hex code into array of 3 values
|
||||
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex.trim());
|
||||
|
||||
//console.log('hex: '+hex)
|
||||
//console.log([parseInt(result[1], 16)/divisor, parseInt(result[2], 16)/divisor, parseInt(result[3], 16)/divisor])
|
||||
//console.log(result)
|
||||
|
||||
return result ? {
|
||||
r: parseInt(result[1], 16)/divisor,
|
||||
g: parseInt(result[2], 16)/divisor,
|
||||
b: parseInt(result[3], 16)/divisor
|
||||
} : null;
|
||||
}
|
32
js/util/hslToRgb.js
Normal file
32
js/util/hslToRgb.js
Normal file
@ -0,0 +1,32 @@
|
||||
function hslToRgb(h, s, l){
|
||||
h /= 255;
|
||||
s /= 255;
|
||||
l /= 255;
|
||||
|
||||
var r, g, b;
|
||||
|
||||
if(s == 0){
|
||||
r = g = b = l; // achromatic
|
||||
}else{
|
||||
var hue2rgb = function hue2rgb(p, q, t){
|
||||
if(t < 0) t += 1;
|
||||
if(t > 1) t -= 1;
|
||||
if(t < 1/6) return p + (q - p) * 6 * t;
|
||||
if(t < 1/2) return q;
|
||||
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
|
||||
return p;
|
||||
}
|
||||
|
||||
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||
var p = 2 * l - q;
|
||||
r = hue2rgb(p, q, h + 1/3);
|
||||
g = hue2rgb(p, q, h);
|
||||
b = hue2rgb(p, q, h - 1/3);
|
||||
}
|
||||
|
||||
return {
|
||||
r:Math.round(r * 255),
|
||||
g:Math.round(g * 255),
|
||||
b:Math.round(b * 255)
|
||||
};
|
||||
}
|
22
js/util/on.js
Normal file
22
js/util/on.js
Normal file
@ -0,0 +1,22 @@
|
||||
//add event listener for any element which calls a function
|
||||
//element can be provided as a direct reference or with just a string of the name
|
||||
|
||||
function on(event, elementId, functionCallback) {
|
||||
|
||||
|
||||
|
||||
//if element provided is string, get the actual element
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
|
||||
//console.log('added '+event+' event listener on '+element)
|
||||
|
||||
element.addEventListener(event,
|
||||
function (e) {
|
||||
// e = event
|
||||
//this = element clicked
|
||||
functionCallback(e, this);
|
||||
//if you need to access the event or this variable, you need to add them
|
||||
//when you define the callback, but you cant use the word this, eg:
|
||||
//on('click', menuButton, function (e, button) {});
|
||||
});
|
||||
}
|
14
js/util/onChildren.js
Normal file
14
js/util/onChildren.js
Normal file
@ -0,0 +1,14 @@
|
||||
//add event listener to each of specified element's children
|
||||
|
||||
function onChildren(event, parentElement, functionCallback) {
|
||||
console.log('onChildren()');
|
||||
|
||||
var parentElement = (typeof parentElement == 'string' ? document.getElementById(parentElement) : parentElement);
|
||||
|
||||
var children = parentElement.children;
|
||||
|
||||
//loop through children and add onClick listener
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
on(event, children[i],functionCallback);
|
||||
}
|
||||
}
|
9
js/util/onClick.js
Normal file
9
js/util/onClick.js
Normal file
@ -0,0 +1,9 @@
|
||||
//DEPRECATED - USE on('click')
|
||||
|
||||
|
||||
//add click event listener for any element which calls a function
|
||||
//element can be provided as a direct reference or with just a string of the name
|
||||
function onClick(elementId, functionCallback) {
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.addEventListener('click',functionCallback);
|
||||
}
|
13
js/util/onClickChildren.js
Normal file
13
js/util/onClickChildren.js
Normal file
@ -0,0 +1,13 @@
|
||||
//add click listener to each of specified element's children
|
||||
|
||||
function onClickChildren(parentElement, functionCallback) {
|
||||
|
||||
var parentElement = (typeof parentElement == 'string' ? document.getElementById(parentElement) : parentElement);
|
||||
|
||||
var children = parentElement.children;
|
||||
|
||||
//loop through children and add onClick listener
|
||||
for (var i = 0; i < children.length; i++) {
|
||||
onClick(children[i],functionCallback);
|
||||
}
|
||||
}
|
23
js/util/rgbToHex.js
Normal file
23
js/util/rgbToHex.js
Normal file
@ -0,0 +1,23 @@
|
||||
//convert rgb values to a hex string for html
|
||||
function rgbToHex (argument0,g,b) {
|
||||
var r;
|
||||
|
||||
//if the first argument is an object
|
||||
if (typeof argument0 === 'object'){
|
||||
r = argument0.r;
|
||||
g = argument0.g;
|
||||
b = argument0.b;
|
||||
}
|
||||
else
|
||||
r = argument0;
|
||||
|
||||
//console.log('converting rgb('+r+','+g+','+b+') to hex');
|
||||
|
||||
//convert a decimal number to 2-digit hex
|
||||
function componentToHex (c) {
|
||||
var hex = c.toString(16);
|
||||
return hex.length == 1 ? "0" + hex : hex;
|
||||
}
|
||||
|
||||
return componentToHex(r) + componentToHex(g) + componentToHex(b);
|
||||
}
|
36
js/util/rgbToHsl.js
Normal file
36
js/util/rgbToHsl.js
Normal file
@ -0,0 +1,36 @@
|
||||
//put in red green blue values and get out hue saturation luminosity values
|
||||
|
||||
function rgbToHsl(argument0, g, b){
|
||||
var r;
|
||||
|
||||
//if the first argument is an object
|
||||
if (typeof argument0 === 'object'){
|
||||
r = argument0.r;
|
||||
g = argument0.g;
|
||||
b = argument0.b;
|
||||
}
|
||||
else
|
||||
r = argument0;
|
||||
|
||||
|
||||
|
||||
r /= 255, g /= 255, b /= 255;
|
||||
|
||||
var max = Math.max(r, g, b), min = Math.min(r, g, b);
|
||||
var hue, saturation, luminosity = (max + min) / 2;
|
||||
|
||||
if(max == min){
|
||||
hue = saturation = 0; // achromatic
|
||||
}else{
|
||||
var d = max - min;
|
||||
saturation = luminosity > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||
switch(max){
|
||||
case r: hue = (g - b) / d + (g < b ? 6 : 0); break;
|
||||
case g: hue = (b - r) / d + 2; break;
|
||||
case b: hue = (r - g) / d + 4; break;
|
||||
}
|
||||
hue /= 6;
|
||||
}
|
||||
|
||||
return {h:hue, s:saturation, l:luminosity};
|
||||
}
|
20
js/util/select.js
Normal file
20
js/util/select.js
Normal file
@ -0,0 +1,20 @@
|
||||
//add class .selected to specified element
|
||||
function select(elementId) {
|
||||
//console.log(arguments.callee.caller.name, 'selected ', elementId);
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.classList.add('selected');
|
||||
}
|
||||
|
||||
//remove .selected class from specified element
|
||||
function deselect(elementId) {
|
||||
//console.log('deselected ', elementId);
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.classList.remove('selected');
|
||||
}
|
||||
|
||||
//toggle the status of the .selected class on the specified element
|
||||
function toggle(elementId) {
|
||||
//console.log('toggled ', elementId);
|
||||
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||||
element.classList.toggle('selected');
|
||||
}
|
Reference in New Issue
Block a user