ordigi/app/html/js/handlers.js

175 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-10-26 10:08:01 +01:00
var __constants__ = {
baseUrl : 'http://localhost:5000'
};
var __process__ = {};
2015-11-03 10:38:53 +01:00
if(typeof(require) === 'function') {
var ipc = require('ipc');
ipc.on('files', function(files) {
__process__.files = files;
});
ipc.on('preview', function(files) {
handlers.renderPreview(files);
});
ipc.on('update-import-success', function(args) {
//var response = JSON.parse(args['stdout']);
handlers.setSuccessTitle();
handlers.removeProgressIcons();
handlers.addSuccessImportMessage(args);
});
ipc.on('update-import-no-photos', function(args) {
//var response = JSON.parse(args['stdout']);
handlers.removeProgressIcons();
});
2015-11-03 10:38:53 +01:00
ipc.on('update-photos-success', function(args) {
var response = JSON.parse(args['stdout']);
handlers.setSuccessTitle();
handlers.removeProgressIcons();
handlers.updateStatus(response);
});
2015-10-26 10:08:01 +01:00
2015-11-03 10:38:53 +01:00
function Broadcast() {
this.send = function(name, message) {
ipc.send(name, message);
};
}
2015-10-26 10:08:01 +01:00
}
function Handlers() {
var self = this;
var broadcast = new Broadcast();
2015-11-03 10:38:53 +01:00
this.click = {};
this.submit = {};
this.change = {};
// CHANGE
this.change.fileSelected = function(ev) {
var el = ev.target,
dir = el.value.substr(el.value.lastIndexOf("\\")+1),
tgt = document.querySelector(el.dataset.display);
tgt.innerHTML = dir;
};
// CLICK
this.click.selectFile = function(ev) {
var el = ev.target,
tgt = document.querySelector(el.dataset.for);
ev.preventDefault();
tgt.click();
};
this.click.launchFinder = function(ev) {
var el = ev.target,
tgt = el.dataset.path;
ev.preventDefault();
broadcast.send('launch-finder', tgt);
};
this.click.quitProgram = function(ev) {
//ev.preventDefault();
console.log('quit');
broadcast.send('program-quit');
};
2015-11-03 10:38:53 +01:00
// SUBMIT
this.submit.importPhotos = function(ev) {
var el = ev.target,
cls = el.className,
params;
2015-11-03 10:38:53 +01:00
ev.preventDefault();
2015-10-26 10:08:01 +01:00
2015-11-03 10:38:53 +01:00
params = {};
params['source'] = document.querySelector('input[name="source"]').value
params['destination'] = document.querySelector('input[name="destination"]').value
if(params['destination'].length === 0 || params['source'].length === 0) {
return;
}
document.querySelector('button.push i').className = 'icon-spin animate-spin';
2015-11-03 10:38:53 +01:00
broadcast.send('import-photos', params);
};
this.submit.updatePhotos = function(ev) {
var el = ev.target,
cls = el.className,
params;
ev.preventDefault();
document.querySelector('button.push i').className = 'icon-spin animate-spin';
2015-10-26 10:08:01 +01:00
params = {};
2015-11-03 10:38:53 +01:00
params['location'] = document.querySelector('input[id="location-field"]').value;
params['datetime'] = document.querySelector('input[id="datetime-field"]').value;
params['album'] = document.querySelector('input[id="album-field"]').value;
params['title'] = document.querySelector('input[id="title-field"]').value;
if(params['location'].length === 0 && params['datetime'].length === 0 && params['album'].length === 0 && params['title'].length === 0)
2015-10-26 10:08:01 +01:00
return;
params['files'] = __process__.files;
broadcast.send('update-photos', params);
2015-10-26 10:08:01 +01:00
};
2015-11-03 10:38:53 +01:00
this.addSuccessImportMessage = function(args) {
document.querySelector('.import-success').innerHTML = 'Your photos were successfully imported. <a href="#" class="launchFinder" data-path="'+args['destination'] +'">View them here</a>.';
};
2015-10-26 10:08:01 +01:00
this.dispatch = function(ev) {
var classes = ev.target.className.split(' ');
for(i=0; i<classes.length; i++) {
2015-11-03 10:38:53 +01:00
if(typeof(self[ev.type][classes[i]]) !== 'undefined') {
self[ev.type][classes[i]](ev);
2015-10-26 10:08:01 +01:00
}
}
};
this.removeProgressIcons = function() {
var els = document.querySelectorAll('i.icon-spin');
for(el in els) {
2015-11-03 10:38:53 +01:00
els[el].className = '';
2015-10-26 10:08:01 +01:00
}
};
this.renderPreview = function(files) {
2015-11-03 10:38:53 +01:00
html = '<label>You selected ' + (files.length > 1 ? 'these photos' : 'this photo') + '</label>';
for(var i=0; i<files.length && i<16; i++) {
if(files[i].match(/(mov|mp4|3gp|avi)/i) === null) {
2015-11-03 10:38:53 +01:00
html += '<div class="center-cropped" style="background-image:url(\'file://'+files[i]+'\');" title="'+files[i]+'"></div>';
} else {
2015-11-03 10:38:53 +01:00
html += '<div class="center-cropped video"></div>';
}
}
if(files.length >= 16) {
2015-11-03 10:38:53 +01:00
html += '<br>...and ' + (files.length -16) + ' more.';
2015-10-26 10:08:01 +01:00
}
2015-11-03 10:38:53 +01:00
document.querySelector('.preview').innerHTML = html;
2015-10-26 10:08:01 +01:00
};
this.setSuccessTitle = function() {
2015-11-03 10:38:53 +01:00
var el = document.querySelector('.titlebar i').className = 'icon-happy';
2015-10-26 10:08:01 +01:00
};
this.updateStatus = function(response) {
var el = document.querySelector('.status'),
source, destination, html;
2015-11-03 10:38:53 +01:00
console.log('update status');
console.log(response);;
if(response.length > 0) {
html = '<label>Status</label><ul>';
for(i=0; i<response.length; i++) {
source = response[i]['source'] || null;
destination = response[i]['destination'] || null;
sourceFileName = source.substr(source.lastIndexOf('/')+1);
if(destination === null) {
html += '<li><i class="icon-unhappy"></i> ' + sourceFileName + '</li>';
} else {
html += '<li><i class="icon-happy"></i> ' + sourceFileName + '<div class="destination" title="'+destination+'">'+destination+'</div></li>';
}
}
html += '</ul>';
el.innerHTML = html;
el.style.display = 'block';
}
};
2015-10-26 10:08:01 +01:00
}
var handlers = new Handlers();
2015-11-03 10:38:53 +01:00
window.addEventListener('click', handlers.dispatch);
window.addEventListener('submit', handlers.dispatch);
window.addEventListener('change', handlers.dispatch);